Skip to content

trailsjs/trailpack-knex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

trailpack-knex

NPM version Build status Dependency Status Code Climate

Provides support for database queries and schema migrations via knex.js.

Install

$ npm install --save trailpack-knex

Configure

main.js

// config/main.js
module.exports = {
  packs: [
    // ... other trailpacks
    require('trailpack-knex')
  ]
}

database.js

// config/database.js
module.exports = {
  stores: {
    knexPostgres: {
      client: 'pg',

      /**
       * knex connection object
       * see: http://knexjs.org/#Installation-client
       */
      connection: {
        host: 'localhost',
        user: 'admin',
        password: '1234',
        database: 'mydb'
      }
    }
  },

  /**
   * Supported Migrate Settings:
   * - drop
   * - create
   */
  migrate: 'create',
  defaultStore: 'knexPostgres'
}

Usage

Models

// api/models/User.js
class User extends Model {
  static schema (table) {
    table.increments('id').primary()
    table.string('username')
    table.string('firstName')
    table.string('lastName')
  }
}

// api/models/Role.js
class Role extends Model {
  static schema (table) {
    table.increments('id').primary()
    table.string('name')
    table.integer('user_id').references('user.id')
  }
}

Services

SchemaMigrationService

create

Create the schema using knex

drop

Drop the schema using knex

alter

Not currently supported.

Contributing

We love contributions! Please check out our Contributor's Guide for more information on how our projects are organized and how to get started.

License

MIT