{"id":20170151,"url":"https://github.com/fabrix-app/spool-sequelize","last_synced_at":"2025-04-10T02:26:07.837Z","repository":{"id":32837665,"uuid":"138081542","full_name":"fabrix-app/spool-sequelize","owner":"fabrix-app","description":"Spool: Sequelize ORM for Fabrix","archived":false,"fork":false,"pushed_at":"2022-12-30T18:56:22.000Z","size":1322,"stargazers_count":7,"open_issues_count":20,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-17T22:55:09.131Z","etag":null,"topics":["fabrix","mysql","orm","postgres","sequelize","spool","spools"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fabrix-app.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-20T20:18:09.000Z","updated_at":"2019-10-24T19:01:06.000Z","dependencies_parsed_at":"2023-01-14T22:23:42.493Z","dependency_job_id":null,"html_url":"https://github.com/fabrix-app/spool-sequelize","commit_stats":null,"previous_names":[],"tags_count":41,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-sequelize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-sequelize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-sequelize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-sequelize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabrix-app","download_url":"https://codeload.github.com/fabrix-app/spool-sequelize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248143677,"owners_count":21054820,"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":["fabrix","mysql","orm","postgres","sequelize","spool","spools"],"created_at":"2024-11-14T01:17:27.770Z","updated_at":"2025-04-10T02:26:07.814Z","avatar_url":"https://github.com/fabrix-app.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spool-sequelize\n\n[![Gitter][gitter-image]][gitter-url]\n[![NPM version][npm-image]][npm-url]\n[![Build Status][ci-image]][ci-url]\n[![Test Coverage][coverage-image]][coverage-url]\n[![Dependency Status][daviddm-image]][daviddm-url]\n[![Follow @FabrixApp on Twitter][twitter-image]][twitter-url]\n\nLoads Application Models (in `api/models`) into the Sequelize ORM; Integrates with [spool-router](https://github.com/fabrix-app/spool-router) to\ngenerate Tapestries for routes.\n\n## Install\n```sh\n$ npm install @fabrix/spool-sequelize --save\n```\n\n## Usage\nSequelize is a SQL orm and this spool uses that to add power to Fabrix models.\n\n### Configure\n\n```js\n// config/main.ts\nimport { SequelizeSpool } from '@fabrix/spool-sequelize'\nexport const main = {\n  // ...\n  spools: [\n    SequelizeSpool\n  ]\n}\n```\n\nA basic `config/store.ts` can be found here : https://github.com/fabrix-app/spool-sequelize/blob/master/archetype/config/stores.ts\n\nA basic `config/models.ts` can be found here : https://github.com/fabrix-app/spool-sequelize/blob/master/archetype/config/models.ts\n\nBut generally, you can configure Sequelize similiarly to this:\n\n```js\n\n// config/stores.ts\nexport const stores = {\n  myStoreName: {\n    orm: 'sequelize', // \u003c-- This is the property that spool-sequelize looks for when building connections \n    // ... General Sequelize Configuration from the library's documentation\n  },\n  // ... Other Store configurations\n}\n\n```\n\n\n### Plugins\nThere are 2 ways to define a plugin for Sequelize in spool-sequelize, for all sequelize connections or just for a particular one:\n\n1) Define `plugins` in `config.sequelize`\n```js\n// config/sequelize.ts\nexport const sequelize = {\n  plugins: {\n    my_global_plugin: require('sequelize-plugin-name')\n  }\n}\n```\n\n2) Define a plugins just for a particular store connection\n```js\n// config/stores.ts\nexport const stores = {\n  uristore: {\n    migrate: 'drop',\n    orm: 'sequelize',\n    uri: 'sqlite://testuser:password@testhost:1234/testdb',\n    plugins: {\n      my_local_plugin: require('sequelize-plugin-name')\n    }\n  }\n}\n```\n\n### Models\n\n```js\nimport { FabrixModel as Model } from '@fabrix/fabrix/dist/common'\nimport { SequelizeResolver } from '@fabrix/spool-sequelize'\n\nexport class User extends Model {\n  // More about supported schema here : http://docs.sequelizejs.com/en/latest/docs/models-definition/\n  static schema (app, Sequelize) {\n    return {\n       name: { type: Sequelize.STRING, allowNull: false },\n       password: Sequelize.STRING,\n       displayName: Sequelize.STRING\n     }\n  }\n\n  static config (app, Sequelize) {\n    return {\n       migrate: 'drop', //override default models configurations if needed\n       store: 'sqlite', //override default models configurations if needed\n       // More informations about supported models options here : http://docs.sequelizejs.com/en/latest/docs/models-definition/#configuration\n       options: {}\n     }\n  }\n  \n  // The Way this model interacts with Sequelize\n  static get resolver () {\n    return SequelizeResolver\n  }\n  \n  // If you need associations, put them here\n  static associate(models) {\n     // More information about associations here : http://docs.sequelizejs.com/en/latest/docs/associations/\n     models.User.hasMany(models.Role, {\n       as: 'roles',\n       onDelete: 'CASCADE',\n       foreignKey: {\n         allowNull: true\n       }\n     })\n  }\n}\n```\n\n### Query\n\n```js\n// api/services/UserService.js\nexport class UserService extends Service {\n  /**\n   * Finds people with the given email.\n   * @return Promise\n   * @example {\n   *    name: 'Ludwig Beethoven',\n   *    email: 'someemail@email.com',\n   *    favoriteColors: [\n   *      { name: 'yellow', hex: 'ffff00' },\n   *      { name: 'black', hex: '000000' }\n   *     ]\n   * }\n   */\n  findUser (email) {\n    //More info about queries here : http://docs.sequelizejs.com/en/latest/docs/models-usage/\n    return this.app.models.User.find({ where: {email: email} })\n  }\n}\n```\n\n## Resolvers \nYou can use the SequleizeResolver to add custom resolutions to your models.\nFor example:\n```js\nimport { SequelizeResolver } from '@fabrix/spool-sequelize'\n\nexport class CustomerResolver extends SequelizeResolver {\n  findHappy(options = {}) {\n    this.findAll({ where: { happy: true} }, options)\n  }\n}\n\n// NOTE: make sure to set CustomerResolver as the resolver on your Fabrix Model!\n```\nNow you can use it.\n```js\nUser.findHappy()\n.then(happy =\u003e {\n  this.app.log.info('This many users are happy', happy.length)\n})\n```\n\n\nFor more information about sequelize queries, please look at [the official documentation](http://docs.sequelizejs.com/en/latest/docs/querying/)\n\n## Tapestries query options\nSome options can be provide as query param for the `find` method, example `GET /api/v1/user`.\n\n### Populate \nYou can add `/api/v1/user?populate=all` to populate all associations or use `/api/v1/user?populate=field1,field2` to populate only some association.\n\n### Pagination\nBy settings `offset` and `limit` you can do some pagination, example `/api/v1/user?offset=10\u0026limit=10` will return only 10 items started from 10 (id 10 to 20). \n\n## Contributing\nWe love contributions! Please check out our [Contributor's Guide](https://github.com/fabrix-app/fabrix/blob/master/CONTRIBUTING.md) for more\ninformation on how our projects are organized and how to get started.\n\n### Release Instructions\nWhen the master is tagged with a release, it will automatically publish to npm, updates the Changelog and bumps the version. Fabrix uses the [standard-version library](https://www.npmjs.com/package/standard-version) to manage it all.\n\nTo run a patch release: \n```bash\nnpm run release -- --release-as patch\n``` \nand then commit to master. `git push --follow-tags origin master`\n\nYou can also test the release by running\n```bash\nnpm run release -- --dry-run --release-as patch\n``` \n\n## License\n[MIT](https://github.com/fabrix-app/spool-sequelize/blob/master/LICENSE)\n\n## Changelog\n[Changelog](https://github.com/fabrix-app/spool-sequelize/blob/master/CHANGELOG.md)\n\n[npm-image]: https://img.shields.io/npm/v/@fabrix/spool-sequelize.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@fabrix/spool-sequelize\n[ci-image]: https://img.shields.io/circleci/project/github/fabrix-app/spool-sequelize/master.svg\n[ci-url]: https://circleci.com/gh/fabrix-app/spool-sequelize/tree/master\n[daviddm-image]: http://img.shields.io/david/fabrix-app/spool-sequelize.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/fabrix-app/spool-sequelize\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/fabrix-app/Lobby\n[twitter-image]: https://img.shields.io/twitter/follow/FabrixApp.svg?style=social\n[twitter-url]: https://twitter.com/FabrixApp\n[coverage-image]: https://img.shields.io/codeclimate/coverage/github/fabrix-app/spool-sequelize.svg?style=flat-square\n[coverage-url]: https://codeclimate.com/github/fabrix-app/spool-sequelize/coverage\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrix-app%2Fspool-sequelize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabrix-app%2Fspool-sequelize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrix-app%2Fspool-sequelize/lists"}