pure-orm 2.2.0 → 4.0.0-0
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/.eslintrc.json +2 -2
- package/README.md +169 -242
- package/examples/basic/bo/person.js +4 -3
- package/examples/basic/dao/person.js +1 -2
- package/examples/basic/db.js +6 -15
- package/examples/basic/orm.js +8 -0
- package/examples/blog/bo/article.js +5 -2
- package/examples/blog/bo/article_tag.js +5 -2
- package/examples/blog/bo/article_tags.js +4 -3
- package/examples/blog/bo/articles.js +4 -3
- package/examples/blog/bo/person.js +4 -2
- package/examples/blog/bo/tag.js +4 -2
- package/examples/blog/business-objects.js +1 -10
- package/examples/blog/orm.js +14 -0
- package/examples/order/bo/line-item.js +5 -2
- package/examples/order/bo/line-items.js +4 -3
- package/examples/order/bo/order.js +5 -2
- package/examples/order/bo/orders.js +4 -3
- package/examples/order/bo/product-variant.js +5 -2
- package/examples/order/bo/product-variants.js +4 -3
- package/examples/order/bo/product.js +5 -2
- package/examples/order/bo/products.js +4 -3
- package/examples/order/bo/utm-source.js +4 -2
- package/examples/order/orm.js +20 -0
- package/examples/order-more/bo/actual-product-variant.js +5 -2
- package/examples/order-more/bo/actual-product-variants.js +4 -3
- package/examples/order-more/bo/color.js +5 -2
- package/examples/order-more/bo/colors.js +4 -3
- package/examples/order-more/bo/customer.js +5 -2
- package/examples/order-more/bo/customers.js +4 -3
- package/examples/order-more/bo/gender.js +5 -2
- package/examples/order-more/bo/genders.js +4 -3
- package/examples/order-more/bo/inventory-level.js +5 -2
- package/examples/order-more/bo/inventory-levels.js +4 -3
- package/examples/order-more/bo/line-item.js +5 -2
- package/examples/order-more/bo/line-items.js +4 -3
- package/examples/order-more/bo/order.js +5 -2
- package/examples/order-more/bo/orders.js +4 -3
- package/examples/order-more/bo/parcel-event.js +5 -2
- package/examples/order-more/bo/parcel-events.js +4 -3
- package/examples/order-more/bo/parcel-line-item.js +5 -2
- package/examples/order-more/bo/parcel-line-items.js +4 -3
- package/examples/order-more/bo/parcel.js +5 -2
- package/examples/order-more/bo/parcels.js +4 -3
- package/examples/order-more/bo/physical-address.js +5 -2
- package/examples/order-more/bo/physical-addresses.js +4 -3
- package/examples/order-more/bo/product-variant-image.js +5 -2
- package/examples/order-more/bo/product-variant-images.js +4 -3
- package/examples/order-more/bo/product-variant.js +5 -2
- package/examples/order-more/bo/product-variants.js +4 -3
- package/examples/order-more/bo/product.js +5 -2
- package/examples/order-more/bo/products.js +4 -3
- package/examples/order-more/bo/refund.js +5 -2
- package/examples/order-more/bo/refunds.js +4 -3
- package/examples/order-more/bo/shipment-actual-product-variant.js +5 -2
- package/examples/order-more/bo/shipment-actual-product-variants.js +4 -3
- package/examples/order-more/bo/shipment.js +5 -2
- package/examples/order-more/bo/shipments.js +4 -3
- package/examples/order-more/bo/size.js +5 -2
- package/examples/order-more/bo/sizes.js +4 -3
- package/examples/order-more/bo/utm-medium.js +4 -2
- package/examples/order-more/bo/utm-source.js +4 -2
- package/examples/order-more/orm.js +49 -0
- package/package.json +4 -2
- package/src/bo.js +393 -0
- package/src/{bo/base-bo.spec.js → bo.spec.js} +73 -37
- package/src/factory.js +167 -0
- package/src/factory.spec.js +11 -0
- package/src/index.js +49 -6
- package/test-utils/five/bo/line-item.js +5 -2
- package/test-utils/five/bo/line-items.js +4 -3
- package/test-utils/five/bo/order.js +5 -2
- package/test-utils/five/bo/orders.js +4 -3
- package/test-utils/five/bo/parcel-event.js +5 -2
- package/test-utils/five/bo/parcel-events.js +4 -3
- package/test-utils/five/bo/parcel-line-item.js +5 -2
- package/test-utils/five/bo/parcel-line-items.js +4 -3
- package/test-utils/five/bo/parcel.js +5 -2
- package/test-utils/five/bo/parcels.js +4 -3
- package/test-utils/five/orm.js +19 -0
- package/test-utils/nine/bo/feature-switch.js +5 -2
- package/test-utils/nine/bo/feature-switches.js +4 -3
- package/test-utils/nine/orm.js +9 -0
- package/test-utils/six/bo/customer.js +5 -2
- package/test-utils/six/bo/customers.js +4 -3
- package/test-utils/six/bo/line-item.js +5 -2
- package/test-utils/six/bo/line-items.js +4 -3
- package/test-utils/six/bo/order.js +5 -2
- package/test-utils/six/bo/orders.js +4 -3
- package/test-utils/six/bo/parcel-line-item.js +5 -2
- package/test-utils/six/bo/parcel-line-items.js +4 -3
- package/test-utils/six/bo/parcel.js +5 -2
- package/test-utils/six/bo/parcels.js +4 -3
- package/test-utils/six/orm.js +19 -0
- package/test-utils/thirteen/bo/audience.js +21 -0
- package/test-utils/thirteen/bo/audiences.js +10 -0
- package/{examples/basic → test-utils/thirteen}/bo/base.js +0 -0
- package/test-utils/thirteen/bo/brand.js +21 -0
- package/test-utils/thirteen/bo/brands.js +10 -0
- package/test-utils/thirteen/bo/categories.js +13 -0
- package/test-utils/thirteen/bo/category.js +21 -0
- package/test-utils/thirteen/bo/member.js +21 -0
- package/test-utils/thirteen/bo/members.js +10 -0
- package/test-utils/thirteen/bo/passion.js +21 -0
- package/test-utils/thirteen/bo/passions.js +10 -0
- package/test-utils/thirteen/bo/product.js +22 -0
- package/test-utils/thirteen/bo/products.js +10 -0
- package/test-utils/thirteen/bo/recommendation-audience.js +27 -0
- package/test-utils/thirteen/bo/recommendation-audiences.js +10 -0
- package/test-utils/thirteen/bo/recommendation.js +33 -0
- package/test-utils/thirteen/bo/recommendations.js +10 -0
- package/test-utils/thirteen/orm.js +25 -0
- package/test-utils/thirteen/results.json +74 -0
- package/test-utils/twelve/bo/member.js +5 -2
- package/test-utils/twelve/bo/members.js +4 -3
- package/test-utils/twelve/bo/prompt.js +5 -2
- package/test-utils/twelve/bo/prompts.js +4 -3
- package/test-utils/twelve/orm.js +10 -0
- package/examples/basic/business-objects.js +0 -9
- package/examples/basic/dao/base.js +0 -9
- package/examples/blog/bo/base.js +0 -5
- package/examples/order/bo/base.js +0 -5
- package/examples/order/business-objects.js +0 -11
- package/examples/order-more/bo/base.js +0 -5
- package/examples/order-more/business-objects.js +0 -26
- package/src/bo/base-bo-collection.js +0 -15
- package/src/bo/base-bo.js +0 -379
- package/src/dao/base-dao.js +0 -146
- package/src/dao/base-dao.spec.js +0 -6
- package/src/util/helpers.js +0 -28
- package/src/util/helpers.spec.js +0 -6
- package/test-utils/five/business-objects.js +0 -11
- package/test-utils/nine/business-objects.js +0 -7
- package/test-utils/six/business-objects.js +0 -11
- package/test-utils/twelve/business-objects.js +0 -8
|
@@ -1,30 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
const { createOneFromDatabase, createFromDatabase } = require('pure-orm');
|
|
3
|
+
const { getBusinessObjects: getBusinessObjectsA } = require('../examples/order/orm');
|
|
4
|
+
const { getBusinessObjects: getBusinessObjectsB } = require('../examples/blog/orm');
|
|
5
|
+
const { getBusinessObjects: getBusinessObjectsC } = require('../examples/order-more/orm');
|
|
6
|
+
const { getBusinessObjects: getBusinessObjectsD } = require('../test-utils/nine/orm');
|
|
7
|
+
const { getBusinessObjects: getBusinessObjectsE } = require('../test-utils/five/orm');
|
|
8
|
+
const { getBusinessObjects: getBusinessObjectsF } = require('../test-utils/six/orm');
|
|
9
|
+
const { getBusinessObjects: getBusinessObjectsG } = require('../test-utils/twelve/orm');
|
|
10
|
+
const { getBusinessObjects: getBusinessObjectsH } = require('../test-utils/thirteen/orm');
|
|
11
|
+
const Articles = require('../examples/blog/bo/articles');
|
|
12
|
+
const one = require('../test-utils/one/results.json');
|
|
13
|
+
const two = require('../test-utils/two/results');
|
|
14
|
+
const three = require('../test-utils/three/results');
|
|
15
|
+
const four = require('../test-utils/four/results.json');
|
|
16
|
+
const five = require('../test-utils/five/results.json');
|
|
17
|
+
const six = require('../test-utils/six/results.json');
|
|
18
|
+
const seven = require('../test-utils/seven/results.json');
|
|
19
|
+
const eight = require('../test-utils/eight/results.json');
|
|
20
|
+
const nine = require('../test-utils/nine/results.json');
|
|
21
|
+
const ten = require('../test-utils/ten/results.json');
|
|
22
|
+
const eleven = require('../test-utils/eleven/results.json');
|
|
23
|
+
const twelve = require('../test-utils/twelve/results.json');
|
|
24
|
+
const thirteen = require('../test-utils/thirteen/results.json');
|
|
23
25
|
|
|
24
26
|
test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with all one-to-one or one-to-many tables)', () => {
|
|
25
|
-
const order =
|
|
27
|
+
const order = createOneFromDatabase(one, getBusinessObjectsA);
|
|
26
28
|
expect(Array.isArray(order)).toBe(false);
|
|
27
29
|
expect(order.id).toEqual(3866);
|
|
30
|
+
debugger;
|
|
28
31
|
expect(order.utmSource.id).toEqual(6);
|
|
29
32
|
expect(order.lineItems.models.length).toEqual(6);
|
|
30
33
|
|
|
@@ -54,7 +57,7 @@ test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with
|
|
|
54
57
|
});
|
|
55
58
|
|
|
56
59
|
test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with many-to-many tables)', () => {
|
|
57
|
-
const article =
|
|
60
|
+
const article = createOneFromDatabase(two, getBusinessObjectsB);
|
|
58
61
|
expect(Array.isArray(article)).toBe(false);
|
|
59
62
|
expect(article.id).toEqual(14);
|
|
60
63
|
expect(article.person.id).toEqual(8);
|
|
@@ -84,7 +87,7 @@ test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with
|
|
|
84
87
|
|
|
85
88
|
test('Bo#parseFromDatabase where multiple rows reduce to many rows with nested objects (with many-to-many tables)', () => {
|
|
86
89
|
|
|
87
|
-
const articles =
|
|
90
|
+
const articles = createFromDatabase(three, getBusinessObjectsB);
|
|
88
91
|
expect(Array.isArray(articles.models)).toBe(true);
|
|
89
92
|
expect(articles instanceof Articles).toBe(true);
|
|
90
93
|
expect(articles.models.length).toEqual(2);
|
|
@@ -136,7 +139,7 @@ test('Bo#parseFromDatabase where multiple rows reduce to many rows with nested o
|
|
|
136
139
|
// living on its own productVariant (which would keep overwriting itself
|
|
137
140
|
// on the actualProductVariant node).
|
|
138
141
|
test('Bo#parseFromDatabase where node is already seen', () => {
|
|
139
|
-
const inventoryLevels =
|
|
142
|
+
const inventoryLevels = createFromDatabase(four, getBusinessObjectsC);
|
|
140
143
|
|
|
141
144
|
const first = inventoryLevels.models[0];
|
|
142
145
|
expect(first.id).toEqual(15);
|
|
@@ -199,7 +202,7 @@ test('Bo#parseFromDatabase where node is already seen', () => {
|
|
|
199
202
|
// be doing that, but since code coverage all-around isn't great and I already
|
|
200
203
|
// had this fuller json dump from production, I just used it all - YOLO.
|
|
201
204
|
test('Bo#parseFromDatabase where a deeply nested models property was misbehaving', () => {
|
|
202
|
-
const orders =
|
|
205
|
+
const orders = createFromDatabase(five, getBusinessObjectsE);
|
|
203
206
|
// The assertion that failed when the bug was present
|
|
204
207
|
expect(
|
|
205
208
|
orders.models[0].lineItems.models[1].parcelLineItems.models[0].parcel.parcelEvents.models.length
|
|
@@ -244,7 +247,7 @@ test('Bo#parseFromDatabase where a deeply nested models property was misbehaving
|
|
|
244
247
|
// Issue occcurs in nestClump
|
|
245
248
|
// Problem only surfaced when custom was included
|
|
246
249
|
test('Bo#parseOneFromDatabase where a deeply nested models property was misbehaving', () => {
|
|
247
|
-
const parcel =
|
|
250
|
+
const parcel = createOneFromDatabase(six, getBusinessObjectsF);
|
|
248
251
|
// The assertion that failed when the bug was present
|
|
249
252
|
expect(parcel.parcelLineItems.models[1].lineItem.order).toBeDefined();
|
|
250
253
|
// Lots of other assertions that are unrelated and shouldn't be here except
|
|
@@ -278,7 +281,7 @@ test('Bo#parseOneFromDatabase where a deeply nested models property was misbehav
|
|
|
278
281
|
// Color
|
|
279
282
|
// Issue occcurs in nestClump
|
|
280
283
|
test('Bo#parseOneFromDatabase where a deeply nested models property was attaching to wrong parent', () => {
|
|
281
|
-
const inventoryLevel =
|
|
284
|
+
const inventoryLevel = createOneFromDatabase(seven, getBusinessObjectsC);
|
|
282
285
|
// The assertion that failed when the bug was present
|
|
283
286
|
expect(inventoryLevel.actualProductVariant.productVariants.models[1]).toBeDefined();
|
|
284
287
|
// Lots of other assertions that are unrelated and shouldn't be here except
|
|
@@ -317,7 +320,7 @@ test('Bo#parseOneFromDatabase where a deeply nested models property was attachin
|
|
|
317
320
|
// Product
|
|
318
321
|
// Issue occcurs in nestClump
|
|
319
322
|
test('Bo#parseOneFromDatabase where a deeply nested models property was attaching to wrong parent 2', () => {
|
|
320
|
-
const shipments =
|
|
323
|
+
const shipments = createFromDatabase(eight, getBusinessObjectsC);
|
|
321
324
|
// The assertion that failed when the bug was present
|
|
322
325
|
expect(shipments.models[0].shipmentActualProductVariants.models[1].actualProductVariant.productVariants).toBeDefined();
|
|
323
326
|
// IN ADDITION TO ABOVE ASSERTION and helpful test description of what is
|
|
@@ -379,7 +382,7 @@ test('Bo#parseFromDatabase with just top level nodes', () => {
|
|
|
379
382
|
let featureSwitches;
|
|
380
383
|
try {
|
|
381
384
|
// This failed when the bug was present
|
|
382
|
-
featureSwitches =
|
|
385
|
+
featureSwitches = createFromDatabase(nine, getBusinessObjectsD);
|
|
383
386
|
} catch (e) {
|
|
384
387
|
expect(e).not.toBeDefined();
|
|
385
388
|
}
|
|
@@ -399,7 +402,7 @@ test('Bo#parseFromDatabase 10', () => {
|
|
|
399
402
|
let orders;
|
|
400
403
|
try {
|
|
401
404
|
// This failed when the bug was present
|
|
402
|
-
orders =
|
|
405
|
+
orders = createFromDatabase(ten, getBusinessObjectsC);
|
|
403
406
|
} catch (e) {
|
|
404
407
|
expect(e).not.toBeDefined();
|
|
405
408
|
}
|
|
@@ -521,14 +524,11 @@ test('Bo#parseFromDatabase 11', () => {
|
|
|
521
524
|
let orders;
|
|
522
525
|
try {
|
|
523
526
|
// This failed when the bug was present
|
|
524
|
-
orders =
|
|
527
|
+
orders = createFromDatabase(eleven, getBusinessObjectsC);
|
|
525
528
|
} catch (e) {
|
|
526
529
|
expect(e).not.toBeDefined();
|
|
527
530
|
}
|
|
528
531
|
expect(orders).toBeDefined();
|
|
529
|
-
// Lots of other assertions that are unrelated and shouldn't be here except
|
|
530
|
-
// I'm insecure about the lack of tests so just going at it cause I can.
|
|
531
|
-
// TODO add more later
|
|
532
532
|
});
|
|
533
533
|
|
|
534
534
|
// Issue occcurs in nestClump
|
|
@@ -537,7 +537,7 @@ test('Bo#parseFromDatabase 12', () => {
|
|
|
537
537
|
let prompt;
|
|
538
538
|
try {
|
|
539
539
|
// This failed when the bug was present
|
|
540
|
-
prompt =
|
|
540
|
+
prompt = createFromDatabase(twelve, getBusinessObjectsG);
|
|
541
541
|
} catch (e) {
|
|
542
542
|
expect(e).not.toBeDefined();
|
|
543
543
|
}
|
|
@@ -545,3 +545,39 @@ test('Bo#parseFromDatabase 12', () => {
|
|
|
545
545
|
// Ideally the below should work
|
|
546
546
|
// expect(prompt.fromMember.id).toEqual(1);
|
|
547
547
|
});
|
|
548
|
+
|
|
549
|
+
// Issue occcurs in nestClump
|
|
550
|
+
// Problem when a table has records that are supposed to nest under root
|
|
551
|
+
// but nest under other stuff below it instead
|
|
552
|
+
// Member
|
|
553
|
+
// Recommendations [1]
|
|
554
|
+
// Brand
|
|
555
|
+
// Recommendations [1]
|
|
556
|
+
// Passion
|
|
557
|
+
// Recommendations [2]
|
|
558
|
+
// -- instead of correct --
|
|
559
|
+
// Member
|
|
560
|
+
// Recommendations[4]
|
|
561
|
+
// Brand
|
|
562
|
+
// Passion
|
|
563
|
+
test('Bo#parseFromDatabase 13', () => {
|
|
564
|
+
const members = createFromDatabase(thirteen, getBusinessObjectsH);
|
|
565
|
+
const member = members.models[0];
|
|
566
|
+
expect(member.recommendations.models.length).toEqual(4);
|
|
567
|
+
expect(member.recommendations.models[0].brand.id).toEqual(2);
|
|
568
|
+
expect(member.recommendations.models[0].product.id).toEqual(7);
|
|
569
|
+
expect(member.recommendations.models[0].category.id).toEqual(1);
|
|
570
|
+
expect(member.recommendations.models[0].recommendationAudiences.models[0].audience.id).toEqual(1);
|
|
571
|
+
expect(member.recommendations.models[1].brand.id).toEqual(2);
|
|
572
|
+
expect(member.recommendations.models[1].product.id).toEqual(1);
|
|
573
|
+
expect(member.recommendations.models[1].category.id).toEqual(2);
|
|
574
|
+
expect(member.recommendations.models[1].recommendationAudiences.models[0].audience.id).toEqual(1);
|
|
575
|
+
expect(member.recommendations.models[2].brand.id).toEqual(3);
|
|
576
|
+
expect(member.recommendations.models[2].product.id).toEqual(27);
|
|
577
|
+
expect(member.recommendations.models[2].category.id).toEqual(3);
|
|
578
|
+
expect(member.recommendations.models[2].recommendationAudiences.models[0].audience.id).toEqual(1);
|
|
579
|
+
expect(member.recommendations.models[3].brand.id).toEqual(6);
|
|
580
|
+
expect(member.recommendations.models[3].product.id).toEqual(75);
|
|
581
|
+
expect(member.recommendations.models[3].category.id).toEqual(4);
|
|
582
|
+
expect(member.recommendations.models[3].recommendationAudiences.models[0].audience.id).toEqual(1);
|
|
583
|
+
});
|
package/src/factory.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const {
|
|
2
|
+
createOneFromDatabase,
|
|
3
|
+
createOneOrNoneFromDatabase,
|
|
4
|
+
createManyFromDatabase,
|
|
5
|
+
createFromDatabase,
|
|
6
|
+
getSqlUpdateParts,
|
|
7
|
+
getSqlInsertParts,
|
|
8
|
+
getTableName,
|
|
9
|
+
getColumns,
|
|
10
|
+
getDisplayName
|
|
11
|
+
} = require('./bo');
|
|
12
|
+
|
|
13
|
+
module.exports.create = ({ getBusinessObjects, db, logError }) => {
|
|
14
|
+
const defaultErrorHandler = err => {
|
|
15
|
+
if (!err.name === 'QueryResultError') {
|
|
16
|
+
logError(err);
|
|
17
|
+
}
|
|
18
|
+
throw err;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/* ------------------------------------------------------------------------*/
|
|
22
|
+
/* Query functions --------------------------------------------------------*/
|
|
23
|
+
/* ------------------------------------------------------------------------*/
|
|
24
|
+
|
|
25
|
+
const one = (query, values, errorHandler = defaultErrorHandler) => {
|
|
26
|
+
return db
|
|
27
|
+
.many(query, values)
|
|
28
|
+
.then(rows => createOneFromDatabase(rows, getBusinessObjects))
|
|
29
|
+
.catch(errorHandler);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const oneOrNone = (query, values, errorHandler = defaultErrorHandler) => {
|
|
33
|
+
return db
|
|
34
|
+
.any(query, values)
|
|
35
|
+
.then(rows => createOneOrNoneFromDatabase(rows, getBusinessObjects))
|
|
36
|
+
.catch(errorHandler);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const many = (query, values, errorHandler = defaultErrorHandler) => {
|
|
40
|
+
return db
|
|
41
|
+
.any(query, values)
|
|
42
|
+
.then(rows => createManyFromDatabase(rows, getBusinessObjects))
|
|
43
|
+
.catch(errorHandler);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const any = (query, values, errorHandler = defaultErrorHandler) => {
|
|
47
|
+
return db
|
|
48
|
+
.any(query, values)
|
|
49
|
+
.then(rows => createFromDatabase(rows, getBusinessObjects))
|
|
50
|
+
.catch(errorHandler);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const none = (query, values, errorHandler = defaultErrorHandler) => {
|
|
54
|
+
return db
|
|
55
|
+
.none(query, values)
|
|
56
|
+
.then(() => null)
|
|
57
|
+
.catch(errorHandler);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/* ------------------------------------------------------------------------*/
|
|
61
|
+
/* Built-in basic CRUD functions ------------------------------------------*/
|
|
62
|
+
/* ------------------------------------------------------------------------*/
|
|
63
|
+
|
|
64
|
+
// Standard create
|
|
65
|
+
const create = bo => {
|
|
66
|
+
const { columns, values, valuesVar } = getSqlInsertParts(bo);
|
|
67
|
+
const query = `
|
|
68
|
+
INSERT INTO "${getTableName(bo)}" ( ${columns} )
|
|
69
|
+
VALUES ( ${valuesVar} )
|
|
70
|
+
RETURNING ${getColumns(bo)};
|
|
71
|
+
`;
|
|
72
|
+
return one(query, values);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// Standard update
|
|
76
|
+
const update = (bo, { on = 'id' } = {}) => {
|
|
77
|
+
const { clause, idVar, values } = getSqlUpdateParts(bo, on);
|
|
78
|
+
const query = `
|
|
79
|
+
UPDATE "${getTableName(bo)}"
|
|
80
|
+
SET ${clause}
|
|
81
|
+
WHERE "${getTableName(bo)}".${on} = ${idVar}
|
|
82
|
+
RETURNING ${getColumns(bo)};
|
|
83
|
+
`;
|
|
84
|
+
return one(query, values);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// Standard delete
|
|
88
|
+
const _delete = bo => {
|
|
89
|
+
const id = bo.id;
|
|
90
|
+
const query = `
|
|
91
|
+
DELETE FROM "${getTableName(bo)}"
|
|
92
|
+
WHERE "${getTableName(bo)}".id = ${id}
|
|
93
|
+
`;
|
|
94
|
+
return none(query);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const deleteMatching = bo => {
|
|
98
|
+
const { whereClause, values } = bo.getMatchingParts();
|
|
99
|
+
const query = `
|
|
100
|
+
DELETE FROM "${getTableName(bo)}"
|
|
101
|
+
WHERE ${whereClause};
|
|
102
|
+
`;
|
|
103
|
+
return none(query, values);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const getMatching = bo => {
|
|
107
|
+
const { whereClause, values } = bo.getMatchingParts();
|
|
108
|
+
const query = `
|
|
109
|
+
SELECT ${getColumns(bo)}
|
|
110
|
+
FROM "${getTableName(bo)}"
|
|
111
|
+
WHERE ${whereClause};
|
|
112
|
+
`;
|
|
113
|
+
return one(query, values);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const getOneOrNoneMatching = bo => {
|
|
117
|
+
const { whereClause, values } = bo.getMatchingParts();
|
|
118
|
+
const query = `
|
|
119
|
+
SELECT ${getColumns(bo)}
|
|
120
|
+
FROM "${getTableName(bo)}"
|
|
121
|
+
WHERE ${whereClause};
|
|
122
|
+
`;
|
|
123
|
+
return oneOrNone(query, values);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const getAnyMatching = bo => {
|
|
127
|
+
const { whereClause, values } = bo.getMatchingParts();
|
|
128
|
+
const query = `
|
|
129
|
+
SELECT ${getColumns(bo)}
|
|
130
|
+
FROM "${getTableName(bo)}"
|
|
131
|
+
WHERE ${whereClause};
|
|
132
|
+
`;
|
|
133
|
+
return any(query, values);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const getAllMatching = bo => {
|
|
137
|
+
const { whereClause, values } = bo.getMatchingParts();
|
|
138
|
+
const query = `
|
|
139
|
+
SELECT ${getColumns(bo)}
|
|
140
|
+
FROM "${getTableName(bo)}"
|
|
141
|
+
WHERE ${whereClause};
|
|
142
|
+
`;
|
|
143
|
+
return many(query, values);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
// Query Functions
|
|
148
|
+
one,
|
|
149
|
+
oneOrNone,
|
|
150
|
+
many,
|
|
151
|
+
any,
|
|
152
|
+
none,
|
|
153
|
+
// Built-in basic CRUD functions
|
|
154
|
+
create,
|
|
155
|
+
update,
|
|
156
|
+
delete: _delete,
|
|
157
|
+
deleteMatching,
|
|
158
|
+
getMatching,
|
|
159
|
+
getOneOrNoneMatching,
|
|
160
|
+
getAnyMatching,
|
|
161
|
+
getAllMatching,
|
|
162
|
+
tables: getBusinessObjects().reduce((accum, Bo) => {
|
|
163
|
+
accum[getDisplayName(Bo)] = getColumns(Bo);
|
|
164
|
+
return accum;
|
|
165
|
+
}, {})
|
|
166
|
+
};
|
|
167
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
const orm = require('../examples/order/orm');
|
|
3
|
+
|
|
4
|
+
test('orm.tables', () => {
|
|
5
|
+
expect(Object.keys(orm.tables).length).toEqual(5);
|
|
6
|
+
expect(orm.tables.utmSource).toEqual('"utm_source".id as "utm_source#id", "utm_source".value as "utm_source#value", "utm_source".label as "utm_source#label", "utm_source".internal as "utm_source#internal"');
|
|
7
|
+
expect(orm.tables.order).toEqual('"order".id as "order#id", "order".email as "order#email", "order".browser_ip as "order#browser_ip", "order".browser_user_agent as "order#browser_user_agent", "order".kujo_imported_date as "order#kujo_imported_date", "order".created_date as "order#created_date", "order".cancel_reason as "order#cancel_reason", "order".cancelled_date as "order#cancelled_date", "order".closed_date as "order#closed_date", "order".processed_date as "order#processed_date", "order".updated_date as "order#updated_date", "order".note as "order#note", "order".subtotal_price as "order#subtotal_price", "order".taxes_included as "order#taxes_included", "order".total_discounts as "order#total_discounts", "order".total_price as "order#total_price", "order".total_tax as "order#total_tax", "order".total_weight as "order#total_weight", "order".order_status_url as "order#order_status_url", "order".utm_source_id as "order#utm_source_id", "order".utm_medium_id as "order#utm_medium_id", "order".utm_campaign as "order#utm_campaign", "order".utm_content as "order#utm_content", "order".utm_term as "order#utm_term"');
|
|
8
|
+
expect(orm.tables.lineItem).toEqual('"line_item".id as "line_item#id", "line_item".product_variant_id as "line_item#product_variant_id", "line_item".order_id as "line_item#order_id", "line_item".fulfillment_status_id as "line_item#fulfillment_status_id", "line_item".fulfillable_quantity as "line_item#fulfillable_quantity", "line_item".fulfillment_service as "line_item#fulfillment_service", "line_item".grams as "line_item#grams", "line_item".price as "line_item#price", "line_item".quantity as "line_item#quantity", "line_item".requires_shipping as "line_item#requires_shipping", "line_item".taxable as "line_item#taxable", "line_item".total_discount as "line_item#total_discount"');
|
|
9
|
+
expect(orm.tables.productVariant).toEqual('"product_variant".id as "product_variant#id", "product_variant".product_id as "product_variant#product_id", "product_variant".actual_product_variant_id as "product_variant#actual_product_variant_id", "product_variant".color_id as "product_variant#color_id", "product_variant".gender_id as "product_variant#gender_id", "product_variant".size_id as "product_variant#size_id", "product_variant".barcode as "product_variant#barcode", "product_variant".price as "product_variant#price", "product_variant".compare_at_price as "product_variant#compare_at_price", "product_variant".created_date as "product_variant#created_date", "product_variant".updated_date as "product_variant#updated_date", "product_variant".grams as "product_variant#grams", "product_variant".requires_shipping as "product_variant#requires_shipping"');
|
|
10
|
+
expect(orm.tables.product).toEqual('"product".id as "product#id", "product".vendor_id as "product#vendor_id", "product".value as "product#value", "product".label as "product#label", "product".product_type as "product#product_type", "product".created_date as "product#created_date", "product".updated_date as "product#updated_date", "product".published_date as "product#published_date", "product".category as "product#category"');
|
|
11
|
+
});
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
const createBaseDAO = require('./dao/base-dao');
|
|
1
|
+
const { create } = require('./factory');
|
|
2
|
+
module.exports.create = create;
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const {
|
|
5
|
+
getPrimaryKey,
|
|
6
|
+
getProperties,
|
|
7
|
+
getSqlColumns,
|
|
8
|
+
getReferences,
|
|
9
|
+
getDisplayName,
|
|
10
|
+
getPrefixedColumnNames,
|
|
11
|
+
getColumns,
|
|
12
|
+
nestClump,
|
|
13
|
+
clumpIntoGroups,
|
|
14
|
+
mapToBos,
|
|
15
|
+
objectifyDatabaseResult,
|
|
16
|
+
createFromDatabase,
|
|
17
|
+
createOneFromDatabase,
|
|
18
|
+
createOneOrNoneFromDatabase,
|
|
19
|
+
createManyFromDatabase,
|
|
20
|
+
getSqlInsertParts,
|
|
21
|
+
getSqlUpdateParts,
|
|
22
|
+
getMatchingParts,
|
|
23
|
+
getMatchingPartsObject,
|
|
24
|
+
getNewWith,
|
|
25
|
+
getValueBySqlColumn,
|
|
26
|
+
getId
|
|
27
|
+
} = require('./bo');
|
|
28
|
+
|
|
29
|
+
module.exports.getPrimaryKey = getPrimaryKey;
|
|
30
|
+
module.exports.getProperties = getProperties;
|
|
31
|
+
module.exports.getSqlColumns = getSqlColumns;
|
|
32
|
+
module.exports.getReferences = getReferences;
|
|
33
|
+
module.exports.getDisplayName = getDisplayName;
|
|
34
|
+
module.exports.getPrefixedColumnNames = getPrefixedColumnNames;
|
|
35
|
+
module.exports.getColumns = getColumns;
|
|
36
|
+
module.exports.nestClump = nestClump;
|
|
37
|
+
module.exports.clumpIntoGroups = clumpIntoGroups;
|
|
38
|
+
module.exports.mapToBos = mapToBos;
|
|
39
|
+
module.exports.objectifyDatabaseResult = objectifyDatabaseResult;
|
|
40
|
+
module.exports.createFromDatabase = createFromDatabase;
|
|
41
|
+
module.exports.createOneFromDatabase = createOneFromDatabase;
|
|
42
|
+
module.exports.createOneOrNoneFromDatabase = createOneOrNoneFromDatabase;
|
|
43
|
+
module.exports.createManyFromDatabase = createManyFromDatabase;
|
|
44
|
+
module.exports.getSqlInsertParts = getSqlInsertParts;
|
|
45
|
+
module.exports.getSqlUpdateParts = getSqlUpdateParts;
|
|
46
|
+
module.exports.getMatchingParts = getMatchingParts;
|
|
47
|
+
module.exports.getMatchingPartsObject = getMatchingPartsObject;
|
|
48
|
+
module.exports.getNewWith = getNewWith;
|
|
49
|
+
module.exports.getValueBySqlColumn = getValueBySqlColumn;
|
|
50
|
+
module.exports.getId = getId;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
const Base = require('./base');
|
|
2
1
|
const LineItems = require('./line-items');
|
|
3
2
|
const Order = require('./order');
|
|
4
3
|
|
|
5
|
-
class LineItem
|
|
4
|
+
class LineItem {
|
|
5
|
+
constructor(props) {
|
|
6
|
+
Object.assign(this, props);
|
|
7
|
+
}
|
|
8
|
+
|
|
6
9
|
get BoCollection() {
|
|
7
10
|
return LineItems;
|
|
8
11
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class LineItems extends BaseBoCollection {
|
|
1
|
+
class LineItems {
|
|
4
2
|
static get Bo() {
|
|
5
3
|
return require('./line-item'); // eslint-disable-line
|
|
6
4
|
}
|
|
5
|
+
constructor(props = {}) {
|
|
6
|
+
this.models = props.models || [];
|
|
7
|
+
}
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
module.exports = LineItems;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class Orders extends BaseBoCollection {
|
|
1
|
+
class Orders {
|
|
4
2
|
static get Bo() {
|
|
5
3
|
return require('./order'); // eslint-disable-line
|
|
6
4
|
}
|
|
5
|
+
constructor(props = {}) {
|
|
6
|
+
this.models = props.models || [];
|
|
7
|
+
}
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
module.exports = Orders;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
const Base = require('./base');
|
|
2
1
|
const ParcelEvents = require('./parcel-events');
|
|
3
2
|
const Parcel = require('./parcel');
|
|
4
3
|
|
|
5
|
-
class ParcelEvent
|
|
4
|
+
class ParcelEvent {
|
|
5
|
+
constructor(props) {
|
|
6
|
+
Object.assign(this, props);
|
|
7
|
+
}
|
|
8
|
+
|
|
6
9
|
get BoCollection() {
|
|
7
10
|
return ParcelEvents;
|
|
8
11
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class ParcelEvents extends BaseBoCollection {
|
|
1
|
+
class ParcelEvents {
|
|
4
2
|
static get Bo() {
|
|
5
3
|
return require('./parcel-event'); // eslint-disable-line
|
|
6
4
|
}
|
|
5
|
+
constructor(props = {}) {
|
|
6
|
+
this.models = props.models || [];
|
|
7
|
+
}
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
module.exports = ParcelEvents;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
const Base = require('./base');
|
|
2
1
|
const ParcelLineItems = require('./parcel-line-items');
|
|
3
2
|
const LineItem = require('./line-item');
|
|
4
3
|
const Parcel = require('./parcel');
|
|
5
4
|
|
|
6
|
-
class ParcelLineItem
|
|
5
|
+
class ParcelLineItem {
|
|
6
|
+
constructor(props) {
|
|
7
|
+
Object.assign(this, props);
|
|
8
|
+
}
|
|
9
|
+
|
|
7
10
|
get BoCollection() {
|
|
8
11
|
return ParcelLineItems;
|
|
9
12
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class ParcelLineItems extends BaseBoCollection {
|
|
1
|
+
class ParcelLineItems {
|
|
4
2
|
static get Bo() {
|
|
5
3
|
return require('./parcel-line-item'); // eslint-disable-line
|
|
6
4
|
}
|
|
5
|
+
constructor(props = {}) {
|
|
6
|
+
this.models = props.models || [];
|
|
7
|
+
}
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
module.exports = ParcelLineItems;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class Parcels extends BaseBoCollection {
|
|
1
|
+
class Parcels {
|
|
4
2
|
static get Bo() {
|
|
5
3
|
return require('./parcel'); // eslint-disable-line
|
|
6
4
|
}
|
|
5
|
+
constructor(props = {}) {
|
|
6
|
+
this.models = props.models || [];
|
|
7
|
+
}
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
module.exports = Parcels;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const { create } = require('pure-orm');
|
|
2
|
+
const Order = require('./bo/order');
|
|
3
|
+
const LineItem = require('./bo/line-item');
|
|
4
|
+
const ParcelLineItem = require('./bo/parcel-line-item');
|
|
5
|
+
const Parcel = require('./bo/parcel');
|
|
6
|
+
const ParcelEvent = require('./bo/parcel-event');
|
|
7
|
+
const getBusinessObjects = () => [
|
|
8
|
+
Order,
|
|
9
|
+
LineItem,
|
|
10
|
+
ParcelLineItem,
|
|
11
|
+
Parcel,
|
|
12
|
+
ParcelEvent
|
|
13
|
+
];
|
|
14
|
+
const orm = create({
|
|
15
|
+
getBusinessObjects,
|
|
16
|
+
db: void 0
|
|
17
|
+
});
|
|
18
|
+
module.exports = orm;
|
|
19
|
+
module.exports.getBusinessObjects = getBusinessObjects;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
const Base = require('./base');
|
|
2
1
|
const FeatureSwitches = require('./feature-switches');
|
|
3
2
|
|
|
4
|
-
class FeatureSwitch
|
|
3
|
+
class FeatureSwitch {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
Object.assign(this, props);
|
|
6
|
+
}
|
|
7
|
+
|
|
5
8
|
get BoCollection() {
|
|
6
9
|
return FeatureSwitches;
|
|
7
10
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class FeatureSwitches extends BaseBoCollection {
|
|
1
|
+
class FeatureSwitches {
|
|
4
2
|
static get displayName() {
|
|
5
3
|
return 'featureSwitches';
|
|
6
4
|
}
|
|
7
5
|
static get Bo() {
|
|
8
6
|
return require('./feature-switch'); // eslint-disable-line
|
|
9
7
|
}
|
|
8
|
+
constructor(props = {}) {
|
|
9
|
+
this.models = props.models || [];
|
|
10
|
+
}
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
module.exports = FeatureSwitches;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const { create } = require('pure-orm');
|
|
2
|
+
const FeatureSwitch = require('./bo/feature-switch');
|
|
3
|
+
const getBusinessObjects = () => [FeatureSwitch];
|
|
4
|
+
const orm = create({
|
|
5
|
+
getBusinessObjects,
|
|
6
|
+
db: void 0
|
|
7
|
+
});
|
|
8
|
+
module.exports = orm;
|
|
9
|
+
module.exports.getBusinessObjects = getBusinessObjects;
|