This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
query.js
154 lines (115 loc) · 4.21 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
'use strict';
const isObject = require('is-plain-obj');
const queryMethods = require('./query-methods');
class Query {
constructor(modelClass) {
this.Model = modelClass;
this.query = [];
}
find(query = {}) {
if (typeof query !== 'undefined' && typeof query !== 'object') {
throw new TypeError(`Expected \`query\` to be object or undefined, got ${typeof query}`);
}
this.where(query);
return this.Model.query('find', this.query)
.then(documents => {
return documents.map(doc => new this.Model(doc));
});
}
findOne(query = {}) {
if (typeof query !== 'undefined' && typeof query !== 'object') {
throw new TypeError(`Expected \`query\` to be object or undefined, got ${typeof query}`);
}
this.where(query);
return this.Model.query('findOne', this.query)
.then(doc => doc ? new this.Model(doc) : null);
}
findOneAndUpdate(query = {}, updateDocument = {}, options = {new: true}) {
if (typeof query !== 'undefined' && typeof query !== 'object') {
throw new TypeError(`Expected \`query\` to be object or undefined, got ${typeof query}`);
}
if (typeof updateDocument !== 'object') {
throw new TypeError(`Expected \`updateDocument\` to be object, got ${typeof updateDocument}`);
}
if (typeof options !== 'undefined' && typeof options !== 'object') {
throw new TypeError(`Expected \`options\` to be object or undefined, got ${typeof options}`);
}
this.query.push(['findOneAndUpdate', [query, updateDocument, options]]);
return this.Model.query('findOneAndUpdate', this.query)
.then(doc => doc && doc.value ? new this.Model(doc.value) : null);
}
findById(id) {
if (typeof id !== 'object' && typeof id !== 'string') {
throw new TypeError(`Expected \`id\` to be object or string, got ${typeof id}`);
}
this.where('_id', id);
return this.Model.query('findOne', this.query)
.then(doc => doc ? new this.Model(doc) : null);
}
remove(query = {}) {
if (typeof query !== 'undefined' && typeof query !== 'object') {
throw new TypeError(`Expected \`query\` to be object or undefined, got ${typeof query}`);
}
this.where(query);
return this.Model.query('remove', this.query);
}
count(query = {}) {
if (typeof query !== 'undefined' && typeof query !== 'object') {
throw new TypeError(`Expected \`query\` to be object or undefined, got ${typeof query}`);
}
this.where(query);
return this.Model.query('count', this.query);
}
include(field, value = 1) {
if (!Array.isArray(field) && typeof field !== 'object' && typeof field !== 'string') {
throw new TypeError(`Expected \`field\` to be array, object or string, got ${typeof field}`);
}
if (typeof value !== 'number') {
throw new TypeError(`Expected \`value\` to be number, got ${typeof value}`);
}
if (Array.isArray(field)) {
field.forEach(field => this.include(field));
return this;
}
const select = isObject(field) ? field : {[field]: value};
this.query.push(['select', select]);
return this;
}
exclude(field, value = 0) {
if (!Array.isArray(field) && typeof field !== 'object' && typeof field !== 'string') {
throw new TypeError(`Expected \`field\` to be array, object or string, got ${typeof field}`);
}
if (typeof value !== 'number') {
throw new TypeError(`Expected \`value\` to be number, got ${typeof value}`);
}
if (Array.isArray(field)) {
field.forEach(field => this.exclude(field));
return this;
}
const select = isObject(field) ? field : {[field]: value};
this.query.push(['select', select]);
return this;
}
sort(field, value = 'desc') {
if (!Array.isArray(field) && typeof field !== 'object' && typeof field !== 'string') {
throw new TypeError(`Expected \`field\` to be array, object or string, got ${typeof field}`);
}
if (typeof value !== 'string' && typeof value !== 'number') {
throw new TypeError(`Expected \`value\` to be string or number, got ${typeof value}`);
}
if (Array.isArray(field)) {
field.forEach(field => this.sort(field));
return this;
}
const sort = isObject(field) ? field : {[field]: value};
this.query.push(['sort', sort]);
return this;
}
}
queryMethods.forEach(name => {
Query.prototype[name] = function (...args) {
this.query.push([name, args]);
return this;
};
});
module.exports = Query;