Provides support for database queries and schema migrations via knex.js.
$ npm install --save trailpack-knex
// config/main.js
module.exports = {
packs: [
// ... other trailpacks
require('trailpack-knex')
]
}
// 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'
}
// 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')
}
}
Create the schema using knex
Drop the schema using knex
Not currently supported.
We love contributions! Please check out our Contributor's Guide for more information on how our projects are organized and how to get started.