Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## This is a quick guide to setup a bookshelf model
- 1. set up a knexfile in the root of your directory
- eg.
- ```
- module.exports = {
- client: 'pg',
- connection: 'postgres://localhost:5432/auth_server',
- migrations: {
- directory: __dirname + '/db/migrations'
- }
- }
- ```
- 2. initialize your bookshelf instance which will be imported into other files
- eg.
- ```
- const environment = process.env.NODE_ENV
- const config = require('../knexfile')
- const knex = require('knex')(config)
- const bookshelf = require('bookshelf')(knex)
- module.exports = bookshelf
- ```
- 3. if necessary, create database
- 4. create migrations
- `$ knex migrate:make migration_name`
- ```
- exports.up = function(knex, Promise) {
- return knex.schema
- .createTable('users', (table) => {
- table.increments('id').primary()
- table.string('email').unique().notNullable()
- table.string('password').notNullable()
- })
- }
- exports.down = function(knex, Promise) {
- return knex.schema
- .dropTable('users')
- }
- ```
- 5. wire up your model
- eg.
- ```
- const bookshelf = require('../db/bookshelf')
- const User = bookshelf.Model.extend({
- tableName: 'users'
- })
- module.exports = User
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement