@clairejs/server 3.19.0 → 3.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/common/request/HttpData.d.ts +9 -1
- package/dist/http/controller/AbstractHttpController.d.ts +1 -0
- package/dist/http/controller/AbstractHttpController.js +4 -4
- package/dist/http/controller/CrudHttpController.d.ts +1 -0
- package/dist/http/controller/CrudHttpController.js +10 -1
- package/dist/http/repository/DtoRepository.d.ts +8 -8
- package/dist/http/repository/DtoRepository.js +4 -4
- package/dist/http/repository/ICrudRepository.d.ts +3 -3
- package/dist/http/repository/ModelRepository.d.ts +3 -3
- package/dist/http/repository/ModelRepository.js +15 -9
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,5 +4,6 @@ import { Transactionable } from "../../common/Transactionable";
|
|
|
4
4
|
export declare abstract class AbstractHttpController extends Transactionable {
|
|
5
5
|
protected readonly db: ITransactionFactory;
|
|
6
6
|
constructor(db: ITransactionFactory);
|
|
7
|
+
protected isEndpoint(metadata: EndpointMetadata): boolean;
|
|
7
8
|
getEndpointMetadata(): Readonly<EndpointMetadata>[];
|
|
8
9
|
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { getObjectMetadata } from "@clairejs/core";
|
|
2
2
|
import { Transactionable } from "../../common/Transactionable";
|
|
3
|
-
const isEndpoint = (metadata) => {
|
|
4
|
-
return !!metadata.method;
|
|
5
|
-
};
|
|
6
3
|
export class AbstractHttpController extends Transactionable {
|
|
7
4
|
db;
|
|
8
5
|
constructor(db) {
|
|
9
6
|
super(db);
|
|
10
7
|
this.db = db;
|
|
11
8
|
}
|
|
9
|
+
isEndpoint(metadata) {
|
|
10
|
+
return !!metadata.method;
|
|
11
|
+
}
|
|
12
12
|
getEndpointMetadata() {
|
|
13
13
|
const controllerMetadata = getObjectMetadata(this.constructor);
|
|
14
14
|
if (!controllerMetadata) {
|
|
15
15
|
return [];
|
|
16
16
|
}
|
|
17
|
-
return controllerMetadata.fields.filter((f) => isEndpoint(f));
|
|
17
|
+
return controllerMetadata.fields.filter((f) => this.isEndpoint(f));
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -14,6 +14,7 @@ export declare class CrudHttpController<T extends Identifiable> extends Abstract
|
|
|
14
14
|
constructor(model: Constructor<T>, crudRepository: ICrudRepository<T>, db: ITransactionFactory & IQueryProvider);
|
|
15
15
|
protected getMountedUrl(): string;
|
|
16
16
|
private getAuthProvider;
|
|
17
|
+
protected isEndpoint(metadata: EndpointMetadata): boolean;
|
|
17
18
|
/**
|
|
18
19
|
* This provides endpoint metadata for mounting /POST request.
|
|
19
20
|
*/
|
|
@@ -40,6 +40,16 @@ export class CrudHttpController extends AbstractHttpController {
|
|
|
40
40
|
}
|
|
41
41
|
return this.principalResolver;
|
|
42
42
|
}
|
|
43
|
+
isEndpoint(metadata) {
|
|
44
|
+
return (super.isEndpoint(metadata) ||
|
|
45
|
+
[
|
|
46
|
+
CrudHttpController.prototype.createMany.name,
|
|
47
|
+
CrudHttpController.prototype.getMany.name,
|
|
48
|
+
CrudHttpController.prototype.updateMany.name,
|
|
49
|
+
CrudHttpController.prototype.deleteMany.name,
|
|
50
|
+
CrudHttpController.prototype.updateRecords.name,
|
|
51
|
+
].includes(metadata.name));
|
|
52
|
+
}
|
|
43
53
|
/**
|
|
44
54
|
* This provides endpoint metadata for mounting /POST request.
|
|
45
55
|
*/
|
|
@@ -197,7 +207,6 @@ export class CrudHttpController extends AbstractHttpController {
|
|
|
197
207
|
async getMany(req) {
|
|
198
208
|
const result = await this.crudRepository.getMany({
|
|
199
209
|
queries: req.getQuery(),
|
|
200
|
-
queryProvider: this.db,
|
|
201
210
|
});
|
|
202
211
|
return ResponseBuilder.json(result).get();
|
|
203
212
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { IPrincipal } from "../../common/auth/IPrincipal";
|
|
1
|
+
import { type Identifiable, type Constructor, type CreateManyRequestBody, type CreateManyResponseBody, type DeepPartial, type GetManyQueries, type GetManyResponseBody, type UpdateManyBody, type UpdateManyQueries, type UpdateManyResponse, AbstractLogger, AbstractModel } from "@clairejs/core";
|
|
2
|
+
import { type ITransaction, type QueryCondition, AbstractDbAdapter } from "@clairejs/orm";
|
|
3
|
+
import { type IPrincipal } from "../../common/auth/IPrincipal";
|
|
4
4
|
import { AbstractRepository } from "./AbstractRepository";
|
|
5
|
-
import { ICrudRepository } from "./ICrudRepository";
|
|
5
|
+
import { type ICrudRepository } from "./ICrudRepository";
|
|
6
6
|
export type DtoDissolver<R extends Identifiable, K extends Identifiable> = (t: DeepPartial<K>) => MappingMetadata<R, any>[];
|
|
7
7
|
export interface MappingMetadata<R extends Identifiable, K extends AbstractModel> {
|
|
8
8
|
multiple: boolean;
|
|
@@ -37,7 +37,7 @@ export declare class DtoRepository<T extends Identifiable> extends AbstractRepos
|
|
|
37
37
|
createMany({ principal, body, tx, logger, }: {
|
|
38
38
|
principal?: IPrincipal | undefined;
|
|
39
39
|
body: CreateManyRequestBody<T>;
|
|
40
|
-
tx: ITransaction
|
|
40
|
+
tx: ITransaction;
|
|
41
41
|
logger?: AbstractLogger;
|
|
42
42
|
}): Promise<CreateManyResponseBody<T>>;
|
|
43
43
|
updateMany({ principal, queries, ops, body, tx, logger, }: {
|
|
@@ -45,13 +45,13 @@ export declare class DtoRepository<T extends Identifiable> extends AbstractRepos
|
|
|
45
45
|
queries?: UpdateManyQueries<T>;
|
|
46
46
|
ops?: QueryCondition<T>[];
|
|
47
47
|
body: UpdateManyBody<T>;
|
|
48
|
-
tx: ITransaction
|
|
48
|
+
tx: ITransaction;
|
|
49
49
|
logger?: AbstractLogger;
|
|
50
50
|
}): Promise<UpdateManyResponse<T>>;
|
|
51
|
-
getMany({ queries, ops,
|
|
51
|
+
getMany({ queries, ops, tx, logger, }: {
|
|
52
52
|
queries?: GetManyQueries<T>;
|
|
53
53
|
ops?: QueryCondition<T>[];
|
|
54
|
-
|
|
54
|
+
tx?: ITransaction;
|
|
55
55
|
logger?: AbstractLogger;
|
|
56
56
|
}): Promise<GetManyResponseBody<T>>;
|
|
57
57
|
deleteMany({ queries, ops, tx, logger, }: {
|
|
@@ -61,7 +61,7 @@ export class DtoRepository extends AbstractRepository {
|
|
|
61
61
|
const repo = new ModelRepository(mapper.modelClass, this.db);
|
|
62
62
|
const currentInstances = await repo.getMany({
|
|
63
63
|
ops: mapper.forwardOps(),
|
|
64
|
-
|
|
64
|
+
tx,
|
|
65
65
|
logger,
|
|
66
66
|
});
|
|
67
67
|
return currentInstances.records;
|
|
@@ -123,7 +123,7 @@ export class DtoRepository extends AbstractRepository {
|
|
|
123
123
|
const repo = new ModelRepository(mapper.modelClass, this.db);
|
|
124
124
|
const currentInstances = await repo.getMany({
|
|
125
125
|
ops: mapper.forwardOps(ops),
|
|
126
|
-
|
|
126
|
+
tx,
|
|
127
127
|
logger,
|
|
128
128
|
});
|
|
129
129
|
return currentInstances.records;
|
|
@@ -173,7 +173,7 @@ export class DtoRepository extends AbstractRepository {
|
|
|
173
173
|
}
|
|
174
174
|
return { modified: result.id ? this.project([result], projection) : [] };
|
|
175
175
|
}
|
|
176
|
-
async getMany({ queries, ops,
|
|
176
|
+
async getMany({ queries, ops, tx, logger, }) {
|
|
177
177
|
//-- check queries for id field
|
|
178
178
|
if (!queries?.fields?.id || queries.fields.id.length !== 1) {
|
|
179
179
|
throw Errors.VALIDATION_ERROR("Missing required id field in query");
|
|
@@ -184,7 +184,7 @@ export class DtoRepository extends AbstractRepository {
|
|
|
184
184
|
//-- forward get
|
|
185
185
|
const instances = await repo.getMany({
|
|
186
186
|
ops: mapper.forwardOps(ops),
|
|
187
|
-
|
|
187
|
+
tx,
|
|
188
188
|
logger,
|
|
189
189
|
});
|
|
190
190
|
return instances.records;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AbstractLogger, type CreateManyRequestBody, type CreateManyResponseBody, type GetManyQueries, type GetManyResponseBody, type UpdateManyBody, type UpdateManyQueries, type UpdateManyResponse } from "@clairejs/core";
|
|
2
|
-
import { type
|
|
2
|
+
import { type ITransaction, type QueryCondition } from "@clairejs/orm";
|
|
3
3
|
import { type IPrincipal } from "../../common/auth/IPrincipal";
|
|
4
4
|
export interface ICrudRepository<T> {
|
|
5
5
|
createMany({ principal, body, tx, logger, }: {
|
|
@@ -8,10 +8,10 @@ export interface ICrudRepository<T> {
|
|
|
8
8
|
logger?: AbstractLogger;
|
|
9
9
|
principal?: IPrincipal;
|
|
10
10
|
}): Promise<CreateManyResponseBody<T>>;
|
|
11
|
-
getMany({ queries, ops,
|
|
11
|
+
getMany({ queries, ops, tx, logger, }: {
|
|
12
12
|
queries?: GetManyQueries<T>;
|
|
13
13
|
ops?: QueryCondition<T>[];
|
|
14
|
-
|
|
14
|
+
tx?: ITransaction;
|
|
15
15
|
logger?: AbstractLogger;
|
|
16
16
|
}): Promise<GetManyResponseBody<T>>;
|
|
17
17
|
updateMany({ principal, queries, ops, body, tx, logger, }: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbstractModel, Constructor, CreateManyRequestBody, CreateManyResponseBody, GetManyQueries, GetManyResponseBody, UpdateManyBody, UpdateManyQueries, UpdateManyResponse, AbstractLogger } from "@clairejs/core";
|
|
2
|
-
import { AbstractDbAdapter,
|
|
2
|
+
import { AbstractDbAdapter, ITransaction, QueryCondition } from "@clairejs/orm";
|
|
3
3
|
import { IPrincipal } from "../../common/auth/IPrincipal";
|
|
4
4
|
import { ICrudRepository } from "./ICrudRepository";
|
|
5
5
|
import { AbstractRepository } from "./AbstractRepository";
|
|
@@ -27,10 +27,10 @@ export declare class ModelRepository<T extends AbstractModel> extends AbstractRe
|
|
|
27
27
|
tx: ITransaction;
|
|
28
28
|
logger?: AbstractLogger;
|
|
29
29
|
}): Promise<UpdateManyResponse<T>>;
|
|
30
|
-
getMany({ queries, ops,
|
|
30
|
+
getMany({ queries, ops, tx, logger, }: {
|
|
31
31
|
queries?: GetManyQueries<T>;
|
|
32
32
|
ops?: QueryCondition<T>[];
|
|
33
|
-
|
|
33
|
+
tx?: ITransaction;
|
|
34
34
|
logger?: AbstractLogger;
|
|
35
35
|
}): Promise<GetManyResponseBody<T>>;
|
|
36
36
|
deleteMany({ queries, ops, tx, logger, }: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataType, getModelById, getServiceProvider, RangeQueryDto, uniqueReducer, leanData, getSystemLocale, Errors, omitData, } from "@clairejs/core";
|
|
2
|
-
import { getDirectFields } from "@clairejs/orm";
|
|
2
|
+
import { getDirectFields, } from "@clairejs/orm";
|
|
3
3
|
import { AbstractFileUploadHandler } from "../file-upload/AbstractFileUploadHandler";
|
|
4
4
|
import { AbstractRepository } from "./AbstractRepository";
|
|
5
5
|
import { LocaleTranslation } from "../../system/locale/LocaleTranslation";
|
|
@@ -280,7 +280,9 @@ export class ModelRepository extends AbstractRepository {
|
|
|
280
280
|
return data;
|
|
281
281
|
});
|
|
282
282
|
await this.beforeCreating(principal, body.records);
|
|
283
|
-
let records = body.records.length
|
|
283
|
+
let records = body.records.length
|
|
284
|
+
? await this.db.use(this.model, tx).createMany(body.records)
|
|
285
|
+
: [];
|
|
284
286
|
await this.beforeReturning(records);
|
|
285
287
|
const projection = this.modelMetadata.fields
|
|
286
288
|
.filter((field) => !field.multiLocaleColumn && (field.pk || field.serverValue || field.mimeProps))
|
|
@@ -364,7 +366,9 @@ export class ModelRepository extends AbstractRepository {
|
|
|
364
366
|
let modified = [];
|
|
365
367
|
let updatedRecords = [];
|
|
366
368
|
if (nestedQueries.length) {
|
|
367
|
-
const tobeUpdated = await this.db
|
|
369
|
+
const tobeUpdated = await this.db
|
|
370
|
+
.use(this.model, tx)
|
|
371
|
+
.getMany(condition, { projection: ["id"] }, nestedQueries);
|
|
368
372
|
modified = tobeUpdated.records.map((r) => r.id);
|
|
369
373
|
if (modified.length) {
|
|
370
374
|
if (directUpdateFields.length) {
|
|
@@ -405,7 +409,9 @@ export class ModelRepository extends AbstractRepository {
|
|
|
405
409
|
field: field.name,
|
|
406
410
|
})))
|
|
407
411
|
.flatMap((arr) => arr);
|
|
408
|
-
const missingEntries = !missing.length
|
|
412
|
+
const missingEntries = !missing.length
|
|
413
|
+
? []
|
|
414
|
+
: await this.db.use(LocaleEntry, tx).createMany(missing.map(() => ({})));
|
|
409
415
|
const updateEntryRecords = [];
|
|
410
416
|
missingEntries.forEach((entry, index) => {
|
|
411
417
|
const missingData = missing[index];
|
|
@@ -512,7 +518,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
512
518
|
await this.beforeReturning(records);
|
|
513
519
|
return { modified: this.project(records, projection) };
|
|
514
520
|
}
|
|
515
|
-
async getMany({ queries, ops,
|
|
521
|
+
async getMany({ queries, ops, tx, logger: _logger, }) {
|
|
516
522
|
const conditions = ops || [];
|
|
517
523
|
if (queries?.fields) {
|
|
518
524
|
conditions.push(...this.getRequestQueryConditionFromQuery(queries.fields, this.modelMetadata));
|
|
@@ -535,7 +541,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
535
541
|
...queries.projection.filter((fieldName) => !this.modelMetadata.fields.find((f) => f.name === fieldName && !!f.hasMany)),
|
|
536
542
|
...localeOfFields.map((f) => f.name),
|
|
537
543
|
];
|
|
538
|
-
const result = await
|
|
544
|
+
const result = await this.db.use(this.model, tx).getMany(conditions.length ? { _and: conditions } : {}, {
|
|
539
545
|
limit: queries?.limit,
|
|
540
546
|
page: queries?.page,
|
|
541
547
|
projection: finalProjection,
|
|
@@ -553,12 +559,12 @@ export class ModelRepository extends AbstractRepository {
|
|
|
553
559
|
? []
|
|
554
560
|
: !queries?.locale
|
|
555
561
|
? //-- if locale is not specified then get translation for all locales
|
|
556
|
-
await
|
|
562
|
+
await this.db.use(LocaleTranslation, tx).getRecords({
|
|
557
563
|
_in: { entryId: allLocaleEntries },
|
|
558
564
|
}, { projection: ["entryId", "localeCode", "translation"] })
|
|
559
565
|
: //-- if locale is specified then get only the translation of that locale
|
|
560
566
|
queries.locale.toLowerCase() !== systemLocale
|
|
561
|
-
? await
|
|
567
|
+
? await this.db.use(LocaleTranslation, tx).getRecords({
|
|
562
568
|
_in: { entryId: allLocaleEntries },
|
|
563
569
|
_eq: { localeCode: queries.locale },
|
|
564
570
|
}, { projection: ["entryId", "localeCode", "translation"] })
|
|
@@ -600,7 +606,7 @@ export class ModelRepository extends AbstractRepository {
|
|
|
600
606
|
? { records: [] }
|
|
601
607
|
: await service.getMany({
|
|
602
608
|
queries: { fields: { [field.hasMany.column]: recordIds }, limit: field.hasMany.single ? 1 : 0 },
|
|
603
|
-
|
|
609
|
+
tx,
|
|
604
610
|
});
|
|
605
611
|
//-- filter corresponding inner records for each master record
|
|
606
612
|
for (const record of result.records) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clairejs/server",
|
|
3
|
-
"version": "3.19.
|
|
3
|
+
"version": "3.19.2",
|
|
4
4
|
"description": "Claire server NodeJs framework written in Typescript.",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@clairejs/core": "^3.7.9",
|
|
37
|
-
"@clairejs/orm": "^3.15.
|
|
37
|
+
"@clairejs/orm": "^3.15.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/cookie-parser": "^1.4.3",
|