@ruiapp/rapid-core 0.11.1 → 0.11.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/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
- propertyNames.forEach((propertyName) => {
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
 
@@ -4411,13 +4411,13 @@ class EntityManager {
4411
4411
  async addRelations(options, plugin) {
4412
4412
  const server = this.#server;
4413
4413
  const model = this.getModel();
4414
- const { id, property, relations, routeContext } = options;
4414
+ const { id: selfId, property, relations, routeContext } = options;
4415
4415
  const entity = await this.findById({
4416
- id,
4416
+ id: selfId,
4417
4417
  routeContext,
4418
4418
  });
4419
4419
  if (!entity) {
4420
- throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
4420
+ throw new Error(`${model.namespace}.${model.singularCode} with id "${selfId}" was not found.`);
4421
4421
  }
4422
4422
  const relationProperty = getEntityPropertyByCode(server, model, property);
4423
4423
  if (!relationProperty) {
@@ -4438,10 +4438,21 @@ class EntityManager {
4438
4438
  FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4439
4439
  WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
4440
4440
  )`;
4441
- const params = [id, relation.id];
4441
+ const targetId = relation[relationProperty.targetIdColumnName] || relation.id;
4442
+ const params = [selfId, targetId];
4442
4443
  await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
4443
4444
  }
4444
4445
  }
4446
+ else {
4447
+ const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4448
+ for (const relation of relations) {
4449
+ relation[relationProperty.selfIdColumnName] = selfId;
4450
+ await targetEntityManager.createEntity({
4451
+ routeContext,
4452
+ entity: relation,
4453
+ });
4454
+ }
4455
+ }
4445
4456
  await server.emitEvent({
4446
4457
  eventName: "entity.addRelations",
4447
4458
  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 = any> {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.11.1",
3
+ "version": "0.11.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1871,13 +1871,13 @@ export default class EntityManager<TEntity = any> {
1871
1871
  async addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<void> {
1872
1872
  const server = this.#server;
1873
1873
  const model = this.getModel();
1874
- const { id, property, relations, routeContext } = options;
1874
+ const { id: selfId, property, relations, routeContext } = options;
1875
1875
  const entity = await this.findById({
1876
- id,
1876
+ id: selfId,
1877
1877
  routeContext,
1878
1878
  });
1879
1879
  if (!entity) {
1880
- throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
1880
+ throw new Error(`${model.namespace}.${model.singularCode} with id "${selfId}" was not found.`);
1881
1881
  }
1882
1882
 
1883
1883
  const relationProperty = getEntityPropertyByCode(server, model, property);
@@ -1901,9 +1901,20 @@ export default class EntityManager<TEntity = any> {
1901
1901
  FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
1902
1902
  WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName!)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName!)}=$2
1903
1903
  )`;
1904
- const params = [id, relation.id];
1904
+
1905
+ const targetId = relation[relationProperty.targetIdColumnName!] || relation.id;
1906
+ const params = [selfId, targetId];
1905
1907
  await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
1906
1908
  }
1909
+ } else {
1910
+ const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
1911
+ for (const relation of relations) {
1912
+ relation[relationProperty.selfIdColumnName!] = selfId;
1913
+ await targetEntityManager.createEntity({
1914
+ routeContext,
1915
+ entity: relation,
1916
+ });
1917
+ }
1907
1918
  }
1908
1919
 
1909
1920
  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
- propertyNames.forEach((propertyName) => {
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 = any> {
858
+ export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes> {
855
859
  eventName: TEventName;
856
860
  modelSingularCode: string;
857
861
  handler: EntityWatchHandler<TEventName>;