Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. ## This is a quick guide to setup a bookshelf model
  2.  
  3. 1. set up a knexfile in the root of your directory
  4. eg.
  5. ```
  6. module.exports = {
  7. client: 'pg',
  8. connection: 'postgres://localhost:5432/auth_server',
  9. migrations: {
  10. directory: __dirname + '/db/migrations'
  11. }
  12. }
  13. ```
  14.  
  15. 2. initialize your bookshelf instance which will be imported into other files
  16. eg.
  17. ```
  18. const environment = process.env.NODE_ENV
  19. const config = require('../knexfile')
  20. const knex = require('knex')(config)
  21. const bookshelf = require('bookshelf')(knex)
  22.  
  23. module.exports = bookshelf
  24. ```
  25.  
  26. 3. if necessary, create database
  27. 4. create migrations
  28. `$ knex migrate:make migration_name`
  29.  
  30. ```
  31. exports.up = function(knex, Promise) {
  32. return knex.schema
  33. .createTable('users', (table) => {
  34. table.increments('id').primary()
  35. table.string('email').unique().notNullable()
  36. table.string('password').notNullable()
  37. })
  38. }
  39.  
  40. exports.down = function(knex, Promise) {
  41. return knex.schema
  42. .dropTable('users')
  43. }
  44. ```
  45.  
  46. 5. wire up your model
  47. eg.
  48.  
  49. ```
  50. const bookshelf = require('../db/bookshelf')
  51.  
  52. const User = bookshelf.Model.extend({
  53. tableName: 'users'
  54. })
  55.  
  56. module.exports = User
  57. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement