{"id":19020237,"url":"https://github.com/trailsjs/trailpack-bookshelf","last_synced_at":"2025-04-23T05:44:37.049Z","repository":{"id":57154031,"uuid":"48298324","full_name":"trailsjs/trailpack-bookshelf","owner":"trailsjs","description":":package: Bookshelf.js Trailpack","archived":false,"fork":false,"pushed_at":"2017-06-15T05:53:47.000Z","size":51,"stargazers_count":4,"open_issues_count":3,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-23T05:44:30.246Z","etag":null,"topics":["bookshelf","orm","trailpack","trails"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trailsjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-19T21:48:00.000Z","updated_at":"2017-01-09T20:19:34.000Z","dependencies_parsed_at":"2022-09-06T12:23:16.646Z","dependency_job_id":null,"html_url":"https://github.com/trailsjs/trailpack-bookshelf","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-bookshelf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-bookshelf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-bookshelf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailsjs%2Ftrailpack-bookshelf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trailsjs","download_url":"https://codeload.github.com/trailsjs/trailpack-bookshelf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250379784,"owners_count":21420841,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bookshelf","orm","trailpack","trails"],"created_at":"2024-11-08T20:16:14.776Z","updated_at":"2025-04-23T05:44:37.028Z","avatar_url":"https://github.com/trailsjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# trailpack-bookshelf\n\n[![Build status][ci-image]][ci-url]\n[![Dependency Status][daviddm-image]][daviddm-url]\n[![codecov.io][codecov-image]][codecov-url]\n\nLoads Application Models (in `api/models`) into the [Bookshelf ORM](http://bookshelfjs.org/); Integrates with [trailpack-router](https://github.com/trailsjs/trailpack-router) to\ngenerate Footprints for routes.\n\n## Install\n```sh\n$ npm install --save trailpack-bookshelf\n```\n\n## Configure\n### main.js\n```js\n// config/main.js\nmodule.exports = {\n  // ...\n  packs: [\n    require('trailpack-bookshelf')\n  ]\n}\n```\n### database.js\n```js\n// config/database.js\nmodule.exports = {\n  stores: {\n    knexPostgres: {\n      orm: 'bookshelf',\n      client: 'pg',\n\n      /**\n       * knex connection object\n       * see: http://knexjs.org/#Installation-client\n       */\n      connection: {\n        host: 'localhost',\n        user: 'admin',\n        password: '1234',\n        database: 'mydb'\n      }\n    }\n  },\n\n  /**\n   * Supported Migrate Settings:\n   * - none\n   * - drop\n   */\n  migrate: 'none',\n  defaultStore: 'knexPostgres'\n}\n```\n## Usage\n### Models\nModels are constructed by `bookshelf.Model.extend()` with values returned by `schema()` as\nthe first argument and values from `config()` as the second argument.\n```js\n// api/models/User.js\nclass User extends Model {\n  static schema(app, table) {\n    //table definition for migrate='drop'\n    if (table) {\n      table.increments('id').primary();\n      table.string('name').notNullable();\n      return\n    } else {\n      // booskelf model prototypeProperties\n      return {\n        profile() {\n          return this.hasOne('profile');\n        }\n      }\n    }\n  }\n}\n\n// api/models/Profile.js\nclass Profile extends Model {\n  static config(app, bookshelf) {\n    // booskelf model classProperties\n    return {\n      tableName: 'user_profile'\n    };\n  }\n  static schema(app, table) {\n    //table definition for migrate='drop'\n    if (table) {\n      table.string('first_name');\n      table.string('last_name');\n      table.integer('user_id').notNullable().references('id').inTable('user');\n      return table;\n    } else {\n      // booskelf model prototypeProperties\n      return {\n        user() {\n          return this.belongsTo('User');\n        }\n      };\n    }\n  }\n}\n```\n### Query\nAfter the trailpack is initialized you can find all your bookshelf models in the `this.app.orm`.\nSee [bookshelf docs](http://bookshelfjs.org/).\n```js\n// api/services/UserService.js\nmodule.exports = {\n  /**\n   * Fetches user with profile by id.\n   * @return Promise\n   * @example {\n   *    name: 'jdoe',\n   *    proflie: {\n   *      first_name: 'John',\n   *      last_name: 'Doe'\n   *    }\n   * }\n   */\n  fetchUserWithProfile(id) {\n    return this.orm.User.forge({ id: id }).fetch({ withRelated: 'profile' });\n  }\n}\n```\n\n## Contributing\nWe love contributions! Please check out our [Contributor's Guide](https://github.com/trailsjs/trails/blob/master/CONTRIBUTING.md) for more\ninformation on how our projects are organized and how to get started.\n\n\n## License\n[MIT](https://github.com/trailsjs/trailpack-waterline/blob/master/LICENSE)\n\n[ci-image]: https://img.shields.io/travis/trailsjs/trailpack-bookshelf/master.svg?style=flat-square\n[ci-url]: https://travis-ci.org/trailsjs/trailpack-bookshelf\n[daviddm-image]: http://img.shields.io/david/trailsjs/trailpack-bookshelf.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/trailsjs/trailpack-bookshelf\n[gitter-image]: http://img.shields.io/badge/+%20GITTER-JOIN%20CHAT%20%E2%86%92-1DCE73.svg?style=flat-square\n[gitter-url]: https://gitter.im/trailsjs/trails\n[codecov-image]: https://codecov.io/github/zuker/trailpack-bookshelf/coverage.svg?branch=master\n[codecov-url]: https://codecov.io/github/zuker/trailpack-bookshelf?branch=master\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailsjs%2Ftrailpack-bookshelf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrailsjs%2Ftrailpack-bookshelf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailsjs%2Ftrailpack-bookshelf/lists"}