pure-orm 2.0.0 → 2.3.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/README.md +36 -11
- package/examples/order-more/bo/customer.js +18 -0
- package/examples/order-more/bo/customers.js +9 -0
- package/examples/order-more/bo/line-item.js +33 -0
- package/examples/order-more/bo/line-items.js +9 -0
- package/examples/order-more/bo/order.js +49 -0
- package/examples/order-more/bo/orders.js +9 -0
- package/examples/order-more/bo/parcel-event.js +19 -0
- package/examples/order-more/bo/parcel-events.js +9 -0
- package/examples/order-more/bo/parcel-line-item.js +24 -0
- package/examples/order-more/bo/parcel-line-items.js +9 -0
- package/examples/order-more/bo/parcel.js +18 -0
- package/examples/order-more/bo/parcels.js +9 -0
- package/examples/order-more/bo/physical-address.js +30 -0
- package/examples/order-more/bo/physical-addresses.js +12 -0
- package/examples/order-more/bo/refund.js +28 -0
- package/examples/order-more/bo/refunds.js +9 -0
- package/examples/order-more/bo/utm-medium.js +13 -0
- package/examples/order-more/bo/utm-source.js +13 -0
- package/examples/order-more/business-objects.js +11 -1
- package/package.json +1 -1
- package/src/bo/base-bo.js +31 -18
- package/src/bo/base-bo.spec.js +180 -0
- package/test-utils/eleven/results.json +818 -0
- package/test-utils/nine/bo/base.js +5 -0
- package/test-utils/nine/bo/feature-switch.js +18 -0
- package/test-utils/nine/bo/feature-switches.js +12 -0
- package/test-utils/nine/business-objects.js +7 -0
- package/test-utils/nine/results.json +12 -0
- package/test-utils/ten/results.json +899 -0
- package/test-utils/twelve/bo/base.js +5 -0
- package/test-utils/twelve/bo/member.js +16 -0
- package/test-utils/twelve/bo/members.js +9 -0
- package/test-utils/twelve/bo/prompt.js +20 -0
- package/test-utils/twelve/bo/prompts.js +9 -0
- package/test-utils/twelve/business-objects.js +8 -0
- package/test-utils/twelve/results.json +8 -0
package/README.md
CHANGED
|
@@ -76,7 +76,7 @@ class PersonDAO extends BaseDAO {
|
|
|
76
76
|
And use it like this:
|
|
77
77
|
|
|
78
78
|
```javascript
|
|
79
|
-
const person = personDAO.get(55);
|
|
79
|
+
const person = await personDAO.get({ id: 55 });
|
|
80
80
|
console.log(person);
|
|
81
81
|
```
|
|
82
82
|
|
|
@@ -100,7 +100,7 @@ Person {
|
|
|
100
100
|
Job {
|
|
101
101
|
id: 278,
|
|
102
102
|
personId: 55,
|
|
103
|
-
employerId: 26
|
|
103
|
+
employerId: 26,
|
|
104
104
|
startDate: '2021-01-01',
|
|
105
105
|
endDate: '2021-12-31',
|
|
106
106
|
employer: Employer {
|
|
@@ -115,11 +115,37 @@ Person {
|
|
|
115
115
|
|
|
116
116
|
> This is a quick showcase. To see how to wire up this code, see the full [Practical Example](#practical-example) below.
|
|
117
117
|
|
|
118
|
-
Things to
|
|
118
|
+
### Things to Note:
|
|
119
119
|
|
|
120
|
-
- Our DAO returns a single Person business object which is properly structured from the relational row records!
|
|
120
|
+
- Our DAO returns a single Person business object which is properly structured from the many relational row records!
|
|
121
121
|
- Our query is executed with a `one` method. The DAO methods for `one`, `oneOrNone`, `many`, `any` ensure their count against the number of generated top level business objects - not the number of relational row records the sql expression returns!
|
|
122
|
-
- Rather than manually specifying our columns in the sql select expression, we use the business object's `getSQLSelectClause`. This is purely a convenience method which namespaces each column with the table name prefix to ensure column names don't collide.
|
|
122
|
+
- Rather than manually specifying our columns in the sql select expression, we use the business object's `getSQLSelectClause`. This is purely a convenience method which namespaces each column with the table name prefix to ensure column names don't collide (for example, the person, job, and employer `id`s would collide if not namespaced, as would person and employer `name`s). You are welcome to do this by hand instead of using the convenience methods (as were used above), if you don't mind the tedium:
|
|
123
|
+
```javascript
|
|
124
|
+
class PersonDAO extends BaseDAO {
|
|
125
|
+
get({ id }) {
|
|
126
|
+
// Example showing you can manually specific the select expression fields
|
|
127
|
+
// instead of using a business object's `getSQLSelectClause` convenience
|
|
128
|
+
// method. Note: you must namespace the field with table name and hashtag.
|
|
129
|
+
const query = `
|
|
130
|
+
SELECT
|
|
131
|
+
person.id as "person#id",
|
|
132
|
+
person.name as "person#name",
|
|
133
|
+
job.id as "job#id",
|
|
134
|
+
job.person_id: "job#person_id",
|
|
135
|
+
job.employer_id: "job#employer_id",
|
|
136
|
+
job.start_date: "job#start_date",
|
|
137
|
+
job.end_date: "job#end_date",
|
|
138
|
+
employer.id as "employer#id",
|
|
139
|
+
employer.name as "employer#name"
|
|
140
|
+
FROM person
|
|
141
|
+
JOIN job on person.id = job.person_id
|
|
142
|
+
JOIN employer on job.employer_id = employer.id
|
|
143
|
+
WHERE id = $(id)
|
|
144
|
+
`;
|
|
145
|
+
return this.one(query, { id });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
123
149
|
|
|
124
150
|
## Usage
|
|
125
151
|
|
|
@@ -160,18 +186,18 @@ const renderProfile = (req, res) => {
|
|
|
160
186
|
endDate: '2020-12-31',
|
|
161
187
|
employer: {
|
|
162
188
|
id: 17,
|
|
163
|
-
name: 'Good Corp'
|
|
189
|
+
name: 'Good Corp'
|
|
164
190
|
}
|
|
165
191
|
},
|
|
166
192
|
{
|
|
167
193
|
id: 278,
|
|
168
194
|
personId: 55,
|
|
169
|
-
employerId: 26
|
|
195
|
+
employerId: 26,
|
|
170
196
|
startDate: '2021-01-01',
|
|
171
197
|
endDate: '2021-12-31',
|
|
172
198
|
employer: {
|
|
173
199
|
id: 26,
|
|
174
|
-
name: 'Better Corp'
|
|
200
|
+
name: 'Better Corp'
|
|
175
201
|
}
|
|
176
202
|
}
|
|
177
203
|
]
|
|
@@ -233,7 +259,7 @@ const renderProfile = (req, res) => {
|
|
|
233
259
|
- {
|
|
234
260
|
- id: 278,
|
|
235
261
|
- personId: 55,
|
|
236
|
-
- employerId: 26
|
|
262
|
+
- employerId: 26,
|
|
237
263
|
- startDate: '2021-01-01',
|
|
238
264
|
- endDate: '2021-12-31',
|
|
239
265
|
- employer: {
|
|
@@ -244,8 +270,7 @@ const renderProfile = (req, res) => {
|
|
|
244
270
|
- ]
|
|
245
271
|
- }
|
|
246
272
|
- };
|
|
247
|
-
+ const
|
|
248
|
-
+ const person = personDAO.get({personId});
|
|
273
|
+
+ const person = personDAO.get({ id: req.params.id });
|
|
249
274
|
res.render('profile.html', person);
|
|
250
275
|
```
|
|
251
276
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const Base = require('./base');
|
|
2
|
+
const Customers = require('./customers');
|
|
3
|
+
|
|
4
|
+
class Customer extends Base {
|
|
5
|
+
get BoCollection() {
|
|
6
|
+
return Customers;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static get tableName() {
|
|
10
|
+
return 'customer';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static get sqlColumnsData() {
|
|
14
|
+
return ['id', 'email'];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = Customer;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const Base = require('./base');
|
|
2
|
+
const LineItems = require('./line-items');
|
|
3
|
+
const ProductVariant = require('./product-variant');
|
|
4
|
+
const Order = require('./order');
|
|
5
|
+
|
|
6
|
+
class LineItem extends Base {
|
|
7
|
+
get BoCollection() {
|
|
8
|
+
return LineItems;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static get tableName() {
|
|
12
|
+
return 'line_item';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static get sqlColumnsData() {
|
|
16
|
+
return [
|
|
17
|
+
'id',
|
|
18
|
+
{ column: 'product_variant_id', references: ProductVariant },
|
|
19
|
+
{ column: 'order_id', references: Order },
|
|
20
|
+
'fulfillment_status_id',
|
|
21
|
+
'fulfillable_quantity',
|
|
22
|
+
'fulfillment_service',
|
|
23
|
+
'grams',
|
|
24
|
+
'price',
|
|
25
|
+
'quantity',
|
|
26
|
+
'requires_shipping',
|
|
27
|
+
'taxable',
|
|
28
|
+
'total_discount'
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = LineItem;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const BaseBo = require('./base');
|
|
2
|
+
const Orders = require('./orders');
|
|
3
|
+
const UtmSource = require('./utm-source');
|
|
4
|
+
const Customer = require('./customer');
|
|
5
|
+
const PhysicalAddress = require('./physical-address');
|
|
6
|
+
|
|
7
|
+
class Order extends BaseBo {
|
|
8
|
+
get BoCollection() {
|
|
9
|
+
return Orders;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static get tableName() {
|
|
13
|
+
return 'order';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static get sqlColumnsData() {
|
|
17
|
+
return [
|
|
18
|
+
'id',
|
|
19
|
+
'email',
|
|
20
|
+
{ column: 'customer_id', references: Customer },
|
|
21
|
+
{ column: 'shipping_address_id', references: PhysicalAddress },
|
|
22
|
+
{ column: 'billing_address_id', references: PhysicalAddress },
|
|
23
|
+
'browser_ip',
|
|
24
|
+
'browser_user_agent',
|
|
25
|
+
'kujo_imported_date',
|
|
26
|
+
'created_date',
|
|
27
|
+
'cancel_reason',
|
|
28
|
+
'cancelled_date',
|
|
29
|
+
'closed_date',
|
|
30
|
+
'processed_date',
|
|
31
|
+
'updated_date',
|
|
32
|
+
'note',
|
|
33
|
+
'subtotal_price',
|
|
34
|
+
'taxes_included',
|
|
35
|
+
'total_discounts',
|
|
36
|
+
'total_price',
|
|
37
|
+
'total_tax',
|
|
38
|
+
'total_weight',
|
|
39
|
+
'order_status_url',
|
|
40
|
+
{ column: 'utm_source_id', references: UtmSource },
|
|
41
|
+
'utm_medium_id',
|
|
42
|
+
'utm_campaign',
|
|
43
|
+
'utm_content',
|
|
44
|
+
'utm_term'
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = Order;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const Base = require('./base');
|
|
2
|
+
const ParcelEvents = require('./parcel-events');
|
|
3
|
+
const Parcel = require('./parcel');
|
|
4
|
+
|
|
5
|
+
class ParcelEvent extends Base {
|
|
6
|
+
get BoCollection() {
|
|
7
|
+
return ParcelEvents;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static get tableName() {
|
|
11
|
+
return 'parcel_event';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static get sqlColumnsData() {
|
|
15
|
+
return ['id', { column: 'parcel_id', references: Parcel }, 'eta', 'status'];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = ParcelEvent;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const Base = require('./base');
|
|
2
|
+
const ParcelLineItems = require('./parcel-line-items');
|
|
3
|
+
const LineItem = require('./line-item');
|
|
4
|
+
const Parcel = require('./parcel');
|
|
5
|
+
|
|
6
|
+
class ParcelLineItem extends Base {
|
|
7
|
+
get BoCollection() {
|
|
8
|
+
return ParcelLineItems;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static get tableName() {
|
|
12
|
+
return 'parcel_line_item';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static get sqlColumnsData() {
|
|
16
|
+
return [
|
|
17
|
+
'id',
|
|
18
|
+
{ column: 'line_item_id', references: LineItem },
|
|
19
|
+
{ column: 'parcel_id', references: Parcel }
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = ParcelLineItem;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const BaseBo = require('./base');
|
|
2
|
+
const Parcels = require('./parcels');
|
|
3
|
+
|
|
4
|
+
class Parcel extends BaseBo {
|
|
5
|
+
get BoCollection() {
|
|
6
|
+
return Parcels;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static get tableName() {
|
|
10
|
+
return 'parcel';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static get sqlColumnsData() {
|
|
14
|
+
return ['id'];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = Parcel;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const Base = require('./base');
|
|
2
|
+
const PhysicalAddresses = require('./physical-addresses');
|
|
3
|
+
|
|
4
|
+
class PhysicalAddress extends Base {
|
|
5
|
+
get BoCollection() {
|
|
6
|
+
return PhysicalAddresses;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static get tableName() {
|
|
10
|
+
return 'physical_address';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static get sqlColumnsData() {
|
|
14
|
+
return [
|
|
15
|
+
'id',
|
|
16
|
+
'address1',
|
|
17
|
+
'address2',
|
|
18
|
+
'city',
|
|
19
|
+
'province',
|
|
20
|
+
'zip',
|
|
21
|
+
'country',
|
|
22
|
+
'province_code',
|
|
23
|
+
'country_code',
|
|
24
|
+
'latitude',
|
|
25
|
+
'longitude'
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = PhysicalAddress;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { BaseBoCollection } = require('../../../src/index');
|
|
2
|
+
|
|
3
|
+
class PhysicalAddresses extends BaseBoCollection {
|
|
4
|
+
static get displayName() {
|
|
5
|
+
return 'physicalAddresses';
|
|
6
|
+
}
|
|
7
|
+
static get Bo() {
|
|
8
|
+
return require('./physical-address'); // eslint-disable-line
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = PhysicalAddresses;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const Base = require('./base');
|
|
2
|
+
const Order = require('./order');
|
|
3
|
+
const Refunds = require('./refunds');
|
|
4
|
+
|
|
5
|
+
class Refund extends Base {
|
|
6
|
+
get BoCollection() {
|
|
7
|
+
return Refunds;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static get tableName() {
|
|
11
|
+
return 'refund';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static get sqlColumnsData() {
|
|
15
|
+
return [
|
|
16
|
+
'id',
|
|
17
|
+
{ column: 'order_id', references: Order },
|
|
18
|
+
'shopify_id',
|
|
19
|
+
'created_date',
|
|
20
|
+
'processed_date',
|
|
21
|
+
'kujo_imported_date',
|
|
22
|
+
'amount',
|
|
23
|
+
'note',
|
|
24
|
+
'restock'
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
module.exports = Refund;
|
|
@@ -10,7 +10,17 @@ const getBusinessObjects = () => [
|
|
|
10
10
|
require('./bo/color'),
|
|
11
11
|
require('./bo/gender'),
|
|
12
12
|
require('./bo/shipment'),
|
|
13
|
-
require('./bo/shipment-actual-product-variant')
|
|
13
|
+
require('./bo/shipment-actual-product-variant'),
|
|
14
|
+
require('./bo/refund'),
|
|
15
|
+
require('./bo/order'),
|
|
16
|
+
require('./bo/line-item'),
|
|
17
|
+
require('./bo/customer'),
|
|
18
|
+
require('./bo/physical-address'),
|
|
19
|
+
require('./bo/utm-source'),
|
|
20
|
+
require('./bo/utm-medium'),
|
|
21
|
+
require('./bo/parcel-line-item'),
|
|
22
|
+
require('./bo/parcel'),
|
|
23
|
+
require('./bo/parcel-event')
|
|
14
24
|
];
|
|
15
25
|
|
|
16
26
|
module.exports = getBusinessObjects;
|
package/package.json
CHANGED
package/src/bo/base-bo.js
CHANGED
|
@@ -147,7 +147,6 @@ module.exports = ({ getBusinessObjects }) =>
|
|
|
147
147
|
|
|
148
148
|
// Wowzer is this both CPU and Memory inefficient
|
|
149
149
|
clump.forEach(array => {
|
|
150
|
-
const oldestParentType = array[0].constructor;
|
|
151
150
|
array.forEach(_bo => {
|
|
152
151
|
const nodeAlreadySeen = nodes.find(
|
|
153
152
|
x =>
|
|
@@ -157,22 +156,36 @@ module.exports = ({ getBusinessObjects }) =>
|
|
|
157
156
|
const bo = nodeAlreadySeen || _bo;
|
|
158
157
|
const isNodeAlreadySeen = !!nodeAlreadySeen;
|
|
159
158
|
const nodePointingToIt = nodes.find(node => {
|
|
160
|
-
const
|
|
161
|
-
bo.constructor
|
|
162
|
-
|
|
163
|
-
if (
|
|
159
|
+
const indexes = Object.values(node.constructor.references)
|
|
160
|
+
.map((x, i) => (x === bo.constructor ? i : null))
|
|
161
|
+
.filter(x => x != null);
|
|
162
|
+
if (!indexes.length) {
|
|
164
163
|
return false;
|
|
165
164
|
}
|
|
166
|
-
const
|
|
167
|
-
|
|
165
|
+
for (const index of indexes) {
|
|
166
|
+
const property = Object.keys(node.constructor.references)[index];
|
|
167
|
+
if (node[property] === bo.id) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
168
172
|
});
|
|
173
|
+
// For first obj type which is has an instance in nodes array,
|
|
174
|
+
// get its index in nodes array
|
|
175
|
+
const indexOfOldestParent = array.reduce((answer, obj) => {
|
|
176
|
+
if (answer != null) {
|
|
177
|
+
return answer;
|
|
178
|
+
}
|
|
179
|
+
const index = nodes.findIndex(
|
|
180
|
+
n => n.constructor === obj.constructor
|
|
181
|
+
);
|
|
182
|
+
if (index !== -1) {
|
|
183
|
+
return index;
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}, null);
|
|
169
187
|
const parentHeirarchy = [
|
|
170
|
-
...nodes
|
|
171
|
-
.slice(
|
|
172
|
-
0,
|
|
173
|
-
nodes.findIndex(n => n.constructor === oldestParentType) + 1
|
|
174
|
-
)
|
|
175
|
-
.reverse(),
|
|
188
|
+
...nodes.slice(0, indexOfOldestParent + 1).reverse(),
|
|
176
189
|
root
|
|
177
190
|
];
|
|
178
191
|
const nodeItPointsTo = parentHeirarchy.find(parent => {
|
|
@@ -197,7 +210,7 @@ module.exports = ({ getBusinessObjects }) =>
|
|
|
197
210
|
// shown to be the parent (of parcel_events).
|
|
198
211
|
const ec = bo[nodePointingToIt.BoCollection.displayName];
|
|
199
212
|
if (ec && ec.models.find(m => m === nodePointingToIt)) {
|
|
200
|
-
|
|
213
|
+
nodes = [bo, ...nodes];
|
|
201
214
|
return;
|
|
202
215
|
}
|
|
203
216
|
}
|
|
@@ -270,13 +283,13 @@ module.exports = ({ getBusinessObjects }) =>
|
|
|
270
283
|
getSqlInsertParts() {
|
|
271
284
|
const columns = this.constructor.sqlColumns
|
|
272
285
|
.filter(
|
|
273
|
-
(column, index) => this[this.constructor.columns[index]]
|
|
286
|
+
(column, index) => this[this.constructor.columns[index]] !== void 0
|
|
274
287
|
)
|
|
275
288
|
.map(col => `"${col}"`)
|
|
276
289
|
.join(', ');
|
|
277
290
|
const values = this.constructor.columns
|
|
278
291
|
.map(column => this[column])
|
|
279
|
-
.filter(value => value
|
|
292
|
+
.filter(value => value !== void 0);
|
|
280
293
|
const valuesVar = values.map((value, index) => `$${index + 1}`);
|
|
281
294
|
return { columns, values, valuesVar };
|
|
282
295
|
}
|
|
@@ -284,14 +297,14 @@ module.exports = ({ getBusinessObjects }) =>
|
|
|
284
297
|
getSqlUpdateParts(on = 'id') {
|
|
285
298
|
const clauseArray = this.constructor.sqlColumns
|
|
286
299
|
.filter(
|
|
287
|
-
(sqlColumn, index) => this[this.constructor.columns[index]]
|
|
300
|
+
(sqlColumn, index) => this[this.constructor.columns[index]] !== void 0
|
|
288
301
|
)
|
|
289
302
|
.map((sqlColumn, index) => `"${sqlColumn}" = $${index + 1}`);
|
|
290
303
|
const clause = clauseArray.join(', ');
|
|
291
304
|
const idVar = `$${clauseArray.length + 1}`;
|
|
292
305
|
const _values = this.constructor.columns
|
|
293
306
|
.map(column => this[column])
|
|
294
|
-
.filter(value => value
|
|
307
|
+
.filter(value => value !== void 0);
|
|
295
308
|
const values = [..._values, this[on]];
|
|
296
309
|
return { clause, idVar, values };
|
|
297
310
|
}
|