@ruiapp/rapid-core 0.11.1 → 0.11.3
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/index.js +24 -6
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
- package/src/dataAccess/entityManager.ts +23 -4
- package/src/dataAccess/entityMapper.ts +2 -2
- package/src/types.ts +5 -1
package/dist/index.js
CHANGED
|
@@ -2676,7 +2676,7 @@ function mapEntityToDbRow(server, model, entity) {
|
|
|
2676
2676
|
return result;
|
|
2677
2677
|
}
|
|
2678
2678
|
const propertyNames = Object.keys(entity);
|
|
2679
|
-
|
|
2679
|
+
for (const propertyName of propertyNames) {
|
|
2680
2680
|
let columnName = propertyName;
|
|
2681
2681
|
const property = getEntityPropertyByCode(server, model, propertyName);
|
|
2682
2682
|
if (property) {
|
|
@@ -2701,7 +2701,7 @@ function mapEntityToDbRow(server, model, entity) {
|
|
|
2701
2701
|
}
|
|
2702
2702
|
}
|
|
2703
2703
|
}
|
|
2704
|
-
}
|
|
2704
|
+
}
|
|
2705
2705
|
return result;
|
|
2706
2706
|
}
|
|
2707
2707
|
|
|
@@ -3516,6 +3516,13 @@ async function createEntity(server, dataAccessor, options, plugin) {
|
|
|
3516
3516
|
if (createdAtProperty) {
|
|
3517
3517
|
entity.createdAt = getNowStringWithTimezone();
|
|
3518
3518
|
}
|
|
3519
|
+
let baseModel;
|
|
3520
|
+
if (model.base) {
|
|
3521
|
+
baseModel = server.getModel({
|
|
3522
|
+
singularCode: model.base,
|
|
3523
|
+
});
|
|
3524
|
+
entity[baseModel.derivedTypePropertyCode] = model.derivedType;
|
|
3525
|
+
}
|
|
3519
3526
|
await server.beforeCreateEntity(model, options);
|
|
3520
3527
|
await server.emitEvent({
|
|
3521
3528
|
eventName: "entity.beforeCreate",
|
|
@@ -4411,13 +4418,13 @@ class EntityManager {
|
|
|
4411
4418
|
async addRelations(options, plugin) {
|
|
4412
4419
|
const server = this.#server;
|
|
4413
4420
|
const model = this.getModel();
|
|
4414
|
-
const { id, property, relations, routeContext } = options;
|
|
4421
|
+
const { id: selfId, property, relations, routeContext } = options;
|
|
4415
4422
|
const entity = await this.findById({
|
|
4416
|
-
id,
|
|
4423
|
+
id: selfId,
|
|
4417
4424
|
routeContext,
|
|
4418
4425
|
});
|
|
4419
4426
|
if (!entity) {
|
|
4420
|
-
throw new Error(`${model.namespace}.${model.singularCode} with id "${
|
|
4427
|
+
throw new Error(`${model.namespace}.${model.singularCode} with id "${selfId}" was not found.`);
|
|
4421
4428
|
}
|
|
4422
4429
|
const relationProperty = getEntityPropertyByCode(server, model, property);
|
|
4423
4430
|
if (!relationProperty) {
|
|
@@ -4438,10 +4445,21 @@ class EntityManager {
|
|
|
4438
4445
|
FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
|
|
4439
4446
|
WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
|
|
4440
4447
|
)`;
|
|
4441
|
-
const
|
|
4448
|
+
const targetId = relation[relationProperty.targetIdColumnName] || relation.id;
|
|
4449
|
+
const params = [selfId, targetId];
|
|
4442
4450
|
await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
|
|
4443
4451
|
}
|
|
4444
4452
|
}
|
|
4453
|
+
else {
|
|
4454
|
+
const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
|
|
4455
|
+
for (const relation of relations) {
|
|
4456
|
+
relation[relationProperty.selfIdColumnName] = selfId;
|
|
4457
|
+
await targetEntityManager.createEntity({
|
|
4458
|
+
routeContext,
|
|
4459
|
+
entity: relation,
|
|
4460
|
+
});
|
|
4461
|
+
}
|
|
4462
|
+
}
|
|
4445
4463
|
await server.emitEvent({
|
|
4446
4464
|
eventName: "entity.addRelations",
|
|
4447
4465
|
payload: {
|
package/dist/types.d.ts
CHANGED
|
@@ -666,8 +666,8 @@ export interface RemoveEntityRelationsOptions {
|
|
|
666
666
|
[k: string]: any;
|
|
667
667
|
}[];
|
|
668
668
|
}
|
|
669
|
-
export type EntityWatcherType = EntityWatcher<"entity.create"> | EntityWatcher<"entity.update"> | EntityWatcher<"entity.delete"> | EntityWatcher<"entity.addRelations"> | EntityWatcher<"entity.removeRelations"> | EntityWatcher<any>;
|
|
670
|
-
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes
|
|
669
|
+
export type EntityWatcherType = EntityWatcher<"entity.beforeCreate"> | EntityWatcher<"entity.create"> | EntityWatcher<"entity.beforeUpdate"> | EntityWatcher<"entity.update"> | EntityWatcher<"entity.beforeDelete"> | EntityWatcher<"entity.delete"> | EntityWatcher<"entity.addRelations"> | EntityWatcher<"entity.removeRelations"> | EntityWatcher<"entity.beforeResponse"> | EntityWatcher<any>;
|
|
670
|
+
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes> {
|
|
671
671
|
eventName: TEventName;
|
|
672
672
|
modelSingularCode: string;
|
|
673
673
|
handler: EntityWatchHandler<TEventName>;
|
package/package.json
CHANGED
|
@@ -829,6 +829,14 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
|
|
|
829
829
|
entity.createdAt = getNowStringWithTimezone();
|
|
830
830
|
}
|
|
831
831
|
|
|
832
|
+
let baseModel: RpdDataModel | undefined;
|
|
833
|
+
if (model.base) {
|
|
834
|
+
baseModel = server.getModel({
|
|
835
|
+
singularCode: model.base,
|
|
836
|
+
});
|
|
837
|
+
entity[baseModel.derivedTypePropertyCode] = model.derivedType;
|
|
838
|
+
}
|
|
839
|
+
|
|
832
840
|
await server.beforeCreateEntity(model, options);
|
|
833
841
|
|
|
834
842
|
await server.emitEvent({
|
|
@@ -1871,13 +1879,13 @@ export default class EntityManager<TEntity = any> {
|
|
|
1871
1879
|
async addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<void> {
|
|
1872
1880
|
const server = this.#server;
|
|
1873
1881
|
const model = this.getModel();
|
|
1874
|
-
const { id, property, relations, routeContext } = options;
|
|
1882
|
+
const { id: selfId, property, relations, routeContext } = options;
|
|
1875
1883
|
const entity = await this.findById({
|
|
1876
|
-
id,
|
|
1884
|
+
id: selfId,
|
|
1877
1885
|
routeContext,
|
|
1878
1886
|
});
|
|
1879
1887
|
if (!entity) {
|
|
1880
|
-
throw new Error(`${model.namespace}.${model.singularCode} with id "${
|
|
1888
|
+
throw new Error(`${model.namespace}.${model.singularCode} with id "${selfId}" was not found.`);
|
|
1881
1889
|
}
|
|
1882
1890
|
|
|
1883
1891
|
const relationProperty = getEntityPropertyByCode(server, model, property);
|
|
@@ -1901,9 +1909,20 @@ export default class EntityManager<TEntity = any> {
|
|
|
1901
1909
|
FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
|
|
1902
1910
|
WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName!)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName!)}=$2
|
|
1903
1911
|
)`;
|
|
1904
|
-
|
|
1912
|
+
|
|
1913
|
+
const targetId = relation[relationProperty.targetIdColumnName!] || relation.id;
|
|
1914
|
+
const params = [selfId, targetId];
|
|
1905
1915
|
await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
|
|
1906
1916
|
}
|
|
1917
|
+
} else {
|
|
1918
|
+
const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
|
|
1919
|
+
for (const relation of relations) {
|
|
1920
|
+
relation[relationProperty.selfIdColumnName!] = selfId;
|
|
1921
|
+
await targetEntityManager.createEntity({
|
|
1922
|
+
routeContext,
|
|
1923
|
+
entity: relation,
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1907
1926
|
}
|
|
1908
1927
|
|
|
1909
1928
|
await server.emitEvent({
|
|
@@ -83,7 +83,7 @@ export function mapEntityToDbRow(server: IRpdServer, model: RpdDataModel, entity
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
const propertyNames = Object.keys(entity);
|
|
86
|
-
|
|
86
|
+
for (const propertyName of propertyNames) {
|
|
87
87
|
let columnName = propertyName;
|
|
88
88
|
const property = getEntityPropertyByCode(server, model, propertyName);
|
|
89
89
|
if (property) {
|
|
@@ -105,7 +105,7 @@ export function mapEntityToDbRow(server: IRpdServer, model: RpdDataModel, entity
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
-
}
|
|
108
|
+
}
|
|
109
109
|
|
|
110
110
|
return result;
|
|
111
111
|
}
|
package/src/types.ts
CHANGED
|
@@ -844,14 +844,18 @@ export interface RemoveEntityRelationsOptions {
|
|
|
844
844
|
}
|
|
845
845
|
|
|
846
846
|
export type EntityWatcherType =
|
|
847
|
+
| EntityWatcher<"entity.beforeCreate">
|
|
847
848
|
| EntityWatcher<"entity.create">
|
|
849
|
+
| EntityWatcher<"entity.beforeUpdate">
|
|
848
850
|
| EntityWatcher<"entity.update">
|
|
851
|
+
| EntityWatcher<"entity.beforeDelete">
|
|
849
852
|
| EntityWatcher<"entity.delete">
|
|
850
853
|
| EntityWatcher<"entity.addRelations">
|
|
851
854
|
| EntityWatcher<"entity.removeRelations">
|
|
855
|
+
| EntityWatcher<"entity.beforeResponse">
|
|
852
856
|
| EntityWatcher<any>;
|
|
853
857
|
|
|
854
|
-
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes
|
|
858
|
+
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes> {
|
|
855
859
|
eventName: TEventName;
|
|
856
860
|
modelSingularCode: string;
|
|
857
861
|
handler: EntityWatchHandler<TEventName>;
|