@goatlab/fluent 0.6.18 → 0.6.21
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/dist/BaseConnector.d.ts +24 -11
- package/dist/BaseConnector.js +90 -49
- package/dist/TypeOrmConnector/TypeOrmConnector.d.ts +3 -2
- package/dist/TypeOrmConnector/TypeOrmConnector.js +15 -5
- package/dist/TypeOrmConnector/test/advanced/advancedTestSuite.js +15 -14
- package/dist/TypeOrmConnector/test/relations/relationsTestsSuite.d.ts +1 -1
- package/dist/TypeOrmConnector/test/relations/relationsTestsSuite.js +21 -18
- package/dist/index.d.ts +7 -2
- package/dist/loadRelations.d.ts +2 -1
- package/dist/loadRelations.js +22 -10
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +5 -10
- package/package.json +3 -1
package/dist/BaseConnector.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { TypedPathWrapper } from 'typed-path';
|
|
2
|
-
import { Filter, DaoOutput, BaseDaoExtendedAttributes, LogicOperator, Primitives, PrimitivesArray } from './types';
|
|
3
2
|
import { Collection } from '@goatlab/js-utils';
|
|
3
|
+
import { Filter, DaoOutput, LogicOperator, Primitives, PrimitivesArray } from './types';
|
|
4
|
+
import { ValidationError } from 'class-validator';
|
|
5
|
+
export declare type Newable<T> = {
|
|
6
|
+
new (...args: any[]): T;
|
|
7
|
+
};
|
|
4
8
|
export interface FluentConnectorInterface<InputDTO, OutputDTO> {
|
|
5
9
|
get(): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
6
10
|
all(filter: Filter): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
7
11
|
findById(id: string): Promise<DaoOutput<InputDTO, OutputDTO>>;
|
|
12
|
+
findByIds(id: string[]): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
8
13
|
find(filter: Filter): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
9
14
|
deleteById(id: string): Promise<string>;
|
|
10
15
|
updateById(id: string, data: InputDTO): Promise<DaoOutput<InputDTO, OutputDTO>>;
|
|
@@ -12,7 +17,7 @@ export interface FluentConnectorInterface<InputDTO, OutputDTO> {
|
|
|
12
17
|
insertMany(data: InputDTO[]): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
13
18
|
}
|
|
14
19
|
export declare abstract class BaseConnector<ModelDTO, InputDTO, OutputDTO> {
|
|
15
|
-
|
|
20
|
+
generatedKeyPath: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never> & {
|
|
16
21
|
$path: (path: import("typed-path").TypedPathKey[]) => string;
|
|
17
22
|
$raw: (path: import("typed-path").TypedPathKey[]) => string[];
|
|
18
23
|
$rawPath: (path: import("typed-path").TypedPathKey[]) => import("typed-path").TypedPathKey[];
|
|
@@ -41,26 +46,27 @@ export declare abstract class BaseConnector<ModelDTO, InputDTO, OutputDTO> {
|
|
|
41
46
|
protected relationQuery: any;
|
|
42
47
|
protected modelRelations: any;
|
|
43
48
|
isMongoDB: boolean;
|
|
44
|
-
protected getExtendedCreateAttributes: () => BaseDaoExtendedAttributes;
|
|
45
49
|
constructor();
|
|
50
|
+
findByIds(ids: string[]): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
46
51
|
get(): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
47
52
|
insertMany(data: InputDTO[]): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
53
|
+
updateById(id: string, data: InputDTO): Promise<DaoOutput<InputDTO, OutputDTO>>;
|
|
48
54
|
owner(user: string): this;
|
|
49
55
|
own(user: string): this;
|
|
50
56
|
first(): Promise<DaoOutput<InputDTO, OutputDTO> | null>;
|
|
51
57
|
collect(): Promise<Collection<DaoOutput<InputDTO, OutputDTO>>>;
|
|
52
|
-
select(
|
|
53
|
-
forceSelect(
|
|
58
|
+
select(paths: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>>[] | TypedPathWrapper<string[], Record<never, never>[]>): this;
|
|
59
|
+
forceSelect(paths: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>>[] | TypedPathWrapper<string[], Record<never, never>[]>): this;
|
|
54
60
|
offset(offset: number): this;
|
|
55
61
|
populate(...relations: any[]): this;
|
|
56
62
|
skip(offset: number): this;
|
|
57
|
-
where(path: TypedPathWrapper<
|
|
58
|
-
andWhere(path: TypedPathWrapper<
|
|
59
|
-
orWhere(path: TypedPathWrapper<
|
|
63
|
+
where(path: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>> | TypedPathWrapper<string[], Record<never, never>>, operator: LogicOperator, value: Primitives | PrimitivesArray): this;
|
|
64
|
+
andWhere(path: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>> | TypedPathWrapper<string[], Record<never, never>>, operator: LogicOperator, value: Primitives | Primitives[]): this;
|
|
65
|
+
orWhere(path: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>> | TypedPathWrapper<string[], Record<never, never>>, operator: LogicOperator, value: Primitives): this;
|
|
60
66
|
limit(limit: number): this;
|
|
61
67
|
take(limit: number): this;
|
|
62
|
-
pluck(path: TypedPathWrapper<
|
|
63
|
-
orderBy(path: TypedPathWrapper<
|
|
68
|
+
pluck(path: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>> | TypedPathWrapper<string[], Record<never, never>>): Promise<string[]>;
|
|
69
|
+
orderBy(path: (p: TypedPathWrapper<ModelDTO & InputDTO & OutputDTO, Record<never, never>>) => TypedPathWrapper<string, Record<never, never>> | TypedPathWrapper<string[], Record<never, never>>, order?: 'asc' | 'desc', orderType?: 'string' | 'number' | 'date'): this;
|
|
64
70
|
protected jsApplySelect(data: any): any[];
|
|
65
71
|
protected jsApplyOrderBy(data: any): any[];
|
|
66
72
|
protected reset(): void;
|
|
@@ -68,12 +74,19 @@ export declare abstract class BaseConnector<ModelDTO, InputDTO, OutputDTO> {
|
|
|
68
74
|
loadFirst(): Promise<this>;
|
|
69
75
|
getLoadedData(): DaoOutput<InputDTO, OutputDTO>[] | DaoOutput<InputDTO, OutputDTO>;
|
|
70
76
|
with(entities: any): this;
|
|
71
|
-
attach(data: InputDTO): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
77
|
+
attach(data: InputDTO | DaoOutput<InputDTO, OutputDTO>): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
72
78
|
associate(id: string): any;
|
|
73
79
|
protected hasMany<T>(Repository: any, relationName: string): T;
|
|
74
80
|
protected hasOne(): void;
|
|
75
81
|
protected belongsTo<T>(Repository: any, relationName: string): T;
|
|
76
82
|
protected belongsToMany<T, R>(Repository: any, Pivot: any, relationName: string): T;
|
|
83
|
+
withPivot(): this;
|
|
77
84
|
protected hasManyThrough(): void;
|
|
78
85
|
private prepareInput;
|
|
86
|
+
validateInput(validationClass: {
|
|
87
|
+
new (): ModelDTO;
|
|
88
|
+
}, input: InputDTO): Promise<{
|
|
89
|
+
errors: ValidationError[] | null;
|
|
90
|
+
result: Awaited<InputDTO>;
|
|
91
|
+
}>;
|
|
79
92
|
}
|
package/dist/BaseConnector.js
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseConnector = void 0;
|
|
4
4
|
const typed_path_1 = require("typed-path");
|
|
5
|
-
const dates_1 = require("@goatlab/dates");
|
|
6
5
|
const js_utils_1 = require("@goatlab/js-utils");
|
|
6
|
+
const class_transformer_1 = require("class-transformer");
|
|
7
|
+
const class_validator_1 = require("class-validator");
|
|
7
8
|
class BaseConnector {
|
|
8
9
|
constructor() {
|
|
9
|
-
this.
|
|
10
|
+
this.generatedKeyPath = (0, typed_path_1.typedPath)();
|
|
10
11
|
this.chainReference = [];
|
|
11
12
|
this.whereArray = [];
|
|
12
13
|
this.orWhereArray = [];
|
|
@@ -24,15 +25,6 @@ class BaseConnector {
|
|
|
24
25
|
this.getFirst = false;
|
|
25
26
|
this.relations = undefined;
|
|
26
27
|
this.loadModels = false;
|
|
27
|
-
this.getExtendedCreateAttributes = () => {
|
|
28
|
-
const date = dates_1.Dates.currentIsoString();
|
|
29
|
-
return {
|
|
30
|
-
id: `${js_utils_1.Ids.objectIdString()}_local`,
|
|
31
|
-
updated: date,
|
|
32
|
-
created: date,
|
|
33
|
-
roles: []
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
28
|
this.chainReference = [];
|
|
37
29
|
this.whereArray = [];
|
|
38
30
|
this.orWhereArray = [];
|
|
@@ -50,12 +42,18 @@ class BaseConnector {
|
|
|
50
42
|
this.outputKeys = [];
|
|
51
43
|
this.getFirst = false;
|
|
52
44
|
}
|
|
45
|
+
async findByIds(ids) {
|
|
46
|
+
throw new Error('findByIds() method not implemented');
|
|
47
|
+
}
|
|
53
48
|
async get() {
|
|
54
49
|
throw new Error('get() method not implemented');
|
|
55
50
|
}
|
|
56
51
|
async insertMany(data) {
|
|
57
52
|
throw new Error('get() method not implemented');
|
|
58
53
|
}
|
|
54
|
+
async updateById(id, data) {
|
|
55
|
+
throw new Error('get() method not implemented');
|
|
56
|
+
}
|
|
59
57
|
owner(user) {
|
|
60
58
|
this.chainReference.push({ method: 'owner', args: user });
|
|
61
59
|
this.ownerId = user;
|
|
@@ -79,19 +77,23 @@ class BaseConnector {
|
|
|
79
77
|
}
|
|
80
78
|
return new js_utils_1.Collection(data);
|
|
81
79
|
}
|
|
82
|
-
select(
|
|
83
|
-
|
|
80
|
+
select(paths) {
|
|
81
|
+
const arrCols = paths(this.generatedKeyPath);
|
|
82
|
+
const cols = arrCols.map(c => c.toString());
|
|
83
|
+
const columns = this.prepareInput(cols);
|
|
84
84
|
this.chainReference.push({ method: 'select', args: columns });
|
|
85
85
|
this.selectArray = this.selectArray
|
|
86
86
|
.concat(columns)
|
|
87
87
|
.filter((elem, pos, arr) => arr.indexOf(elem) === pos);
|
|
88
88
|
return this;
|
|
89
89
|
}
|
|
90
|
-
forceSelect(
|
|
90
|
+
forceSelect(paths) {
|
|
91
91
|
if (typeof module === 'undefined' || !module.exports) {
|
|
92
92
|
throw new Error('forceSelect cant be used in frontend');
|
|
93
93
|
}
|
|
94
|
-
|
|
94
|
+
const arrCols = paths(this.generatedKeyPath);
|
|
95
|
+
const cols = arrCols.map(c => c.toString());
|
|
96
|
+
const columns = this.prepareInput(cols);
|
|
95
97
|
this.chainReference.push({ method: 'forceSelect', args: columns });
|
|
96
98
|
this.forceSelectArray = this.forceSelectArray
|
|
97
99
|
.concat(columns)
|
|
@@ -112,7 +114,8 @@ class BaseConnector {
|
|
|
112
114
|
return this.offset(offset);
|
|
113
115
|
}
|
|
114
116
|
where(path, operator, value) {
|
|
115
|
-
const
|
|
117
|
+
const stringP = path(this.generatedKeyPath);
|
|
118
|
+
const stringPath = stringP.toString();
|
|
116
119
|
const chainedWhere = [stringPath, operator, value];
|
|
117
120
|
this.chainReference.push({ method: 'where', chainedWhere });
|
|
118
121
|
this.whereArray = [];
|
|
@@ -120,14 +123,16 @@ class BaseConnector {
|
|
|
120
123
|
return this;
|
|
121
124
|
}
|
|
122
125
|
andWhere(path, operator, value) {
|
|
123
|
-
const
|
|
126
|
+
const stringP = path(this.generatedKeyPath);
|
|
127
|
+
const stringPath = stringP.toString();
|
|
124
128
|
const chainedWhere = [stringPath, operator, value];
|
|
125
129
|
this.chainReference.push({ method: 'andWhere', chainedWhere });
|
|
126
130
|
this.whereArray.push(chainedWhere);
|
|
127
131
|
return this;
|
|
128
132
|
}
|
|
129
133
|
orWhere(path, operator, value) {
|
|
130
|
-
const
|
|
134
|
+
const stringP = path(this.generatedKeyPath);
|
|
135
|
+
const stringPath = stringP.toString();
|
|
131
136
|
const chainedWhere = [stringPath, operator, value];
|
|
132
137
|
this.chainReference.push({ method: 'orWhere', chainedWhere });
|
|
133
138
|
this.orWhereArray.push(chainedWhere);
|
|
@@ -142,7 +147,8 @@ class BaseConnector {
|
|
|
142
147
|
return this.limit(limit);
|
|
143
148
|
}
|
|
144
149
|
async pluck(path) {
|
|
145
|
-
const
|
|
150
|
+
const stringP = path(this.generatedKeyPath);
|
|
151
|
+
const stringPath = stringP.toString();
|
|
146
152
|
this.chainReference.push({ method: 'pluck', args: stringPath });
|
|
147
153
|
const data = await this.get();
|
|
148
154
|
const result = data.map(e => {
|
|
@@ -154,7 +160,8 @@ class BaseConnector {
|
|
|
154
160
|
return result;
|
|
155
161
|
}
|
|
156
162
|
orderBy(path, order = 'desc', orderType = 'string') {
|
|
157
|
-
const
|
|
163
|
+
const stringP = path(this.generatedKeyPath);
|
|
164
|
+
const stringPath = stringP.toString();
|
|
158
165
|
const orderB = [stringPath, order, orderType];
|
|
159
166
|
this.chainReference.push({ method: 'orderBy', orderB });
|
|
160
167
|
this.orderByArray = orderB;
|
|
@@ -279,39 +286,53 @@ class BaseConnector {
|
|
|
279
286
|
if (!this.relationQuery.relation || !this.relationQuery.data) {
|
|
280
287
|
throw new Error('Attached can only be called as a related model');
|
|
281
288
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
289
|
+
const foreignKeyName = this.relationQuery.relation.inverseSidePropertyPath;
|
|
290
|
+
const D = Array.isArray(this.relationQuery.data)
|
|
291
|
+
? this.relationQuery.data
|
|
292
|
+
: [this.relationQuery.data];
|
|
293
|
+
const relatedData = D.map(d => ({
|
|
294
|
+
[foreignKeyName]: this.isMongoDB
|
|
295
|
+
? js_utils_1.Ids.objectID(d.id)
|
|
296
|
+
: d.id,
|
|
297
|
+
...data
|
|
298
|
+
}));
|
|
299
|
+
const existingData = await this.findByIds(relatedData.map(r => r.id));
|
|
300
|
+
const updateQueries = [];
|
|
301
|
+
const insertQueries = [];
|
|
302
|
+
for (const related of relatedData) {
|
|
303
|
+
const exists = existingData.find(d => d.id === related.id);
|
|
304
|
+
if (exists) {
|
|
305
|
+
updateQueries.push(this.updateById(exists.id, {
|
|
306
|
+
...exists,
|
|
307
|
+
[foreignKeyName]: related[foreignKeyName]
|
|
308
|
+
}));
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
insertQueries.push(related);
|
|
312
|
+
}
|
|
293
313
|
}
|
|
314
|
+
const updateResult = await Promise.all(updateQueries);
|
|
315
|
+
const insertedResult = await this.insertMany(insertQueries);
|
|
316
|
+
return [...updateResult, ...insertedResult];
|
|
294
317
|
}
|
|
295
318
|
associate(id) {
|
|
296
|
-
if (!this.relationQuery
|
|
297
|
-
throw new Error('
|
|
298
|
-
}
|
|
299
|
-
if (this.relationQuery && this.relationQuery.data) {
|
|
300
|
-
const D = Array.isArray(this.relationQuery.data)
|
|
301
|
-
? this.relationQuery.data
|
|
302
|
-
: [this.relationQuery.data];
|
|
303
|
-
const relatedData = D.map(d => ({
|
|
304
|
-
[this.relationQuery.relation.joinColumns[0].propertyName]: this
|
|
305
|
-
.isMongoDB
|
|
306
|
-
? js_utils_1.Ids.objectID(d.id)
|
|
307
|
-
: d.id,
|
|
308
|
-
[this.relationQuery.relation.inverseJoinColumns[0].propertyName]: this
|
|
309
|
-
.isMongoDB
|
|
310
|
-
? js_utils_1.Ids.objectID(id)
|
|
311
|
-
: id
|
|
312
|
-
}));
|
|
313
|
-
return this.relationQuery.pivot.insertMany(relatedData);
|
|
319
|
+
if (!this.relationQuery?.relation || !this.relationQuery.data) {
|
|
320
|
+
throw new Error('Associate can only be called as a related model');
|
|
314
321
|
}
|
|
322
|
+
const D = Array.isArray(this.relationQuery.data)
|
|
323
|
+
? this.relationQuery.data
|
|
324
|
+
: [this.relationQuery.data];
|
|
325
|
+
const relatedData = D.map(d => ({
|
|
326
|
+
[this.relationQuery.relation.joinColumns[0].propertyName]: this
|
|
327
|
+
.isMongoDB
|
|
328
|
+
? js_utils_1.Ids.objectID(d.id)
|
|
329
|
+
: d.id,
|
|
330
|
+
[this.relationQuery.relation.inverseJoinColumns[0].propertyName]: this
|
|
331
|
+
.isMongoDB
|
|
332
|
+
? js_utils_1.Ids.objectID(id)
|
|
333
|
+
: id
|
|
334
|
+
}));
|
|
335
|
+
return this.relationQuery.pivot.insertMany(relatedData);
|
|
315
336
|
}
|
|
316
337
|
hasMany(Repository, relationName) {
|
|
317
338
|
if (this.relationQuery) {
|
|
@@ -346,6 +367,12 @@ class BaseConnector {
|
|
|
346
367
|
this.reset();
|
|
347
368
|
return newClass;
|
|
348
369
|
}
|
|
370
|
+
withPivot() {
|
|
371
|
+
if (this.relationQuery?.pivot) {
|
|
372
|
+
this.relationQuery.returnPivot = true;
|
|
373
|
+
}
|
|
374
|
+
return this;
|
|
375
|
+
}
|
|
349
376
|
hasManyThrough() {
|
|
350
377
|
throw new Error('Method not implemented');
|
|
351
378
|
}
|
|
@@ -357,5 +384,19 @@ class BaseConnector {
|
|
|
357
384
|
cols.filter((elem, pos, arr) => arr.indexOf(elem) === pos);
|
|
358
385
|
return cols;
|
|
359
386
|
}
|
|
387
|
+
async validateInput(validationClass, input) {
|
|
388
|
+
const validationOptions = {
|
|
389
|
+
whitelist: true,
|
|
390
|
+
skipMissingProperties: false,
|
|
391
|
+
forbidUnknownValues: true,
|
|
392
|
+
stopAtFirstError: false,
|
|
393
|
+
};
|
|
394
|
+
const instance = (0, class_transformer_1.plainToInstance)(validationClass, { ...input });
|
|
395
|
+
const errors = await (0, class_validator_1.validate)(instance, validationOptions);
|
|
396
|
+
return {
|
|
397
|
+
errors: errors && errors.length ? errors : null,
|
|
398
|
+
result: errors && errors.length ? undefined : instance,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
360
401
|
}
|
|
361
402
|
exports.BaseConnector = BaseConnector;
|
|
@@ -6,8 +6,8 @@ export declare const getRelations: (typeOrmRepo: any) => {
|
|
|
6
6
|
relations: {};
|
|
7
7
|
};
|
|
8
8
|
export declare class TypeOrmConnector<ModelDTO = BaseDataElement, InputDTO = ModelDTO, OutputDTO = InputDTO> extends BaseConnector<ModelDTO, InputDTO, OutputDTO> implements FluentConnectorInterface<InputDTO, DaoOutput<InputDTO, OutputDTO>> {
|
|
9
|
-
private repository;
|
|
10
|
-
private dataSource;
|
|
9
|
+
private readonly repository;
|
|
10
|
+
private readonly dataSource;
|
|
11
11
|
constructor(entity: any, dataSource: DataSource, relationQuery?: any);
|
|
12
12
|
get(): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
13
13
|
getPaginated(): Promise<PaginatedData<DaoOutput<InputDTO, OutputDTO>>>;
|
|
@@ -22,6 +22,7 @@ export declare class TypeOrmConnector<ModelDTO = BaseDataElement, InputDTO = Mod
|
|
|
22
22
|
clear({ sure }: Sure): Promise<boolean>;
|
|
23
23
|
deleteById(id: string): Promise<string>;
|
|
24
24
|
findById(id: string): Promise<DaoOutput<InputDTO, OutputDTO>>;
|
|
25
|
+
findByIds(ids: string[]): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
|
|
25
26
|
private getPage;
|
|
26
27
|
private getPaginatorLimit;
|
|
27
28
|
private getPopulate;
|
|
@@ -11,7 +11,7 @@ const generatorDatasource_1 = require("../generatorDatasource");
|
|
|
11
11
|
const getRelations = typeOrmRepo => {
|
|
12
12
|
const relations = {};
|
|
13
13
|
for (const relation of typeOrmRepo.metadata.relations) {
|
|
14
|
-
relations[relation.
|
|
14
|
+
relations[relation.propertyName] = {
|
|
15
15
|
isOneToMany: relation.isOneToMany,
|
|
16
16
|
isManyToOne: relation.isManyToOne,
|
|
17
17
|
isManyToMany: relation.isManyToMany,
|
|
@@ -140,7 +140,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
140
140
|
...{ updated: new Date() }
|
|
141
141
|
}
|
|
142
142
|
: data;
|
|
143
|
-
|
|
143
|
+
await this.repository.update(id, dataToInsert);
|
|
144
144
|
const dbResult = await this.repository.findBy({
|
|
145
145
|
id: (0, typeorm_1.In)([parsedId])
|
|
146
146
|
});
|
|
@@ -185,8 +185,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
185
185
|
if (!sure || sure !== true) {
|
|
186
186
|
throw new Error('Clear() method will delete everything!, you must set the "sure" parameter "clear({sure:true})" to continue');
|
|
187
187
|
}
|
|
188
|
-
|
|
189
|
-
const data = await this.repository.clear();
|
|
188
|
+
await this.repository.clear();
|
|
190
189
|
this.reset();
|
|
191
190
|
return true;
|
|
192
191
|
}
|
|
@@ -194,7 +193,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
194
193
|
const parsedId = this.isMongoDB
|
|
195
194
|
? new mongodb_1.ObjectId(id)
|
|
196
195
|
: id;
|
|
197
|
-
|
|
196
|
+
await this.repository.delete(parsedId);
|
|
198
197
|
this.reset();
|
|
199
198
|
return id;
|
|
200
199
|
}
|
|
@@ -209,6 +208,17 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
209
208
|
this.reset();
|
|
210
209
|
return result[0];
|
|
211
210
|
}
|
|
211
|
+
async findByIds(ids) {
|
|
212
|
+
const parsedIds = [...ids];
|
|
213
|
+
if (this.isMongoDB) {
|
|
214
|
+
parsedIds.map(id => new mongodb_1.ObjectId(id));
|
|
215
|
+
}
|
|
216
|
+
const data = await this.repository.findBy({
|
|
217
|
+
id: (0, typeorm_1.In)(parsedIds)
|
|
218
|
+
});
|
|
219
|
+
const result = this.jsApplySelect(data);
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
212
222
|
getPage() {
|
|
213
223
|
const page = 'page=';
|
|
214
224
|
if (this.paginator && this.paginator.page) {
|
|
@@ -48,48 +48,49 @@ const advancedTestSuite = Model => {
|
|
|
48
48
|
});
|
|
49
49
|
it('pluck() should return a single array', async () => {
|
|
50
50
|
await insertTestData(Model);
|
|
51
|
-
const data = await Model.pluck(
|
|
51
|
+
const data = await Model.pluck(keys => keys.test);
|
|
52
52
|
expect(typeof data[0]).toBe('boolean');
|
|
53
53
|
});
|
|
54
54
|
it('orderBy() should order results desc', async () => {
|
|
55
55
|
await insertTestData(Model);
|
|
56
|
-
const forms = await Model.select(
|
|
57
|
-
.orderBy(
|
|
56
|
+
const forms = await Model.select(keys => [keys.test, keys.nestedTest.b.c, keys.order])
|
|
57
|
+
.orderBy(keys => keys.order, 'desc')
|
|
58
58
|
.get();
|
|
59
59
|
expect(forms[0].order).toBe(3);
|
|
60
60
|
expect(forms[0].nestedTest.b.c).toBe(true);
|
|
61
61
|
});
|
|
62
62
|
it('orderBy() should order results asc', async () => {
|
|
63
63
|
await insertTestData(Model);
|
|
64
|
-
const forms = await Model.select(
|
|
65
|
-
.orderBy(
|
|
64
|
+
const forms = await Model.select(keys => [keys.test, keys.nestedTest.b.c, keys.order])
|
|
65
|
+
.orderBy(keys => keys.order, 'asc')
|
|
66
66
|
.get();
|
|
67
67
|
expect(forms[0].order).toBe(1);
|
|
68
68
|
});
|
|
69
69
|
it('orderBy() should order by Dates with Select()', async () => {
|
|
70
70
|
await insertTestData(Model);
|
|
71
|
-
const forms = await Model.select(
|
|
72
|
-
.orderBy(
|
|
71
|
+
const forms = await Model.select(keys => [keys.created, keys.order])
|
|
72
|
+
.orderBy(keys => keys.created, 'asc', 'date')
|
|
73
73
|
.get();
|
|
74
74
|
expect(forms[0].order).toBe(3);
|
|
75
75
|
});
|
|
76
76
|
it('orderBy() should order by Dates without Select()', async () => {
|
|
77
77
|
await insertTestData(Model);
|
|
78
|
-
const forms = await Model.orderBy(
|
|
78
|
+
const forms = await Model.orderBy(keys => keys.created, 'asc', 'date').get();
|
|
79
79
|
expect(forms[0].order).toBe(3);
|
|
80
80
|
});
|
|
81
81
|
it('limit() should limit the amount of results', async () => {
|
|
82
82
|
await insertTestData(Model);
|
|
83
|
-
const forms = await Model.select(
|
|
84
|
-
.orderBy(
|
|
83
|
+
const forms = await Model.select(keys => [keys.created, keys.order])
|
|
84
|
+
.orderBy(keys => keys.created, 'asc', 'date')
|
|
85
85
|
.limit(2)
|
|
86
86
|
.get();
|
|
87
|
+
console.log('FOOORMS LENGTH', forms[0]);
|
|
87
88
|
expect(forms.length > 0).toBe(true);
|
|
88
89
|
expect(forms.length <= 2).toBe(true);
|
|
89
90
|
});
|
|
90
91
|
it('offset() should start at the given position', async () => {
|
|
91
92
|
await insertTestData(Model);
|
|
92
|
-
const forms = await Model.select(
|
|
93
|
+
const forms = await Model.select(keys => [keys.created, keys.order])
|
|
93
94
|
.offset(1)
|
|
94
95
|
.limit(1)
|
|
95
96
|
.get();
|
|
@@ -97,7 +98,7 @@ const advancedTestSuite = Model => {
|
|
|
97
98
|
});
|
|
98
99
|
it('where() should filter the data', async () => {
|
|
99
100
|
await insertTestData(Model);
|
|
100
|
-
const forms = await Model.where(
|
|
101
|
+
const forms = await Model.where(keys => keys.nestedTest.c, '>=', 3).get();
|
|
101
102
|
expect(forms.length > 0).toBe(true);
|
|
102
103
|
forms.forEach(form => {
|
|
103
104
|
expect(form.nestedTest.c >= 3).toBe(true);
|
|
@@ -105,8 +106,8 @@ const advancedTestSuite = Model => {
|
|
|
105
106
|
});
|
|
106
107
|
it('first() should take the first result from data', async () => {
|
|
107
108
|
await insertTestData(Model);
|
|
108
|
-
const form = await Model.select(
|
|
109
|
-
.where(
|
|
109
|
+
const form = await Model.select(keys => [keys.nestedTest.c, keys.id])
|
|
110
|
+
.where(keys => keys.nestedTest.c, '>=', 3)
|
|
110
111
|
.first();
|
|
111
112
|
expect(typeof form.nestedTest.c).toBe('number');
|
|
112
113
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const relationsTestSuite: (
|
|
1
|
+
export declare const relationsTestSuite: (ModelF: any, BelongsToModelF: any, ManyToManyModelF: any) => void;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.relationsTestSuite = void 0;
|
|
4
|
-
|
|
4
|
+
let Model;
|
|
5
|
+
let BelongsToModel;
|
|
6
|
+
let ManyToManyModel;
|
|
7
|
+
const relationsTestSuite = (ModelF, BelongsToModelF, ManyToManyModelF) => {
|
|
5
8
|
beforeAll(() => {
|
|
6
|
-
Model = new
|
|
7
|
-
BelongsToModel = new
|
|
8
|
-
ManyToManyModel = new
|
|
9
|
+
Model = new ModelF();
|
|
10
|
+
BelongsToModel = new BelongsToModelF();
|
|
11
|
+
ManyToManyModel = new ManyToManyModelF();
|
|
9
12
|
});
|
|
10
13
|
test('Attach - OneToMany - Should insert data', async () => {
|
|
11
14
|
const insertedUser = await Model.insert({
|
|
@@ -13,7 +16,7 @@ const relationsTestSuite = (Model, BelongsToModel, ManyToManyModel) => {
|
|
|
13
16
|
age: 20
|
|
14
17
|
});
|
|
15
18
|
expect(typeof insertedUser.id).toBe('string');
|
|
16
|
-
const user = await Model.where(
|
|
19
|
+
const user = await Model.where(keys => keys.id, '=', insertedUser.id).load();
|
|
17
20
|
const cars = await user.cars().attach({ name: 'Another new car' });
|
|
18
21
|
expect(Array.isArray(cars)).toBe(true);
|
|
19
22
|
expect(cars[0].name).toBe('Another new car');
|
|
@@ -24,36 +27,36 @@ const relationsTestSuite = (Model, BelongsToModel, ManyToManyModel) => {
|
|
|
24
27
|
age: 20
|
|
25
28
|
});
|
|
26
29
|
expect(typeof insertedUser.id).toBe('string');
|
|
27
|
-
const user1 = await Model.where(
|
|
30
|
+
const user1 = await Model.where(keys => keys.id, '=', insertedUser.id).load();
|
|
28
31
|
const cars = await user1.cars().attach({ name: 'My new car' });
|
|
29
32
|
expect(Array.isArray(cars)).toBe(true);
|
|
30
|
-
const searchUserWithRelation = await Model.where(
|
|
31
|
-
.with({ cars:
|
|
33
|
+
const searchUserWithRelation = await Model.where(keys => keys.id, '=', insertedUser.id)
|
|
34
|
+
.with({ cars: BelongsToModelF })
|
|
32
35
|
.get();
|
|
33
36
|
expect(Array.isArray(searchUserWithRelation[0].cars)).toBe(true);
|
|
34
37
|
expect(searchUserWithRelation[0].cars.length > 0).toBe(true);
|
|
35
38
|
const searchCar = await user1
|
|
36
39
|
.cars()
|
|
37
|
-
.where(
|
|
40
|
+
.where(keys => keys.name, '=', 'My new car')
|
|
38
41
|
.get();
|
|
39
42
|
expect(Array.isArray(searchCar)).toBe(true);
|
|
40
43
|
expect(searchCar.length > 0).toBe(true);
|
|
41
44
|
const searchCar2 = await user1
|
|
42
45
|
.cars()
|
|
43
|
-
.where(
|
|
46
|
+
.where(keys => keys.name, '=', 'My.......')
|
|
44
47
|
.get();
|
|
45
48
|
expect(Array.isArray(searchCar2)).toBe(true);
|
|
46
49
|
expect(searchCar2.length === 0).toBe(true);
|
|
47
50
|
});
|
|
48
|
-
test('Query related model -
|
|
51
|
+
test('Query related model - BelongTo', async () => {
|
|
49
52
|
const insertedUser = await Model.insert({
|
|
50
53
|
name: 'testUser',
|
|
51
54
|
age: 20
|
|
52
55
|
});
|
|
53
56
|
expect(typeof insertedUser.id).toBe('string');
|
|
54
|
-
const user1 = await Model.where(
|
|
57
|
+
const user1 = await Model.where(keys => keys.id, '=', insertedUser.id).load();
|
|
55
58
|
await user1.cars().attach({ name: 'My new car' });
|
|
56
|
-
const results = await BelongsToModel.with({
|
|
59
|
+
const results = await BelongsToModel.with({ user: ModelF }).get();
|
|
57
60
|
expect(Array.isArray(results)).toBe(true);
|
|
58
61
|
expect(results.length > 0).toBe(true);
|
|
59
62
|
expect(typeof results[0].user.name).toBe('string');
|
|
@@ -67,21 +70,21 @@ const relationsTestSuite = (Model, BelongsToModel, ManyToManyModel) => {
|
|
|
67
70
|
const adminRole = await ManyToManyModel.insert({
|
|
68
71
|
name: 'Administrator'
|
|
69
72
|
});
|
|
70
|
-
const user = await Model.where(
|
|
73
|
+
const user = await Model.where(keys => keys.id, '=', insertedUser.id).load();
|
|
71
74
|
const associated = await user.roles().associate(adminRole.id);
|
|
72
75
|
expect(associated[0].userId).toBe(insertedUser.id);
|
|
73
76
|
expect(associated[0].roleId).toBe(adminRole.id);
|
|
74
|
-
const searchUserWithRelation = await Model.where(
|
|
77
|
+
const searchUserWithRelation = await Model.where(keys => keys.id, '=', insertedUser.id)
|
|
75
78
|
.with({
|
|
76
|
-
roles:
|
|
79
|
+
roles: ManyToManyModelF
|
|
77
80
|
})
|
|
78
81
|
.get();
|
|
79
82
|
expect(Array.isArray(searchUserWithRelation[0].roles)).toBe(true);
|
|
80
83
|
expect(searchUserWithRelation[0].roles.length > 0).toBe(true);
|
|
81
84
|
expect(typeof searchUserWithRelation[0].roles[0].name).toBe('string');
|
|
82
|
-
const roles = await ManyToManyModel.where(
|
|
85
|
+
const roles = await ManyToManyModel.where(keys => keys.name, '=', 'Administrator')
|
|
83
86
|
.with({
|
|
84
|
-
users:
|
|
87
|
+
users: ModelF
|
|
85
88
|
})
|
|
86
89
|
.get();
|
|
87
90
|
expect(Array.isArray(roles)).toBe(true);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ApiHideProperty, ApiProperty, Column, HideField, InputType, ObjectType, OmitType, PartialType, getModelSchemaRef } from './core/types';
|
|
2
|
-
import type {
|
|
2
|
+
import type { BaseDataElement, DaoOutput, Deleted, Filter, LogicOperator, PaginatedData, Paginator, Paths, Primitives, PrimitivesArray, Sure, WhereClause } from './types';
|
|
3
3
|
import { PaginationLinks, PaginationMeta } from './core/dtos/pagination.dto';
|
|
4
4
|
import { Access } from './core/dtos/access.dto';
|
|
5
5
|
import { BaseConnector } from './BaseConnector';
|
|
@@ -13,6 +13,11 @@ import { getOutputKeys } from './outputKeys';
|
|
|
13
13
|
import { loadRelations } from './loadRelations';
|
|
14
14
|
import { modelGeneratorDataSource } from './generatorDatasource';
|
|
15
15
|
import { Cache } from './cache';
|
|
16
|
+
import { ValidationError } from 'class-validator';
|
|
16
17
|
export { DataSource } from 'typeorm';
|
|
17
18
|
export { Access, ApiHideProperty, ApiProperty, BaseConnector, Collection, Column, Decorators, Fluent, getModelSchemaRef, getOutputKeys, HideField, InputType, loadRelations, modelGeneratorDataSource, ObjectType, OmitType, PaginationLinks, PaginationMeta, PartialType, TypeOrmConnector, Cache };
|
|
18
|
-
|
|
19
|
+
interface ValidatedInput<T> {
|
|
20
|
+
errors: ValidationError[] | null;
|
|
21
|
+
result: Awaited<T>;
|
|
22
|
+
}
|
|
23
|
+
export type { BaseDataElement, DaoOutput, Deleted, Filter, FluentConnectorInterface, LogicOperator, PaginatedData, Paginator, Paths, Primitives, PrimitivesArray, Sure, WhereClause, SchemaObject, ValidatedInput };
|
package/dist/loadRelations.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ interface RelationshipLoader {
|
|
|
6
6
|
dataSource?: DataSource;
|
|
7
7
|
provider?: 'typeorm' | 'firebase';
|
|
8
8
|
self: any;
|
|
9
|
+
returnPivot?: boolean;
|
|
9
10
|
}
|
|
10
|
-
export declare const loadRelations: ({ data, relations, modelRelations,
|
|
11
|
+
export declare const loadRelations: ({ data, relations, modelRelations, provider, self, returnPivot }: RelationshipLoader) => Promise<any[]>;
|
|
11
12
|
export {};
|