{"id":17714508,"url":"https://github.com/tenry92/orm-js","last_synced_at":"2025-03-13T23:30:40.676Z","repository":{"id":43134197,"uuid":"63898426","full_name":"tenry92/orm-js","owner":"tenry92","description":"Database abstraction layer using decorators.","archived":false,"fork":false,"pushed_at":"2016-09-05T18:25:11.000Z","size":15,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-02-22T22:03:35.950Z","etag":null,"topics":["database","entity","orm","orm-library","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/tenry92.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-07-21T19:55:21.000Z","updated_at":"2022-09-16T10:48:26.000Z","dependencies_parsed_at":"2022-09-26T17:00:54.716Z","dependency_job_id":null,"html_url":"https://github.com/tenry92/orm-js","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/tenry92%2Form-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tenry92%2Form-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tenry92%2Form-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tenry92%2Form-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tenry92","download_url":"https://codeload.github.com/tenry92/orm-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243499734,"owners_count":20300681,"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","entity","orm","orm-library","typescript"],"created_at":"2024-10-25T11:15:04.341Z","updated_at":"2025-03-13T23:30:40.380Z","avatar_url":"https://github.com/tenry92.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# orm-js\n\nNote: this module is still in the early alpha phase and still very experimental.\nSupport for JavaScript (i.e., not using TypeScript) isn't provided yet.\n\nThis module allows you to map your entity classes, as written in JavaScript or\nTypeScript, to a database schema, using decorators. Support for SQLite is\navailable through the separate module `orm-js-sqlite`, support for other\ndatabases is planned.\n\n*orm-js* is designed to work with JavaScript *Promise*. It's suitable to work\nwith `async/await` in TypeScript.\n\n\n## Introduction\n\nMany web applications consist of code (such as JavaScript) and data stored in a\ndatabase system (such as MySQL, SQLite). *orm-js* provides you some abstraction\nin order to save your data object to the database or retieve the data mapped\nback to an object. It also takes care for the database creation and the\nrelations between the various tables (entities).\n\nTake a look at the following code snippet:\n\n~~~ts\nimport * as orm from 'orm-js/decorators';\n\n\n@orm.entity() // mark the following class as an entity\nexport default class User {\n  @orm.field()\n  @orm.id()\n  id: number;\n  \n  @orm.field()\n  userName: string;\n  \n  @orm.field()\n  emailAddress: string;\n  \n  @orm.field()\n  passwordHash: string;\n}\n~~~\n\nThe entity name (`User`) as well as the property names and types are\nautomatically determined by *orm-js*, you don't need to take care of the naming\n(unless you have two entities with the same name).\n\nProperties missing the `@orm.field()` decorator will not be saved to the\ndatabase, the field won't be created in the schema. You can optionally use\n`@orm.id()` for a single field to mark it as the primary key, which is used to\nuniquely identify a single data record.\n\nYou can declare your entities in several JavaScript or TypeScript files or put\nseveral of them in a single file. It's your responsibility to load all these\nfiles in order to use the *orm-js* system. For example, you can put all your\nentity files in a single directory, which you can scan for files to `require()`.\nIn this example, a simple `require('./user')` is enough to tell `orm-js` about\nthis entity (because of the `@orm.entity()` decorator).\n\nAfter you've loaded all your entities, you can connect to your database and use\n`orm-js`.\n\n\n## Database Connection\n\n*orm-js* itself does not provide connection to a database. Currently there's\nonly `orm-js-sqlite` available. Before you can use *orm-js* (you may load your\nentities before, however), you have to bind a database connection:\n\n~~~ts\nimport * as orm from 'orm-js/orm';\nimport SqliteDatabase from 'orm-js-sqlite';\n\nlet connection = new SqliteDatabase('database.db');\norm.setDatabase(connection);\n\n(async () =\u003e {\n  try {\n    await orm.connect();\n    console.log('Database is connected!');\n  } catch(err) {\n    console.error(err);\n  }\n})();\n~~~\n\n\n## Schema Creation\n\nThe database schema (the tables, fields, relations etc.) is managed by `orm-js`,\nyou usually can't use any existing schema. After you've loaded your entities and\nconnected to your database, you use `build()` to create a new database schema:\n\n~~~ts\nimport * as orm from 'orm-js/orm';\n\n(async () =\u003e {\n  try {\n    await orm.build();\n    console.log('Schema created!');\n  } catch(err) {\n    console.error(err);\n  }\n})();\n~~~\n\n\n## Repositories\n\nIn order to get or save entity objects, you can use the `Repository` class\nprovided by `orm-js/orm`. For each entity, you have to instanciate a new\n`Repository` with the entity class passed as its parameter. The following\nexample creates a new user and retrieves it back from the database:\n\n~~~ts\nimport * as orm from 'orm-js/orm';\nimport User from './user';\n\n(async () =\u003e {\n  let repo = new orm.Repository(User);\n  \n  let user = new User();\n  user.userName = 'Hero Brain';\n  user.emailAddress = 'hero@brain.net';\n  \n  // you can use insert() for both new and updating entities\n  await repo.insert(user);\n  \n  // a numeric ID field is automatically set by the insert operation\n  console.log(`User got the id: ${user.id}`);\n  \n  // get an array of User entities\n  let userList = await repo.findAll();\n  console.log(userList);\n})();\n~~~\n\n\n## Install\n\nVia npm:\n\n    $ npm install orm-js\n\nYou'll need to install a database connector for *orm-js*, in order to connect to\nan actual database. Currently there's only `orm-js-sqlite` available:\n\n    $ npm install orm-js-sqlite\n\nWhen using TypeScript, make sure to enable both `experimentalDecorators` and\n`emitDecoratorMetadata` in your tsconfig.json:\n\n~~~json\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true\n  }\n}\n~~~\n\n\n## License\n\norm-js is licensed under the MIT License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftenry92%2Form-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftenry92%2Form-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftenry92%2Form-js/lists"}