@sqb/connect 4.0.13 → 4.1.0

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.
Files changed (82) hide show
  1. package/dist/client/Adapter.d.ts +1 -1
  2. package/dist/client/Cursor.d.ts +2 -2
  3. package/dist/client/Cursor.js +3 -3
  4. package/dist/client/CursorStream.js +1 -1
  5. package/dist/client/SqbClient.d.ts +2 -2
  6. package/dist/client/SqbClient.js +5 -5
  7. package/dist/client/SqbConnection.d.ts +3 -3
  8. package/dist/client/SqbConnection.js +7 -7
  9. package/dist/client/helpers.d.ts +2 -2
  10. package/dist/client/types.d.ts +4 -4
  11. package/dist/index.d.ts +19 -15
  12. package/dist/index.js +20 -16
  13. package/dist/orm/base-entity.d.ts +8 -0
  14. package/dist/orm/base-entity.js +28 -0
  15. package/dist/orm/commands/command.helper.d.ts +4 -4
  16. package/dist/orm/commands/command.helper.js +5 -3
  17. package/dist/orm/commands/count.command.d.ts +3 -3
  18. package/dist/orm/commands/create.command.d.ts +4 -4
  19. package/dist/orm/commands/create.command.js +13 -7
  20. package/dist/orm/commands/destroy.command.d.ts +3 -3
  21. package/dist/orm/commands/find.command.d.ts +9 -9
  22. package/dist/orm/commands/find.command.js +13 -11
  23. package/dist/orm/commands/row-converter.d.ts +4 -4
  24. package/dist/orm/commands/update.command.d.ts +5 -5
  25. package/dist/orm/commands/update.command.js +7 -5
  26. package/dist/orm/decorators/column.decorator.d.ts +2 -2
  27. package/dist/orm/decorators/column.decorator.js +13 -4
  28. package/dist/orm/decorators/embedded.decorator.d.ts +3 -2
  29. package/dist/orm/decorators/embedded.decorator.js +6 -7
  30. package/dist/orm/decorators/entity.decorator.d.ts +31 -4
  31. package/dist/orm/decorators/entity.decorator.js +142 -5
  32. package/dist/orm/decorators/events.decorator.js +13 -25
  33. package/dist/orm/decorators/foreignkey.decorator.js +3 -3
  34. package/dist/orm/decorators/index.decorator.d.ts +3 -3
  35. package/dist/orm/decorators/index.decorator.js +5 -6
  36. package/dist/orm/decorators/link.decorator.d.ts +1 -1
  37. package/dist/orm/decorators/link.decorator.js +3 -3
  38. package/dist/orm/decorators/primarykey.decorator.d.ts +3 -3
  39. package/dist/orm/decorators/primarykey.decorator.js +7 -6
  40. package/dist/orm/decorators/transform.decorator.js +5 -5
  41. package/dist/orm/model/association-element-metadata.d.ts +10 -0
  42. package/dist/orm/model/association-element-metadata.js +15 -0
  43. package/dist/orm/model/association.d.ts +6 -6
  44. package/dist/orm/model/association.js +9 -8
  45. package/dist/orm/model/{entity-column-element.d.ts → column-element-metadata.d.ts} +21 -10
  46. package/dist/orm/model/column-element-metadata.js +32 -0
  47. package/dist/orm/model/element-metadata.d.ts +7 -0
  48. package/dist/orm/model/element-metadata.js +2 -0
  49. package/dist/orm/model/embedded-element-metadata.d.ts +14 -0
  50. package/dist/orm/model/embedded-element-metadata.js +28 -0
  51. package/dist/orm/model/entity-metadata.d.ts +49 -0
  52. package/dist/orm/model/entity-metadata.js +329 -0
  53. package/dist/orm/model/index-metadata.d.ts +18 -0
  54. package/dist/orm/model/index-metadata.js +2 -0
  55. package/dist/orm/orm.const.d.ts +1 -1
  56. package/dist/orm/orm.const.js +3 -3
  57. package/dist/orm/orm.type.d.ts +1 -92
  58. package/dist/orm/repository.class.d.ts +5 -4
  59. package/dist/orm/repository.class.js +27 -5
  60. package/dist/orm/util/apply-mixins.d.ts +1 -0
  61. package/dist/orm/util/apply-mixins.js +12 -0
  62. package/dist/orm/util/extract-keyvalues.d.ts +2 -2
  63. package/dist/orm/util/extract-keyvalues.js +3 -2
  64. package/dist/orm/util/orm.helper.d.ts +8 -9
  65. package/dist/orm/util/orm.helper.js +8 -9
  66. package/dist/orm/util/serialize-element.d.ts +2 -2
  67. package/package.json +10 -10
  68. package/dist/orm/base-entity.class.d.ts +0 -16
  69. package/dist/orm/base-entity.class.js +0 -56
  70. package/dist/orm/model/abstract-entityelement.d.ts +0 -8
  71. package/dist/orm/model/abstract-entityelement.js +0 -10
  72. package/dist/orm/model/entity-association-element.d.ts +0 -9
  73. package/dist/orm/model/entity-association-element.js +0 -13
  74. package/dist/orm/model/entity-column-element.js +0 -27
  75. package/dist/orm/model/entity-model.d.ts +0 -59
  76. package/dist/orm/model/entity-model.js +0 -295
  77. package/dist/orm/model/entity-object-element.d.ts +0 -12
  78. package/dist/orm/model/entity-object-element.js +0 -24
  79. package/dist/orm/model/index-meta.d.ts +0 -9
  80. package/dist/orm/model/index-meta.js +0 -16
  81. package/dist/orm/util/entity-mapping.d.ts +0 -12
  82. package/dist/orm/util/entity-mapping.js +0 -154
@@ -1,5 +1,5 @@
1
1
  import { Type } from 'ts-gems';
2
- import { DataType } from '..';
2
+ export declare type Ctor = Type | Function;
3
3
  export declare type ElementKind = 'column' | 'object' | 'association';
4
4
  export declare type AssociationKind = 'to' | 'to-many' | 'from' | 'from-many';
5
5
  /**
@@ -12,30 +12,6 @@ export declare type TypeThunk<T = any> = Type<T> | TypeResolver<T>;
12
12
  export declare type EnumValue = (FieldValue)[] | Object;
13
13
  export declare type FieldValue = string | number | boolean | Date | null;
14
14
  export declare type DefaultValueGetter = (obj?: any) => FieldValue;
15
- export interface EntityConfig {
16
- /**
17
- * Name of the table name. Default: entity class name.
18
- */
19
- tableName?: string;
20
- /**
21
- * Schema name which entity belongs to
22
- */
23
- schema?: string;
24
- /**
25
- * Table comment
26
- */
27
- comment?: string;
28
- }
29
- export interface IndexOptions {
30
- /**
31
- * Name of the index
32
- */
33
- name?: string;
34
- /**
35
- * Specifies if index is unique
36
- */
37
- unique?: boolean;
38
- }
39
15
  export interface AssociationSettings {
40
16
  source: TypeThunk;
41
17
  target: TypeThunk;
@@ -43,70 +19,3 @@ export interface AssociationSettings {
43
19
  targetKey?: string;
44
20
  kind?: AssociationKind;
45
21
  }
46
- export interface DataPropertyOptions {
47
- type?: Function;
48
- /**
49
- * Column data type
50
- */
51
- dataType?: DataType;
52
- /**
53
- *
54
- */
55
- isArray?: boolean;
56
- /**
57
- * Field name in the database table. Default: property name
58
- */
59
- fieldName?: string;
60
- /**
61
- * Field comment
62
- */
63
- comment?: string;
64
- /**
65
- * Character or byte length of column
66
- */
67
- length?: number;
68
- /**
69
- * Column's default value
70
- */
71
- default?: FieldValue | DefaultValueGetter;
72
- /**
73
- * The precision for a decimal field
74
- */
75
- precision?: number;
76
- /**
77
- * The scale for a decimal field
78
- */
79
- scale?: number;
80
- /**
81
- * Fields's collation.
82
- */
83
- collation?: string;
84
- /**
85
- * Indicates auto generation strategy
86
- */
87
- autoGenerated?: ColumnAutoGenerationStrategy;
88
- /**
89
- * Indicates enum values
90
- */
91
- enum?: EnumValue;
92
- /**
93
- * Indicates if column value can be null
94
- */
95
- notNull?: boolean;
96
- /**
97
- * Indicates whether or not to hide this column by default when making queries.
98
- */
99
- hidden?: boolean;
100
- /**
101
- * Indicates if column value is used in update queries
102
- */
103
- noUpdate?: boolean;
104
- /**
105
- * Indicates if column value is used in insert queries
106
- */
107
- noInsert?: boolean;
108
- }
109
- export interface EmbeddedTypeOptions {
110
- fieldNamePrefix?: string;
111
- fieldNameSuffix?: string;
112
- }
@@ -1,10 +1,10 @@
1
1
  import { DeepPartial, Type } from 'ts-gems';
2
+ import { FieldInfoMap } from '../client/FieldInfoMap';
2
3
  import { SqbClient } from '../client/SqbClient';
3
4
  import { SqbConnection } from '../client/SqbConnection';
4
- import { EntityModel } from './model/entity-model';
5
5
  import { QueryRequest, TransactionFunction } from '../client/types';
6
6
  import { InstanceValues } from '../types';
7
- import { FieldInfoMap } from '../client/FieldInfoMap';
7
+ import { EntityMetadata } from './model/entity-metadata';
8
8
  export declare namespace Repository {
9
9
  type TransformRowFunction = (fields: FieldInfoMap, row: object, obj: object) => void;
10
10
  interface CommandOptions {
@@ -72,8 +72,8 @@ export declare class Repository<T> extends Repository_base {
72
72
  private readonly _executor;
73
73
  private readonly _entity;
74
74
  private readonly _schema?;
75
- constructor(entityDef: EntityModel, executor: SqbClient | SqbConnection, schema?: string);
76
- get entity(): EntityModel;
75
+ constructor(entityDef: EntityMetadata, executor: SqbClient | SqbConnection, schema?: string);
76
+ get entity(): EntityMetadata;
77
77
  get type(): Type<T>;
78
78
  create(values: InstanceValues<T>, options?: Repository.CreateOptions): Promise<DeepPartial<T>>;
79
79
  createOnly(values: InstanceValues<T>, options?: Repository.CreateOptions): Promise<void>;
@@ -121,5 +121,6 @@ export declare class Repository<T> extends Repository_base {
121
121
  protected _updateAll(values: InstanceValues<T>, options: Repository.UpdateAllOptions & {
122
122
  connection: SqbConnection;
123
123
  }): Promise<number>;
124
+ protected _emit(event: string, ...args: any[]): Promise<void>;
124
125
  }
125
126
  export {};
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Repository = void 0;
4
4
  const strict_typed_events_1 = require("strict-typed-events");
5
5
  const SqbConnection_1 = require("../client/SqbConnection");
6
- const extract_keyvalues_1 = require("./util/extract-keyvalues");
7
6
  const count_command_1 = require("./commands/count.command");
8
7
  const create_command_1 = require("./commands/create.command");
9
- const find_command_1 = require("./commands/find.command");
10
8
  const destroy_command_1 = require("./commands/destroy.command");
9
+ const find_command_1 = require("./commands/find.command");
11
10
  const update_command_1 = require("./commands/update.command");
11
+ const extract_keyvalues_1 = require("./util/extract-keyvalues");
12
12
  class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
13
13
  constructor(entityDef, executor, schema) {
14
14
  super();
@@ -102,6 +102,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
102
102
  });
103
103
  }
104
104
  async _create(values, options) {
105
+ await this._emit('before-create', values, options);
105
106
  const keyValues = await create_command_1.CreateCommand.execute({
106
107
  ...options,
107
108
  entity: this._entity,
@@ -111,9 +112,11 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
111
112
  const result = keyValues && (await this.findByPk(keyValues, options));
112
113
  if (!result)
113
114
  throw new Error('Unable to insert new row');
115
+ await this._emit('after-create', result, options);
114
116
  return result;
115
117
  }
116
118
  async _createOnly(values, options) {
119
+ await this._emit('before-create', values, options);
117
120
  await create_command_1.CreateCommand.execute({
118
121
  ...options,
119
122
  entity: this._entity,
@@ -166,6 +169,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
166
169
  return await this._findOne({ ...options, filter, offset: 0 });
167
170
  }
168
171
  async _destroy(keyValue, options) {
172
+ await this._emit('before-destroy', keyValue, options);
169
173
  const filter = [(0, extract_keyvalues_1.extractKeyValues)(this._entity, keyValue, true)];
170
174
  if (options && options.filter) {
171
175
  if (Array.isArray(options.filter))
@@ -173,21 +177,27 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
173
177
  else
174
178
  filter.push(options.filter);
175
179
  }
176
- return !!(await destroy_command_1.DestroyCommand.execute({
180
+ const result = !!(await destroy_command_1.DestroyCommand.execute({
177
181
  ...options,
178
182
  filter,
179
183
  entity: this._entity,
180
184
  }));
185
+ await this._emit('after-destroy', result, keyValue, options);
186
+ return result;
181
187
  }
182
188
  async _destroyAll(options) {
183
- return destroy_command_1.DestroyCommand.execute({
189
+ await this._emit('before-destroy-all', options);
190
+ const rowsAffected = destroy_command_1.DestroyCommand.execute({
184
191
  ...options,
185
192
  entity: this._entity,
186
193
  filter: options?.filter,
187
194
  params: options?.params
188
195
  });
196
+ await this._emit('after-destroy-all', rowsAffected, options);
197
+ return rowsAffected;
189
198
  }
190
199
  async _update(keyValue, values, options) {
200
+ await this._emit('before-update', keyValue, values, options);
191
201
  const keyValues = (0, extract_keyvalues_1.extractKeyValues)(this._entity, keyValue, true);
192
202
  const filter = [keyValues];
193
203
  if (options.filter) {
@@ -204,14 +214,26 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
204
214
  values: updateValues,
205
215
  filter
206
216
  });
217
+ await this._emit('after-update', rowsAffected, keyValues, options);
207
218
  return rowsAffected ? keyValues : undefined;
208
219
  }
209
220
  async _updateAll(values, options) {
210
- return await update_command_1.UpdateCommand.execute({
221
+ await this._emit('before-update-all', values, options);
222
+ const rowsAffected = await update_command_1.UpdateCommand.execute({
211
223
  ...options,
212
224
  entity: this._entity,
213
225
  values
214
226
  });
227
+ await this._emit('after-update-all', rowsAffected, values, options);
228
+ return rowsAffected;
229
+ }
230
+ async _emit(event, ...args) {
231
+ const events = this._entity.eventListeners && this._entity.eventListeners[event];
232
+ if (events) {
233
+ for (const fn of events) {
234
+ await fn(this, ...args);
235
+ }
236
+ }
215
237
  }
216
238
  }
217
239
  exports.Repository = Repository;
@@ -0,0 +1 @@
1
+ export declare function applyMixins(derivedCtor: any, baseCtor: any, filter?: (k: string) => boolean): void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyMixins = void 0;
4
+ function applyMixins(derivedCtor, baseCtor, filter) {
5
+ for (const name of Object.getOwnPropertyNames(baseCtor.prototype)) {
6
+ if (filter && !filter(name))
7
+ continue;
8
+ Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
9
+ Object.create(null));
10
+ }
11
+ }
12
+ exports.applyMixins = applyMixins;
@@ -1,2 +1,2 @@
1
- import { EntityModel } from '../model/entity-model';
2
- export declare function extractKeyValues<T>(entityDef: EntityModel, valueOrInstance: any | Record<string, any> | T, keepOther?: boolean): Record<string, any>;
1
+ import { EntityMetadata } from '../model/entity-metadata';
2
+ export declare function extractKeyValues<T>(entityDef: EntityMetadata, valueOrInstance: any | Record<string, any> | T, keepOther?: boolean): Record<string, any>;
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.extractKeyValues = void 0;
4
+ const entity_metadata_1 = require("../model/entity-metadata");
4
5
  const orm_helper_1 = require("./orm.helper");
5
6
  function extractKeyValues(entityDef, valueOrInstance, keepOther) {
6
- const primaryIndex = entityDef.primaryIndex;
7
+ const primaryIndex = entity_metadata_1.EntityMetadata.getPrimaryIndex(entityDef);
7
8
  if (!primaryIndex)
8
9
  throw new Error(`No primary fields defined for "${entityDef.name}" entity`);
9
10
  const validateCol = (k) => {
10
- const col = entityDef.getElement(k);
11
+ const col = entity_metadata_1.EntityMetadata.getElement(entityDef, k);
11
12
  if (!col)
12
13
  throw new Error(`Unknown column (${k}) defined as primary key in entity "${entityDef.name}"`);
13
14
  if (!(0, orm_helper_1.isColumnElement)(col))
@@ -1,14 +1,13 @@
1
- import 'reflect-metadata';
2
1
  import { Type } from 'ts-gems';
2
+ import { AssociationElementMetadata } from '../model/association-element-metadata';
3
+ import type { ColumnElementMetadata } from '../model/column-element-metadata';
4
+ import { EmbeddedElementMetadata } from '../model/embedded-element-metadata';
5
+ import type { EntityMetadata } from '../model/entity-metadata';
3
6
  import type { TypeThunk } from '../orm.type';
4
- import type { EntityModel } from '../model/entity-model';
5
- import type { EntityColumnElement } from '../model/entity-column-element';
6
- import type { EntityObjectElement } from '../model/entity-object-element';
7
- import type { EntityAssociationElement } from '../model/entity-association-element';
8
7
  export declare function isClass(fn: any): fn is Type;
9
8
  export declare function isEntityClass(fn: any): fn is Type;
10
- export declare function isColumnElement(f: any): f is EntityColumnElement;
11
- export declare const isObjectElement: (f: any) => f is EntityObjectElement;
12
- export declare const isAssociationElement: (f: any) => f is EntityAssociationElement;
9
+ export declare function isColumnElement(f: any): f is ColumnElementMetadata;
10
+ export declare const isEmbeddedElement: (f: any) => f is EmbeddedElementMetadata;
11
+ export declare const isAssociationElement: (f: any) => f is AssociationElementMetadata;
13
12
  export declare function resolveEntity(ctorThunk: TypeThunk): Promise<Type | undefined>;
14
- export declare function resolveEntityMeta(ctorThunk: TypeThunk): Promise<EntityModel | undefined>;
13
+ export declare function resolveEntityMeta(ctorThunk: TypeThunk): Promise<EntityMetadata | undefined>;
@@ -1,26 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveEntityMeta = exports.resolveEntity = exports.isAssociationElement = exports.isObjectElement = exports.isColumnElement = exports.isEntityClass = exports.isClass = void 0;
4
- require("reflect-metadata");
3
+ exports.resolveEntityMeta = exports.resolveEntity = exports.isAssociationElement = exports.isEmbeddedElement = exports.isColumnElement = exports.isEntityClass = exports.isClass = void 0;
5
4
  const orm_const_1 = require("../orm.const");
6
5
  function isClass(fn) {
7
6
  return typeof fn === 'function' && /^\s*class/.test(fn.toString());
8
7
  }
9
8
  exports.isClass = isClass;
10
9
  function isEntityClass(fn) {
11
- return !!(isClass(fn) && fn[orm_const_1.ENTITY_DEFINITION_KEY]);
10
+ return isClass(fn) && Reflect.hasMetadata(orm_const_1.ENTITY_METADATA_KEY, fn);
12
11
  }
13
12
  exports.isEntityClass = isEntityClass;
14
13
  function isColumnElement(f) {
15
- return !!(f && f.entity && f.kind === 'column');
14
+ return !!(f && f.name && f.kind === 'column');
16
15
  }
17
16
  exports.isColumnElement = isColumnElement;
18
- const isObjectElement = (f) => {
19
- return !!(f && f.entity && f.kind === 'object');
17
+ const isEmbeddedElement = (f) => {
18
+ return !!(f && f.name && f.kind === 'object');
20
19
  };
21
- exports.isObjectElement = isObjectElement;
20
+ exports.isEmbeddedElement = isEmbeddedElement;
22
21
  const isAssociationElement = (f) => {
23
- return !!(f && f.entity && f.kind === 'association');
22
+ return !!(f && f.name && f.kind === 'association');
24
23
  };
25
24
  exports.isAssociationElement = isAssociationElement;
26
25
  async function resolveEntity(ctorThunk) {
@@ -34,6 +33,6 @@ async function resolveEntity(ctorThunk) {
34
33
  exports.resolveEntity = resolveEntity;
35
34
  async function resolveEntityMeta(ctorThunk) {
36
35
  const ctor = await resolveEntity(ctorThunk);
37
- return ctor && ctor[orm_const_1.ENTITY_DEFINITION_KEY];
36
+ return ctor && Reflect.getMetadata(orm_const_1.ENTITY_METADATA_KEY, ctor);
38
37
  }
39
38
  exports.resolveEntityMeta = resolveEntityMeta;
@@ -1,2 +1,2 @@
1
- import { AbstractEntityelement } from '../model/abstract-entityelement';
2
- export declare function serializeColumn(col: AbstractEntityelement, v: any): any;
1
+ import { ElementMetadata } from '../model/element-metadata';
2
+ export declare function serializeColumn(col: ElementMetadata, v: any): any;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqb/connect",
3
3
  "description": "Multi-dialect database connection framework written with TypeScript",
4
- "version": "4.0.13",
4
+ "version": "4.1.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -29,21 +29,21 @@
29
29
  "mysql"
30
30
  ],
31
31
  "dependencies": {
32
- "debug": "^4.3.2",
32
+ "debug": "^4.3.4",
33
33
  "lightning-pool": "^3.1.3",
34
34
  "lodash": "^4.17.21",
35
35
  "putil-isplainobject": "^1.1.4",
36
- "putil-merge": "^3.7.0",
36
+ "putil-merge": "^3.8.0",
37
37
  "putil-promisify": "^1.8.5",
38
- "putil-taskqueue": "^2.5.3",
39
- "putil-varhelpers": "^1.6.1",
38
+ "putil-taskqueue": "^2.5.4",
39
+ "putil-varhelpers": "^1.6.3",
40
40
  "reflect-metadata": "^0.1.13",
41
41
  "strict-typed-events": "^2.1.0",
42
- "ts-gems": "^1.5.0"
42
+ "ts-gems": "^1.5.2"
43
43
  },
44
44
  "devDependencies": {
45
- "@types/debug": "^4.1.6",
46
- "@types/lodash": "^4.14.171"
45
+ "@types/debug": "^4.1.7",
46
+ "@types/lodash": "^4.14.180"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "@sqb/builder": "^4.x.x"
@@ -51,7 +51,7 @@
51
51
  "main": "dist/index.js",
52
52
  "types": "dist/index.d.ts",
53
53
  "engines": {
54
- "node": ">= 14.0"
54
+ "node": ">= 16.0"
55
55
  },
56
56
  "directories": {
57
57
  "lib": "dist",
@@ -63,7 +63,7 @@
63
63
  "dist/"
64
64
  ],
65
65
  "scripts": {
66
- "build": "tsc -b tsconfig.build.json",
66
+ "build": "tsc -b tsconfig-build.json",
67
67
  "prebuild": "npm run lint && npm run clean:dist",
68
68
  "postbuild": "copyfiles package.json README.md LICENSE ../../build/connect",
69
69
  "clean": "ts-cleanup -s src --all & ts-cleanup -s test",
@@ -1,16 +0,0 @@
1
- import { PickReadonly, PickWritable, Type } from 'ts-gems';
2
- import { REPOSITORY_KEY } from './orm.const';
3
- export declare abstract class BaseEntity {
4
- private [REPOSITORY_KEY];
5
- constructor(partial?: any);
6
- destroy(): Promise<boolean>;
7
- exists(): Promise<boolean>;
8
- toJSON(): any;
9
- }
10
- export declare function getElementNames<T, K extends keyof T>(classRef: Type<T>): K[];
11
- export declare function getColumnNames<T, K extends keyof PickWritable<T>>(classRef: Type<T>): K[];
12
- export declare function getObjectElementNames<T, K extends keyof PickWritable<T>>(classRef: Type<T>): K[];
13
- export declare function getAssociationElementNames<T, K extends keyof PickReadonly<T>>(classRef: Type<T>): K[];
14
- export declare function getNonAssociationElementNames<T, K extends keyof PickWritable<T>>(classRef: Type<T>): K[];
15
- export declare function getInsertColumnNames<T, K extends keyof PickWritable<T>>(classRef: Type<T>): K[];
16
- export declare function getUpdateColumnNames<T, K extends keyof PickWritable<T>>(classRef: Type<T>): K[];
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getUpdateColumnNames = exports.getInsertColumnNames = exports.getNonAssociationElementNames = exports.getAssociationElementNames = exports.getObjectElementNames = exports.getColumnNames = exports.getElementNames = exports.BaseEntity = void 0;
4
- const orm_const_1 = require("./orm.const");
5
- const entity_model_1 = require("./model/entity-model");
6
- class BaseEntity {
7
- constructor(partial) {
8
- const elements = entity_model_1.EntityModel.getColumnNames(Object.getPrototypeOf(this).constructor);
9
- if (elements && partial) {
10
- for (const k of elements)
11
- if (partial[k] !== undefined)
12
- this[k] = partial[k];
13
- }
14
- }
15
- async destroy() {
16
- const repo = this[orm_const_1.REPOSITORY_KEY];
17
- return repo.destroy(this);
18
- }
19
- async exists() {
20
- const repo = this[orm_const_1.REPOSITORY_KEY];
21
- return repo.exists(this);
22
- }
23
- toJSON() {
24
- // this method is an placeholder an will be overwritten by declareEntity() method
25
- return this;
26
- }
27
- }
28
- exports.BaseEntity = BaseEntity;
29
- function getElementNames(classRef) {
30
- return (entity_model_1.EntityModel.getElementNames(classRef) || []);
31
- }
32
- exports.getElementNames = getElementNames;
33
- function getColumnNames(classRef) {
34
- return (entity_model_1.EntityModel.getColumnNames(classRef) || []);
35
- }
36
- exports.getColumnNames = getColumnNames;
37
- function getObjectElementNames(classRef) {
38
- return (entity_model_1.EntityModel.getObjectElementNames(classRef) || []);
39
- }
40
- exports.getObjectElementNames = getObjectElementNames;
41
- function getAssociationElementNames(classRef) {
42
- return (entity_model_1.EntityModel.getAssociationElementNames(classRef) || []);
43
- }
44
- exports.getAssociationElementNames = getAssociationElementNames;
45
- function getNonAssociationElementNames(classRef) {
46
- return (entity_model_1.EntityModel.getNonAssociationElementNames(classRef) || []);
47
- }
48
- exports.getNonAssociationElementNames = getNonAssociationElementNames;
49
- function getInsertColumnNames(classRef) {
50
- return (entity_model_1.EntityModel.getInsertColumnNames(classRef) || []);
51
- }
52
- exports.getInsertColumnNames = getInsertColumnNames;
53
- function getUpdateColumnNames(classRef) {
54
- return (entity_model_1.EntityModel.getUpdateColumnNames(classRef) || []);
55
- }
56
- exports.getUpdateColumnNames = getUpdateColumnNames;
@@ -1,8 +0,0 @@
1
- import type { EntityModel } from './entity-model';
2
- import { ElementKind } from '../orm.type';
3
- export declare abstract class AbstractEntityelement {
4
- readonly entity: EntityModel;
5
- readonly name: string;
6
- abstract kind: ElementKind;
7
- protected constructor(entity: EntityModel, name: string);
8
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractEntityelement = void 0;
4
- class AbstractEntityelement {
5
- constructor(entity, name) {
6
- this.entity = entity;
7
- this.name = name;
8
- }
9
- }
10
- exports.AbstractEntityelement = AbstractEntityelement;
@@ -1,9 +0,0 @@
1
- import type { EntityModel } from './entity-model';
2
- import { ElementKind } from '../orm.type';
3
- import { AbstractEntityelement } from './abstract-entityelement';
4
- import type { AssociationNode } from './association-node';
5
- export declare class EntityAssociationElement extends AbstractEntityelement {
6
- readonly association: AssociationNode;
7
- readonly kind: ElementKind;
8
- constructor(entity: EntityModel, name: string, association: AssociationNode);
9
- }
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EntityAssociationElement = void 0;
4
- const abstract_entityelement_1 = require("./abstract-entityelement");
5
- class EntityAssociationElement extends abstract_entityelement_1.AbstractEntityelement {
6
- constructor(entity, name, association) {
7
- super(entity, name);
8
- this.association = association;
9
- this.kind = 'association';
10
- this.association.name = this.entity.name + '.' + name;
11
- }
12
- }
13
- exports.EntityAssociationElement = EntityAssociationElement;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EntityColumnElement = void 0;
4
- const tslib_1 = require("tslib");
5
- require("reflect-metadata");
6
- const lodash_1 = tslib_1.__importDefault(require("lodash"));
7
- const abstract_entityelement_1 = require("./abstract-entityelement");
8
- class EntityColumnElement extends abstract_entityelement_1.AbstractEntityelement {
9
- constructor(entity, name, options = {}) {
10
- super(entity, name);
11
- this.kind = 'column';
12
- this.fieldName = name;
13
- if (options)
14
- this.assign(options);
15
- }
16
- assign(options) {
17
- Object.assign(this, lodash_1.default.omit(options, ['entity', 'name', 'kind']));
18
- }
19
- checkEnumValue(v) {
20
- if (v === undefined || !this.enum || (v == null && !this.notNull))
21
- return;
22
- const enumKeys = Array.isArray(this.enum) ? this.enum : Object.keys(this.enum);
23
- if (!enumKeys.includes(v))
24
- throw new Error(`${this.entity.name}.${this.name} value must be one of (${enumKeys})`);
25
- }
26
- }
27
- exports.EntityColumnElement = EntityColumnElement;
@@ -1,59 +0,0 @@
1
- import { Type, Maybe } from 'ts-gems';
2
- import { IndexOptions, TypeThunk, DataPropertyOptions } from '../orm.type';
3
- import { IndexMeta } from './index-meta';
4
- import { Association } from './association';
5
- import { EntityColumnElement } from './entity-column-element';
6
- import { EntityObjectElement } from './entity-object-element';
7
- import { EntityAssociationElement } from './entity-association-element';
8
- import { AssociationNode } from './association-node';
9
- export declare type EntityElement = EntityColumnElement | EntityObjectElement | EntityAssociationElement;
10
- export declare class EntityModel {
11
- readonly ctor: Type;
12
- private _elementKeys?;
13
- readonly name: string;
14
- tableName?: string;
15
- schema?: string;
16
- comment?: string;
17
- primaryIndex?: IndexMeta;
18
- elements: Map<string, EntityElement>;
19
- indexes: IndexMeta[];
20
- foreignKeys: Association[];
21
- eventListeners: {
22
- event: string;
23
- fn: Function;
24
- }[];
25
- constructor(ctor: Type);
26
- get elementKeys(): string[];
27
- getElement(name: string): Maybe<EntityElement>;
28
- getColumnElement(name: string): Maybe<EntityColumnElement>;
29
- getColumnElementByFieldName(fieldName: string): Maybe<EntityColumnElement>;
30
- getObjectElement(name: string): Maybe<EntityObjectElement>;
31
- getAssociationElement(name: string): Maybe<EntityAssociationElement>;
32
- defineColumnElement(propertyKey: string, options?: DataPropertyOptions): EntityColumnElement;
33
- defineAssociationElement(propertyKey: string, association: AssociationNode): EntityAssociationElement;
34
- defineObjectElement(propertyKey: string, type?: TypeThunk): EntityObjectElement;
35
- setPrimaryIndex(column: string | string[], options?: IndexOptions): void;
36
- addIndex(column: string | string[], options?: IndexOptions): void;
37
- addForeignKey(propertyKey: string, target: TypeThunk, targetKey?: string): void;
38
- before(event: 'insert' | 'update' | 'destroy', fn: Type): void;
39
- after(event: 'insert' | 'update' | 'destroy', fn: Type): void;
40
- getPrimaryIndexColumns(): EntityColumnElement[];
41
- getElementNames(fn: (el: EntityElement) => boolean): string[];
42
- getColumnNames(): string[];
43
- getObjectElementNames(): string[];
44
- getAssociationElementNames(): string[];
45
- getNonAssociationElementNames(): string[];
46
- getInsertColumnNames(): string[];
47
- getUpdateColumnNames(): string[];
48
- getForeignKeyFor(t: EntityModel): Promise<Maybe<Association>>;
49
- static get(ctor: Function): Maybe<EntityModel>;
50
- static hasOwn(ctor: Function): boolean;
51
- static attachTo(ctor: Function): EntityModel;
52
- static getElementNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
53
- static getColumnNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
54
- static getAssociationElementNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
55
- static getNonAssociationElementNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
56
- static getObjectElementNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
57
- static getInsertColumnNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
58
- static getUpdateColumnNames<T extends Function, K extends keyof T>(ctor: T): K[] | undefined;
59
- }