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

trailpack-bookshelf's People

Contributors

greenkeeperio-bot avatar jamesjwarren avatar jaumard avatar tjwebb avatar zuker avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

zuker

trailpack-bookshelf's Issues

hasTimestamps config is broken

Setting timestamps in model config doesn't override global setting:

static config () {
  return {
    hasTimestamps: ['created', 'updated']
  };
}

Error running bookshelf - Not a aclass

Trying to get baseline working with Bookshelf after install got error...

trail pack Bookshelf validationError: migrate is not allowed. defaultStore is not allowed

Then added the user, and profile from example .. After npm start get ...

throw new Error(${resourceName} should be a class. It is a regular object)
^

Error: User should be a class. It is a regular object
at _.mapValues (...S\trails_ems2\node_modules\trails\lib\core.js:60:15)

Package versions ..
"dependencies": {
"express": "^5.0.0-alpha.2",
"trailpack-bookshelf": "^1.0.0-alpha-1",
"trailpack-express": "^2.0.0-rc3",
"trailpack-repl": "v2-latest",
"trailpack-router": "v2-latest",
"trails": "v2-latest",
"winston": "^2.3"
},

XXXXXX node version ...

{
"app": "0.0.0",
"node": "v6.2.1",
"libs": {
"http_parser": "2.7.0",
"node": "6.2.1",
"v8": "5.0.71.52",
"uv": "1.9.1",
"zlib": "1.2.8",
"ares": "1.10.1-DEV",
"icu": "57.1",
"modules": "48",
"openssl": "1.0.2h"
},
"trailpacks": [
{
"name": "repl",
"version": "2.1.0"
},
{
"name": "router",
"version": "2.1.0"
},
{
"name": "express",
"version": "2.0.0-rc3"
}
]
}

README is wrong

return this.orm.Person.forge({ name }) - not compatible with node 4

Doc comment is wrong
Finds people with the given birthday.
...
findPeopleByName(name)

Any roadmap on this?

Glad to see trails decided to provide an official trailpack for bookshelf. Any roadmap on this?

ID attribute can only be incremental

The schema migration service implementation limits the ability to modify the type of field used for the ID attribute. As it's defined as incremental here, it is not possible to override it in a model's schema. Unless there is a way around this that i have missed? I would suggest we take this definition out of here and leave it to be implemented in the model schema.

Deploy on npm

@tjwebb can you update travis config file to allow npm deployment ? And also accept #6 please

Thanks.

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.