@rws-framework/db 2.3.2 → 2.4.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 (92) hide show
  1. package/.bin/add-v.sh +9 -9
  2. package/.bin/emerge.sh +10 -10
  3. package/.eslintrc.json +53 -53
  4. package/README.md +404 -367
  5. package/dist/decorators/InverseRelation.d.ts +12 -12
  6. package/dist/decorators/InverseRelation.js +24 -24
  7. package/dist/decorators/InverseTimeSeries.d.ts +8 -8
  8. package/dist/decorators/InverseTimeSeries.js +13 -13
  9. package/dist/decorators/RWSCollection.d.ts +12 -12
  10. package/dist/decorators/RWSCollection.js +16 -16
  11. package/dist/decorators/Relation.d.ts +19 -19
  12. package/dist/decorators/Relation.js +26 -26
  13. package/dist/decorators/TrackType.d.ts +20 -20
  14. package/dist/decorators/TrackType.js +41 -41
  15. package/dist/decorators/index.d.ts +5 -5
  16. package/dist/decorators/index.js +14 -14
  17. package/dist/helper/DbHelper.d.ts +8 -8
  18. package/dist/helper/DbHelper.js +141 -141
  19. package/dist/helper/FieldsHelper.d.ts +4 -4
  20. package/dist/helper/FieldsHelper.js +35 -35
  21. package/dist/index.d.ts +12 -12
  22. package/dist/index.js +19 -19
  23. package/dist/models/TimeSeriesModel.d.ts +7 -7
  24. package/dist/models/TimeSeriesModel.js +33 -33
  25. package/dist/models/_model.d.ts +6 -6
  26. package/dist/models/_model.js +9 -9
  27. package/dist/models/core/RWSModel.d.ts +66 -66
  28. package/dist/models/core/RWSModel.js +400 -400
  29. package/dist/models/core/TimeSeriesModel.d.ts +1 -1
  30. package/dist/models/core/TimeSeriesModel.js +14 -14
  31. package/dist/models/index.d.ts +7 -7
  32. package/dist/models/index.js +8 -8
  33. package/dist/models/interfaces/IModel.d.ts +11 -11
  34. package/dist/models/interfaces/IModel.js +2 -2
  35. package/dist/models/interfaces/IRWSModelServices.d.ts +6 -6
  36. package/dist/models/interfaces/IRWSModelServices.js +2 -2
  37. package/dist/models/interfaces/OpModelType.d.ts +31 -31
  38. package/dist/models/interfaces/OpModelType.js +2 -2
  39. package/dist/models/types/RelationTypes.d.ts +24 -24
  40. package/dist/models/types/RelationTypes.js +2 -2
  41. package/dist/models/utils/ModelUtils.d.ts +10 -10
  42. package/dist/models/utils/ModelUtils.js +56 -56
  43. package/dist/models/utils/PaginationUtils.d.ts +5 -5
  44. package/dist/models/utils/PaginationUtils.js +32 -32
  45. package/dist/models/utils/RelationUtils.d.ts +14 -14
  46. package/dist/models/utils/RelationUtils.js +65 -65
  47. package/dist/models/utils/TimeSeriesUtils.d.ts +11 -11
  48. package/dist/models/utils/TimeSeriesUtils.js +35 -35
  49. package/dist/services/DBService.d.ts +37 -37
  50. package/dist/services/DBService.js +198 -198
  51. package/dist/types/DbConfigHandler.d.ts +9 -9
  52. package/dist/types/DbConfigHandler.js +2 -2
  53. package/dist/types/FindParams.d.ts +14 -14
  54. package/dist/types/FindParams.js +2 -2
  55. package/dist/types/IRWSModel.d.ts +3 -3
  56. package/dist/types/IRWSModel.js +2 -2
  57. package/dist/types/ITimeSeries.d.ts +6 -6
  58. package/dist/types/ITimeSeries.js +2 -2
  59. package/exec/console.js +110 -110
  60. package/exec/db.rws.webpack.config.js +168 -168
  61. package/exec/src/cli.ts +74 -73
  62. package/exec/tsconfig.json +32 -32
  63. package/exec/webpackFilters.js +17 -17
  64. package/package.json +36 -36
  65. package/src/decorators/InverseRelation.ts +36 -36
  66. package/src/decorators/InverseTimeSeries.ts +21 -21
  67. package/src/decorators/RWSCollection.ts +27 -27
  68. package/src/decorators/Relation.ts +47 -47
  69. package/src/decorators/TrackType.ts +69 -69
  70. package/src/decorators/index.ts +7 -7
  71. package/src/empty.js +0 -0
  72. package/src/helper/DbHelper.ts +177 -177
  73. package/src/helper/FieldsHelper.ts +34 -34
  74. package/src/index.ts +36 -36
  75. package/src/models/_model.ts +29 -29
  76. package/src/models/core/RWSModel.ts +520 -520
  77. package/src/models/core/TimeSeriesModel.ts +19 -19
  78. package/src/models/index.ts +20 -20
  79. package/src/models/interfaces/IModel.ts +12 -12
  80. package/src/models/interfaces/IRWSModelServices.ts +7 -7
  81. package/src/models/interfaces/OpModelType.ts +49 -49
  82. package/src/models/types/RelationTypes.ts +25 -25
  83. package/src/models/utils/ModelUtils.ts +65 -65
  84. package/src/models/utils/PaginationUtils.ts +42 -42
  85. package/src/models/utils/RelationUtils.ts +76 -76
  86. package/src/models/utils/TimeSeriesUtils.ts +38 -38
  87. package/src/services/DBService.ts +277 -277
  88. package/src/types/DbConfigHandler.ts +12 -11
  89. package/src/types/FindParams.ts +13 -13
  90. package/src/types/IRWSModel.ts +2 -2
  91. package/src/types/ITimeSeries.ts +5 -5
  92. package/tsconfig.json +22 -22
package/exec/src/cli.ts CHANGED
@@ -1,73 +1,74 @@
1
- import 'reflect-metadata';
2
-
3
- const args: string[] = process.argv.slice(2).filter(cmd => cmd !== '--rebuild');
4
- import fs from 'fs';
5
- import path from 'path';
6
- import { DbHelper, DBService, OpModelType, RWSModel } from '../../src';
7
- import { IDbConfigHandler, IDbConfigParams } from '../../src/types/DbConfigHandler';
8
- import chalk from 'chalk';
9
-
10
- class Config implements IDbConfigHandler {
11
- private data: IDbConfigParams = {
12
- db_models: [],
13
- mongo_db: null,
14
- mongo_url: null
15
- };
16
-
17
- private modelsDir: string;
18
- private cliExecRoot: string;
19
- private static _instance: Config = null;
20
-
21
- private constructor(){}
22
-
23
- static async getInstance(): Promise<Config>
24
- {
25
- if(!this._instance){
26
- this._instance = new Config();
27
- }
28
-
29
- await this._instance.fill();
30
-
31
- return this._instance;
32
- }
33
-
34
-
35
- async fill(): Promise<void>
36
- {
37
- this.data.mongo_url = args[0];
38
- this.data.mongo_db = args[1];
39
-
40
-
41
- this.modelsDir = args[2];
42
- this.cliExecRoot = args[3];
43
-
44
- console.log({args})
45
-
46
- // this.data.db_models = (await import('@V/index')).default;
47
- }
48
-
49
- getModelsDir(): string
50
- {
51
- return this.modelsDir
52
- }
53
-
54
- getCliExecRoot(): string
55
- {
56
- return this.cliExecRoot
57
- }
58
-
59
- get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K] {
60
- return this.data[key];
61
- }
62
- }
63
-
64
- async function main(): Promise<void>
65
- {
66
- console.log('INSTALL PRISMA');
67
- const cfg = await Config.getInstance();
68
- DbHelper.installPrisma(cfg, new DBService(cfg), false);
69
- }
70
-
71
- console.log(chalk.bgGreen('[RWS DB CLI] Starting systems...'));
72
-
73
- main();
1
+ import 'reflect-metadata';
2
+
3
+ const args: string[] = process.argv.slice(2).filter(cmd => cmd !== '--rebuild');
4
+ import fs from 'fs';
5
+ import path from 'path';
6
+ import { DbHelper, DBService, OpModelType, RWSModel } from '../../src';
7
+ import { IDbConfigHandler, IDbConfigParams } from '../../src/types/DbConfigHandler';
8
+ import chalk from 'chalk';
9
+
10
+ class Config implements IDbConfigHandler {
11
+ private data: IDbConfigParams = {
12
+ db_models: [],
13
+ mongo_db: null,
14
+ mongo_url: null
15
+ };
16
+
17
+ private modelsDir: string;
18
+ private cliExecRoot: string;
19
+ private static _instance: Config = null;
20
+
21
+ private constructor(){}
22
+
23
+ static async getInstance(): Promise<Config>
24
+ {
25
+ if(!this._instance){
26
+ this._instance = new Config();
27
+ }
28
+
29
+ await this._instance.fill();
30
+
31
+ return this._instance;
32
+ }
33
+
34
+
35
+ async fill(): Promise<void>
36
+ {
37
+ this.data.db_url = args[0];
38
+ this.data.db_name = args[1];
39
+ this.data.db_type = args[2];
40
+
41
+
42
+ this.modelsDir = args[3];
43
+ this.cliExecRoot = args[4];
44
+
45
+ console.log({args})
46
+
47
+ // this.data.db_models = (await import('@V/index')).default;
48
+ }
49
+
50
+ getModelsDir(): string
51
+ {
52
+ return this.modelsDir
53
+ }
54
+
55
+ getCliExecRoot(): string
56
+ {
57
+ return this.cliExecRoot
58
+ }
59
+
60
+ get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K] {
61
+ return this.data[key];
62
+ }
63
+ }
64
+
65
+ async function main(): Promise<void>
66
+ {
67
+ console.log('INSTALL PRISMA');
68
+ const cfg = await Config.getInstance();
69
+ DbHelper.installPrisma(cfg, new DBService(cfg), false);
70
+ }
71
+
72
+ console.log(chalk.bgGreen('[RWS DB CLI] Starting systems...'));
73
+
74
+ main();
@@ -1,32 +1,32 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": "./",
4
- "experimentalDecorators": true,
5
- "emitDecoratorMetadata": true,
6
- "emitDeclarationOnly": false,
7
- "target": "ES2018",
8
- "module": "commonjs",
9
- "moduleResolution": "node",
10
- "strict": true,
11
- "skipLibCheck": true,
12
- "esModuleInterop": true,
13
- "resolveJsonModule": true,
14
- "strictNullChecks": false,
15
- "allowSyntheticDefaultImports": true,
16
- "sourceMap": true,
17
- "declaration": true,
18
- "outDir": "build/vendors",
19
- "preserveWatchOutput": true,
20
- "allowJs": true,
21
- "paths": {
22
- "@V/*": ["./build/vendors/*"]
23
- }
24
- },
25
- "include": [
26
- "src/cli.ts",
27
- "../src/*"
28
- ],
29
- "exclude": [
30
- "node_modules"
31
- ]
32
- }
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": "./",
4
+ "experimentalDecorators": true,
5
+ "emitDecoratorMetadata": true,
6
+ "emitDeclarationOnly": false,
7
+ "target": "ES2018",
8
+ "module": "commonjs",
9
+ "moduleResolution": "node",
10
+ "strict": true,
11
+ "skipLibCheck": true,
12
+ "esModuleInterop": true,
13
+ "resolveJsonModule": true,
14
+ "strictNullChecks": false,
15
+ "allowSyntheticDefaultImports": true,
16
+ "sourceMap": true,
17
+ "declaration": true,
18
+ "outDir": "build/vendors",
19
+ "preserveWatchOutput": true,
20
+ "allowJs": true,
21
+ "paths": {
22
+ "@V/*": ["./build/vendors/*"]
23
+ }
24
+ },
25
+ "include": [
26
+ "src/cli.ts",
27
+ "../src/*"
28
+ ],
29
+ "exclude": [
30
+ "node_modules"
31
+ ]
32
+ }
@@ -1,18 +1,18 @@
1
- module.exports = [
2
- /aws-crt/,
3
- /express\/lib\/view/,
4
- /mongodb-client-encryption\/lib\/providers\/gcp/,
5
- /mongodb/,
6
- /nest-commander/,
7
- /snappy/,
8
- /mongodb-js\/zstd/,
9
- /puppeteer/,
10
- /kerberos/,
11
- /@zip\.js\/zip.js/,
12
- /Module not found: Error: Can't resolve 'fsevents' in/,
13
- /Critical dependency: the request of a dependency is an expression/,
14
- /Module not found: Error: Can't resolve 'aws4'/,
15
- /Critical dependency: require function is used in a way in which dependencies cannot be statically extracted/,
16
- /Can't resolve .* in '.*kerberos.*'/,
17
- /Can't resolve .* in '.*mongodb-client-encryption.*'/
1
+ module.exports = [
2
+ /aws-crt/,
3
+ /express\/lib\/view/,
4
+ /mongodb-client-encryption\/lib\/providers\/gcp/,
5
+ /mongodb/,
6
+ /nest-commander/,
7
+ /snappy/,
8
+ /mongodb-js\/zstd/,
9
+ /puppeteer/,
10
+ /kerberos/,
11
+ /@zip\.js\/zip.js/,
12
+ /Module not found: Error: Can't resolve 'fsevents' in/,
13
+ /Critical dependency: the request of a dependency is an expression/,
14
+ /Module not found: Error: Can't resolve 'aws4'/,
15
+ /Critical dependency: require function is used in a way in which dependencies cannot be statically extracted/,
16
+ /Can't resolve .* in '.*kerberos.*'/,
17
+ /Can't resolve .* in '.*mongodb-client-encryption.*'/
18
18
  ];
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "@rws-framework/db",
3
- "private": false,
4
- "version": "2.3.2",
5
- "description": "",
6
- "main": "dist/index.js",
7
- "types": "dist/index.d.ts",
8
- "scripts": {},
9
- "author": "papablack",
10
- "license": "ISC",
11
- "bin": {
12
- "rws-db": "./exec/console.js"
13
- },
14
- "dependencies": {
15
- "@rws-framework/console": "*",
16
- "@prisma/client": "^5.1.1",
17
- "@mongodb-js/zstd": "^1.2.0",
18
- "mongodb": "^6.8.1",
19
- "mongodb-client-encryption": "^6.1.1",
20
- "xml2js": "^0.6.2",
21
- "crypto": "^1.0.1",
22
- "source-map-support": "^0.5.21",
23
- "tsconfig-paths": "^4.2.0",
24
- "tsconfig-paths-webpack-plugin": "^4.1.0"
25
- },
26
- "devDependencies": {
27
- "@types/xml2js": "^0.4.14",
28
- "typescript": "^5.7.2"
29
-
30
- },
31
- "repository": {
32
- "type": "git",
33
- "url": "https://github.com/rws-framework/db.git"
34
- },
35
- "_rws": true
36
- }
1
+ {
2
+ "name": "@rws-framework/db",
3
+ "private": false,
4
+ "version": "2.4.0",
5
+ "description": "",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {},
9
+ "author": "papablack",
10
+ "license": "ISC",
11
+ "bin": {
12
+ "rws-db": "./exec/console.js"
13
+ },
14
+ "dependencies": {
15
+ "@rws-framework/console": "*",
16
+ "@prisma/client": "^5.1.1",
17
+ "@mongodb-js/zstd": "^1.2.0",
18
+ "mongodb": "^6.8.1",
19
+ "mongodb-client-encryption": "^6.1.1",
20
+ "xml2js": "^0.6.2",
21
+ "crypto": "^1.0.1",
22
+ "source-map-support": "^0.5.21",
23
+ "tsconfig-paths": "^4.2.0",
24
+ "tsconfig-paths-webpack-plugin": "^4.1.0"
25
+ },
26
+ "devDependencies": {
27
+ "@types/xml2js": "^0.4.14",
28
+ "typescript": "^5.7.2"
29
+
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/rws-framework/db.git"
34
+ },
35
+ "_rws": true
36
+ }
@@ -1,37 +1,37 @@
1
- import 'reflect-metadata';
2
- import { RWSModel, OpModelType } from '../models/_model';
3
-
4
- interface InverseRelationOpts{
5
- key: string,
6
- inversionModel: OpModelType<RWSModel<any>>,
7
- foreignKey: string,
8
- singular?: boolean
9
- relationName?: string
10
- }
11
-
12
- function InverseRelation(inversionModel: () => OpModelType<RWSModel<any>>, sourceModel: () => OpModelType<RWSModel<any>>, relationOptions: Partial<InverseRelationOpts> = null) {
13
- return function(target: any, key: string) {
14
- const metadataPromise = Promise.resolve().then(() => {
15
- const model = inversionModel();
16
- const source = sourceModel();
17
-
18
- const metaOpts: InverseRelationOpts = {
19
- ...relationOptions,
20
- key,
21
- inversionModel: model,
22
- foreignKey: relationOptions && relationOptions.foreignKey ? relationOptions.foreignKey : `${source._collection}_id`
23
- };
24
-
25
- return metaOpts;
26
- });
27
-
28
- // Store both the promise and the key information
29
- Reflect.defineMetadata(`InverseRelation:${key}`, {
30
- promise: metadataPromise,
31
- key
32
- }, target);
33
- };
34
- }
35
-
36
- export default InverseRelation;
1
+ import 'reflect-metadata';
2
+ import { RWSModel, OpModelType } from '../models/_model';
3
+
4
+ interface InverseRelationOpts{
5
+ key: string,
6
+ inversionModel: OpModelType<RWSModel<any>>,
7
+ foreignKey: string,
8
+ singular?: boolean
9
+ relationName?: string
10
+ }
11
+
12
+ function InverseRelation(inversionModel: () => OpModelType<RWSModel<any>>, sourceModel: () => OpModelType<RWSModel<any>>, relationOptions: Partial<InverseRelationOpts> = null) {
13
+ return function(target: any, key: string) {
14
+ const metadataPromise = Promise.resolve().then(() => {
15
+ const model = inversionModel();
16
+ const source = sourceModel();
17
+
18
+ const metaOpts: InverseRelationOpts = {
19
+ ...relationOptions,
20
+ key,
21
+ inversionModel: model,
22
+ foreignKey: relationOptions && relationOptions.foreignKey ? relationOptions.foreignKey : `${source._collection}_id`
23
+ };
24
+
25
+ return metaOpts;
26
+ });
27
+
28
+ // Store both the promise and the key information
29
+ Reflect.defineMetadata(`InverseRelation:${key}`, {
30
+ promise: metadataPromise,
31
+ key
32
+ }, target);
33
+ };
34
+ }
35
+
36
+ export default InverseRelation;
37
37
  export {InverseRelationOpts};
@@ -1,22 +1,22 @@
1
- import 'reflect-metadata';
2
-
3
- interface InverseTimeSeriesOpts{
4
- timeSeriesModel: string
5
- hydrationField: string
6
- }
7
-
8
- function InverseTimeSeries(timeSeriesModel: string, hydrationField: string) {
9
-
10
- const metaOpts: InverseTimeSeriesOpts = {
11
- timeSeriesModel: timeSeriesModel,
12
- hydrationField: hydrationField
13
- };
14
-
15
-
16
- return function(target: any, key: string) {
17
- Reflect.defineMetadata(`InverseTimeSeries:${key}`, metaOpts, target);
18
- };
19
- }
20
-
21
- export default InverseTimeSeries;
1
+ import 'reflect-metadata';
2
+
3
+ interface InverseTimeSeriesOpts{
4
+ timeSeriesModel: string
5
+ hydrationField: string
6
+ }
7
+
8
+ function InverseTimeSeries(timeSeriesModel: string, hydrationField: string) {
9
+
10
+ const metaOpts: InverseTimeSeriesOpts = {
11
+ timeSeriesModel: timeSeriesModel,
12
+ hydrationField: hydrationField
13
+ };
14
+
15
+
16
+ return function(target: any, key: string) {
17
+ Reflect.defineMetadata(`InverseTimeSeries:${key}`, metaOpts, target);
18
+ };
19
+ }
20
+
21
+ export default InverseTimeSeries;
22
22
  export {InverseTimeSeriesOpts};
@@ -1,28 +1,28 @@
1
- import { OpModelType } from "../models/_model";
2
-
3
- export interface IRWSCollectionOpts {
4
- relations?: {
5
- [key: string]: boolean
6
- },
7
- ignored_keys?: string[]
8
- }
9
-
10
- export interface IRWSCollectionMeta {
11
- collectionName: string,
12
- options?: IRWSCollectionOpts
13
- }
14
-
15
-
16
- export function RWSCollection(collectionName: string, options?: IRWSCollectionOpts) {
17
- const metaOpts: IRWSCollectionMeta = { collectionName, options };
18
- return function(target: OpModelType<any>) {
19
- target._collection = collectionName;
20
- if(options && options.relations){
21
- target._RELATIONS = options.relations;
22
- }
23
- if(options && options.ignored_keys){
24
- target._CUT_KEYS = options.ignored_keys;
25
- }
26
- Reflect.defineMetadata(`RWSCollection`, metaOpts, target);
27
- };
1
+ import { OpModelType } from "../models/_model";
2
+
3
+ export interface IRWSCollectionOpts {
4
+ relations?: {
5
+ [key: string]: boolean
6
+ },
7
+ ignored_keys?: string[]
8
+ }
9
+
10
+ export interface IRWSCollectionMeta {
11
+ collectionName: string,
12
+ options?: IRWSCollectionOpts
13
+ }
14
+
15
+
16
+ export function RWSCollection(collectionName: string, options?: IRWSCollectionOpts) {
17
+ const metaOpts: IRWSCollectionMeta = { collectionName, options };
18
+ return function(target: OpModelType<any>) {
19
+ target._collection = collectionName;
20
+ if(options && options.relations){
21
+ target._RELATIONS = options.relations;
22
+ }
23
+ if(options && options.ignored_keys){
24
+ target._CUT_KEYS = options.ignored_keys;
25
+ }
26
+ Reflect.defineMetadata(`RWSCollection`, metaOpts, target);
27
+ };
28
28
  }
@@ -1,48 +1,48 @@
1
- import 'reflect-metadata';
2
- import { RWSModel, OpModelType } from '../models/_model';
3
-
4
- type CascadingSetup = 'Cascade' | 'Restrict' | 'NoAction' | 'SetNull';
5
-
6
- interface IRelationOpts {
7
- required?: boolean
8
- key: string
9
- relationField: string
10
- relatedToField?: string
11
- relatedTo: OpModelType<RWSModel<any>>
12
- many?: boolean
13
- embed?: boolean
14
- cascade: {
15
- onDelete: CascadingSetup,
16
- onUpdate: CascadingSetup
17
- }
18
- }
19
-
20
- const _DEFAULTS: Partial<IRelationOpts> = { required: false, many: false, embed: false, cascade: { onDelete: 'SetNull', onUpdate: 'Cascade' }};
21
-
22
- function Relation(theModel: () => OpModelType<RWSModel<any>>, relationOptions: Partial<IRelationOpts> = _DEFAULTS) {
23
- return function(target: any, key: string) {
24
- // Store the promise in metadata immediately
25
- const metadataPromise = Promise.resolve().then(() => {
26
- const relatedTo = theModel();
27
-
28
- const metaOpts: IRelationOpts = {
29
- ...relationOptions,
30
- cascade: relationOptions.cascade || _DEFAULTS.cascade,
31
- relatedTo,
32
- relationField: relationOptions.relationField ? relationOptions.relationField : relatedTo._collection + '_id',
33
- key
34
- };
35
- return metaOpts;
36
- });
37
-
38
- // Store both the promise and the key information
39
- Reflect.defineMetadata(`Relation:${key}`, {
40
- promise: metadataPromise,
41
- key
42
- }, target);
43
- };
44
- }
45
-
46
-
47
- export default Relation;
1
+ import 'reflect-metadata';
2
+ import { RWSModel, OpModelType } from '../models/_model';
3
+
4
+ type CascadingSetup = 'Cascade' | 'Restrict' | 'NoAction' | 'SetNull';
5
+
6
+ interface IRelationOpts {
7
+ required?: boolean
8
+ key: string
9
+ relationField: string
10
+ relatedToField?: string
11
+ relatedTo: OpModelType<RWSModel<any>>
12
+ many?: boolean
13
+ embed?: boolean
14
+ cascade: {
15
+ onDelete: CascadingSetup,
16
+ onUpdate: CascadingSetup
17
+ }
18
+ }
19
+
20
+ const _DEFAULTS: Partial<IRelationOpts> = { required: false, many: false, embed: false, cascade: { onDelete: 'SetNull', onUpdate: 'Cascade' }};
21
+
22
+ function Relation(theModel: () => OpModelType<RWSModel<any>>, relationOptions: Partial<IRelationOpts> = _DEFAULTS) {
23
+ return function(target: any, key: string) {
24
+ // Store the promise in metadata immediately
25
+ const metadataPromise = Promise.resolve().then(() => {
26
+ const relatedTo = theModel();
27
+
28
+ const metaOpts: IRelationOpts = {
29
+ ...relationOptions,
30
+ cascade: relationOptions.cascade || _DEFAULTS.cascade,
31
+ relatedTo,
32
+ relationField: relationOptions.relationField ? relationOptions.relationField : relatedTo._collection + '_id',
33
+ key
34
+ };
35
+ return metaOpts;
36
+ });
37
+
38
+ // Store both the promise and the key information
39
+ Reflect.defineMetadata(`Relation:${key}`, {
40
+ promise: metadataPromise,
41
+ key
42
+ }, target);
43
+ };
44
+ }
45
+
46
+
47
+ export default Relation;
48
48
  export {IRelationOpts};