Skip to content

fabrix-app/spool-knex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spool-knex

Gitter NPM version Build Status Test Coverage Dependency Status Follow @FabrixApp on Twitter

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

Install

$ npm install --save @fabrix/spool-knex

Configure

main.ts

// config/main.ts
export const main = {
  spools: [
    // ... other spools
    require('@fabrix/spool-knex').KnexSpool
  ]
}

database.ts

// config/stores.ts
export const stores = {
  knexPostgres: {
    orm: 'knex',
    client: 'pg',

    /**
     * knex connection object
     * see: http://knexjs.org/#Installation-client
     */
    connection: {
      host: 'localhost',
      user: 'admin',
      password: '1234',
      database: 'mydb'
    }
  }
}
// config/models.ts
export const models = {
  /**
   * Supported Migrate Settings:
   * - drop
   * - create
   */
  migrate: 'create',
  defaultStore: 'knexPostgres'
}

Usage

Models

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

// api/models/Role.ts
class Role extends Model {
  static schema (app, 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