-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.js
143 lines (113 loc) · 3.11 KB
/
schema.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const FilterError = require('./errors/filter');
const Model = require('./model');
const NField = require('./schemas/nfield');
const compose = require('koa-compose');
const kObservers = Symbol('observers');
const kObserverChain = Symbol('observerChain');
const kAttrs = Symbol('attrs');
class Schema {
constructor ({ name, fields = [], observers = [], modelClass = Model }) {
if (!name) {
throw new Error('Schema name is required');
}
this.name = name;
this.fields = [...fields];
this.modelClass = modelClass;
this[kObservers] = [];
this[kAttrs] = {};
observers.forEach(observer => this.addObserver(observer));
}
set (key, value) {
this[kAttrs][key] = value;
return this;
}
get (key) {
return this[kAttrs][key];
}
getField (name) {
return this.fields.find(f => f.name === name) || new NField(name);
}
addField (field) {
const existingField = this.fields.find(f => f.name === field.name);
if (existingField) {
throw new Error(`Duplicate field (${field.name})`);
}
this.fields.push(field);
}
addObserver (observer) {
if ('initialize' in observer) {
observer.initialize(this);
}
this[kObservers].push(observer);
// reset observer chain when observer added
this[kObserverChain] = undefined;
}
removeObserver (observer) {
const index = this[kObservers].indexOf(observer);
/* istanbul ignore if */
if (index === -1) {
return;
}
this[kObservers].splice(index, 1);
if ('uninitialize' in observer) {
observer.uninitialize(this);
}
// reset observer chain when observer removed
this[kObserverChain] = undefined;
}
attach (row, partial = false) {
const Model = this.modelClass;
this.fields.forEach(field => {
switch (row[field.name]) {
case undefined:
if (!partial) {
row[field.name] = null;
}
break;
case null:
row[field.name] = null;
break;
default:
row[field.name] = field.attach(row[field.name]);
}
});
return new Model(row);
}
observe (ctx, next) {
if (!this[kObserverChain]) {
const units = this[kObservers].map(observer => {
return (ctx, next) => {
if (typeof observer[ctx.mode] !== 'function') {
return next();
}
return observer[ctx.mode](ctx, next);
};
});
this[kObserverChain] = compose(units);
}
return this[kObserverChain](ctx, next);
}
async filter (row, { session, partial = false }) {
const error = new FilterError();
if (!row) {
error.add(new Error('Cannot filter empty row'));
throw error;
}
await Promise.all(this.fields.map(async field => {
try {
if (partial && row[field.name] === undefined) {
return;
}
row[field.name] = await field.execFilter(row[field.name], { session, row, schema: this });
} catch (err) {
err.field = field;
error.add(err);
}
}));
if (!error.empty()) {
throw error;
}
return row;
}
}
module.exports = Schema;