UNPKG

6.69 kBMarkdownView Raw
1# ah-bookshelf-plugin
2
3[Bookshelf](http://bookshelfjs.org/) plugin for [actionhero](http://www.actionherojs.com/)
4
5[![Build Status][travis-image]][travis-url]
6[![NPM package][npm-image]][npm-url]
7[![Dependency Status][depstat-image]][depstat-url]
8[![devDependency Status][devdepstat-image]][devdepstat-url]
9[![License][license-image]][license-url]
10
11
12## Description
13
14## Features
15
16## Install
17
18```
19npm install --save ah-bookshelf-plugin
20```
21
22```
23# Then add one of the following:
24npm install --save pg
25npm install --save mysql
26npm install --save mariasql
27npm install --save sqlite3
28```
29
30Be sure to enable the plugin within actionhero.
31Add the `"ah-bookshelf-plugin"` to `config/plugins.js`.
32
33```javascript
34// config/plugins.js
35
36"use strict";
37
38module.exports = {
39 "default": {
40 general: function(api) {
41 return {
42 plugins: [
43 "ah-bookshelf-plugin"
44 ]
45 };
46 }
47 },
48 //...
49}};
50```
51
52Be sure to enable the task for database.
53Add the `require("ah-bookshelf-plugin/grunt")(grunt);` to `gruntfile.js`.
54
55```javascript
56// gruntfile.js
57
58"use strict";
59
60var grunt = require("grunt")
61require("actionhero/grunt")(grunt);
62
63// ah-bookshelf-plugin
64require("ah-bookshelf-plugin/grunt")(grunt);
65```
66
67
68## Configuration
69
70After installation, `bookshelf.js` is copied to the `config/plugins` directory.
71Please correct.
72
73```javascript
74// config/plugins/bookshelf.js
75
76"use strict";
77
78module.exports = {
79 "default": {
80 bookshelf: function(api) {
81 return {
82 // http://knexjs.org/#Installation-debug
83 debug: true,
84
85 // http://knexjs.org/#Installation-client
86 client: "postgres",
87 connection: {
88 host: "127.0.0.1",
89 port: 5432,
90 user: "postgres",
91 password: "postgres",
92 database: "db_development",
93 charset: "utf8"
94 },
95
96 // http://knexjs.org/#Installation-migrations
97 migrations: {
98 tableName: "knex_migrations",
99 directory: api.projectRoot + "/database/migrations"
100 },
101
102 // http://knexjs.org/#Seeds-API
103 seeds: {
104 directory: api.projectRoot + "/database/seeds"
105 },
106
107 // models directory
108 models: {
109 directory: api.projectRoot + "/models"
110 }
111 };
112 }
113 },
114 //...
115};
116```
117
118## Model
119
120After installation, `base.js` is copied to the `models` directory.
121Please describe the basic class of models and collections to `base.js`.
122Model is allowed to inherit the base class, and add to `api.models`.
123
124```javascript
125// models/base.js
126
127"use strict";
128
129module.exports = function(api) {
130
131 // Base Model
132 api.bookshelf.Model = api.bookshelf.Model.extend({
133
134 hasTimestamps: true
135 //...
136
137 }, {
138
139 find: function(id, options) {
140 if (options == null) { options = {} }
141 if (!options.require) { options.require = true }
142 return this.forge({id: id}).fetch(options);
143 }
144 //...
145
146 });
147
148
149 // Base Collection
150 api.bookshelf.Collection = api.bookshelf.Collection.extend({
151
152 model: api.bookshelf.Model
153
154 });
155
156};
157```
158
159```javascript
160// models/user.js
161
162"use strict";
163
164module.exports = function(api) {
165
166 // User Model
167 api.models.User = api.bookshelf.Model.extend({
168
169 tableName: "users"
170 //...
171
172 }, {
173
174 findByUsername: function(username, options) {
175 if (options == null) { options = {} }
176 if (!options.require) { options.require = true }
177 return this.forge({username: username}).fetch(options);
178 }
179 //...
180
181 });
182
183
184 // User Collection
185 api.models.Users = api.bookshelf.Collection.extend({
186
187 model: api.models.User
188
189 });
190
191};
192```
193
194## Task
195
196Create or delete a database, the execution of the migration, the creation of the initial data.
197
198```
199// grunt --help
200
201 db:version Show the current migration version
202 db:rollback Rollback the database
203 db:migrate:make Make migrate file (:name)
204 db:migrate Migrate the database
205 db:seed:make Make seed file (:name)
206 db:seed Create the seed data
207 db:create Create the database
208 db:drop Drop the database
209 db:migrate:reset Runs db:drop db:create db:migrate
210 db:reset Runs db:migrate:reset db:seed
211```
212
213## Migration
214
215Using the database task to create a migration file.
216
217```
218grunt db:migrate:make:create_users
219
220Running "db:migrate:make:users" (db:migrate:make) task
221>> Make /path/to/project/database/migrations/20150210164757_create_users.js
222```
223
224Please edit.
225
226```javascript
227// database/migrations/20150210164757_create_users.js
228
229"use strict";
230
231exports.up = function(knex, Promise) {
232 return knex.schema.createTable("users", function(t) {
233 t.increments();
234 t.string("username");
235 t.timestamps();
236 });
237};
238
239exports.down = function(knex, Promise) {
240 return knex.schema.dropTable("users");
241};
242```
243
244Please run migrate task.
245
246```javascript
247grunt db:migrate
248```
249
250
251## Seed
252
253Using the database task to create a seed file.
254
255```
256grunt db:seed:make:users
257
258Running "db:seed:make:users" (db:seed:make) task
259>> Make /path/to/project/database/seeds/users.js
260```
261
262Please edit.
263
264```javascript
265// database/seeds/users.js
266
267"use strict";
268
269exports.seed = function(knex, Promise) {
270 return knex("users").insert({
271 username: "ah-bookshelf-plugin",
272 created_at: new Date(),
273 updated_at: new Date()
274 });
275};
276```
277
278Please run seed task.
279
280```javascript
281grunt db:seed
282```
283
284
285
286## Usage
287
288The api is exposed in `api.bookshelf` and `api.models` object.
289`api.bookshelf` is an instance of the bookshelf.
290
291
292```javascript
293// actions/users.js
294
295"use strict";
296
297exports.index = {
298 name: "users.index",
299 description: "users.index",
300 run: function(api, connection, next){
301 api.models.User.fetchAll()
302 .then(function(users) {
303 connection.response.users = users;
304 next(connection, true);
305 })
306 .catch(function(err) {
307 connection.error = err;
308 next(connection, true);
309 });
310 }
311};
312```
313
314
315
316[npm-url]: https://npmjs.org/package/ah-bookshelf-plugin
317[npm-image]: https://badge.fury.io/js/ah-bookshelf-plugin.svg
318
319[travis-url]: http://travis-ci.org/keifukuda/ah-bookshelf-plugin
320[travis-image]: https://secure.travis-ci.org/keifukuda/ah-bookshelf-plugin.svg?branch=master
321
322[depstat-url]: https://david-dm.org/keifukuda/ah-bookshelf-plugin
323[depstat-image]: https://david-dm.org/keifukuda/ah-bookshelf-plugin.svg
324
325[devdepstat-url]: https://david-dm.org/keifukuda/ah-bookshelf-plugin#info=devDependencies
326[devdepstat-image]: https://david-dm.org/keifukuda/ah-bookshelf-plugin/dev-status.svg
327
328[license-url]: https://github.com/keifukuda/ah-bookshelf-plugin/blob/master/MIT-LICENSE.txt
329[license-image]: http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat