Giter Club home page Giter Club logo

trailpack-bookshelf's Introduction

trailpack-bookshelf

Build status Dependency Status codecov.io

Loads Application Models (in api/models) into the Bookshelf ORM; Integrates with trailpack-router to generate Footprints for routes.

Install

$ npm install --save trailpack-bookshelf

Configure

main.js

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

database.js

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

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

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

Usage

Models

Models are constructed by bookshelf.Model.extend() with values returned by schema() as the first argument and values from config() as the second argument.

// api/models/User.js
class User extends Model {
  static schema(app, table) {
    //table definition for migrate='drop'
    if (table) {
      table.increments('id').primary();
      table.string('name').notNullable();
      return
    } else {
      // booskelf model prototypeProperties
      return {
        profile() {
          return this.hasOne('profile');
        }
      }
    }
  }
}

// api/models/Profile.js
class Profile extends Model {
  static config(app, bookshelf) {
    // booskelf model classProperties
    return {
      tableName: 'user_profile'
    };
  }
  static schema(app, table) {
    //table definition for migrate='drop'
    if (table) {
      table.string('first_name');
      table.string('last_name');
      table.integer('user_id').notNullable().references('id').inTable('user');
      return table;
    } else {
      // booskelf model prototypeProperties
      return {
        user() {
          return this.belongsTo('User');
        }
      };
    }
  }
}

Query

After the trailpack is initialized you can find all your bookshelf models in the this.app.orm. See bookshelf docs.

// api/services/UserService.js
module.exports = {
  /**
   * Fetches user with profile by id.
   * @return Promise
   * @example {
   *    name: 'jdoe',
   *    proflie: {
   *      first_name: 'John',
   *      last_name: 'Doe'
   *    }
   * }
   */
  fetchUserWithProfile(id) {
    return this.orm.User.forge({ id: id }).fetch({ withRelated: 'profile' });
  }
}

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.