{"id":18435739,"url":"https://github.com/kisiwu/storehouse-sequelize","last_synced_at":"2026-01-06T21:52:33.136Z","repository":{"id":57163032,"uuid":"391618212","full_name":"kisiwu/storehouse-sequelize","owner":"kisiwu","description":"Sequelize (ORM) manager for @storehouse/core.","archived":false,"fork":false,"pushed_at":"2024-05-29T00:12:37.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-29T14:31:07.819Z","etag":null,"topics":["database","nodejs","orm","sequelize"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/kisiwu.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-01T12:15:09.000Z","updated_at":"2024-05-29T00:12:41.000Z","dependencies_parsed_at":"2024-11-06T06:09:15.318Z","dependency_job_id":"4cd9ab57-736f-4da2-807e-0fe6b59c3756","html_url":"https://github.com/kisiwu/storehouse-sequelize","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kisiwu%2Fstorehouse-sequelize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kisiwu%2Fstorehouse-sequelize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kisiwu%2Fstorehouse-sequelize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kisiwu%2Fstorehouse-sequelize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kisiwu","download_url":"https://codeload.github.com/kisiwu/storehouse-sequelize/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245898331,"owners_count":20690466,"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":["database","nodejs","orm","sequelize"],"created_at":"2024-11-06T06:09:12.693Z","updated_at":"2026-01-06T21:52:33.131Z","avatar_url":"https://github.com/kisiwu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# storehouse-sequelize\nSequelize (ORM) manager for @storehouse/core.\n\n#### Note\n\nIf you are already familiar with [sequelize](https://sequelize.org/), code in typescript and define your models by extending the class [Model](https://sequelize.org/master/class/lib/model.js~Model.html), we suggest you don't use this package as you will still need to import your models everytime. However we will still cover that case [here](#extending-model). \n\n\n## Add a manager\n\n```ts\nimport Storehouse from '@storehouse/core';\nimport { SequelizeManager } from '@storehouse/sequelize';\n\n// register\nStorehouse.add({\n  local: {\n    // type: '@storehouse/sequelize' if you called Storehouse.setManagerType(SequelizeManager)\n    type: SequelizeManager, \n    config: {\n      // Options\n      options: {\n        dialect: 'mysql',\n        host: 'localhost',\n        database: 'database',\n        username: 'root',\n        password: '',\n        logging: false\n      },\n\n      // ModelSettings[]\n      models: []\n    }\n  }\n});\n```\n\nAbout **`config`**:\n\n- **`options?`** contains Sequelize's connection [options](https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor).\n- **`models?`** is an object that will help define the models to sequelize. Each object may contain:\n    - **`attributes`**: Model attributes.\n    - **`options?`**: Model options. No need for a connection instance (`sequelize`) here.\n    - **`model?`**: The model class extending [Model](https://sequelize.org/master/class/lib/model.js~Model.html).\n\n\n### Logging\n\nIf you don't apply `logging` option, you can enable the default logs with the package [debug](https://www.npmjs.com/package/debug).\n```ts\nimport debug from 'debug';\ndebug.enable('@storehouse/sequelize*');\n```\n\n## Model definition\n\nAs for sequelize, there are 2 ways of defining models. We will give examples in typescript but the same could be done in javascript.\n\n### Simple usage\n\n```ts\nimport { \n  DataTypes, \n  Model,\n  ModelStatic, \n  ModelAttributes, \n  ModelOptions, \n  Optional \n} from 'sequelize';\nimport Storehouse from '@storehouse/core';\nimport { ModelSettings, SequelizeManager } from '@storehouse/sequelize';\n\ninterface MovieAttributes {\n  id: number;\n  title: string;\n  rate?: number | null;\n}\n\ntype MovieCreationAttributes = Optional\u003cMovieAttributes, 'id'\u003e;\n\ninterface MovieInstance\n  extends Model\u003cMovieAttributes, MovieCreationAttributes\u003e, MovieAttributes { }\n\nconst movieSchema: ModelAttributes\u003cMovieInstance, MovieAttributes\u003e = {\n  id: {\n    type: DataTypes.INTEGER,\n    autoIncrement: true,\n    primaryKey: true\n  },\n  title: {\n    type: DataTypes.STRING,\n    allowNull: false,\n    unique: true\n  },\n  rate: {\n    type: DataTypes.TINYINT,\n    validate: {\n      max: 5,\n      min: 1,\n      isInt: true\n    }\n  }\n};\n\nconst movieOptions: ModelOptions\u003cModel\u003cMovieAttributes, MovieCreationAttributes\u003e\u003e = {\n  modelName: 'movies', // We need to choose the model name\n};\n\nconst movieSettings: ModelSettings\u003cMovieAttributes, MovieCreationAttributes\u003e = {\n  attributes: movieSchema,\n  options: movieOptions\n};\n\nStorehouse.add({\n  local: {\n    type: SequelizeManager, \n    config: {\n      models: [\n        // ModelSettings\n        movieSettings\n      ],\n      options: {\n        // ...\n      }\n    }\n  }\n});\n\n// retrieve a model\nconst Movies = Storehouse.getModel\u003cModelStatic\u003cMovieInstance\u003e\u003e('local', 'movies');\nif (Movies) {\n  const newMovie: MovieCreationAttributes = {\n    title: `Last Knight ${Math.ceil(Math.random() * 1000) + 1}`,\n    rate: 3\n  };\n  const r = await Movies.create(newMovie);\n  console.log('added new movie', r.id, r.title);\n}\n\n// or retrieve the manager\nconst manager = Storehouse.getManager\u003cSequelizeManager\u003e('local');\nif(manager) {\n  // then retrieve the model as\n  manager.getModel\u003cModelStatic\u003cMovieInstance\u003e\u003e('movies');\n  // or\n  manager.getModel\u003cMovieInstance\u003e('movies');\n}\n```\n\n### Extending Model\n\n```ts\nimport { \n  DataTypes, \n  Model, \n  ModelAttributes, \n  ModelOptions, \n  Optional  \n} from 'sequelize';\nimport Storehouse from '@storehouse/core';\nimport { ModelSettings, SequelizeManager } from '@storehouse/sequelize';\n\ninterface MovieAttributes {\n  id: number;\n  title: string;\n  rate?: number | null;\n}\n\ntype MovieCreationAttributes = Optional\u003cMovieAttributes, 'id'\u003e;\n\nclass Movie extends Model\u003cMovieAttributes, MovieCreationAttributes\u003e implements MovieAttributes {\n  id!: number;\n  title!: string;\n  rate?: null | number;\n\n  // timestamps!\n  public readonly createdAt!: Date;\n  public readonly updatedAt!: Date;\n\n  static createMovie(title: string, rate?: number): Promise\u003cMovie\u003e {\n    return Movie.create({ title, rate });\n  }\n\n  get fullTitle(): string {\n    return [this?.id, this?.title].join(' ');\n  }\n}\n\ntype MovieCtor = typeof Movie \u0026 { new(): Movie };\n\nconst movieSchema: ModelAttributes\u003cMovie, MovieAttributes\u003e = {\n  id: {\n    type: DataTypes.INTEGER,\n    autoIncrement: true,\n    primaryKey: true\n  },\n  title: {\n    type: DataTypes.STRING,\n    allowNull: false,\n    unique: true\n  },\n  rate: {\n    type: DataTypes.TINYINT,\n    validate: {\n      max: 5,\n      min: 1,\n      isInt: true\n    },\n  }\n};\n\nconst movieOptions: ModelOptions\u003cModel\u003cMovieAttributes, MovieCreationAttributes\u003e\u003e = {\n  // If \"modelName\" is not specified, \n  // it will be the name of the class extending Model (\"Movie\")\n  modelName: 'movies', \n  tableName: 'movies'\n};\n\nconst movieSettings: ModelSettings\u003cMovieAttributes, MovieCreationAttributes\u003e = {\n  model: Movie,\n  attributes: movieSchema,\n  options: movieOptions\n};\n\nStorehouse.add({\n  local: {\n    type: SequelizeManager, \n    config: {\n      models: [\n        // ModelSettings\n        movieSettings\n      ],\n      options: {\n        // ...\n      }\n    }\n  }\n});\n\n\n// retrieve a model\nconst Movies = Storehouse.getModel\u003cMovieCtor\u003e('local', 'movies');\nif (Movies) {\n  const r = await Movies.createMovie('Movie title', 3);\n  console.log('added new movie', r.fullTitle);\n}\n\n// or retrieve the manager\nconst manager = Storehouse.getManager\u003cSequelizeManager\u003e('local');\nif(manager) {\n  // then retrieve the model as\n  manager.getModel\u003cMovieCtor\u003e('movies');\n}\n```\n\n### SequelizeManager\n\n`SequelizeManager` extends the class [Sequelize](https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html), so you have access to its properties and methods.\n\nExample:\n```ts\nawait Storehouse.getManager\u003cSequelizeManager\u003e('local')?.sync();\n// or\nawait Storehouse.getConnection\u003cSequelize\u003e('local')?.sync();\n// or\nawait Storehouse.getManager\u003cSequelizeManager\u003e('local')?.getConnection().sync();\n```\n\n## References\n\n- [Sequelize](http://sequelize.org/)\n- [@storehouse/core](https://www.npmjs.com/package/@storehouse/core)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkisiwu%2Fstorehouse-sequelize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkisiwu%2Fstorehouse-sequelize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkisiwu%2Fstorehouse-sequelize/lists"}