pure-orm 4.0.0 → 4.0.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 +97 -43
- package/coverage/clover.xml +969 -626
- package/coverage/coverage-final.json +34 -22
- package/coverage/lcov-report/dist/src/core.js.html +220 -190
- package/coverage/lcov-report/dist/src/driver-integrations/index.html +97 -0
- package/coverage/lcov-report/dist/src/driver-integrations/pgp.js.html +240 -0
- package/coverage/lcov-report/dist/src/index.html +44 -18
- package/coverage/lcov-report/dist/src/index.js.html +2 -2
- package/coverage/lcov-report/dist/src/orm.js.html +125 -119
- package/coverage/lcov-report/dist/test-utils/blog/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/blog/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/blog/models/article.js.html +15 -15
- package/coverage/lcov-report/dist/test-utils/blog/models/article_tag.js.html +7 -7
- package/coverage/lcov-report/dist/test-utils/blog/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/blog/models/person.js.html +13 -13
- package/coverage/lcov-report/dist/test-utils/blog/models/tag.js.html +4 -4
- package/coverage/lcov-report/dist/test-utils/five/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/models/line-item.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/models/order.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/models/parcel-event.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/models/parcel-line-item.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/five/models/parcel.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/fourteen/entities.js.html +87 -0
- package/coverage/lcov-report/dist/test-utils/fourteen/index.html +97 -0
- package/coverage/lcov-report/dist/test-utils/fourteen/models/customer.js.html +159 -0
- package/coverage/lcov-report/dist/test-utils/fourteen/models/index.html +110 -0
- package/coverage/lcov-report/dist/test-utils/fourteen/models/person.js.html +138 -0
- package/coverage/lcov-report/dist/test-utils/nine/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/nine/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/nine/models/feature-switch.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/nine/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order/entities.js.html +11 -11
- package/coverage/lcov-report/dist/test-utils/order/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order/models/line-item.js.html +24 -24
- package/coverage/lcov-report/dist/test-utils/order/models/order.js.html +42 -42
- package/coverage/lcov-report/dist/test-utils/order/models/product-variant.js.html +23 -23
- package/coverage/lcov-report/dist/test-utils/order/models/product.js.html +17 -17
- package/coverage/lcov-report/dist/test-utils/order/models/utm-source.js.html +12 -12
- package/coverage/lcov-report/dist/test-utils/order-more/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/actual-product-variant.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/color.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/customer.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/gender.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/inventory-level.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/line-item.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/order.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/parcel-event.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/parcel-line-item.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/parcel.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/physical-address.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/product-variant-image.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/product-variant.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/product.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/refund.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/shipment-actual-product-variant.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/shipment.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/size.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/utm-medium.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order-more/models/utm-source.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/models/customer.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/models/line-item.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/models/order.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/models/parcel-line-item.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/six/models/parcel.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/audience.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/brand.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/category.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/member.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/passion.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/product.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/recommendation-audience.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/thirteen/models/recommendation.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/three/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/three/results.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/twelve/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/twelve/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/twelve/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/twelve/models/member.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/twelve/models/prompt.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/two/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/two/results.js.html +1 -1
- package/coverage/lcov-report/index.html +105 -27
- package/coverage/lcov-report/src/core.ts.html +235 -196
- package/coverage/lcov-report/src/driver-integrations/index.html +97 -0
- package/coverage/lcov-report/src/driver-integrations/pgp.ts.html +345 -0
- package/coverage/lcov-report/src/index.html +44 -18
- package/coverage/lcov-report/src/index.ts.html +3 -15
- package/coverage/lcov-report/src/orm.ts.html +256 -130
- package/coverage/lcov-report/test-utils/blog/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/blog/index.html +1 -1
- package/coverage/lcov-report/test-utils/blog/models/article.ts.html +15 -15
- package/coverage/lcov-report/test-utils/blog/models/article_tag.ts.html +7 -7
- package/coverage/lcov-report/test-utils/blog/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/blog/models/person.ts.html +13 -13
- package/coverage/lcov-report/test-utils/blog/models/tag.ts.html +4 -4
- package/coverage/lcov-report/test-utils/five/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/five/index.html +1 -1
- package/coverage/lcov-report/test-utils/five/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/five/models/line-item.ts.html +1 -1
- package/coverage/lcov-report/test-utils/five/models/order.ts.html +1 -1
- package/coverage/lcov-report/test-utils/five/models/parcel-event.ts.html +1 -1
- package/coverage/lcov-report/test-utils/five/models/parcel-line-item.ts.html +1 -1
- package/coverage/lcov-report/test-utils/five/models/parcel.ts.html +1 -1
- package/coverage/lcov-report/test-utils/fourteen/entities.ts.html +78 -0
- package/coverage/lcov-report/test-utils/fourteen/index.html +97 -0
- package/coverage/lcov-report/test-utils/fourteen/models/customer.ts.html +177 -0
- package/coverage/lcov-report/test-utils/fourteen/models/index.html +110 -0
- package/coverage/lcov-report/test-utils/fourteen/models/person.ts.html +150 -0
- package/coverage/lcov-report/test-utils/nine/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/nine/index.html +1 -1
- package/coverage/lcov-report/test-utils/nine/models/feature-switch.ts.html +1 -1
- package/coverage/lcov-report/test-utils/nine/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/order/entities.ts.html +5 -5
- package/coverage/lcov-report/test-utils/order/index.html +1 -1
- package/coverage/lcov-report/test-utils/order/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/order/models/line-item.ts.html +18 -18
- package/coverage/lcov-report/test-utils/order/models/order.ts.html +37 -37
- package/coverage/lcov-report/test-utils/order/models/product-variant.ts.html +18 -18
- package/coverage/lcov-report/test-utils/order/models/product.ts.html +13 -13
- package/coverage/lcov-report/test-utils/order/models/utm-source.ts.html +8 -8
- package/coverage/lcov-report/test-utils/order-more/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/index.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/actual-product-variant.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/color.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/customer.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/gender.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/inventory-level.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/line-item.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/order.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/parcel-event.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/parcel-line-item.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/parcel.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/physical-address.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/product-variant-image.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/product-variant.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/product.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/refund.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/shipment-actual-product-variant.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/shipment.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/size.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/utm-medium.ts.html +1 -1
- package/coverage/lcov-report/test-utils/order-more/models/utm-source.ts.html +1 -1
- package/coverage/lcov-report/test-utils/six/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/six/index.html +1 -1
- package/coverage/lcov-report/test-utils/six/models/customer.ts.html +1 -1
- package/coverage/lcov-report/test-utils/six/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/six/models/line-item.ts.html +1 -1
- package/coverage/lcov-report/test-utils/six/models/order.ts.html +1 -1
- package/coverage/lcov-report/test-utils/six/models/parcel-line-item.ts.html +1 -1
- package/coverage/lcov-report/test-utils/six/models/parcel.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/index.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/audience.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/brand.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/category.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/member.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/passion.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/product.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/recommendation-audience.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/recommendation.ts.html +1 -1
- package/coverage/lcov-report/test-utils/three/index.html +1 -1
- package/coverage/lcov-report/test-utils/three/results.js.html +1 -1
- package/coverage/lcov-report/test-utils/twelve/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/twelve/index.html +1 -1
- package/coverage/lcov-report/test-utils/twelve/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/twelve/models/member.ts.html +1 -1
- package/coverage/lcov-report/test-utils/twelve/models/prompt.ts.html +1 -1
- package/coverage/lcov-report/test-utils/two/index.html +1 -1
- package/coverage/lcov-report/test-utils/two/results.js.html +1 -1
- package/coverage/lcov.info +1686 -1005
- package/dist/src/core.d.ts +1 -1
- package/dist/src/core.js +18 -8
- package/dist/src/core.spec.js +643 -540
- package/dist/src/driver-integrations/index.d.ts +1 -0
- package/dist/src/orm.d.ts +21 -0
- package/dist/src/orm.js +6 -2
- package/dist/src/orm.spec.d.ts +1 -0
- package/dist/src/orm.spec.js +102 -0
- package/dist/test-utils/fourteen/entities.d.ts +6 -0
- package/dist/test-utils/fourteen/entities.js +6 -0
- package/dist/test-utils/fourteen/entities.ts +3 -0
- package/dist/test-utils/fourteen/models/customer.d.ts +19 -0
- package/dist/test-utils/fourteen/models/customer.js +30 -0
- package/dist/test-utils/fourteen/models/customer.ts +36 -0
- package/dist/test-utils/fourteen/models/person.d.ts +17 -0
- package/dist/test-utils/fourteen/models/person.js +23 -0
- package/dist/test-utils/fourteen/models/person.ts +27 -0
- package/dist/test-utils/fourteen/results.json +8 -0
- package/dist/test-utils/order/entities.js +2 -2
- package/dist/test-utils/order/entities.ts +2 -2
- package/dist/test-utils/order/models/order.js +1 -1
- package/dist/test-utils/order/models/order.ts +1 -1
- package/package.json +1 -1
- package/src/core.spec.ts +1112 -986
- package/src/core.ts +27 -14
- package/src/driver-integrations/index.ts +2 -0
- package/src/orm.spec.ts +113 -0
- package/src/orm.ts +54 -8
- package/test-utils/fourteen/entities.ts +3 -0
- package/test-utils/fourteen/models/customer.ts +36 -0
- package/test-utils/fourteen/models/person.ts +27 -0
- package/test-utils/fourteen/results.json +8 -0
- package/test-utils/order/entities.ts +2 -2
- package/test-utils/order/models/order.ts +1 -1
package/src/core.ts
CHANGED
|
@@ -74,7 +74,7 @@ export interface ICore {
|
|
|
74
74
|
createFromDatabase: <T extends ICollection<IModel>>(rows: any) => T;
|
|
75
75
|
createAnyFromDatabase: <T extends ICollection<IModel>>(
|
|
76
76
|
rows: any,
|
|
77
|
-
rootKey: string |
|
|
77
|
+
rootKey: string | IModelClass
|
|
78
78
|
) => T;
|
|
79
79
|
createOneFromDatabase: <T extends IModel>(rows: any) => T;
|
|
80
80
|
createOneOrNoneFromDatabase: <T extends IModel>(rows: any) => T | void;
|
|
@@ -209,14 +209,20 @@ export const createCore = ({
|
|
|
209
209
|
new Map()
|
|
210
210
|
);
|
|
211
211
|
|
|
212
|
-
const
|
|
213
|
-
|
|
212
|
+
const getEntityByModelClass = (
|
|
213
|
+
Model: IModelClass
|
|
214
|
+
): IEntityInternal<IModel> => {
|
|
215
|
+
const entity = modelToEntityMap.get(Model);
|
|
214
216
|
if (!entity) {
|
|
215
|
-
throw new Error(`Could not find entity for class ${
|
|
217
|
+
throw new Error(`Could not find entity for class ${Model}`);
|
|
216
218
|
}
|
|
217
219
|
return entity;
|
|
218
220
|
};
|
|
219
221
|
|
|
222
|
+
const getEntityByModel = (model: IModel): IEntityInternal<IModel> => {
|
|
223
|
+
return getEntityByModelClass(model.constructor as IModelClass);
|
|
224
|
+
};
|
|
225
|
+
|
|
220
226
|
/*
|
|
221
227
|
* In:
|
|
222
228
|
* [
|
|
@@ -286,16 +292,23 @@ export const createCore = ({
|
|
|
286
292
|
...nodes.slice(0, indexOfOldestParent + 1).reverse()
|
|
287
293
|
];
|
|
288
294
|
const nodeItPointsTo = parentHeirarchy.find((parent) => {
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
295
|
+
const indexes = Object.values(getEntityByModel(model).references)
|
|
296
|
+
.map((x: IModelClass, i: number) =>
|
|
297
|
+
x === parent.constructor ? i : null
|
|
298
|
+
)
|
|
299
|
+
.filter((x: number | null, i) => x != null) as Array<number>;
|
|
300
|
+
if (!indexes.length) {
|
|
293
301
|
return false;
|
|
294
302
|
}
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
303
|
+
for (const index of indexes) {
|
|
304
|
+
const property = Object.keys(getEntityByModel(model).references)[
|
|
305
|
+
index
|
|
306
|
+
];
|
|
307
|
+
if (model[property] === parent.id) {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return false;
|
|
299
312
|
});
|
|
300
313
|
if (isNodeAlreadySeen) {
|
|
301
314
|
if (nodeItPointsTo && !nodePointingToIt) {
|
|
@@ -447,13 +460,13 @@ export const createCore = ({
|
|
|
447
460
|
|
|
448
461
|
const createAnyFromDatabase = <T extends ICollection<IModel>>(
|
|
449
462
|
rows: any,
|
|
450
|
-
rootKey: string |
|
|
463
|
+
rootKey: string | IModelClass
|
|
451
464
|
): T => {
|
|
452
465
|
if (!rows || !rows.length) {
|
|
453
466
|
const Collection =
|
|
454
467
|
typeof rootKey === 'string'
|
|
455
468
|
? getEntityByTableName(rootKey).Collection
|
|
456
|
-
:
|
|
469
|
+
: getEntityByModelClass(rootKey).Collection;
|
|
457
470
|
return new Collection({ models: [] }) as T;
|
|
458
471
|
}
|
|
459
472
|
return <T>createFromDatabase<T>(rows);
|
|
@@ -7,6 +7,8 @@ export interface ICreateForDriverOptions {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface ICoreIntegratedDriver extends ICore {
|
|
10
|
+
db: any;
|
|
11
|
+
|
|
10
12
|
/* ------------------------------------------------------------------------*/
|
|
11
13
|
/* Query methods ----------------------------------------------------------*/
|
|
12
14
|
/* ------------------------------------------------------------------------*/
|
package/src/orm.spec.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
import { create } from './index';
|
|
3
|
+
import { entities as orderEntities } from '../test-utils/order/entities';
|
|
4
|
+
|
|
5
|
+
test('getSqlInsertParts', () => {
|
|
6
|
+
const orm = create({
|
|
7
|
+
entities: orderEntities,
|
|
8
|
+
db: { $config: { pgp: true } }
|
|
9
|
+
});
|
|
10
|
+
const order = new orderEntities[0].Model({
|
|
11
|
+
id: 1,
|
|
12
|
+
email: 'test@test.com',
|
|
13
|
+
subtotalPrice: 100,
|
|
14
|
+
utmSourceId: 10
|
|
15
|
+
});
|
|
16
|
+
expect(orm.getSqlInsertParts(order)).toEqual({
|
|
17
|
+
columns: '"id", "email", "subtotal_price", "utm_source_id"',
|
|
18
|
+
values: [1, 'test@test.com', 100, 10],
|
|
19
|
+
valuesVar: ['$1', '$2', '$3', '$4']
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('getSqlUpdateParts', () => {
|
|
24
|
+
const orm = create({
|
|
25
|
+
entities: orderEntities,
|
|
26
|
+
db: { $config: { pgp: true } }
|
|
27
|
+
});
|
|
28
|
+
const order = new orderEntities[0].Model({
|
|
29
|
+
id: 1,
|
|
30
|
+
email: 'test@test.com',
|
|
31
|
+
subtotalPrice: 100,
|
|
32
|
+
utmSourceId: 10
|
|
33
|
+
});
|
|
34
|
+
expect(orm.getSqlUpdateParts(order)).toEqual({
|
|
35
|
+
clause:
|
|
36
|
+
'"id" = $1, "email" = $2, "subtotal_price" = $3, "utm_source_id" = $4',
|
|
37
|
+
idVar: '$5',
|
|
38
|
+
values: [1, 'test@test.com', 100, 10, 1]
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('getMatchingParts', () => {
|
|
43
|
+
const orm = create({
|
|
44
|
+
entities: orderEntities,
|
|
45
|
+
db: { $config: { pgp: true } }
|
|
46
|
+
});
|
|
47
|
+
const order = new orderEntities[0].Model({
|
|
48
|
+
id: 1,
|
|
49
|
+
email: 'test@test.com',
|
|
50
|
+
subtotalPrice: 100,
|
|
51
|
+
utmSourceId: 10
|
|
52
|
+
});
|
|
53
|
+
expect(orm.getMatchingParts(order)).toEqual({
|
|
54
|
+
values: [1, 'test@test.com', 100, 10],
|
|
55
|
+
whereClause:
|
|
56
|
+
'"order"."id" = $1 AND "order"."email" = $2 AND "order"."subtotal_price" = $3 AND "order"."utm_source_id" = $4'
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('getMatchingPartsObject', () => {
|
|
61
|
+
const orm = create({
|
|
62
|
+
entities: orderEntities,
|
|
63
|
+
db: { $config: { pgp: true } }
|
|
64
|
+
});
|
|
65
|
+
const order = new orderEntities[0].Model({
|
|
66
|
+
id: 1,
|
|
67
|
+
email: 'test@test.com',
|
|
68
|
+
subtotalPrice: 100,
|
|
69
|
+
utmSourceId: 10
|
|
70
|
+
});
|
|
71
|
+
expect(orm.getMatchingPartsObject(order)).toEqual({
|
|
72
|
+
values: {
|
|
73
|
+
1: 1,
|
|
74
|
+
2: 'test@test.com',
|
|
75
|
+
3: 100,
|
|
76
|
+
4: 10
|
|
77
|
+
},
|
|
78
|
+
whereClause:
|
|
79
|
+
'"order"."id" = $(1) AND "order"."email" = $(2) AND "order"."subtotal_price" = $(3) AND "order"."utm_source_id" = $(4)'
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('getValueBySqlColumn', () => {
|
|
84
|
+
const orm = create({
|
|
85
|
+
entities: orderEntities,
|
|
86
|
+
db: { $config: { pgp: true } }
|
|
87
|
+
});
|
|
88
|
+
const order = new orderEntities[0].Model({
|
|
89
|
+
id: 1,
|
|
90
|
+
email: 'test@test.com',
|
|
91
|
+
subtotalPrice: 100,
|
|
92
|
+
utmSourceId: 10
|
|
93
|
+
});
|
|
94
|
+
expect(orm.getValueBySqlColumn(order, 'id')).toEqual(1);
|
|
95
|
+
expect(orm.getValueBySqlColumn(order, 'email')).toEqual('test@test.com');
|
|
96
|
+
expect(orm.getValueBySqlColumn(order, 'subtotal_price')).toEqual(100);
|
|
97
|
+
expect(orm.getValueBySqlColumn(order, 'utm_source_id')).toEqual(10);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('getSqlColumnForPropertyName', () => {
|
|
101
|
+
const orm = create({
|
|
102
|
+
entities: orderEntities,
|
|
103
|
+
db: { $config: { pgp: true } }
|
|
104
|
+
});
|
|
105
|
+
const order = new orderEntities[0].Model({ id: 1 });
|
|
106
|
+
expect(orm.getSqlColumnForPropertyName(order, 'id')).toEqual('id');
|
|
107
|
+
expect(orm.getSqlColumnForPropertyName(order, 'utmSourceId')).toEqual(
|
|
108
|
+
'utm_source_id'
|
|
109
|
+
);
|
|
110
|
+
expect(orm.getSqlColumnForPropertyName(order, 'browserIP')).toEqual(
|
|
111
|
+
'browser_ip'
|
|
112
|
+
);
|
|
113
|
+
});
|
package/src/orm.ts
CHANGED
|
@@ -28,6 +28,31 @@ export interface IPureORM extends ICoreIntegratedDriver {
|
|
|
28
28
|
update: <T extends IModel>(model: T, options: { on: string }) => T;
|
|
29
29
|
delete: <T extends IModel>(model: T) => void;
|
|
30
30
|
deleteMatching: <T extends IModel>(model: T) => void;
|
|
31
|
+
|
|
32
|
+
/* ------------------------------------------------------------------------*/
|
|
33
|
+
/* Helper Utility Functions -----------------------------------------------*/
|
|
34
|
+
/* ------------------------------------------------------------------------*/
|
|
35
|
+
|
|
36
|
+
getSqlInsertParts: (model: IModel) => {
|
|
37
|
+
columns: string;
|
|
38
|
+
values: Array<string>;
|
|
39
|
+
valuesVar: Array<string>;
|
|
40
|
+
};
|
|
41
|
+
getSqlUpdateParts: (
|
|
42
|
+
model: IModel,
|
|
43
|
+
on?: string
|
|
44
|
+
) => { clause: string; idVar: string; values: Array<string> };
|
|
45
|
+
getMatchingParts: (model: IModel) => {
|
|
46
|
+
whereClause: string;
|
|
47
|
+
values: Array<string>;
|
|
48
|
+
};
|
|
49
|
+
getMatchingPartsObject: (model: IModel) => {
|
|
50
|
+
whereClause: string;
|
|
51
|
+
values: Array<string>;
|
|
52
|
+
};
|
|
53
|
+
getNewWith: (model: IModel, sqlColumns: any, values: any) => IModel;
|
|
54
|
+
getValueBySqlColumn: (model: IModel, sqlColumn: string) => string;
|
|
55
|
+
getSqlColumnForPropertyName: (model: IModel, propertyName: string) => string;
|
|
31
56
|
}
|
|
32
57
|
|
|
33
58
|
export const create = ({
|
|
@@ -50,7 +75,9 @@ export const create = ({
|
|
|
50
75
|
/* Helper Utilities for CRUD functions ------------------------------------*/
|
|
51
76
|
/* ------------------------------------------------------------------------*/
|
|
52
77
|
|
|
53
|
-
const getSqlInsertParts = (
|
|
78
|
+
const getSqlInsertParts = (
|
|
79
|
+
model: IModel
|
|
80
|
+
): { columns: string; values: Array<string>; valuesVar: Array<string> } => {
|
|
54
81
|
const columns = orm
|
|
55
82
|
.getEntityByModel(model)
|
|
56
83
|
.columnNames.filter(
|
|
@@ -75,7 +102,10 @@ export const create = ({
|
|
|
75
102
|
return { columns, values, valuesVar };
|
|
76
103
|
};
|
|
77
104
|
|
|
78
|
-
const getSqlUpdateParts = (
|
|
105
|
+
const getSqlUpdateParts = (
|
|
106
|
+
model: IModel,
|
|
107
|
+
on = 'id'
|
|
108
|
+
): { clause: string; idVar: string; values: Array<string> } => {
|
|
79
109
|
const clauseArray = orm
|
|
80
110
|
.getEntityByModel(model)
|
|
81
111
|
.columnNames.filter(
|
|
@@ -101,7 +131,9 @@ export const create = ({
|
|
|
101
131
|
return { clause, idVar, values };
|
|
102
132
|
};
|
|
103
133
|
|
|
104
|
-
const getMatchingParts = (
|
|
134
|
+
const getMatchingParts = (
|
|
135
|
+
model: IModel
|
|
136
|
+
): { whereClause: string; values: Array<string> } => {
|
|
105
137
|
const whereClause = orm
|
|
106
138
|
.getEntityByModel(model)
|
|
107
139
|
.propertyNames.map((property: string, index: number) =>
|
|
@@ -127,7 +159,9 @@ export const create = ({
|
|
|
127
159
|
|
|
128
160
|
// This one returns an object, which allows it to be more versatile.
|
|
129
161
|
// To-do: make this one even better and use it instead of the one above.
|
|
130
|
-
const getMatchingPartsObject = (
|
|
162
|
+
const getMatchingPartsObject = (
|
|
163
|
+
model: IModel
|
|
164
|
+
): { whereClause: string; values: Array<string> } => {
|
|
131
165
|
const whereClause = orm
|
|
132
166
|
.getEntityByModel(model)
|
|
133
167
|
.propertyNames.map((property: string, index: number) =>
|
|
@@ -156,7 +190,7 @@ export const create = ({
|
|
|
156
190
|
return { whereClause, values };
|
|
157
191
|
};
|
|
158
192
|
|
|
159
|
-
const getNewWith = (model: IModel, sqlColumns: any, values: any) => {
|
|
193
|
+
const getNewWith = (model: IModel, sqlColumns: any, values: any): IModel => {
|
|
160
194
|
const Constructor = model.constructor as any;
|
|
161
195
|
const modelKeys = sqlColumns.map(
|
|
162
196
|
(key: string) =>
|
|
@@ -174,7 +208,7 @@ export const create = ({
|
|
|
174
208
|
return new Constructor(modelData);
|
|
175
209
|
};
|
|
176
210
|
|
|
177
|
-
const getValueBySqlColumn = (model: IModel, sqlColumn: string) => {
|
|
211
|
+
const getValueBySqlColumn = (model: IModel, sqlColumn: string): string => {
|
|
178
212
|
return model[
|
|
179
213
|
orm.getEntityByModel(model).propertyNames[
|
|
180
214
|
orm.getEntityByModel(model).columnNames.indexOf(sqlColumn)
|
|
@@ -182,6 +216,15 @@ export const create = ({
|
|
|
182
216
|
];
|
|
183
217
|
};
|
|
184
218
|
|
|
219
|
+
const getSqlColumnForPropertyName = (
|
|
220
|
+
model: IModel,
|
|
221
|
+
propertyName: string
|
|
222
|
+
): string => {
|
|
223
|
+
return orm.getEntityByModel(model).columnNames[
|
|
224
|
+
orm.getEntityByModel(model).propertyNames.indexOf(propertyName)
|
|
225
|
+
];
|
|
226
|
+
};
|
|
227
|
+
|
|
185
228
|
/* ------------------------------------------------------------------------*/
|
|
186
229
|
/* Built-in basic CRUD functions ------------------------------------------*/
|
|
187
230
|
/* ------------------------------------------------------------------------*/
|
|
@@ -203,7 +246,9 @@ export const create = ({
|
|
|
203
246
|
const query = `
|
|
204
247
|
UPDATE "${orm.getEntityByModel(model).tableName}"
|
|
205
248
|
SET ${clause}
|
|
206
|
-
WHERE "${
|
|
249
|
+
WHERE "${
|
|
250
|
+
orm.getEntityByModel(model).tableName
|
|
251
|
+
}".${getSqlColumnForPropertyName(model, on)} = ${idVar}
|
|
207
252
|
RETURNING ${orm.getEntityByModel(model).selectColumnsClause};
|
|
208
253
|
`;
|
|
209
254
|
return orm.one<T>(query, values);
|
|
@@ -286,6 +331,7 @@ export const create = ({
|
|
|
286
331
|
getMatchingParts,
|
|
287
332
|
getMatchingPartsObject,
|
|
288
333
|
getNewWith,
|
|
289
|
-
getValueBySqlColumn
|
|
334
|
+
getValueBySqlColumn,
|
|
335
|
+
getSqlColumnForPropertyName
|
|
290
336
|
});
|
|
291
337
|
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { IModel, ICollection, IColumns } from '../../../src/index';
|
|
2
|
+
import { Person } from './person';
|
|
3
|
+
|
|
4
|
+
export const tableName: string = 'customer';
|
|
5
|
+
|
|
6
|
+
export const columns: IColumns = [
|
|
7
|
+
'id',
|
|
8
|
+
{ column: 'locked_to_affiliate_id', references: Person },
|
|
9
|
+
{ column: 'locked_to_salesperson_id', references: Person }
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export class Customer implements IModel {
|
|
13
|
+
id: number;
|
|
14
|
+
lockedToAffiliateId: number;
|
|
15
|
+
lockedToSalespersonId: number;
|
|
16
|
+
|
|
17
|
+
constructor(props: any) {
|
|
18
|
+
this.id = props.id;
|
|
19
|
+
this.lockedToAffiliateId = props.lockedToAffiliateId;
|
|
20
|
+
this.lockedToSalespersonId = props.lockedToSalespersonId;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Customers implements ICollection<Customer> {
|
|
25
|
+
models: Array<Customer>;
|
|
26
|
+
constructor({ models }: any) {
|
|
27
|
+
this.models = models;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const customerEntity = {
|
|
32
|
+
tableName,
|
|
33
|
+
columns,
|
|
34
|
+
Model: Customer,
|
|
35
|
+
Collection: Customers
|
|
36
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IModel, ICollection, IColumns } from '../../../src/index';
|
|
2
|
+
|
|
3
|
+
export const tableName: string = 'person';
|
|
4
|
+
|
|
5
|
+
export const columns: IColumns = ['id'];
|
|
6
|
+
|
|
7
|
+
export class Person implements IModel {
|
|
8
|
+
id: number;
|
|
9
|
+
|
|
10
|
+
constructor(props: any) {
|
|
11
|
+
this.id = props.id;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class Persons implements ICollection<Person> {
|
|
16
|
+
models: Array<Person>;
|
|
17
|
+
constructor({ models }: any) {
|
|
18
|
+
this.models = models;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const personEntity = {
|
|
23
|
+
tableName,
|
|
24
|
+
columns,
|
|
25
|
+
Model: Person,
|
|
26
|
+
Collection: Persons
|
|
27
|
+
};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { utmSourceEntity } from './models/utm-source';
|
|
2
1
|
import { orderEntity } from './models/order';
|
|
2
|
+
import { utmSourceEntity } from './models/utm-source';
|
|
3
3
|
import { lineItemEntity } from './models/line-item';
|
|
4
4
|
import { productVariantEntity } from './models/product-variant';
|
|
5
5
|
import { productEntity } from './models/product';
|
|
6
6
|
|
|
7
7
|
export const entities = [
|
|
8
|
-
utmSourceEntity,
|
|
9
8
|
orderEntity,
|
|
9
|
+
utmSourceEntity,
|
|
10
10
|
lineItemEntity,
|
|
11
11
|
productVariantEntity,
|
|
12
12
|
productEntity
|