-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdb2.js
66 lines (55 loc) · 1.99 KB
/
db2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const rdb = require('../src/index');
const nameSchema = {
type: 'string',
};
function validateName(value) {
if (value && value.length > 10)
throw new Error('Length cannot exceed 10 characters');
}
function truthy(value) {
if (!value)
throw new Error('Name must be set');
}
const map = rdb.map(x => ({
customer: x.table('customer').map(({ column }) => ({
id: column('id').numeric().primary().notNullExceptInsert(),
name: column('name').string().validate(validateName).validate(truthy).JSONSchema(nameSchema),
balance: column('balance').numeric(),
isActive: column('isActive').boolean(),
})),
package: x.table('package').map(({ column }) => ({
id: column('packageId').numeric().primary().notNullExceptInsert(),
lineId: column('lineId').numeric().notNullExceptInsert(),
sscc: column('sscc').string()
})),
order: x.table('order').map(({ column }) => ({
id: column('id').numeric().primary().notNullExceptInsert(),
orderDate: column('orderDate').date().notNull(),
customerId: column('customerId').numeric().notNullExceptInsert(),
})),
orderLine: x.table('orderLine').map(({ column }) => ({
id: column('id').numeric().primary().notNullExceptInsert(),
orderId: column('orderId').numeric(),
product: column('product').string(),
})),
deliveryAddress: x.table('deliveryAddress').map(({ column }) => ({
id: column('id').numeric().primary().notNullExceptInsert(),
orderId: column('orderId').numeric(),
name: column('name').string(),
street: column('street').string(),
postalCode: column('postalCode').string(),
postalPlace: column('postalPlace').string(),
countryCode: column('countryCode').string(),
}))
})).map(x => ({
orderLine: x.orderLine.map(({ hasMany }) => ({
packages: hasMany(x.package).by('lineId')
}))
})).map(x => ({
order: x.order.map(({ hasOne, hasMany, references }) => ({
customer: references(x.customer).by('customerId'),
deliveryAddress: hasOne(x.deliveryAddress).by('orderId'),
lines: hasMany(x.orderLine).by('orderId')
}))
}));
module.exports = map;