@rws-framework/db 2.1.11 → 2.2.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 (49) hide show
  1. package/README.md +26 -17
  2. package/dist/decorators/TrackType.d.ts +1 -1
  3. package/dist/models/_model.d.ts +5 -116
  4. package/dist/models/_model.js +7 -497
  5. package/dist/models/core/RWSModel.d.ts +65 -0
  6. package/dist/models/core/RWSModel.js +375 -0
  7. package/dist/models/core/TimeSeriesModel.d.ts +7 -0
  8. package/dist/models/core/TimeSeriesModel.js +34 -0
  9. package/dist/models/index.d.ts +8 -0
  10. package/dist/models/index.js +12 -0
  11. package/dist/models/interfaces/IModel.d.ts +10 -0
  12. package/dist/models/interfaces/IModel.js +2 -0
  13. package/dist/models/interfaces/IRWSModelServices.d.ts +6 -0
  14. package/dist/models/interfaces/IRWSModelServices.js +2 -0
  15. package/dist/models/interfaces/OpModelType.d.ts +32 -0
  16. package/dist/models/interfaces/OpModelType.js +2 -0
  17. package/dist/models/types/RelationTypes.d.ts +24 -0
  18. package/dist/models/types/RelationTypes.js +2 -0
  19. package/dist/models/utils/ModelUtils.d.ts +10 -0
  20. package/dist/models/utils/ModelUtils.js +56 -0
  21. package/dist/models/utils/PaginationUtils.d.ts +5 -0
  22. package/dist/models/utils/PaginationUtils.js +32 -0
  23. package/dist/models/utils/RelationUtils copy.d.ts +13 -0
  24. package/dist/models/utils/RelationUtils copy.js +65 -0
  25. package/dist/models/utils/RelationUtils.d.ts +14 -0
  26. package/dist/models/utils/RelationUtils.js +65 -0
  27. package/dist/models/utils/TimeSeriesUtils.d.ts +11 -0
  28. package/dist/models/utils/TimeSeriesUtils.js +35 -0
  29. package/dist/services/DBService.d.ts +3 -2
  30. package/dist/services/DBService.js +5 -1
  31. package/dist/types/DbConfigHandler.d.ts +1 -1
  32. package/dist/types/FindParams.d.ts +5 -0
  33. package/package.json +1 -1
  34. package/src/decorators/TrackType.ts +2 -2
  35. package/src/models/_model.ts +26 -675
  36. package/src/models/core/RWSModel.ts +482 -0
  37. package/src/models/core/TimeSeriesModel.ts +19 -0
  38. package/src/models/index.ts +20 -0
  39. package/src/models/interfaces/IModel.ts +11 -0
  40. package/src/models/interfaces/IRWSModelServices.ts +7 -0
  41. package/src/models/interfaces/OpModelType.ts +49 -0
  42. package/src/models/types/RelationTypes.ts +25 -0
  43. package/src/models/utils/ModelUtils.ts +65 -0
  44. package/src/models/utils/PaginationUtils.ts +42 -0
  45. package/src/models/utils/RelationUtils.ts +76 -0
  46. package/src/models/utils/TimeSeriesUtils.ts +38 -0
  47. package/src/services/DBService.ts +14 -3
  48. package/src/types/DbConfigHandler.ts +2 -2
  49. package/src/types/FindParams.ts +10 -4
package/README.md CHANGED
@@ -10,38 +10,34 @@ import User from "./User";
10
10
 
11
11
  export const models = [ User, ApiKey];
12
12
  ```
13
+
14
+
13
15
  ## Example user model
14
16
 
15
17
  ```typescript
16
- import { TrackType, InverseRelation, RWSCollection, RWSModel } from '@rws-framework/db';
18
+ import { RWSannotations, RWSModel } from '@rws-framework/server';
17
19
 
18
- import IUser from './interfaces/IUser';
19
- import 'reflect-metadata';
20
+ import IUser from './interfaces/IUser';
21
+ import 'reflect-metadata';
20
22
 
21
- import ApiKey from './ApiKey';
22
- import IApiKey from './interfaces/IApiKey';
23
+ import ApiKey from './ApiKey';
24
+ import IApiKey from './interfaces/IApiKey';
25
+ const { RWSTrackType, InverseRelation } = RWSannotations.modelAnnotations;
23
26
 
24
- @RWSCollection('users', {
25
- relations: {
26
- transcriptions: true,
27
- apiKeys: true
28
- },
29
- ignored_keys: ['passwd']
30
- })
31
27
  class User extends RWSModel<User> implements IUser {
32
- @TrackType(String)
28
+ @RWSTrackType(String)
33
29
  username: string;
34
30
 
35
- @TrackType(String) // Can also handle Object and Number
31
+ @RWSTrackType(String) // Can also handle Object and Number
36
32
  passwd: string;
37
33
 
38
- @TrackType(Boolean)
34
+ @RWSTrackType(Boolean)
39
35
  active: boolean;
40
36
 
41
- @TrackType(Date, { required: true })
37
+ @RWSTrackType(Date, { required: true })
42
38
  created_at: Date;
43
39
 
44
- @TrackType(Date)
40
+ @RWSTrackType(Date)
45
41
  updated_at: Date;
46
42
 
47
43
  /**
@@ -51,12 +47,25 @@ class User extends RWSModel<User> implements IUser {
51
47
  @InverseRelation(() => ApiKey, () => User)
52
48
  apiKeys: IApiKey[];
53
49
 
50
+ static _collection = 'user';
51
+
52
+ static _RELATIONS = {
53
+ transcriptions: true,
54
+ apiKeys: true
55
+ };
56
+
57
+ static _CUT_KEYS = ['passwd'];
58
+
54
59
  constructor(data?: IUser) {
55
60
  super(data);
56
61
 
57
62
  if(!this.created_at){
58
63
  this.created_at = new Date();
59
64
  }
65
+ }
66
+
67
+ addMessage(message: string){
68
+ this.messages.push(message);
60
69
  }
61
70
  }
62
71
 
@@ -1,5 +1,5 @@
1
1
  import 'reflect-metadata';
2
- import { OpModelType } from '../models/_model';
2
+ import { OpModelType } from '../models/interfaces/OpModelType';
3
3
  interface ITrackerOpts {
4
4
  required?: boolean;
5
5
  relationField?: string;
@@ -1,116 +1,5 @@
1
- import { DBService } from '../services/DBService';
2
- import { IRWSModel } from '../types/IRWSModel';
3
- import { IDbConfigHandler } from '../types/DbConfigHandler';
4
- import { TrackType, IMetaOpts } from '../decorators';
5
- import { FindByType } from '../types/FindParams';
6
- interface IModel {
7
- [key: string]: any;
8
- id: string | null;
9
- save: () => void;
10
- getCollection: () => string | null;
11
- configService?: IDbConfigHandler;
12
- dbService?: DBService;
13
- }
14
- interface IRWSModelServices {
15
- configService?: IDbConfigHandler;
16
- dbService?: DBService;
17
- }
18
- type RelationBindType = {
19
- connect: {
20
- id: string;
21
- };
22
- };
23
- type RelOneMetaType<T extends IRWSModel> = {
24
- [key: string]: {
25
- required: boolean;
26
- key?: string;
27
- model: OpModelType<T>;
28
- hydrationField: string;
29
- foreignKey: string;
30
- };
31
- };
32
- type RelManyMetaType<T extends IRWSModel> = {
33
- [key: string]: {
34
- key: string;
35
- inversionModel: OpModelType<T>;
36
- foreignKey: string;
37
- };
38
- };
39
- export interface OpModelType<ChildClass> {
40
- new (data?: any | null): ChildClass;
41
- services: IRWSModelServices;
42
- name: string;
43
- _collection: string;
44
- _RELATIONS: {
45
- [key: string]: boolean;
46
- };
47
- _CUT_KEYS: string[];
48
- allModels: OpModelType<any>[];
49
- loadModels: () => OpModelType<any>[];
50
- checkForInclusionWithThrow: (className: string) => void;
51
- checkForInclusion: (className: string) => boolean;
52
- findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams: FindByType): Promise<T | null>;
53
- find<T extends RWSModel<T>>(this: OpModelType<T>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
54
- findBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams: FindByType): Promise<T[]>;
55
- delete<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, conditions: any): Promise<void>;
56
- create<T extends RWSModel<T>>(this: OpModelType<T>, data: T): Promise<T>;
57
- getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
58
- getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
59
- getCollection(): string;
60
- setServices(services: IRWSModelServices): void;
61
- }
62
- declare class RWSModel<ChildClass> implements IModel {
63
- static services: IRWSModelServices;
64
- [key: string]: any;
65
- id: string;
66
- static _collection: string;
67
- static _RELATIONS: {};
68
- static _BANNED_KEYS: string[];
69
- static allModels: OpModelType<any>[];
70
- static _CUT_KEYS: string[];
71
- constructor(data: any);
72
- checkForInclusionWithThrow(): void;
73
- static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void;
74
- checkForInclusion(): boolean;
75
- static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean;
76
- _fill(data: any): RWSModel<ChildClass>;
77
- protected hasRelation(key: string): boolean;
78
- protected bindRelation(key: string, relatedModel: RWSModel<any>): RelationBindType;
79
- _asyncFill(data: any, fullDataMode?: boolean, allowRelations?: boolean): Promise<ChildClass>;
80
- private getModelScalarFields;
81
- private getTimeSeriesModelFields;
82
- private getRelationOneMeta;
83
- static getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<RWSModel<any>>>;
84
- private getRelationManyMeta;
85
- static getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<RWSModel<any>>>;
86
- toMongo(): Promise<any>;
87
- getCollection(): string | null;
88
- static getCollection(): string | null;
89
- save(): Promise<this>;
90
- static getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {
91
- annotationType: string;
92
- metadata: any;
93
- }>>;
94
- preUpdate(): void;
95
- postUpdate(): void;
96
- preCreate(): void;
97
- postCreate(): void;
98
- static isSubclass<T extends RWSModel<T>, C extends new () => T>(constructor: C, baseClass: new () => T): boolean;
99
- hasTimeSeries(): boolean;
100
- static checkTimeSeries(constructor: any): boolean;
101
- isDbVariable(variable: string): Promise<boolean>;
102
- static checkDbVariable(constructor: any, variable: string): Promise<boolean>;
103
- sanitizeDBData(data: any): any;
104
- static watchCollection<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, preRun: () => void): Promise<any>;
105
- static findOneBy<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, findParams?: FindByType): Promise<ChildClass | null>;
106
- static find<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<ChildClass | null>;
107
- static findBy<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, findParams?: FindByType): Promise<ChildClass[]>;
108
- static delete<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, conditions: any): Promise<void>;
109
- delete<ChildClass extends RWSModel<ChildClass>>(): Promise<void>;
110
- static create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
111
- static loadModels(): OpModelType<any>[];
112
- loadModels(): OpModelType<any>[];
113
- private checkRelEnabled;
114
- static setServices(services: IRWSModelServices): void;
115
- }
116
- export { IModel, TrackType, IMetaOpts, RWSModel };
1
+ /**
2
+ * @deprecated This file is deprecated. Import from 'models' directory instead.
3
+ */
4
+ import { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TrackType, IMetaOpts } from './index';
5
+ export { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TrackType, IMetaOpts };