pure-orm 4.0.3 → 4.1.4
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/.benchmarks/bench-core-baseline.json +303 -0
- package/README.md +0 -7
- package/babel.config.js +4 -1
- package/coverage/clover.xml +1492 -1231
- package/coverage/coverage-final.json +103 -103
- package/coverage/lcov-report/dist/src/core.js.html +590 -383
- package/coverage/lcov-report/dist/src/driver-integrations/index.html +20 -20
- package/coverage/lcov-report/dist/src/driver-integrations/pgp.js.html +52 -52
- package/coverage/lcov-report/dist/src/index.html +28 -28
- package/coverage/lcov-report/dist/src/index.js.html +2 -2
- package/coverage/lcov-report/dist/src/orm.js.html +392 -209
- 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 +2 -2
- 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 +1 -1
- 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 +4 -4
- package/coverage/lcov-report/dist/test-utils/five/models/order.js.html +3 -3
- package/coverage/lcov-report/dist/test-utils/five/models/parcel-event.js.html +6 -6
- package/coverage/lcov-report/dist/test-utils/five/models/parcel-line-item.js.html +7 -7
- package/coverage/lcov-report/dist/test-utils/five/models/parcel.js.html +2 -2
- package/coverage/lcov-report/dist/test-utils/fourteen/entities.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/fourteen/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/fourteen/models/customer.js.html +1 -1
- package/coverage/lcov-report/dist/test-utils/fourteen/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/fourteen/models/person.js.html +1 -1
- 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 +6 -6
- package/coverage/lcov-report/dist/test-utils/nine/models/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order/entities.js.html +9 -9
- package/coverage/lcov-report/dist/test-utils/order/index.html +1 -1
- package/coverage/lcov-report/dist/test-utils/order/models/index.html +14 -14
- package/coverage/lcov-report/dist/test-utils/order/models/line-item.js.html +11 -11
- package/coverage/lcov-report/dist/test-utils/order/models/order.js.html +41 -41
- package/coverage/lcov-report/dist/test-utils/order/models/product-variant.js.html +18 -18
- 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 +3 -3
- package/coverage/lcov-report/dist/test-utils/order-more/models/color.js.html +6 -6
- package/coverage/lcov-report/dist/test-utils/order-more/models/customer.js.html +3 -3
- package/coverage/lcov-report/dist/test-utils/order-more/models/gender.js.html +4 -4
- package/coverage/lcov-report/dist/test-utils/order-more/models/index.html +23 -23
- package/coverage/lcov-report/dist/test-utils/order-more/models/inventory-level.js.html +11 -11
- package/coverage/lcov-report/dist/test-utils/order-more/models/line-item.js.html +15 -15
- package/coverage/lcov-report/dist/test-utils/order-more/models/order.js.html +39 -39
- package/coverage/lcov-report/dist/test-utils/order-more/models/parcel-event.js.html +6 -6
- package/coverage/lcov-report/dist/test-utils/order-more/models/parcel-line-item.js.html +7 -7
- package/coverage/lcov-report/dist/test-utils/order-more/models/parcel.js.html +2 -2
- package/coverage/lcov-report/dist/test-utils/order-more/models/physical-address.js.html +12 -12
- package/coverage/lcov-report/dist/test-utils/order-more/models/product-variant-image.js.html +7 -7
- package/coverage/lcov-report/dist/test-utils/order-more/models/product-variant.js.html +22 -22
- package/coverage/lcov-report/dist/test-utils/order-more/models/product.js.html +11 -11
- package/coverage/lcov-report/dist/test-utils/order-more/models/refund.js.html +11 -11
- package/coverage/lcov-report/dist/test-utils/order-more/models/shipment-actual-product-variant.js.html +9 -9
- package/coverage/lcov-report/dist/test-utils/order-more/models/shipment.js.html +4 -4
- package/coverage/lcov-report/dist/test-utils/order-more/models/size.js.html +4 -4
- package/coverage/lcov-report/dist/test-utils/order-more/models/utm-medium.js.html +15 -15
- package/coverage/lcov-report/dist/test-utils/order-more/models/utm-source.js.html +17 -17
- 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 +3 -3
- package/coverage/lcov-report/dist/test-utils/six/models/index.html +21 -21
- package/coverage/lcov-report/dist/test-utils/six/models/line-item.js.html +10 -10
- package/coverage/lcov-report/dist/test-utils/six/models/order.js.html +13 -13
- package/coverage/lcov-report/dist/test-utils/six/models/parcel-line-item.js.html +2 -2
- package/coverage/lcov-report/dist/test-utils/six/models/parcel.js.html +2 -2
- 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 +2 -2
- package/coverage/lcov-report/dist/test-utils/thirteen/models/brand.js.html +2 -2
- 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 +14 -14
- package/coverage/lcov-report/dist/test-utils/thirteen/models/member.js.html +2 -2
- package/coverage/lcov-report/dist/test-utils/thirteen/models/passion.js.html +2 -2
- package/coverage/lcov-report/dist/test-utils/thirteen/models/product.js.html +10 -10
- package/coverage/lcov-report/dist/test-utils/thirteen/models/recommendation-audience.js.html +2 -2
- package/coverage/lcov-report/dist/test-utils/thirteen/models/recommendation.js.html +2 -2
- 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 +2 -2
- 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 +103 -103
- package/coverage/lcov-report/src/core.ts.html +841 -415
- package/coverage/lcov-report/src/driver-integrations/index.html +20 -20
- package/coverage/lcov-report/src/driver-integrations/pgp.ts.html +58 -58
- package/coverage/lcov-report/src/index.html +28 -28
- package/coverage/lcov-report/src/index.ts.html +1 -1
- package/coverage/lcov-report/src/orm.ts.html +361 -298
- 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 +2 -2
- 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 +1 -1
- 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 +4 -4
- package/coverage/lcov-report/test-utils/five/models/order.ts.html +3 -3
- package/coverage/lcov-report/test-utils/five/models/parcel-event.ts.html +6 -6
- package/coverage/lcov-report/test-utils/five/models/parcel-line-item.ts.html +7 -7
- package/coverage/lcov-report/test-utils/five/models/parcel.ts.html +2 -2
- package/coverage/lcov-report/test-utils/fourteen/entities.ts.html +1 -1
- package/coverage/lcov-report/test-utils/fourteen/index.html +1 -1
- package/coverage/lcov-report/test-utils/fourteen/models/customer.ts.html +1 -1
- package/coverage/lcov-report/test-utils/fourteen/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/fourteen/models/person.ts.html +1 -1
- 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 +8 -8
- package/coverage/lcov-report/test-utils/nine/models/index.html +1 -1
- package/coverage/lcov-report/test-utils/order/entities.ts.html +2 -2
- package/coverage/lcov-report/test-utils/order/index.html +1 -1
- package/coverage/lcov-report/test-utils/order/models/index.html +14 -14
- package/coverage/lcov-report/test-utils/order/models/line-item.ts.html +5 -5
- package/coverage/lcov-report/test-utils/order/models/order.ts.html +36 -36
- package/coverage/lcov-report/test-utils/order/models/product-variant.ts.html +13 -13
- 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 +3 -3
- package/coverage/lcov-report/test-utils/order-more/models/color.ts.html +6 -6
- package/coverage/lcov-report/test-utils/order-more/models/customer.ts.html +3 -3
- package/coverage/lcov-report/test-utils/order-more/models/gender.ts.html +4 -4
- package/coverage/lcov-report/test-utils/order-more/models/index.html +23 -23
- package/coverage/lcov-report/test-utils/order-more/models/inventory-level.ts.html +11 -11
- package/coverage/lcov-report/test-utils/order-more/models/line-item.ts.html +15 -15
- package/coverage/lcov-report/test-utils/order-more/models/order.ts.html +45 -45
- package/coverage/lcov-report/test-utils/order-more/models/parcel-event.ts.html +6 -6
- package/coverage/lcov-report/test-utils/order-more/models/parcel-line-item.ts.html +7 -7
- package/coverage/lcov-report/test-utils/order-more/models/parcel.ts.html +2 -2
- package/coverage/lcov-report/test-utils/order-more/models/physical-address.ts.html +12 -12
- package/coverage/lcov-report/test-utils/order-more/models/product-variant-image.ts.html +7 -7
- package/coverage/lcov-report/test-utils/order-more/models/product-variant.ts.html +22 -22
- package/coverage/lcov-report/test-utils/order-more/models/product.ts.html +11 -11
- package/coverage/lcov-report/test-utils/order-more/models/refund.ts.html +11 -11
- package/coverage/lcov-report/test-utils/order-more/models/shipment-actual-product-variant.ts.html +9 -9
- package/coverage/lcov-report/test-utils/order-more/models/shipment.ts.html +4 -4
- package/coverage/lcov-report/test-utils/order-more/models/size.ts.html +4 -4
- package/coverage/lcov-report/test-utils/order-more/models/utm-medium.ts.html +15 -15
- package/coverage/lcov-report/test-utils/order-more/models/utm-source.ts.html +17 -17
- 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 +3 -3
- package/coverage/lcov-report/test-utils/six/models/index.html +21 -21
- package/coverage/lcov-report/test-utils/six/models/line-item.ts.html +10 -10
- package/coverage/lcov-report/test-utils/six/models/order.ts.html +13 -13
- package/coverage/lcov-report/test-utils/six/models/parcel-line-item.ts.html +2 -2
- package/coverage/lcov-report/test-utils/six/models/parcel.ts.html +2 -2
- 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 +2 -2
- package/coverage/lcov-report/test-utils/thirteen/models/brand.ts.html +2 -2
- package/coverage/lcov-report/test-utils/thirteen/models/category.ts.html +1 -1
- package/coverage/lcov-report/test-utils/thirteen/models/index.html +14 -14
- package/coverage/lcov-report/test-utils/thirteen/models/member.ts.html +2 -2
- package/coverage/lcov-report/test-utils/thirteen/models/passion.ts.html +2 -2
- package/coverage/lcov-report/test-utils/thirteen/models/product.ts.html +10 -10
- package/coverage/lcov-report/test-utils/thirteen/models/recommendation-audience.ts.html +2 -2
- package/coverage/lcov-report/test-utils/thirteen/models/recommendation.ts.html +2 -2
- 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 +2 -2
- 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 +2134 -1989
- package/dist/src/core.d.ts +6 -0
- package/dist/src/core.js +258 -189
- package/dist/src/core.spec.js +413 -0
- package/dist/src/driver-integrations/pgp.spec.d.ts +1 -0
- package/dist/src/driver-integrations/pgp.spec.js +376 -0
- package/dist/src/orm.d.ts +1 -1
- package/dist/src/orm.js +137 -76
- package/dist/src/orm.spec.js +535 -85
- package/dist/test-utils/nine/models/feature-switch.d.ts +2 -2
- package/dist/test-utils/nine/models/feature-switch.ts +2 -2
- package/package.json +5 -3
- package/scripts/bench-core.js +636 -0
- package/scripts/check-bench-scenarios.js +47 -0
- package/src/core.spec.ts +485 -2
- package/src/core.ts +369 -227
- package/src/driver-integrations/pgp.spec.ts +444 -0
- package/src/orm.spec.ts +592 -88
- package/src/orm.ts +149 -128
- package/test-utils/nine/models/feature-switch.ts +2 -2
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
/* eslint-disable max-len */
|
|
13
|
+
const pgp_1 = require("./pgp");
|
|
14
|
+
const core_1 = require("../core");
|
|
15
|
+
const entities_1 = require("../../test-utils/order/entities");
|
|
16
|
+
class SimpleModel {
|
|
17
|
+
constructor(props) {
|
|
18
|
+
this.id = props.id;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
class SimpleCollection {
|
|
22
|
+
constructor({ models }) {
|
|
23
|
+
this.models = models;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const simpleEntities = [
|
|
27
|
+
{
|
|
28
|
+
tableName: 'widget',
|
|
29
|
+
columns: ['id'],
|
|
30
|
+
Model: SimpleModel,
|
|
31
|
+
Collection: SimpleCollection
|
|
32
|
+
}
|
|
33
|
+
];
|
|
34
|
+
const makeRow = (id) => ({
|
|
35
|
+
'widget#id': id
|
|
36
|
+
});
|
|
37
|
+
const makeOrderRow = (id, email) => ({
|
|
38
|
+
'order#id': id,
|
|
39
|
+
'order#email': email,
|
|
40
|
+
'order#browser_ip': null,
|
|
41
|
+
'order#browser_user_agent': null,
|
|
42
|
+
'order#kujo_imported_date': null,
|
|
43
|
+
'order#created_date': null,
|
|
44
|
+
'order#cancel_reason': null,
|
|
45
|
+
'order#cancelled_date': null,
|
|
46
|
+
'order#closed_date': null,
|
|
47
|
+
'order#processed_date': null,
|
|
48
|
+
'order#updated_date': null,
|
|
49
|
+
'order#note': null,
|
|
50
|
+
'order#subtotal_price': null,
|
|
51
|
+
'order#taxes_included': null,
|
|
52
|
+
'order#total_discounts': null,
|
|
53
|
+
'order#total_price': null,
|
|
54
|
+
'order#total_tax': null,
|
|
55
|
+
'order#total_weight': null,
|
|
56
|
+
'order#order_status_url': null,
|
|
57
|
+
'order#utm_source_id': null,
|
|
58
|
+
'order#utm_medium_id': null,
|
|
59
|
+
'order#utm_campaign': null,
|
|
60
|
+
'order#utm_content': null,
|
|
61
|
+
'order#utm_term': null
|
|
62
|
+
});
|
|
63
|
+
/* -------------------------------------------------------------------------*/
|
|
64
|
+
/* one ---------------------------------------------------------------------*/
|
|
65
|
+
/* -------------------------------------------------------------------------*/
|
|
66
|
+
describe('one', () => {
|
|
67
|
+
test('calls db.many and returns a single model', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
69
|
+
const db = { many: jest.fn().mockResolvedValue([makeRow(1)]) };
|
|
70
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
71
|
+
const result = yield pgp.one('SELECT ...', { id: 1 });
|
|
72
|
+
expect(db.many).toHaveBeenCalledWith('SELECT ...', { id: 1 });
|
|
73
|
+
expect(result.id).toEqual(1);
|
|
74
|
+
}));
|
|
75
|
+
test('throws when db.many returns empty array', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
77
|
+
const db = { many: jest.fn().mockResolvedValue([]) };
|
|
78
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
79
|
+
yield expect(pgp.one('SELECT ...')).rejects.toThrow();
|
|
80
|
+
}));
|
|
81
|
+
test('throws when result nests into more than one model', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
82
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
83
|
+
const db = {
|
|
84
|
+
many: jest.fn().mockResolvedValue([makeRow(1), makeRow(2)])
|
|
85
|
+
};
|
|
86
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
87
|
+
yield expect(pgp.one('SELECT ...')).rejects.toThrow();
|
|
88
|
+
}));
|
|
89
|
+
test('passes values to db.many', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
90
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
91
|
+
const db = { many: jest.fn().mockResolvedValue([makeRow(5)]) };
|
|
92
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
93
|
+
yield pgp.one('SELECT ... WHERE id = $1', [5]);
|
|
94
|
+
expect(db.many).toHaveBeenCalledWith('SELECT ... WHERE id = $1', [5]);
|
|
95
|
+
}));
|
|
96
|
+
});
|
|
97
|
+
/* -------------------------------------------------------------------------*/
|
|
98
|
+
/* oneOrNone ---------------------------------------------------------------*/
|
|
99
|
+
/* -------------------------------------------------------------------------*/
|
|
100
|
+
describe('oneOrNone', () => {
|
|
101
|
+
test('returns a model when one result exists', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
103
|
+
const db = { any: jest.fn().mockResolvedValue([makeRow(1)]) };
|
|
104
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
105
|
+
const result = yield pgp.oneOrNone('SELECT ...', { id: 1 });
|
|
106
|
+
expect(db.any).toHaveBeenCalledWith('SELECT ...', { id: 1 });
|
|
107
|
+
expect(result.id).toEqual(1);
|
|
108
|
+
}));
|
|
109
|
+
test('returns undefined when no results', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
110
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
111
|
+
const db = { any: jest.fn().mockResolvedValue([]) };
|
|
112
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
113
|
+
const result = yield pgp.oneOrNone('SELECT ...');
|
|
114
|
+
expect(result).toBeUndefined();
|
|
115
|
+
}));
|
|
116
|
+
test('throws when more than one result', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
118
|
+
const db = {
|
|
119
|
+
any: jest.fn().mockResolvedValue([makeRow(1), makeRow(2)])
|
|
120
|
+
};
|
|
121
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
122
|
+
yield expect(pgp.oneOrNone('SELECT ...')).rejects.toThrow();
|
|
123
|
+
}));
|
|
124
|
+
});
|
|
125
|
+
/* -------------------------------------------------------------------------*/
|
|
126
|
+
/* many --------------------------------------------------------------------*/
|
|
127
|
+
/* -------------------------------------------------------------------------*/
|
|
128
|
+
describe('many', () => {
|
|
129
|
+
test('returns a collection with at least one model', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
130
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
131
|
+
const db = {
|
|
132
|
+
any: jest.fn().mockResolvedValue([makeRow(1), makeRow(2)])
|
|
133
|
+
};
|
|
134
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
135
|
+
const result = yield pgp.many('SELECT ...');
|
|
136
|
+
expect(db.any).toHaveBeenCalledWith('SELECT ...', undefined);
|
|
137
|
+
expect(result.models.length).toEqual(2);
|
|
138
|
+
expect(result.models[0].id).toEqual(1);
|
|
139
|
+
expect(result.models[1].id).toEqual(2);
|
|
140
|
+
}));
|
|
141
|
+
test('works with a single result row', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
143
|
+
const db = { any: jest.fn().mockResolvedValue([makeRow(42)]) };
|
|
144
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
145
|
+
const result = yield pgp.many('SELECT ...');
|
|
146
|
+
expect(result.models.length).toEqual(1);
|
|
147
|
+
expect(result.models[0].id).toEqual(42);
|
|
148
|
+
}));
|
|
149
|
+
test('throws when no results', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
150
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
151
|
+
const db = { any: jest.fn().mockResolvedValue([]) };
|
|
152
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
153
|
+
yield expect(pgp.many('SELECT ...')).rejects.toThrow();
|
|
154
|
+
}));
|
|
155
|
+
test('passes values to db.any', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
156
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
157
|
+
const db = { any: jest.fn().mockResolvedValue([makeRow(1)]) };
|
|
158
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
159
|
+
yield pgp.many('SELECT ... WHERE x = $1', [10]);
|
|
160
|
+
expect(db.any).toHaveBeenCalledWith('SELECT ... WHERE x = $1', [10]);
|
|
161
|
+
}));
|
|
162
|
+
});
|
|
163
|
+
/* -------------------------------------------------------------------------*/
|
|
164
|
+
/* any ---------------------------------------------------------------------*/
|
|
165
|
+
/* -------------------------------------------------------------------------*/
|
|
166
|
+
describe('any', () => {
|
|
167
|
+
test('returns a collection when results exist', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
168
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
169
|
+
const db = {
|
|
170
|
+
result: jest.fn().mockResolvedValue({
|
|
171
|
+
rows: [makeRow(1), makeRow(2)],
|
|
172
|
+
fields: [{ name: 'widget#id' }]
|
|
173
|
+
})
|
|
174
|
+
};
|
|
175
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
176
|
+
const result = yield pgp.any('SELECT ...');
|
|
177
|
+
expect(db.result).toHaveBeenCalledWith('SELECT ...', undefined);
|
|
178
|
+
expect(result.models.length).toEqual(2);
|
|
179
|
+
}));
|
|
180
|
+
test('returns empty collection when no results', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
181
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
182
|
+
const db = {
|
|
183
|
+
result: jest.fn().mockResolvedValue({
|
|
184
|
+
rows: [],
|
|
185
|
+
fields: [{ name: 'widget#id' }]
|
|
186
|
+
})
|
|
187
|
+
};
|
|
188
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
189
|
+
const result = yield pgp.any('SELECT ...');
|
|
190
|
+
expect(result.models).toBeDefined();
|
|
191
|
+
expect(result.models.length).toEqual(0);
|
|
192
|
+
}));
|
|
193
|
+
test('extracts table name from first field name', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
194
|
+
const core = (0, core_1.createCore)({ entities: entities_1.entities });
|
|
195
|
+
const db = {
|
|
196
|
+
result: jest.fn().mockResolvedValue({
|
|
197
|
+
rows: [makeOrderRow(1, 'a@b.com')],
|
|
198
|
+
fields: [{ name: 'order#id' }]
|
|
199
|
+
})
|
|
200
|
+
};
|
|
201
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
202
|
+
const result = yield pgp.any('SELECT ...');
|
|
203
|
+
expect(result.models.length).toEqual(1);
|
|
204
|
+
expect(result.models[0].id).toEqual(1);
|
|
205
|
+
}));
|
|
206
|
+
test('passes values to db.result', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
207
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
208
|
+
const db = {
|
|
209
|
+
result: jest.fn().mockResolvedValue({
|
|
210
|
+
rows: [],
|
|
211
|
+
fields: [{ name: 'widget#id' }]
|
|
212
|
+
})
|
|
213
|
+
};
|
|
214
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
215
|
+
yield pgp.any('SELECT ... WHERE x = $1', [5]);
|
|
216
|
+
expect(db.result).toHaveBeenCalledWith('SELECT ... WHERE x = $1', [5]);
|
|
217
|
+
}));
|
|
218
|
+
});
|
|
219
|
+
/* -------------------------------------------------------------------------*/
|
|
220
|
+
/* none --------------------------------------------------------------------*/
|
|
221
|
+
/* -------------------------------------------------------------------------*/
|
|
222
|
+
describe('none', () => {
|
|
223
|
+
test('calls db.none and resolves to null', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
224
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
225
|
+
const db = { none: jest.fn().mockResolvedValue(undefined) };
|
|
226
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
227
|
+
const result = yield pgp.none('DELETE ...');
|
|
228
|
+
expect(db.none).toHaveBeenCalledWith('DELETE ...', undefined);
|
|
229
|
+
expect(result).toBeNull();
|
|
230
|
+
}));
|
|
231
|
+
test('passes values to db.none', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
232
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
233
|
+
const db = { none: jest.fn().mockResolvedValue(undefined) };
|
|
234
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
235
|
+
yield pgp.none('DELETE ... WHERE id = $(id)', { id: 7 });
|
|
236
|
+
expect(db.none).toHaveBeenCalledWith('DELETE ... WHERE id = $(id)', {
|
|
237
|
+
id: 7
|
|
238
|
+
});
|
|
239
|
+
}));
|
|
240
|
+
});
|
|
241
|
+
/* -------------------------------------------------------------------------*/
|
|
242
|
+
/* Error handling ----------------------------------------------------------*/
|
|
243
|
+
/* -------------------------------------------------------------------------*/
|
|
244
|
+
describe('error handling', () => {
|
|
245
|
+
test('defaultErrorHandler rethrows the error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
246
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
247
|
+
const err = new Error('db error');
|
|
248
|
+
const db = { many: jest.fn().mockRejectedValue(err) };
|
|
249
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
250
|
+
yield expect(pgp.one('SELECT ...')).rejects.toThrow('db error');
|
|
251
|
+
}));
|
|
252
|
+
test('defaultErrorHandler calls logError for non-QueryResultError', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
253
|
+
const logError = jest.fn((err) => {
|
|
254
|
+
throw err;
|
|
255
|
+
});
|
|
256
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
257
|
+
const err = new Error('unexpected error');
|
|
258
|
+
const db = { many: jest.fn().mockRejectedValue(err) };
|
|
259
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db, logError });
|
|
260
|
+
yield expect(pgp.one('SELECT ...')).rejects.toThrow('unexpected error');
|
|
261
|
+
expect(logError).toHaveBeenCalledWith(err);
|
|
262
|
+
}));
|
|
263
|
+
test('defaultErrorHandler does not call logError for QueryResultError', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
264
|
+
const logError = jest.fn((err) => {
|
|
265
|
+
throw err;
|
|
266
|
+
});
|
|
267
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
268
|
+
const err = new Error('no data');
|
|
269
|
+
err.name = 'QueryResultError';
|
|
270
|
+
const db = { many: jest.fn().mockRejectedValue(err) };
|
|
271
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db, logError });
|
|
272
|
+
yield expect(pgp.one('SELECT ...')).rejects.toThrow('no data');
|
|
273
|
+
expect(logError).not.toHaveBeenCalled();
|
|
274
|
+
}));
|
|
275
|
+
test('custom errorHandler overrides defaultErrorHandler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
276
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
277
|
+
const err = new Error('db failure');
|
|
278
|
+
const db = { many: jest.fn().mockRejectedValue(err) };
|
|
279
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
280
|
+
const customHandler = jest.fn((_err) => {
|
|
281
|
+
throw new Error('custom handled');
|
|
282
|
+
});
|
|
283
|
+
yield expect(pgp.one('SELECT ...', undefined, customHandler)).rejects.toThrow('custom handled');
|
|
284
|
+
expect(customHandler).toHaveBeenCalledWith(err);
|
|
285
|
+
}));
|
|
286
|
+
test('custom errorHandler works for oneOrNone', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
287
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
288
|
+
const err = new Error('fail');
|
|
289
|
+
const db = { any: jest.fn().mockRejectedValue(err) };
|
|
290
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
291
|
+
const customHandler = jest.fn((_err) => {
|
|
292
|
+
throw new Error('caught');
|
|
293
|
+
});
|
|
294
|
+
yield expect(pgp.oneOrNone('SELECT ...', undefined, customHandler)).rejects.toThrow('caught');
|
|
295
|
+
expect(customHandler).toHaveBeenCalledWith(err);
|
|
296
|
+
}));
|
|
297
|
+
test('custom errorHandler works for many', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
298
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
299
|
+
const err = new Error('fail');
|
|
300
|
+
const db = { any: jest.fn().mockRejectedValue(err) };
|
|
301
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
302
|
+
const customHandler = jest.fn((_err) => {
|
|
303
|
+
throw new Error('caught');
|
|
304
|
+
});
|
|
305
|
+
yield expect(pgp.many('SELECT ...', undefined, customHandler)).rejects.toThrow('caught');
|
|
306
|
+
}));
|
|
307
|
+
test('custom errorHandler works for any', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
308
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
309
|
+
const err = new Error('fail');
|
|
310
|
+
const db = { result: jest.fn().mockRejectedValue(err) };
|
|
311
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
312
|
+
const customHandler = jest.fn((_err) => {
|
|
313
|
+
throw new Error('caught');
|
|
314
|
+
});
|
|
315
|
+
yield expect(pgp.any('SELECT ...', undefined, customHandler)).rejects.toThrow('caught');
|
|
316
|
+
}));
|
|
317
|
+
test('custom errorHandler works for none', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
318
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
319
|
+
const err = new Error('fail');
|
|
320
|
+
const db = { none: jest.fn().mockRejectedValue(err) };
|
|
321
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
322
|
+
const customHandler = jest.fn((_err) => {
|
|
323
|
+
throw new Error('caught');
|
|
324
|
+
});
|
|
325
|
+
yield expect(pgp.none('DELETE ...', undefined, customHandler)).rejects.toThrow('caught');
|
|
326
|
+
}));
|
|
327
|
+
});
|
|
328
|
+
/* -------------------------------------------------------------------------*/
|
|
329
|
+
/* Returned object structure -----------------------------------------------*/
|
|
330
|
+
/* -------------------------------------------------------------------------*/
|
|
331
|
+
describe('createForPGP return structure', () => {
|
|
332
|
+
test('includes all core methods', () => {
|
|
333
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
334
|
+
const db = {
|
|
335
|
+
many: jest.fn(),
|
|
336
|
+
any: jest.fn(),
|
|
337
|
+
result: jest.fn(),
|
|
338
|
+
none: jest.fn()
|
|
339
|
+
};
|
|
340
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
341
|
+
expect(typeof pgp.createFromDatabase).toBe('function');
|
|
342
|
+
expect(typeof pgp.createOneFromDatabase).toBe('function');
|
|
343
|
+
expect(typeof pgp.createOneOrNoneFromDatabase).toBe('function');
|
|
344
|
+
expect(typeof pgp.createManyFromDatabase).toBe('function');
|
|
345
|
+
expect(typeof pgp.createAnyFromDatabase).toBe('function');
|
|
346
|
+
expect(typeof pgp.getEntityByModel).toBe('function');
|
|
347
|
+
expect(typeof pgp.getEntityByTableName).toBe('function');
|
|
348
|
+
expect(pgp.tables).toBeDefined();
|
|
349
|
+
});
|
|
350
|
+
test('includes all query methods', () => {
|
|
351
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
352
|
+
const db = {
|
|
353
|
+
many: jest.fn(),
|
|
354
|
+
any: jest.fn(),
|
|
355
|
+
result: jest.fn(),
|
|
356
|
+
none: jest.fn()
|
|
357
|
+
};
|
|
358
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
359
|
+
expect(typeof pgp.one).toBe('function');
|
|
360
|
+
expect(typeof pgp.oneOrNone).toBe('function');
|
|
361
|
+
expect(typeof pgp.many).toBe('function');
|
|
362
|
+
expect(typeof pgp.any).toBe('function');
|
|
363
|
+
expect(typeof pgp.none).toBe('function');
|
|
364
|
+
});
|
|
365
|
+
test('exposes db reference', () => {
|
|
366
|
+
const core = (0, core_1.createCore)({ entities: simpleEntities });
|
|
367
|
+
const db = {
|
|
368
|
+
many: jest.fn(),
|
|
369
|
+
any: jest.fn(),
|
|
370
|
+
result: jest.fn(),
|
|
371
|
+
none: jest.fn()
|
|
372
|
+
};
|
|
373
|
+
const pgp = (0, pgp_1.createForPGP)({ core, db });
|
|
374
|
+
expect(pgp.db).toBe(db);
|
|
375
|
+
});
|
|
376
|
+
});
|
package/dist/src/orm.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface IPureORM extends ICoreIntegratedDriver {
|
|
|
11
11
|
getAnyMatching: <T extends ICollection<IModel>>(model: IModel) => Promise<T | void>;
|
|
12
12
|
getAllMatching: <T extends ICollection<IModel>>(model: IModel) => Promise<T>;
|
|
13
13
|
create: <T extends IModel>(model: T) => Promise<T>;
|
|
14
|
-
update: <T extends IModel>(model: T, options
|
|
14
|
+
update: <T extends IModel>(model: T, options?: {
|
|
15
15
|
on: string;
|
|
16
16
|
}) => Promise<T>;
|
|
17
17
|
delete: <T extends IModel>(model: T) => Promise<void>;
|