-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.js
195 lines (152 loc) · 4.55 KB
/
query.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const Context = require('./context');
class Query {
constructor ({ session, connection, schema, criteria }) {
this.session = session;
this.connection = connection;
this.schema = schema;
this.find(criteria);
this.rows = [];
this.sets = {};
this.length = -1;
this.offset = 0;
this.sorts = undefined;
}
clone () {
const query = new Query(this);
query.length = this.length;
query.offset = this.offset;
query.sorts = this.sorts;
return query;
}
find (criteria = {}) {
this.criteria = typeof criteria === 'object' ? criteria : { id: criteria };
return this;
}
insert (row) {
this.mode = 'insert';
this.rows.push(this.schema.attach(row));
return this;
}
sort (sorts) {
this.sorts = sorts;
return this;
}
limit (length) {
this.length = length;
return this;
}
skip (offset) {
this.offset = offset;
return this;
}
set (set) {
this.mode = 'update';
this.sets = this.schema.attach(set, true);
return this;
}
async delete ({ observer = true } = {}) {
this.mode = 'delete';
const conDelete = async () => {
const connection = await this.session.acquire(this.connection);
const result = await connection.delete(this);
return result;
};
const ctx = new Context({ query: this });
if (observer) {
await this.schema.observe(ctx, ctx => conDelete(ctx));
} else {
await conDelete(ctx);
}
return ctx.result;
}
async save ({ filter = true, observer = true } = {}) {
const ctx = new Context({ query: this, filter });
const conSave = async (ctx) => {
const connection = await this.session.acquire(this.connection);
const { session } = this;
const { filter } = ctx;
if (this.mode === 'insert') {
if (this.rows.length === 0) {
throw new Error('Failed to insert empty rows');
}
if (filter) {
await Promise.all(this.rows.map(row => this.schema.filter(row, { session })));
}
const rows = [];
this.affected = await connection.insert(this, row => rows.push(this.schema.attach(row)));
this.rows = rows;
return;
}
if (this.mode === 'update') {
if (Object.keys(this.sets).length === 0) {
throw new Error('Failed to update empty set');
}
if (filter) {
const partial = true;
await this.schema.filter(this.sets, { session, partial });
}
if (Object.keys(this.sets).length === 0) {
throw new Error('Neither insert and update to save');
}
this.affected = await connection.update(this);
return;
}
throw new Error(`Invalid mode=${this.mode} to save`);
};
if (observer) {
await this.schema.observe(ctx, ctx => conSave(ctx));
} else {
await conSave(ctx);
}
return this;
}
async drop () {
const connection = await this.session.acquire(this.connection);
const result = await connection.drop(this);
return result;
}
async truncate () {
const connection = await this.session.acquire(this.connection);
const result = await connection.truncate(this);
return result;
}
async all () {
const rows = [];
const connection = await this.session.acquire(this.connection);
await connection.load(this, row => rows.push(this.schema.attach(row)));
return rows;
}
async count (useSkipAndLimit = false) {
const connection = await this.session.acquire(this.connection);
if (typeof connection.count !== 'function') {
throw new Error('Connection does not implement count()');
}
return connection.count(this, useSkipAndLimit);
}
async single () {
const [row] = await this.limit(1).all();
return row;
}
async defined () {
const connection = await this.session.acquire(this.connection);
if (typeof connection.defined !== 'function') {
throw new Error('Connection does not implement defined()');
}
return connection.defined(this.schema);
}
async define () {
const connection = await this.session.acquire(this.connection);
if (typeof connection.define !== 'function') {
throw new Error('Connection does not implement define()');
}
return connection.define(this.schema);
}
async undefine () {
const connection = await this.session.acquire(this.connection);
if (typeof connection.undefine !== 'function') {
throw new Error('Connection does not implement undefine()');
}
return connection.undefine(this.schema);
}
}
module.exports = Query;