{"id":13681498,"url":"https://github.com/mattecapu/dataloader-sequelize-wrapper","last_synced_at":"2026-01-20T15:06:16.595Z","repository":{"id":57224531,"uuid":"70507535","full_name":"mattecapu/dataloader-sequelize-wrapper","owner":"mattecapu","description":"wrap your Sequelize objects in a dataloader caching layer","archived":false,"fork":false,"pushed_at":"2017-07-19T07:29:16.000Z","size":18,"stargazers_count":12,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-21T13:04:18.416Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattecapu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-10T16:35:27.000Z","updated_at":"2019-05-17T02:19:19.000Z","dependencies_parsed_at":"2022-09-04T07:01:27.912Z","dependency_job_id":null,"html_url":"https://github.com/mattecapu/dataloader-sequelize-wrapper","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattecapu%2Fdataloader-sequelize-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattecapu%2Fdataloader-sequelize-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattecapu%2Fdataloader-sequelize-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattecapu%2Fdataloader-sequelize-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattecapu","download_url":"https://codeload.github.com/mattecapu/dataloader-sequelize-wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251635314,"owners_count":21619205,"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":[],"created_at":"2024-08-02T13:01:31.599Z","updated_at":"2026-01-20T15:06:16.560Z","avatar_url":"https://github.com/mattecapu.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# dataloader-sequelize-wrapper\n### wrap your Sequelize objects in a dataloader caching layer\n\n## Install\n```\nnpm install dataloader-sequelize-wrapper --save\n```\n\n## Usage\n```js\nimport Cache from 'dataloader-sequelize-wrapper';\n\n/* you probably want to import this from another file */\nconst mySequelizeOrm = new Sequelize({\n\t// ...\n})\n\nconst cache = new Cache(mySequelizeOrm);\n\n/* provide a primary key to the load() method */\ncache.from('mymodel').load(2)\n\t.then(x =\u003e console.log(x.id));\n\n/* object 2 is not fetched again, object 1 and 3 are */\ncache.from('mymodel').loadMany([ 1, 2, 3 ])\n\t.then(x =\u003e x.forEach(y =\u003e console.log(y.id)));\n\n/* a cached object exposes all the get*() methods */\ncache.from('mymodel').load(4)\n\t/* the associated object is fetched and cached as well */\n\t.then(x =\u003e x.getAssociatedObject())\n\t.then(x =\u003e console.log(x.id)) // 5\n\n/* object 5 is in cache now and thus\nwon't be refetched by the following code */\ncache.from('myassociatedmodel').load(5)\n\t.then(x =\u003e console.log(x.id))\n```\n\n## Features\nThis package provide a smart caching layer which augments your Sequelize objects with cache-aware relationship accessors.\nThis is achieved through:\n\n* **Smart caching**. The cache is aware of your Sequelize schema and thus can intercede also when fetching relationship. Thus objects fetched from an association, like in the example, are cached too. This exponentially improves the hit rate of the cache.\n\n* **Immutability**. No object can be modified to avoid consistency disasters. The only allowed operations are addition and deletion from the cache.\n\n* **Minimal API change**. Your code stays the same, but under the hood Sequelize objects are augmented with caching. If you wrote your code neatly, it will be just matter of converting `sequelize.where` calls to `cache.from.load` calls.\nIf your fetching logic isn't *pure*, immutability will give you problems (but expect problems to always come up from an idempotent interface with a non-idempotent implementation).\n\n## Non-features\nBecause of immutability, accessing mutating methods on an augmented Sequelize object will fire a warning.\nAlso the whole thing is designed to work with full objects, so it doesn't support options for any of the methods it wraps with caching. This is unlikely to change in the future because handling the general case and tracking partial objects is way more difficult and prone to error.\n\n## API\n\n#### `class Cache`\nIt's a container for a dictionary of dataloader-like caches. Each of your Sequelize models gets its cache (lazily).\n\n##### ↪ `from(modelName: string): DataLoader`\nGet the cache for model `modelName`.\n\n\n#### `class DataLoader`\nIt's a [dataloader cache](https://github.com/facebook/dataloader). Objects of this class are returned by `Cache.from`.\n\n##### ↪ `load/loadMany(id: vary): AugmentedSequelizeObject`\nLoad objects with the given IDs to cache and returns a promise to them. Objects returned are augmented objects. Order is preserved when possible.\n\n##### ↪ `clear(id: vary)`\nDelete the object with the given ID from the cache\n\n##### ↪ `clearAll()`\nWipes the cache\n\n\n#### `interface AugmentedSequelizeObject`\nIt's a modified version of a Sequelize object, which exposes every attribute a normal object does but wraps `get*()`, `has*()` and `count*()` accessors for relationships. When invoking `get*()` methods, relationships are also loaded from cache and stored.\nCalling any method with args will disable caching for that call.\nCalling `add*()`, `set*()`, `create*()` and `remove*()` accessors will \"work\" (i.e. won't throw) but will fire a warning and obliterate consistency. Don't do that.\n\n## Contribution\nPRs and issues are welcome! You can always [tweet me](https://twitter.com/user/mattecapu) anyway.\n\n## License\nISC\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattecapu%2Fdataloader-sequelize-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattecapu%2Fdataloader-sequelize-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattecapu%2Fdataloader-sequelize-wrapper/lists"}