@ruiapp/rapid-core 0.1.14 → 0.1.16
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/dataAccess/entityManager.d.ts +1 -1
- package/dist/dataAccess/entityMapper.d.ts +1 -1
- package/dist/index.js +26 -18
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/dataAccess/entityManager.ts +27 -18
- package/src/dataAccess/entityMapper.ts +3 -1
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +1 -1
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +0 -2
- package/src/types.ts +1 -0
|
@@ -6,7 +6,7 @@ export default class EntityManager<TEntity = any> {
|
|
|
6
6
|
getModel(): RpdDataModel;
|
|
7
7
|
findEntities(options: FindEntityOptions): Promise<TEntity[]>;
|
|
8
8
|
findEntity(options: FindEntityOptions): Promise<TEntity | null>;
|
|
9
|
-
findById(id: any): Promise<TEntity | null>;
|
|
9
|
+
findById(id: any, keepNonPropertyFields?: boolean): Promise<TEntity | null>;
|
|
10
10
|
createEntity(options: CreateEntityOptions, plugin: RapidPlugin): Promise<TEntity>;
|
|
11
11
|
updateEntityById(options: UpdateEntityByIdOptions, plugin: RapidPlugin): Promise<TEntity>;
|
|
12
12
|
count(options: CountEntityOptions): Promise<CountEntityResult>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { RpdDataModel } from "../types";
|
|
2
|
-
export declare function mapDbRowToEntity(model: RpdDataModel, row: any): any;
|
|
2
|
+
export declare function mapDbRowToEntity(model: RpdDataModel, row: any, keepNonPropertyFields: boolean): any;
|
|
3
3
|
export declare function mapEntityToDbRow(model: RpdDataModel, entity: any): any;
|
package/dist/index.js
CHANGED
|
@@ -1616,7 +1616,7 @@ function isRelationProperty(property) {
|
|
|
1616
1616
|
}
|
|
1617
1617
|
|
|
1618
1618
|
// TODO Generate mapper and cache it.
|
|
1619
|
-
function mapDbRowToEntity(model, row) {
|
|
1619
|
+
function mapDbRowToEntity(model, row, keepNonPropertyFields) {
|
|
1620
1620
|
if (!row) {
|
|
1621
1621
|
return null;
|
|
1622
1622
|
}
|
|
@@ -1638,6 +1638,9 @@ function mapDbRowToEntity(model, row) {
|
|
|
1638
1638
|
isRelationProp = true;
|
|
1639
1639
|
propertyName = property.code;
|
|
1640
1640
|
}
|
|
1641
|
+
else if (keepNonPropertyFields) {
|
|
1642
|
+
propertyName = columnName;
|
|
1643
|
+
}
|
|
1641
1644
|
}
|
|
1642
1645
|
if (isRelationProp) {
|
|
1643
1646
|
if (row[propertyName] && !result[propertyName]) {
|
|
@@ -1775,7 +1778,7 @@ async function findEntities(server, dataAccessor, options) {
|
|
|
1775
1778
|
___namespace.forEach(entities, (entity) => {
|
|
1776
1779
|
entity[relationProperty.code] = ___namespace.filter(relationLinks, (link) => {
|
|
1777
1780
|
return link[relationProperty.selfIdColumnName] == entity["id"];
|
|
1778
|
-
}).map(link => mapDbRowToEntity(targetModel, link.targetEntity));
|
|
1781
|
+
}).map(link => mapDbRowToEntity(targetModel, link.targetEntity, false));
|
|
1779
1782
|
});
|
|
1780
1783
|
}
|
|
1781
1784
|
}
|
|
@@ -1795,24 +1798,36 @@ async function findEntities(server, dataAccessor, options) {
|
|
|
1795
1798
|
if (isManyRelation) {
|
|
1796
1799
|
entity[relationProperty.code] = ___namespace.filter(relatedEntities, (relatedEntity) => {
|
|
1797
1800
|
return relatedEntity[relationProperty.selfIdColumnName] == entity.id;
|
|
1798
|
-
}).map(item => mapDbRowToEntity(targetModel, item));
|
|
1801
|
+
}).map(item => mapDbRowToEntity(targetModel, item, false));
|
|
1799
1802
|
}
|
|
1800
1803
|
else {
|
|
1801
1804
|
entity[relationProperty.code] = mapDbRowToEntity(targetModel, ___namespace.find(relatedEntities, (relatedEntity) => {
|
|
1802
1805
|
// TODO: id property code should be configurable.
|
|
1803
1806
|
return relatedEntity["id"] == entity[relationProperty.targetIdColumnName];
|
|
1804
|
-
}));
|
|
1807
|
+
}), false);
|
|
1805
1808
|
}
|
|
1806
1809
|
});
|
|
1807
1810
|
}
|
|
1808
1811
|
}
|
|
1809
1812
|
}
|
|
1810
|
-
return entities.map(item => mapDbRowToEntity(model, item));
|
|
1813
|
+
return entities.map(item => mapDbRowToEntity(model, item, options.keepNonPropertyFields));
|
|
1811
1814
|
}
|
|
1812
1815
|
async function findEntity(server, dataAccessor, options) {
|
|
1813
1816
|
const entities = await findEntities(server, dataAccessor, options);
|
|
1814
1817
|
return ___namespace.first(entities);
|
|
1815
1818
|
}
|
|
1819
|
+
async function findById(server, dataAccessor, id, keepNonPropertyFields = false) {
|
|
1820
|
+
return await findEntity(server, dataAccessor, {
|
|
1821
|
+
filters: [
|
|
1822
|
+
{
|
|
1823
|
+
operator: "eq",
|
|
1824
|
+
field: "id",
|
|
1825
|
+
value: id,
|
|
1826
|
+
}
|
|
1827
|
+
],
|
|
1828
|
+
keepNonPropertyFields,
|
|
1829
|
+
});
|
|
1830
|
+
}
|
|
1816
1831
|
async function replaceFiltersWithFiltersOperator(server, model, filters) {
|
|
1817
1832
|
if (!filters || !filters.length) {
|
|
1818
1833
|
return [];
|
|
@@ -2031,7 +2046,7 @@ async function createEntity(server, dataAccessor, options) {
|
|
|
2031
2046
|
}
|
|
2032
2047
|
}
|
|
2033
2048
|
const newRow = await dataAccessor.create(row);
|
|
2034
|
-
const newEntity = mapDbRowToEntity(model, newRow);
|
|
2049
|
+
const newEntity = mapDbRowToEntity(model, newRow, false);
|
|
2035
2050
|
// save many-relation properties
|
|
2036
2051
|
for (const property of manyRelationPropertiesToCreate) {
|
|
2037
2052
|
newEntity[property.code] = [];
|
|
@@ -2108,7 +2123,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
|
|
|
2108
2123
|
if (!id) {
|
|
2109
2124
|
throw new Error("Id is required when updating an entity.");
|
|
2110
2125
|
}
|
|
2111
|
-
const entity = await
|
|
2126
|
+
const entity = await findById(server, dataAccessor, id);
|
|
2112
2127
|
if (!entity) {
|
|
2113
2128
|
throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
|
|
2114
2129
|
}
|
|
@@ -2246,16 +2261,8 @@ class EntityManager {
|
|
|
2246
2261
|
async findEntity(options) {
|
|
2247
2262
|
return await findEntity(this.#server, this.#dataAccessor, options);
|
|
2248
2263
|
}
|
|
2249
|
-
async findById(id) {
|
|
2250
|
-
return await this
|
|
2251
|
-
filters: [
|
|
2252
|
-
{
|
|
2253
|
-
operator: "eq",
|
|
2254
|
-
field: "id",
|
|
2255
|
-
value: id,
|
|
2256
|
-
}
|
|
2257
|
-
]
|
|
2258
|
-
});
|
|
2264
|
+
async findById(id, keepNonPropertyFields = false) {
|
|
2265
|
+
return await findById(this.#server, this.#dataAccessor, id);
|
|
2259
2266
|
}
|
|
2260
2267
|
async createEntity(options, plugin) {
|
|
2261
2268
|
const model = this.getModel();
|
|
@@ -2275,7 +2282,7 @@ class EntityManager {
|
|
|
2275
2282
|
}
|
|
2276
2283
|
async deleteById(id, plugin) {
|
|
2277
2284
|
const model = this.getModel();
|
|
2278
|
-
const entity = await this.findById(id);
|
|
2285
|
+
const entity = await this.findById(id, true);
|
|
2279
2286
|
if (!entity) {
|
|
2280
2287
|
return;
|
|
2281
2288
|
}
|
|
@@ -3228,6 +3235,7 @@ async function handler$b(plugin, ctx, options) {
|
|
|
3228
3235
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3229
3236
|
await entityManager.deleteById(input.id, plugin);
|
|
3230
3237
|
ctx.status = 200;
|
|
3238
|
+
ctx.output = {};
|
|
3231
3239
|
}
|
|
3232
3240
|
|
|
3233
3241
|
var deleteCollectionEntityById = /*#__PURE__*/Object.freeze({
|
package/dist/types.d.ts
CHANGED
|
@@ -272,6 +272,7 @@ export interface FindEntityOptions {
|
|
|
272
272
|
orderBy?: FindEntityOrderByOptions[];
|
|
273
273
|
pagination?: FindEntityPaginationOptions;
|
|
274
274
|
properties?: string[] | Record<string, any>;
|
|
275
|
+
keepNonPropertyFields?: boolean;
|
|
275
276
|
}
|
|
276
277
|
export interface FindEntityRelationalFilterOptions {
|
|
277
278
|
field: string;
|
package/package.json
CHANGED
|
@@ -107,7 +107,7 @@ async function findEntities(
|
|
|
107
107
|
_.forEach(entities, (entity: any) => {
|
|
108
108
|
entity[relationProperty.code] = _.filter(relationLinks, (link: any) => {
|
|
109
109
|
return link[relationProperty.selfIdColumnName!] == entity["id"];
|
|
110
|
-
}).map(link => mapDbRowToEntity(targetModel, link.targetEntity));
|
|
110
|
+
}).map(link => mapDbRowToEntity(targetModel, link.targetEntity, false));
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
113
|
} else {
|
|
@@ -147,7 +147,7 @@ async function findEntities(
|
|
|
147
147
|
(relatedEntity: any) => {
|
|
148
148
|
return relatedEntity[relationProperty.selfIdColumnName!] == entity.id;
|
|
149
149
|
},
|
|
150
|
-
).map(item => mapDbRowToEntity(targetModel!, item));
|
|
150
|
+
).map(item => mapDbRowToEntity(targetModel!, item, false));
|
|
151
151
|
} else {
|
|
152
152
|
entity[relationProperty.code] = mapDbRowToEntity(targetModel!, _.find(
|
|
153
153
|
relatedEntities,
|
|
@@ -155,13 +155,13 @@ async function findEntities(
|
|
|
155
155
|
// TODO: id property code should be configurable.
|
|
156
156
|
return relatedEntity["id"] == entity[relationProperty.targetIdColumnName!];
|
|
157
157
|
},
|
|
158
|
-
));
|
|
158
|
+
), false);
|
|
159
159
|
}
|
|
160
160
|
});
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
return entities.map(item => mapDbRowToEntity(model, item));
|
|
164
|
+
return entities.map(item => mapDbRowToEntity(model, item, options.keepNonPropertyFields));
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
async function findEntity(
|
|
@@ -173,6 +173,24 @@ async function findEntity(
|
|
|
173
173
|
return _.first(entities);
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
async function findById(
|
|
177
|
+
server: IRpdServer,
|
|
178
|
+
dataAccessor: IRpdDataAccessor,
|
|
179
|
+
id: any,
|
|
180
|
+
keepNonPropertyFields: boolean = false
|
|
181
|
+
): Promise<any> {
|
|
182
|
+
return await findEntity(server, dataAccessor, {
|
|
183
|
+
filters: [
|
|
184
|
+
{
|
|
185
|
+
operator: "eq",
|
|
186
|
+
field: "id",
|
|
187
|
+
value: id,
|
|
188
|
+
}
|
|
189
|
+
],
|
|
190
|
+
keepNonPropertyFields,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
176
194
|
async function replaceFiltersWithFiltersOperator(
|
|
177
195
|
server: IRpdServer,
|
|
178
196
|
model: RpdDataModel,
|
|
@@ -452,8 +470,7 @@ async function createEntity(
|
|
|
452
470
|
}
|
|
453
471
|
|
|
454
472
|
const newRow = await dataAccessor.create(row);
|
|
455
|
-
const newEntity = mapDbRowToEntity(model, newRow);
|
|
456
|
-
|
|
473
|
+
const newEntity = mapDbRowToEntity(model, newRow, false);
|
|
457
474
|
|
|
458
475
|
// save many-relation properties
|
|
459
476
|
for (const property of manyRelationPropertiesToCreate) {
|
|
@@ -544,7 +561,7 @@ async function updateEntityById(
|
|
|
544
561
|
throw new Error("Id is required when updating an entity.")
|
|
545
562
|
}
|
|
546
563
|
|
|
547
|
-
const entity = await
|
|
564
|
+
const entity = await findById(server, dataAccessor, id);
|
|
548
565
|
if (!entity) {
|
|
549
566
|
throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
|
|
550
567
|
}
|
|
@@ -701,16 +718,8 @@ export default class EntityManager<TEntity=any> {
|
|
|
701
718
|
return await findEntity(this.#server, this.#dataAccessor, options);
|
|
702
719
|
}
|
|
703
720
|
|
|
704
|
-
async findById(id: any): Promise<TEntity | null> {
|
|
705
|
-
return await this
|
|
706
|
-
filters: [
|
|
707
|
-
{
|
|
708
|
-
operator: "eq",
|
|
709
|
-
field: "id",
|
|
710
|
-
value: id,
|
|
711
|
-
}
|
|
712
|
-
]
|
|
713
|
-
});
|
|
721
|
+
async findById(id: any, keepNonPropertyFields: boolean = false): Promise<TEntity | null> {
|
|
722
|
+
return await findById(this.#server, this.#dataAccessor, id);
|
|
714
723
|
}
|
|
715
724
|
|
|
716
725
|
async createEntity(options: CreateEntityOptions, plugin: RapidPlugin): Promise<TEntity> {
|
|
@@ -740,7 +749,7 @@ export default class EntityManager<TEntity=any> {
|
|
|
740
749
|
|
|
741
750
|
async deleteById(id: any, plugin: RapidPlugin): Promise<void> {
|
|
742
751
|
const model = this.getModel();
|
|
743
|
-
const entity = await this.findById(id);
|
|
752
|
+
const entity = await this.findById(id, true);
|
|
744
753
|
if (!entity) {
|
|
745
754
|
return;
|
|
746
755
|
}
|
|
@@ -3,7 +3,7 @@ import { isRelationProperty } from "~/utilities/rapidUtility";
|
|
|
3
3
|
|
|
4
4
|
// TODO Generate mapper and cache it.
|
|
5
5
|
|
|
6
|
-
export function mapDbRowToEntity(model: RpdDataModel, row: any) {
|
|
6
|
+
export function mapDbRowToEntity(model: RpdDataModel, row: any, keepNonPropertyFields: boolean) {
|
|
7
7
|
if (!row) {
|
|
8
8
|
return null;
|
|
9
9
|
}
|
|
@@ -25,6 +25,8 @@ export function mapDbRowToEntity(model: RpdDataModel, row: any) {
|
|
|
25
25
|
if (property) {
|
|
26
26
|
isRelationProp = true;
|
|
27
27
|
propertyName = property.code;
|
|
28
|
+
} else if (keepNonPropertyFields) {
|
|
29
|
+
propertyName = columnName;
|
|
28
30
|
}
|
|
29
31
|
}
|
|
30
32
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { RunEntityActionHandlerOptions } from "~/types";
|
|
2
|
-
import { mapDbRowToEntity } from "~/dataAccess/entityMapper";
|
|
3
2
|
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
4
3
|
import { RapidPlugin } from "~/core/server";
|
|
5
4
|
|
|
@@ -17,4 +16,5 @@ export async function handler(
|
|
|
17
16
|
await entityManager.deleteById(input.id, plugin);
|
|
18
17
|
|
|
19
18
|
ctx.status = 200;
|
|
19
|
+
ctx.output = {};
|
|
20
20
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { RunEntityActionHandlerOptions } from "~/types";
|
|
2
|
-
import { getEntityPartChanges } from "~/helpers/entityHelpers";
|
|
3
2
|
import { mergeInput } from "~/helpers/inputHelper";
|
|
4
|
-
import { mapDbRowToEntity } from "~/dataAccess/entityMapper";
|
|
5
3
|
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
6
4
|
import { RapidPlugin } from "~/core/server";
|
|
7
5
|
|
package/src/types.ts
CHANGED
|
@@ -370,6 +370,7 @@ export interface FindEntityOptions {
|
|
|
370
370
|
orderBy?: FindEntityOrderByOptions[];
|
|
371
371
|
pagination?: FindEntityPaginationOptions;
|
|
372
372
|
properties?: string[] | Record<string, any>;
|
|
373
|
+
keepNonPropertyFields?: boolean;
|
|
373
374
|
}
|
|
374
375
|
|
|
375
376
|
export interface FindEntityRelationalFilterOptions {
|