@mikro-orm/mssql 6.1.13-dev.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,389 @@
1
+ <h1 align="center">
2
+ <a href="https://mikro-orm.io"><img src="https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/logo-readme.svg?sanitize=true" alt="MikroORM" /></a>
3
+ </h1>
4
+
5
+ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite (including libSQL) databases.
6
+
7
+ > Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
8
+
9
+ [![NPM version](https://img.shields.io/npm/v/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
10
+ [![NPM dev version](https://img.shields.io/npm/v/@mikro-orm/core/next.svg)](https://www.npmjs.com/package/@mikro-orm/core)
11
+ [![Chat on discord](https://img.shields.io/badge/chat-on%20discord-blue.svg)](https://discord.gg/w8bjxFHS7X)
12
+ [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
13
+ [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
14
+ [![Maintainability](https://api.codeclimate.com/v1/badges/27999651d3adc47cfa40/maintainability)](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
15
+ [![Build Status](https://github.com/mikro-orm/mikro-orm/workflows/tests/badge.svg?branch=master)](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
16
+
17
+ ## 🤔 Unit of What?
18
+
19
+ You might be asking: _What the hell is Unit of Work and why should I care about it?_
20
+
21
+ > Unit of Work maintains a list of objects (_entities_) affected by a business transaction
22
+ > and coordinates the writing out of changes. [(Martin Fowler)](https://www.martinfowler.com/eaaCatalog/unitOfWork.html)
23
+
24
+ > Identity Map ensures that each object (_entity_) gets loaded only once by keeping every
25
+ > loaded object in a map. Looks up objects using the map when referring to them.
26
+ > [(Martin Fowler)](https://www.martinfowler.com/eaaCatalog/identityMap.html)
27
+
28
+ So what benefits does it bring to us?
29
+
30
+ ### Implicit Transactions
31
+
32
+ First and most important implication of having Unit of Work is that it allows handling transactions automatically.
33
+
34
+ When you call `em.flush()`, all computed changes are queried inside a database transaction (if supported by given driver). This means that you can control the boundaries of transactions simply by calling `em.persistLater()` and once all your changes are ready, calling `flush()` will run them inside a transaction.
35
+
36
+ > You can also control the transaction boundaries manually via `em.transactional(cb)`.
37
+
38
+ ```typescript
39
+ const user = await em.findOneOrFail(User, 1);
40
+ user.email = 'foo@bar.com';
41
+ const car = new Car();
42
+ user.cars.add(car);
43
+
44
+ // thanks to bi-directional cascading we only need to persist user entity
45
+ // flushing will create a transaction, insert new car and update user with new email
46
+ // as user entity is managed, calling flush() is enough
47
+ await em.flush();
48
+ ```
49
+
50
+ ### ChangeSet based persistence
51
+
52
+ MikroORM allows you to implement your domain/business logic directly in the entities. To maintain always valid entities, you can use constructors to mark required properties. Let's define the `User` entity used in previous example:
53
+
54
+ ```typescript
55
+ @Entity()
56
+ export class User {
57
+
58
+ @PrimaryKey()
59
+ id!: number;
60
+
61
+ @Property()
62
+ name!: string;
63
+
64
+ @OneToOne(() => Address)
65
+ address?: Address;
66
+
67
+ @ManyToMany(() => Car)
68
+ cars = new Collection<Car>(this);
69
+
70
+ constructor(name: string) {
71
+ this.name = name;
72
+ }
73
+
74
+ }
75
+ ```
76
+
77
+ Now to create new instance of the `User` entity, we are forced to provide the `name`:
78
+
79
+ ```typescript
80
+ const user = new User('John Doe'); // name is required to create new user instance
81
+ user.address = new Address('10 Downing Street'); // address is optional
82
+ ```
83
+
84
+ Once your entities are loaded, make a number of synchronous actions on your entities,
85
+ then call `em.flush()`. This will trigger computing of change sets. Only entities
86
+ (and properties) that were changed will generate database queries, if there are no changes,
87
+ no transaction will be started.
88
+
89
+ ```typescript
90
+ const user = await em.findOneOrFail(User, 1, {
91
+ populate: ['cars', 'address.city'],
92
+ });
93
+ user.title = 'Mr.';
94
+ user.address.street = '10 Downing Street'; // address is 1:1 relation of Address entity
95
+ user.cars.getItems().forEach(car => car.forSale = true); // cars is 1:m collection of Car entities
96
+ const car = new Car('VW');
97
+ user.cars.add(car);
98
+
99
+ // now we can flush all changes done to managed entities
100
+ await em.flush();
101
+ ```
102
+
103
+ `em.flush()` will then execute these queries from the example above:
104
+
105
+ ```sql
106
+ begin;
107
+ update "user" set "title" = 'Mr.' where "id" = 1;
108
+ update "user_address" set "street" = '10 Downing Street' where "id" = 123;
109
+ update "car"
110
+ set "for_sale" = case
111
+ when ("id" = 1) then true
112
+ when ("id" = 2) then true
113
+ when ("id" = 3) then true
114
+ else "for_sale" end
115
+ where "id" in (1, 2, 3)
116
+ insert into "car" ("brand", "owner") values ('VW', 1);
117
+ commit;
118
+ ```
119
+
120
+ ### Identity Map
121
+
122
+ Thanks to Identity Map, you will always have only one instance of given entity in one context. This allows for some optimizations (skipping loading of already loaded entities), as well as comparison by identity (`ent1 === ent2`).
123
+
124
+ ## 📖 Documentation
125
+
126
+ MikroORM documentation, included in this repo in the root directory, is built with [Docusaurus](https://docusaurus.io) and publicly hosted on GitHub Pages at https://mikro-orm.io.
127
+
128
+ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit messages (via `semantic-release`).
129
+
130
+ ## ✨ Core Features
131
+
132
+ - [Clean and Simple Entity Definition](https://mikro-orm.io/docs/defining-entities)
133
+ - [Identity Map](https://mikro-orm.io/docs/identity-map)
134
+ - [Entity References](https://mikro-orm.io/docs/entity-references)
135
+ - [Using Entity Constructors](https://mikro-orm.io/docs/entity-constructors)
136
+ - [Modelling Relationships](https://mikro-orm.io/docs/relationships)
137
+ - [Collections](https://mikro-orm.io/docs/collections)
138
+ - [Unit of Work](https://mikro-orm.io/docs/unit-of-work)
139
+ - [Transactions](https://mikro-orm.io/docs/transactions)
140
+ - [Cascading persist and remove](https://mikro-orm.io/docs/cascading)
141
+ - [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
142
+ - [Filters](https://mikro-orm.io/docs/filters)
143
+ - [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
144
+ - [Preloading Deeply Nested Structures via populate](https://mikro-orm.io/docs/nested-populate)
145
+ - [Property Validation](https://mikro-orm.io/docs/property-validation)
146
+ - [Lifecycle Hooks](https://mikro-orm.io/docs/lifecycle-hooks)
147
+ - [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
148
+ - [Schema Generator](https://mikro-orm.io/docs/schema-generator)
149
+ - [Entity Generator](https://mikro-orm.io/docs/entity-generator)
150
+
151
+ ## 📦 Example Integrations
152
+
153
+ You can find example integrations for some popular frameworks in the [`mikro-orm-examples` repository](https://github.com/mikro-orm/mikro-orm-examples):
154
+
155
+ ### TypeScript Examples
156
+
157
+ - [Express + MongoDB](https://github.com/mikro-orm/express-ts-example-app)
158
+ - [Nest + MySQL](https://github.com/mikro-orm/nestjs-example-app)
159
+ - [RealWorld example app (Nest + MySQL)](https://github.com/mikro-orm/nestjs-realworld-example-app)
160
+ - [Koa + SQLite](https://github.com/mikro-orm/koa-ts-example-app)
161
+ - [GraphQL + PostgreSQL](https://github.com/driescroons/mikro-orm-graphql-example)
162
+ - [Inversify + PostgreSQL](https://github.com/PodaruDragos/inversify-example-app)
163
+ - [NextJS + MySQL](https://github.com/jonahallibone/mikro-orm-nextjs)
164
+ - [Accounts.js REST and GraphQL authentication + SQLite](https://github.com/darkbasic/mikro-orm-accounts-example)
165
+ - [Nest + Shopify + PostgreSQL + GraphQL](https://github.com/Cloudshelf/Shopify_CSConnector)
166
+
167
+ ### JavaScript Examples
168
+
169
+ - [Express + SQLite](https://github.com/mikro-orm/express-js-example-app)
170
+
171
+ ## 🚀 Quick Start
172
+
173
+ First install the module via `yarn` or `npm` and do not forget to install the database driver as well:
174
+
175
+ > Since v4, you should install the driver package, but not the db connector itself, e.g. install `@mikro-orm/sqlite`, but not `sqlite3` as that is already included in the driver package.
176
+
177
+ ```sh
178
+ yarn add @mikro-orm/core @mikro-orm/mongodb # for mongo
179
+ yarn add @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
180
+ yarn add @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
181
+ yarn add @mikro-orm/core @mikro-orm/postgresql # for postgresql
182
+ yarn add @mikro-orm/core @mikro-orm/mssql # for mssql
183
+ yarn add @mikro-orm/core @mikro-orm/sqlite # for sqlite
184
+ yarn add @mikro-orm/core @mikro-orm/better-sqlite # for better-sqlite
185
+ yarn add @mikro-orm/core @mikro-orm/libsql # for libsql
186
+ ```
187
+
188
+ or
189
+
190
+ ```sh
191
+ npm i -s @mikro-orm/core @mikro-orm/mongodb # for mongo
192
+ npm i -s @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
193
+ npm i -s @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
194
+ npm i -s @mikro-orm/core @mikro-orm/postgresql # for postgresql
195
+ npm i -s @mikro-orm/core @mikro-orm/mssql # for mssql
196
+ npm i -s @mikro-orm/core @mikro-orm/sqlite # for sqlite
197
+ npm i -s @mikro-orm/core @mikro-orm/better-sqlite # for better-sqlite
198
+ npm i -s @mikro-orm/core @mikro-orm/libsql # for libsql
199
+ ```
200
+
201
+ Next, if you want to use decorators for your entity definition, you will need to enable support for [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) as well as `esModuleInterop` in `tsconfig.json` via:
202
+
203
+ ```json
204
+ "experimentalDecorators": true,
205
+ "emitDecoratorMetadata": true,
206
+ "esModuleInterop": true,
207
+ ```
208
+
209
+ Alternatively, you can use [`EntitySchema`](https://mikro-orm.io/docs/entity-schema).
210
+
211
+ Then call `MikroORM.init` as part of bootstrapping your app:
212
+
213
+ > To access driver specific methods like `em.createQueryBuilder()` we need to specify the driver type when calling `MikroORM.init()`. Alternatively we can cast the `orm.em` to `EntityManager` exported from the driver package:
214
+ >
215
+ > ```ts
216
+ > import { EntityManager } from '@mikro-orm/postgresql';
217
+ > const em = orm.em as EntityManager;
218
+ > const qb = em.createQueryBuilder(...);
219
+ > ```
220
+
221
+ ```typescript
222
+ import type { PostgreSqlDriver } from '@mikro-orm/postgresql'; // or any other SQL driver package
223
+
224
+ const orm = await MikroORM.init<PostgreSqlDriver>({
225
+ entities: ['./dist/entities'], // path to your JS entities (dist), relative to `baseDir`
226
+ dbName: 'my-db-name',
227
+ type: 'postgresql',
228
+ });
229
+ console.log(orm.em); // access EntityManager via `em` property
230
+ ```
231
+
232
+ There are more ways to configure your entities, take a look at [installation page](https://mikro-orm.io/docs/installation/).
233
+
234
+ > Read more about all the possible configuration options in [Advanced Configuration](https://mikro-orm.io/docs/configuration) section.
235
+
236
+ Then you will need to fork entity manager for each request so their [identity maps](https://mikro-orm.io/docs/identity-map/) will not collide. To do so, use the `RequestContext` helper:
237
+
238
+ ```typescript
239
+ const app = express();
240
+
241
+ app.use((req, res, next) => {
242
+ RequestContext.create(orm.em, next);
243
+ });
244
+ ```
245
+
246
+ > You should register this middleware as the last one just before request handlers and before any of your custom middleware that is using the ORM. There might be issues when you register it before request processing middleware like `queryParser` or `bodyParser`, so definitely register the context after them.
247
+
248
+ More info about `RequestContext` is described [here](https://mikro-orm.io/docs/identity-map/#request-context).
249
+
250
+ Now you can start defining your entities (in one of the `entities` folders). This is how simple entity can look like in mongo driver:
251
+
252
+ **`./entities/MongoBook.ts`**
253
+
254
+ ```typescript
255
+ @Entity()
256
+ export class MongoBook {
257
+
258
+ @PrimaryKey()
259
+ _id: ObjectID;
260
+
261
+ @SerializedPrimaryKey()
262
+ id: string;
263
+
264
+ @Property()
265
+ title: string;
266
+
267
+ @ManyToOne(() => Author)
268
+ author: Author;
269
+
270
+ @ManyToMany(() => BookTag)
271
+ tags = new Collection<BookTag>(this);
272
+
273
+ constructor(title: string, author: Author) {
274
+ this.title = title;
275
+ this.author = author;
276
+ }
277
+
278
+ }
279
+ ```
280
+
281
+ For SQL drivers, you can use `id: number` PK:
282
+
283
+ **`./entities/SqlBook.ts`**
284
+
285
+ ```typescript
286
+ @Entity()
287
+ export class SqlBook {
288
+
289
+ @PrimaryKey()
290
+ id: number;
291
+
292
+ }
293
+ ```
294
+
295
+ Or if you want to use UUID primary keys:
296
+
297
+ **`./entities/UuidBook.ts`**
298
+
299
+ ```typescript
300
+ import { v4 } from 'uuid';
301
+
302
+ @Entity()
303
+ export class UuidBook {
304
+
305
+ @PrimaryKey()
306
+ uuid = v4();
307
+
308
+ }
309
+ ```
310
+
311
+ More information can be found in [defining entities section](https://mikro-orm.io/docs/defining-entities/) in docs.
312
+
313
+ When you have your entities defined, you can start using ORM either via `EntityManager` or via `EntityRepository`s.
314
+
315
+ To save entity state to database, you need to persist it. Persist takes care or deciding whether to use `insert` or `update` and computes appropriate change-set. Entity references that are not persisted yet (does not have identifier) will be cascade persisted automatically.
316
+
317
+ ```typescript
318
+ // use constructors in your entities for required parameters
319
+ const author = new Author('Jon Snow', 'snow@wall.st');
320
+ author.born = new Date();
321
+
322
+ const publisher = new Publisher('7K publisher');
323
+
324
+ const book1 = new Book('My Life on The Wall, part 1', author);
325
+ book1.publisher = publisher;
326
+ const book2 = new Book('My Life on The Wall, part 2', author);
327
+ book2.publisher = publisher;
328
+ const book3 = new Book('My Life on The Wall, part 3', author);
329
+ book3.publisher = publisher;
330
+
331
+ // just persist books, author and publisher will be automatically cascade persisted
332
+ await em.persistAndFlush([book1, book2, book3]);
333
+ ```
334
+
335
+ To fetch entities from database you can use `find()` and `findOne()` of `EntityManager`:
336
+
337
+ ```typescript
338
+ const authors = em.find(Author, {}, { populate: ['books'] });
339
+
340
+ for (const author of authors) {
341
+ console.log(author); // instance of Author entity
342
+ console.log(author.name); // Jon Snow
343
+
344
+ for (const book of author.books) { // iterating books collection
345
+ console.log(book); // instance of Book entity
346
+ console.log(book.title); // My Life on The Wall, part 1/2/3
347
+ }
348
+ }
349
+ ```
350
+
351
+ More convenient way of fetching entities from database is by using `EntityRepository`, that carries the entity name, so you do not have to pass it to every `find` and `findOne` calls:
352
+
353
+ ```typescript
354
+ const booksRepository = em.getRepository(Book);
355
+
356
+ const books = await booksRepository.find({ author: '...' }, {
357
+ populate: ['author'],
358
+ limit: 1,
359
+ offset: 2,
360
+ orderBy: { title: QueryOrder.DESC },
361
+ });
362
+
363
+ console.log(books); // Loaded<Book, 'author'>[]
364
+ ```
365
+
366
+ Take a look at docs about [working with `EntityManager`](https://mikro-orm.io/docs/entity-manager/) or [using `EntityRepository` instead](https://mikro-orm.io/docs/repositories/).
367
+
368
+ ## 🤝 Contributing
369
+
370
+ Contributions, issues and feature requests are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for submitting pull requests to us.
371
+
372
+ ## Authors
373
+
374
+ 👤 **Martin Adámek**
375
+
376
+ - Twitter: [@B4nan](https://twitter.com/B4nan)
377
+ - Github: [@b4nan](https://github.com/b4nan)
378
+
379
+ See also the list of contributors who [participated](https://github.com/mikro-orm/mikro-orm/contributors) in this project.
380
+
381
+ ## Show Your Support
382
+
383
+ Please ⭐️ this repository if this project helped you!
384
+
385
+ ## 📝 License
386
+
387
+ Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
388
+
389
+ This project is licensed under the MIT License - see the [LICENSE file](LICENSE) for details.
@@ -0,0 +1,16 @@
1
+ import { Type, type EntityProperty } from '@mikro-orm/core';
2
+ export declare class UnicodeString {
3
+ readonly value: string;
4
+ constructor(value: string);
5
+ valueOf(): string;
6
+ toString(): string;
7
+ toJSON(): string;
8
+ [Symbol.toPrimitive](): string;
9
+ }
10
+ export declare class UnicodeStringType extends Type<string | null, string | null> {
11
+ getColumnType(prop: EntityProperty): string;
12
+ convertToJSValue(value: string | null | UnicodeString): string | null;
13
+ convertToDatabaseValue(value: string | null): string | null;
14
+ get runtimeType(): string;
15
+ toJSON(value: string | null | UnicodeString): string | null;
16
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UnicodeStringType = exports.UnicodeString = void 0;
4
+ const core_1 = require("@mikro-orm/core");
5
+ class UnicodeString {
6
+ value;
7
+ constructor(value) {
8
+ this.value = value;
9
+ }
10
+ valueOf() {
11
+ return this.value;
12
+ }
13
+ toString() {
14
+ return this.value;
15
+ }
16
+ toJSON() {
17
+ return this.value;
18
+ }
19
+ [Symbol.toPrimitive]() {
20
+ return this.value;
21
+ }
22
+ }
23
+ exports.UnicodeString = UnicodeString;
24
+ class UnicodeStringType extends core_1.Type {
25
+ getColumnType(prop) {
26
+ const length = prop.length === -1 ? 'max' : (prop.length ?? 255);
27
+ return `nvarchar(${length})`;
28
+ }
29
+ convertToJSValue(value) {
30
+ /* istanbul ignore if */
31
+ if (value instanceof UnicodeString) {
32
+ return value.value;
33
+ }
34
+ return value;
35
+ }
36
+ convertToDatabaseValue(value) {
37
+ if (typeof value === 'string') {
38
+ return new UnicodeString(value);
39
+ }
40
+ return value;
41
+ }
42
+ get runtimeType() {
43
+ return 'string';
44
+ }
45
+ toJSON(value) {
46
+ return this.convertToJSValue(value);
47
+ }
48
+ }
49
+ exports.UnicodeStringType = UnicodeStringType;
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from '@mikro-orm/knex';
2
+ export * from './MsSqlConnection';
3
+ export * from './MsSqlDriver';
4
+ export * from './MsSqlPlatform';
5
+ export * from './MsSqlSchemaHelper';
6
+ export * from './MsSqlExceptionConverter';
7
+ export * from './UnicodeStringType';
8
+ export { MsSqlMikroORM as MikroORM, MsSqlOptions as Options, defineMsSqlConfig as defineConfig, } from './MsSqlMikroORM';
package/index.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.defineConfig = exports.MikroORM = void 0;
18
+ /* istanbul ignore file */
19
+ __exportStar(require("@mikro-orm/knex"), exports);
20
+ __exportStar(require("./MsSqlConnection"), exports);
21
+ __exportStar(require("./MsSqlDriver"), exports);
22
+ __exportStar(require("./MsSqlPlatform"), exports);
23
+ __exportStar(require("./MsSqlSchemaHelper"), exports);
24
+ __exportStar(require("./MsSqlExceptionConverter"), exports);
25
+ __exportStar(require("./UnicodeStringType"), exports);
26
+ var MsSqlMikroORM_1 = require("./MsSqlMikroORM");
27
+ Object.defineProperty(exports, "MikroORM", { enumerable: true, get: function () { return MsSqlMikroORM_1.MsSqlMikroORM; } });
28
+ Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return MsSqlMikroORM_1.defineMsSqlConfig; } });