{"_id":"@egomobile/orm-mssql","_rev":"1-f9e165f10efa2f07cacdb3b39c6d97a2","name":"@egomobile/orm-mssql","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@egomobile/orm-mssql","version":"0.0.1","description":"A MSSQL data adapter and other utilities for @egomobile/orm module.","main":"lib/index.js","engines":{"node":">=18.0.0"},"keywords":["node","js","ts","javascript","typescript","orm","mapper","entity","entities","migration","migrations","db","database","mssql","microsoft sql server","tsql","t-sql","ego"],"scripts":{"build":"del ./lib && tsc","docs:generate":"del ./docs && typedoc --out ./docs ./src","lint":"eslint -c .eslintrc.js --ext .ts src","lint:fix":"eslint --fix -c .eslintrc.js --ext .ts src","sandbox":"nodemon --watch './sandbox/index.ts' ./sandbox/index.ts --inspect=0.0.0.0:9229"},"repository":{"type":"git","url":"git+https://github.com/egomobile/node-orm-mssql.git"},"author":{"name":"Next.e.GO Mobile SE, Aachen, Germany"},"license":"LGPL-3.0","dependencies":{"change-case":"4.1.2","sanitize-filename":"1.6.3"},"devDependencies":{"@egomobile/orm":"^0.13.1","@egomobile/tsconfig":"^5.0.0","@types/node":"18.17.6","@types/mssql":"8.1.2","del-cli":"5.0.0","eslint":"8.48.0","eslint-config-ego":"^0.17.1","nodemon":"3.0.1","mssql":"10.0.0","ts-node":"10.9.1","typedoc":"0.25.1","typescript":"4.7.4"},"peerDependencies":{"@egomobile/orm":">= 0.13.1","mssql":">= 10.0.0"},"types":"./lib/index.d.ts","gitHead":"ae618c56cb925b9bff895eab7b7e80e9258680fb","bugs":{"url":"https://github.com/egomobile/node-orm-mssql/issues"},"homepage":"https://github.com/egomobile/node-orm-mssql#readme","_id":"@egomobile/orm-mssql@0.0.1","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-n+SzKQ9PhEuHh8ICmSd+PSM2BNlF9BnksRPRADuljt6TV8nvm+7leAZ8ShwXt/77FeTtKgGlQHG6MgB1JiRkKQ==","shasum":"22add0057306d9bf2adfb1a5a2bb26415667209c","tarball":"https://registry.npmjs.org/@egomobile/orm-mssql/-/orm-mssql-0.0.1.tgz","fileCount":24,"unpackedSize":83584,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDSA/G8PX3qcWwA/VgSyfYJJw7jVae33rgRDxmpzmJruAiAm3WOMoPc21slqvFaNXc6pFz3jDuyqvESxtHmVxkLZsA=="}]},"_npmUser":{"name":"egodux","email":"dux-developer@e-go-mobile.com"},"directories":{},"maintainers":[{"name":"ekmobile","email":"eric.kesseler@e-go-mobile.com"},{"name":"egodux","email":"dux-developer@e-go-mobile.com"},{"name":"mkloubertego","email":"marcel.kloubert@e-go-mobile.com"},{"name":"mkloubert","email":"marcel.kloubert@gmx.net"},{"name":"ekegodigital","email":"eric.kesseler@e-go-digital.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/orm-mssql_0.0.1_1694101304966_0.34494044484787034"},"_hasShrinkwrap":false}},"time":{"created":"2023-09-07T15:41:44.853Z","0.0.1":"2023-09-07T15:41:45.250Z","modified":"2024-05-22T18:03:47.851Z"},"maintainers":[{"email":"eric.kesseler@e-go-mobile.com","name":"ekmobile"},{"email":"dux-developer@e-go-mobile.com","name":"egodux"},{"email":"marcel.kloubert@gmx.net","name":"mkloubert"},{"email":"eric.kesseler@e-go-digital.com","name":"ekegodigital"}],"description":"A MSSQL data adapter and other utilities for @egomobile/orm module.","homepage":"https://github.com/egomobile/node-orm-mssql#readme","keywords":["node","js","ts","javascript","typescript","orm","mapper","entity","entities","migration","migrations","db","database","mssql","microsoft sql server","tsql","t-sql","ego"],"repository":{"type":"git","url":"git+https://github.com/egomobile/node-orm-mssql.git"},"author":{"name":"Next.e.GO Mobile SE, Aachen, Germany"},"bugs":{"url":"https://github.com/egomobile/node-orm-mssql/issues"},"license":"LGPL-3.0","readme":"[![npm](https://img.shields.io/npm/v/@egomobile/orm-mssql.svg)](https://www.npmjs.com/package/@egomobile/orm-mssql)\n[![last build](https://img.shields.io/github/workflow/status/egomobile/node-orm-mssql/Publish)](https://github.com/egomobile/node-orm-mssql/actions?query=workflow%3APublish)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/egomobile/node-orm-mssql/pulls)\n\n# @egomobile/orm-mssql\n\n> A Microsoft SQL Server data adapter and other utilities for [@egomobile/orm](https://github.com/egomobile/node-orm) module.\n\n## Install\n\nExecute the following command from your project folder, where your `package.json` file is stored:\n\n```bash\nnpm install --save @egomobile/orm-mssql\n```\n\nThe following modules are defined in [peerDependencies](https://nodejs.org/uk/blog/npm/peer-dependencies/) and have to be installed manually:\n\n- [@egomobile/orm](https://github.com/egomobile/node-orm)\n- [mssql](https://github.com/tediousjs/node-mssql)\n\n## Usage\n\n```typescript\nimport { createDataContext } from \"@egomobile/orm\";\nimport { MSSQLDataAdapter } from \"@egomobile/orm-mssql\";\nimport type { IResult } from \"mssql\";\n\nclass User {\n  // non-nullable fields\n  public id: number = undefined!;\n  public first_name: string = undefined!;\n  public last_name: string = undefined!;\n\n  // nullable fields\n  public email: string | null = undefined!;\n}\n\nasync function main() {\n  const context = await createDataContext({\n    adapter: new MSSQLDataAdapter(),\n    entities: {\n      // name of the entity / table\n      users: {\n        ids: [\"id\"], // list of column(s) which represent the ID\n        type: User, // the class / type to use to create objects from\n      },\n    },\n    noDbNull: true,\n  });\n\n  const listOfUsers: User[] = await context.find(User, {\n    // WHERE clause\n    where: \"[is_active]=@_1 AND [is_deleted]=@_2\",\n    params: [true, false], // @_1, @_2\n\n    offset: 1, // skip the first\n    limit: 100, // only return 100 rows\n  });\n\n  // return a user with ID 5979\n  const specificUser: User | null = await context.findOne(User, {\n    where: \"[id]=@_1\",\n    params: [5979], // @_1\n  });\n\n  if (specificUser !== null) {\n    // update with new data\n    specificUser.last_name = \"Doe\";\n    specificUser.first_name = \"Jane\";\n    specificUser.email = null;\n    await context.update(specificUser);\n\n    // remove from database\n    await context.remove(specificUser);\n  } else {\n    console.log(\"User not found\");\n  }\n}\n\n// create new POCO\nconst newUser = new User();\nnewUser.first_name = \"John\";\nnewUser.last_name = \"Doe\";\n// ... and add it to database\nawait context.insert(newUser);\n\n// do raw queries\nconst result: IResult<any> = await context.query(\n  \"SELECT * FROM [users] WHERE [id]=@_1 AND [is_active]=@_2;\",\n  23979,\n  true\n);\nconsole.log(result);\n\nmain().catch(console.error);\n```\n\n## Migrations\n\nBefore you can use migrations, first keep sure to have an existing `migrations` table in your database:\n\n```sql\nCREATE TABLE [dbo].[migrations] (\n    [id]        BIGINT         IDENTITY (1, 1) NOT NULL,\n    [timestamp] BIGINT         NOT NULL,\n    [name]      NVARCHAR (MAX) NOT NULL,\n    CONSTRAINT [PK_migrations] PRIMARY KEY CLUSTERED ([id] ASC)\n);\n```\n\nA quick example how to use [MSSQLMigrationContext class](https://egomobile.github.io/node-orm-mssql/classes/MSSQLMigrationContext.html):\n\n```typescript\nimport { MSSQLDataAdapter, MSSQLMigrationContext } from \"@egomobile/orm-mssql\";\n\nasync function main() {\n  const context = new MSSQLMigrationContext({\n    // a default adapter\n    adapter: new MSSQLDataAdapter(),\n\n    // scan for .js files\n    // inside ./migration subfolder\n    // with the following format:\n    //\n    // <UNIX-TIMESTAMP>-<NAME-OF-THE-MIGRATION>.js\n    //\n    // example: 1746942104690-CreateUserTable.js\n    migrations: __dirname + \"/migration\",\n\n    table: \"migrations\",\n  });\n\n  // UP-GRADE database\n  await context.up();\n\n  // DOWN-GRADE database\n  await context.down();\n}\n\nmain().catch(console.error);\n```\n\nA migration file looks like this:\n\n```javascript\n/**\n * Function to UP-GRADE the database.\n */\nmodule.exports.up = async (context) => {\n  // context => https://egomobile.github.io/node-orm/interfaces/IDataContext.html\n\n  await context.query(`\nCREATE TABLE [dbo].[tdta_user]\n(\n    [id] BIGINT IDENTITY (1, 1) NOT NULL,\n    [email] NVARCHAR (MAX) NOT NULL\n);\n`);\n};\n\n/**\n * Function to DOWN-GRADE the database.\n */\nmodule.exports.down = async (context) => {\n  // context => https://egomobile.github.io/node-orm/interfaces/IDataContext.html\n\n  await context.query(`DROP TABLE [dbo].[tdta_user];`);\n};\n```\n\nYou are also able to create a migration file programmatically:\n\n```typescript\nimport { createNewMigrationFile } from \"@egomobile/orm-mssql\";\n\nconst newFilePath = await createNewMigrationFile(\"the name of the migration\", {\n  // create output file inside ./migrations sub folder\n  dir: __dirname + \"/migrations\",\n\n  // generate and add optional header and footer to the file\n  header: ({ name, timestamp }) =>\n    `// Hello, this is migration '${name}' created on ${timestamp}\\n\\n`,\n  footer: \"\\n\\n// Copyright (x) e.GO Mobile SE, Aachen, Germany\\n\\n\",\n});\n\nconsole.log(\"Migration file has been created in\", newFilePath);\n```\n\n## Documentation\n\nThe API documentation can be found [here](https://egomobile.github.io/node-orm-mssql/).\n","readmeFilename":"README.md"}