Skip to content

Commit f34b17c

Browse files
small refactor
1 parent e09d78f commit f34b17c

File tree

19 files changed

+638
-472
lines changed

19 files changed

+638
-472
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -458,35 +458,35 @@ interface PureORM {
458458
one: <T extends IModel>(
459459
query: string,
460460
values?: object,
461-
errorHandler = defaultErrorHandler
461+
errorHandler?: (err: Error) => never
462462
) => T;
463463

464464
// Execute a query returning either single model or undefined, or throws.
465465
oneOrNone: <T extends IModel>(
466466
query: string,
467467
values?: object,
468-
errorHandler = defaultErrorHandler
468+
errorHandler?: (err: Error) => never
469469
) => T | void;
470470

471471
// Execute a query returning a Collection with at least one model, or throws.
472472
many: <T extends ICollection<IModel>>(
473473
query: string,
474474
values?: object,
475-
errorHandler = defaultErrorHandler
475+
errorHandler?: (err: Error) => never
476476
) => T;
477477

478478
// Execute a query returning a Collection.
479479
any: <T extends ICollection<IModel>>(
480480
query: string,
481481
values?: object,
482-
errorHandler = defaultErrorHandler
482+
errorHandler?: (err: Error) => never
483483
) => T | void;
484484

485485
// Execute a query returning null.
486486
none: (
487487
query: string,
488488
values?: object,
489-
errorHandler = defaultErrorHandler
489+
errorHandler?: (err: Error) => never
490490
) => void;
491491

492492
/* ------------------------------------------------------------------------*/
@@ -505,9 +505,9 @@ interface PureORM {
505505
getAnyMatching: <T extends ICollection<IModel>>(model: IModel) => T | void;
506506
getAllMatching: <T extends ICollection<IModel>>(model: IModel) => T;
507507
create: <T extends IModel>(model: T) => T;
508-
update: <T extends IModel>(model: T, { on = 'id' } = {}) => T;
509-
_delete: <T extends IModel>(model: T) => void;
510-
deleteMatching: <T extends IModel>(model => T);
508+
update: <T extends IModel>(model: T, options: { on: string }) => T;
509+
delete: <T extends IModel>(model: T) => void;
510+
deleteMatching: <T extends IModel>(model: T) => void;
511511

512512
/* ------------------------------------------------------------------------*/
513513
/* Helpful Properties -----------------------------------------------------*/
@@ -519,7 +519,7 @@ interface PureORM {
519519
* and namespacing them to the table to avoid collisions and as required
520520
* for PureORM mapping.
521521
*/
522-
tables: { [key:string]: { 'columns': string; }};
522+
tables: { [key: string]: { columns: string } };
523523
db: DataBaseDriver;
524524
}
525525
```

package-lock.json

+19-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@babel/preset-typescript": "^7.16.7",
2525
"@types/jest": "^27.4.1",
2626
"babel-jest": "^27.5.1",
27-
"camelcase": "^5.0.0"
27+
"camelcase": "^6.3.0"
2828
},
2929
"devDependencies": {
3030
"@types/express": "^4.17.13",

src/factory.spec.ts renamed to src/core.spec.ts

+55-41
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable max-len */
2-
import orm from '../test-utils/order/orm';
3-
import ormA from '../test-utils/order/orm';
4-
import ormB from '../test-utils/blog/orm';
5-
import ormC from '../test-utils/order-more/orm';
6-
import ormD from '../test-utils/nine/orm';
7-
import ormE from '../test-utils/five/orm';
8-
import ormF from '../test-utils/six/orm';
9-
import ormG from '../test-utils/twelve/orm';
10-
import ormH from '../test-utils/thirteen/orm';
2+
import { createCore } from './core';
3+
import { entities as orderEntities } from '../test-utils/order/entities';
4+
import { entities as blogEntities } from '../test-utils/blog/entities';
5+
import { entities as orderMoreEntities } from '../test-utils/order-more/entities';
6+
import { entities as nineEntities } from '../test-utils/nine/entities';
7+
import { entities as fiveEntities } from '../test-utils/five/entities';
8+
import { entities as sixEntities } from '../test-utils/six/entities';
9+
import { entities as twelveEntities } from '../test-utils/twelve/entities';
10+
import { entities as thirteenEntities } from '../test-utils/thirteen/entities';
1111
import { Articles } from '../test-utils/blog/models/article';
1212
const two = require('../test-utils/two/results');
1313
const three = require('../test-utils/three/results');
@@ -23,8 +23,9 @@ const eleven = require('../test-utils/eleven/results.json');
2323
const twelve = require('../test-utils/twelve/results.json');
2424
const thirteen = require('../test-utils/thirteen/results.json');
2525

26-
test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with all one-to-one or one-to-many tables)', () => {
27-
const order = ormA.createOneFromDatabase(one);
26+
test('createOneFromDatabase where multiple rows reduce to one nested object (with all one-to-one or one-to-many tables)', () => {
27+
const core = createCore({ entities: orderEntities });
28+
const order = core.createOneFromDatabase(one);
2829
expect(Array.isArray(order)).toBe(false);
2930
expect(order?.id).toEqual(3866);
3031
expect(order?.utmSource.id).toEqual(6);
@@ -55,8 +56,9 @@ test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with
5556
expect(order.lineItems.models[5].productVariant.product.id).toEqual(3);
5657
});
5758

58-
test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with many-to-many tables)', () => {
59-
const article = ormB.createOneFromDatabase(two);
59+
test('createOneFromDatabase where multiple rows reduce to one nested object (with many-to-many tables)', () => {
60+
const core = createCore({ entities: blogEntities });
61+
const article = core.createOneFromDatabase(two);
6062
expect(Array.isArray(article)).toBe(false);
6163
expect(article.id).toEqual(14);
6264
expect(article.person.id).toEqual(8);
@@ -84,8 +86,9 @@ test('Bo#parseFromDatabase where multiple rows reduce to one nested object (with
8486
expect(article.articleTags.models[9].tag.id).toEqual(16);
8587
});
8688

87-
test('Bo#parseFromDatabase where multiple rows reduce to many rows with nested objects (with many-to-many tables)', () => {
88-
const articles = ormB.createFromDatabase(three);
89+
test('createFromDatabase where multiple rows reduce to many rows with nested objects (with many-to-many tables)', () => {
90+
const core = createCore({ entities: blogEntities });
91+
const articles = core.createFromDatabase(three);
8992
expect(Array.isArray(articles.models)).toBe(true);
9093
expect(articles instanceof Articles).toBe(true);
9194
expect(articles.models.length).toEqual(2);
@@ -136,8 +139,9 @@ test('Bo#parseFromDatabase where multiple rows reduce to many rows with nested o
136139
// productVariantImages append to it, instead of each productVariantImage
137140
// living on its own productVariant (which would keep overwriting itself
138141
// on the actualProductVariant node).
139-
test('Bo#parseFromDatabase where node is already seen', () => {
140-
const inventoryLevels = ormC.createFromDatabase(four);
142+
test('createFromDatabase where node is already seen', () => {
143+
const core = createCore({ entities: orderMoreEntities });
144+
const inventoryLevels = core.createFromDatabase(four);
141145

142146
expect(inventoryLevels).toBeDefined();
143147

@@ -272,8 +276,9 @@ test('Bo#parseFromDatabase where node is already seen', () => {
272276
// the two relevant line items of the one relevant order. This test should
273277
// be doing that, but since code coverage all-around isn't great and I already
274278
// had this fuller json dump from production, I just used it all - YOLO.
275-
test('Bo#parseFromDatabase where a deeply nested models property was misbehaving', () => {
276-
const orders = ormE.createFromDatabase(five);
279+
test('createFromDatabase where a deeply nested models property was misbehaving', () => {
280+
const core = createCore({ entities: fiveEntities });
281+
const orders = core.createFromDatabase(five);
277282
// The assertion that failed when the bug was present
278283
expect(
279284
orders?.models[0].lineItems.models[1].parcelLineItems.models[0].parcel
@@ -349,8 +354,9 @@ test('Bo#parseFromDatabase where a deeply nested models property was misbehaving
349354
// [MISSING ORDER]
350355
// Issue occcurs in nestClump
351356
// Problem only surfaced when custom was included
352-
test('Bo#parseOneFromDatabase where a deeply nested models property was misbehaving', () => {
353-
const parcel = ormF.createOneFromDatabase(six);
357+
test('createOneFromDatabase where a deeply nested models property was misbehaving', () => {
358+
const core = createCore({ entities: sixEntities });
359+
const parcel = core.createOneFromDatabase(six);
354360
// The assertion that failed when the bug was present
355361
expect(parcel.parcelLineItems.models[1].lineItem.order).toBeDefined();
356362
// Lots of other assertions that are unrelated and shouldn't be here except
@@ -391,8 +397,9 @@ test('Bo#parseOneFromDatabase where a deeply nested models property was misbehav
391397
// ProductVariant(2)
392398
// Color
393399
// Issue occcurs in nestClump
394-
test('Bo#parseOneFromDatabase where a deeply nested models property was attaching to wrong parent', () => {
395-
const inventoryLevel = ormC.createOneFromDatabase(seven);
400+
test('createOneFromDatabase where a deeply nested models property was attaching to wrong parent', () => {
401+
const core = createCore({ entities: orderMoreEntities });
402+
const inventoryLevel = core.createOneFromDatabase(seven);
396403
// The assertion that failed when the bug was present
397404
expect(
398405
inventoryLevel.actualProductVariant.productVariants.models[1]
@@ -450,8 +457,9 @@ test('Bo#parseOneFromDatabase where a deeply nested models property was attachin
450457
// ProductVariant(2)
451458
// Product
452459
// Issue occcurs in nestClump
453-
test('Bo#parseOneFromDatabase where a deeply nested models property was attaching to wrong parent 2', () => {
454-
const shipments = ormC.createFromDatabase(eight);
460+
test('createFromDatabase where a deeply nested models property was attaching to wrong parent 2', () => {
461+
const core = createCore({ entities: orderMoreEntities });
462+
const shipments = core.createFromDatabase(eight);
455463
// The assertion that failed when the bug was present
456464
expect(
457465
shipments?.models[0].shipmentActualProductVariants.models[1]
@@ -629,11 +637,12 @@ test('Bo#parseOneFromDatabase where a deeply nested models property was attachin
629637

630638
// Issue occcurs in nestClump
631639
// Problem is with only top level nodes
632-
test('Bo#parseFromDatabase with just top level nodes', () => {
640+
test('createFromDatabase with just top level nodes', () => {
641+
const core = createCore({ entities: nineEntities });
633642
let featureSwitches;
634643
try {
635644
// This failed when the bug was present
636-
featureSwitches = ormD.createFromDatabase(nine);
645+
featureSwitches = core.createFromDatabase(nine);
637646
} catch (e) {
638647
expect(e).not.toBeDefined();
639648
}
@@ -649,11 +658,12 @@ test('Bo#parseFromDatabase with just top level nodes', () => {
649658
// Problem is when oldest parent is an empty join record and is not included
650659
// which results in the oldest parent search returning -1 and parent heirarchy
651660
// is thus messed up.
652-
test('Bo#parseFromDatabase 10', () => {
661+
test('createFromDatabase 10', () => {
662+
const core = createCore({ entities: orderMoreEntities });
653663
let orders;
654664
try {
655665
// This failed when the bug was present
656-
orders = ormC.createFromDatabase(ten);
666+
orders = core.createFromDatabase(ten);
657667
} catch (e) {
658668
expect(e).not.toBeDefined();
659669
}
@@ -954,11 +964,12 @@ test('Bo#parseFromDatabase 10', () => {
954964

955965
// Issue occcurs in nestClump
956966
// Problem from early returning not logging bo so parent hierarcy was missing it
957-
test('Bo#parseFromDatabase 11', () => {
967+
test('createFromDatabase 11', () => {
968+
const core = createCore({ entities: orderMoreEntities });
958969
let orders;
959970
try {
960971
// This failed when the bug was present
961-
orders = ormC.createFromDatabase(eleven);
972+
orders = core.createFromDatabase(eleven);
962973
} catch (e) {
963974
expect(e).not.toBeDefined();
964975
}
@@ -967,11 +978,12 @@ test('Bo#parseFromDatabase 11', () => {
967978

968979
// Issue occcurs in nestClump
969980
// Problem when a table references another model twice (two columns)
970-
test('Bo#parseFromDatabase 12', () => {
981+
test('createFromDatabase 12', () => {
982+
const core = createCore({ entities: twelveEntities });
971983
let prompt;
972984
try {
973985
// This failed when the bug was present
974-
prompt = ormG.createFromDatabase(twelve);
986+
prompt = core.createFromDatabase(twelve);
975987
} catch (e) {
976988
expect(e).not.toBeDefined();
977989
}
@@ -994,8 +1006,9 @@ test('Bo#parseFromDatabase 12', () => {
9941006
// Recommendations[4]
9951007
// Brand
9961008
// Passion
997-
test('Bo#parseFromDatabase 13', () => {
998-
const members = ormH.createFromDatabase(thirteen);
1009+
test('createFromDatabase 13', () => {
1010+
const core = createCore({ entities: thirteenEntities });
1011+
const members = core.createFromDatabase(thirteen);
9991012
const member = members?.models[0];
10001013
expect(member?.recommendations.models.length).toEqual(4);
10011014
expect(member?.recommendations.models[0].brand.id).toEqual(2);
@@ -1029,20 +1042,21 @@ test('Bo#parseFromDatabase 13', () => {
10291042
});
10301043

10311044
test('orm.tables', () => {
1032-
expect(Object.keys(ormA.tables).length).toEqual(5);
1033-
expect(ormA.tables.utmSource.columns).toEqual(
1045+
const core = createCore({ entities: orderEntities });
1046+
expect(Object.keys(core.tables).length).toEqual(5);
1047+
expect(core.tables.utmSource.columns).toEqual(
10341048
'"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"'
10351049
);
1036-
expect(ormA.tables.order.columns).toEqual(
1050+
expect(core.tables.order.columns).toEqual(
10371051
'"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"'
10381052
);
1039-
expect(ormA.tables.lineItem.columns).toEqual(
1053+
expect(core.tables.lineItem.columns).toEqual(
10401054
'"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"'
10411055
);
1042-
expect(ormA.tables.productVariant.columns).toEqual(
1056+
expect(core.tables.productVariant.columns).toEqual(
10431057
'"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"'
10441058
);
1045-
expect(ormA.tables.product.columns).toEqual(
1059+
expect(core.tables.product.columns).toEqual(
10461060
'"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"'
10471061
);
10481062
});

0 commit comments

Comments
 (0)