{"id":20111146,"url":"https://github.com/devtography/tyodm","last_synced_at":"2026-06-10T10:31:35.235Z","repository":{"id":57108037,"uuid":"384236528","full_name":"Devtography/tyodm","owner":"Devtography","description":"A Realm inspired ODM designed to work with various NoSQL database engines","archived":false,"fork":false,"pushed_at":"2021-09-29T18:51:34.000Z","size":1307,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-13T08:51:10.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Devtography.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["iamWing"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://paypal.me/iamWing0w0"]}},"created_at":"2021-07-08T20:06:41.000Z","updated_at":"2025-01-19T11:29:53.000Z","dependencies_parsed_at":"2022-08-21T05:00:08.472Z","dependency_job_id":null,"html_url":"https://github.com/Devtography/tyodm","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devtography%2Ftyodm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devtography%2Ftyodm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devtography%2Ftyodm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devtography%2Ftyodm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Devtography","download_url":"https://codeload.github.com/Devtography/tyodm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241556023,"owners_count":19981867,"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-11-13T18:14:37.682Z","updated_at":"2026-06-10T10:31:35.180Z","avatar_url":"https://github.com/Devtography.png","language":"TypeScript","funding_links":["https://github.com/sponsors/iamWing","https://paypal.me/iamWing0w0"],"categories":[],"sub_categories":[],"readme":"# TyODM\n\n![npm version](https://img.shields.io/npm/v/@devtography/tyodm)\n\nFully typed opinionated ODM inspired by Realm \u0026 Mongoose. Designed to be \ncompatible with various NoSQL database engines.\n\n## Getting started\n\n```sh\nnpm install @devtography/tyodm\n```\n\n```typescript\nimport * as odm from '@devtography/tyodm';\n\nclass SampleModel extends odm.Obj {\n  static SCHEMA: Schema = {\n    name: 'SampleModel',\n    props: {\n      metadata: {\n        type: 'single',\n        attr: {\n          name: 'string',\n          desc: 'string?',\n          active: 'bool',\n          extra: { someNumber: 'double' },\n        },\n      },\n      itemCollection: {\n        type: 'collection',\n        identifier: 'itemId',\n        attr: { itemId: 'string', name: 'string' },\n      },\n    },\n  };\n\n  metadata?: {\n    name: string,\n    desc?: string,\n    active: boolean,\n    extra?: { someNumber: number }, // Nested object must be optional\n  };\n  itemCollection?: Map\u003cstring, { itemId: string, name: string }\u003e;\n\n  objectSchema(): Schema {\n    return SampleModel.SCHEMA; // Expect to return the same schema from class.\n  }\n}\n\n// Using DynamoDB as sample here.\n// A table with `pk` as `HASH` \u0026 `sk` as `RANGE` is needed to be created first\n// before connecting the `TyODM` instance to the target table.\nconst odm = new odm.TyODM({\n  region: 'us-east-2',\n  endpoint: 'http://localhost:8000',\n  table: 'sample',\n  schema: new Map\u003cstring, odm.Schema\u003e([\n    'SampleModel': SampleModel.SCHEMA,\n  ]),\n});\n\nconst obj = new SampleModel();\nobj.metadata = { name: 'Sample Object', active: true };\nobj.itemCollection = new Map([\n  ['1', { itemId: '1', name: 'item 1' }],\n  ['2', { itemId: '2', name: 'item 2' }],\n]);\n\n(async () =\u003e {\n  await odm.attach();\n\n  await odm.write(() =\u003e {\n    obj.insertObj();\n  });\n\n  const objFromDb = await odm.objectByKey(SampleModel, obj.objectId);\n  console.log(objFromDb);\n})().catch((err) =\u003e {\n  console.log(err);\n});\n```\n\n## Remarks\n\nWhile you can have multiple tables/collections in your database, the way `TyODM`\nhandles the data pretty much follows the single table design. The following\nexample shows how the data schema looks like in your database correspond to the\ndata models.\n\n```typescript\nclass Alpha extends odm.Obj {\n  static SCHEMA: odm.Schema = { \n    name: 'Alpha',\n    props: {\n      singularRecord: {\n        type: 'single',\n        attr: { fieldA: 'string' },\n      },\n      subCat1: {\n        type: 'collection',\n        identifier: 'id',\n        attr: { id: 'string', subFieldA: 'int' }\n      },\n      subCat2: {\n        type: 'collection',\n        identifier: 'id',\n        attr: { id: 'string', subFieldA: 'double', subFieldB: 'int' },\n      },\n    }\n   };\n\n  singularRecord?: { fieldA: string };\n  subCat1?: Map\u003cstring, { id: string, subFieldA: number }\u003e;\n  subCat2?: Map\u003cstring, { id; string, subFieldA: number, subFieldB: string }\u003e;\n\n  ...\n}\n```\n\n| Collection                       | Sub collection |            |               |               |\n|----------------------------------|----------------|------------|---------------|---------------|\n|                                  |                | __fieldA__ |               |               |\n| Alpha#01FCBEBM470CA8BN7B2H95SQ7X | singularRecord | `'abc'`    |               |               |\n|                                  |                | __id__     | __subFieldA__ |               |\n|                                  | subCat1#01A    | `'01A'`    | `10`          |               |\n|                                  | subCat1#01B    | `'01B'`    | `11`          |               |\n|                                  |                | __id__     | __subFieldA__ | __subFieldB__ |\n|                                  | subCat2#02A    | `'02A'`    | `7.62`        | `1`           |\n|                                  | subCat2#02B    | `'02B'`    | `0.45`        | `2`           |\n\n### __Custom identifier \u0026 constructor__\n\nFor the class/collection level unique identifier, by default there's an `ULID`\ngenerated for each instance on initialisation. Alternatively, a custom\nidentifier can also be set via the constructor as following:\n\n```typescript\nclass Beta extends odm.Obj {\n  static SCHEMA: odm.Schema = {\n    name: 'Beta',\n    identifier: 'customId',\n    props: { ... }\n  };\n\n  customId: string;\n\n  constructor(objId?: string) {\n    // Only constructor with optional parameters is allowed. 1st parameter\n    // must be an optional string and needs to be passed to the parent \n    // constructor so the internal functions can set the `objectId` properly.\n    // Constructor with non optional parameters will result in runtime exception.\n    super(objId);\n\n    // It is important to assign value to your custom identifier here instead\n    // of doing it on the line you declare the property. Otherwise the value of\n    // you custom identifier will be overwritten by the value assigned there\n    // when retrieve the object from database.\n    this.customId = 'your custom unique identifier';\n  }\n\n  ...\n}\n```\n\n__Be caution__, by setting your own class/collection level identifier,\nyou must make sure its' value for each instance under the same class is unique,\notherwise you might corrupt your data on the table.\n\nIn terms of sub-collection level identifier, there's no default provided like\nthe class/collection level one. The sub-collection level will have to be\nspecified in the schema as following:\n\n```typescript\nclass Charlie extends odm.Obj {\n  static SCHEMA: odm.Schema = {\n    name: 'Charlie',\n    props: {\n      sub: {\n        type: 'collection',\n        identifier: 'relatedAlpha',\n        attr: { relatedAlpha: 'string', isCharlie: 'bool' },\n      },\n    },\n  };\n\n  sub: Map\u003cstring, { relatedAlpha: string, isCharlie: boolean }\u003e\n  \n  ...\n}\n\n// console.log\n//   Charlie {\n//     sub: { relatedAlpha: '01FCBEBM470CA8BN7B2H95SQ7X', isCharlie: true )\n//   }\n```\n\n*The value can only be `string` for both class/collection and sub-collection\nlevel identifiers. Exception may be thrown if otherwise.*\n\n## Support the project\nContributions via pull requests are welcome and encouraged. If there's anything\nyou consider essential that should be included in this boilerplate, please don't\nhesitate to implement yourself and make a pull request :)\n\nSame as the other open sources projects in [@Devtography], I maintain \u0026 further\ndevelop this boilerplate with my free time. If you found my work useful and\nwanna support me keep on doing all these, please consider\n[donate/sponsor](https://github.com/sponsors/iamWing) me.\n\n## Author\n[Wing Chau](https://github.com/iamWing) [@Devtography]\n\n## License\nTyODM is open source software [licensed as MIT](LICENSE).\n\n[@Devtography]: https://github.com/Devtography\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevtography%2Ftyodm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevtography%2Ftyodm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevtography%2Ftyodm/lists"}