-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
188 lines (153 loc) · 5.09 KB
/
index.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
const crypto = require('crypto');
const errors = require('@feathersjs/errors');
const { _ } = require('@feathersjs/commons');
const { select, AdapterService } = require('@feathersjs/adapter-commons');
const { nfcall, getSelect } = require('./utils');
// Create the service.
class Service extends AdapterService {
constructor (options) {
if (!options || !options.Model) {
throw new Error('NeDB datastore `Model` needs to be provided');
}
super(Object.assign({ id: '_id' }, options));
}
getModel (params) {
return this.options.Model;
}
multiOptions (id, params) {
const { query } = this.filterQuery(params);
const options = Object.assign({ multi: true }, params.nedb || params.options);
if (id !== null) {
options.multi = false;
query[this.id] = id;
}
return { query, options };
}
async _find (params = {}) {
// Start with finding all, and limit when necessary.
const { filters, query, paginate } = this.filterQuery(params);
let q = this.getModel(params).find(query);
// $select uses a specific find syntax, so it has to come first.
if (filters.$select) {
q = this.getModel(params).find(query, getSelect(filters.$select));
}
// Handle $sort
if (filters.$sort) {
q.sort(filters.$sort);
}
// Handle $limit
if (filters.$limit) {
q.limit(filters.$limit);
}
// Handle $skip
if (filters.$skip) {
q.skip(filters.$skip);
}
let runQuery = total => nfcall(q, 'exec').then(data => {
return {
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data: data.map(select(params, this.id))
};
});
if (filters.$limit === 0) {
runQuery = total => {
return Promise.resolve({
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data: []
});
};
}
if (paginate && paginate.default) {
return nfcall(this.getModel(params), 'count', query).then(runQuery);
}
return runQuery().then(page => page.data);
}
async _get (id, params = {}) {
const { query } = this.filterQuery(params);
const findOptions = Object.assign({ $and: [{ [this.id]: id }, query] });
return nfcall(this.getModel(params), 'findOne', findOptions)
.then(doc => {
if (!doc) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
return doc;
})
.then(select(params, this.id));
}
async _findOrGet (id, params = {}) {
if (id === null) {
return this._find(_.extend({}, params, {
paginate: false
}));
}
return this._get(id, params);
}
_create (raw, params = {}) {
const addId = item => {
if (this.id !== '_id' && item[this.id] === undefined) {
return Object.assign({
[this.id]: crypto.randomBytes(8).toString('hex')
}, item);
}
return item;
};
const data = Array.isArray(raw) ? raw.map(addId) : addId(raw);
return nfcall(this.getModel(params), 'insert', data)
.then(select(params, this.id));
}
_patch (id, data, params = {}) {
const { query, options } = this.multiOptions(id, params);
const mapIds = data => data.map(current => current[this.id]);
// By default we will just query for the one id. For multi patch
// we create a list of the ids of all items that will be changed
// to re-query them after the update
const ids = this._findOrGet(id, Object.assign({}, params, {
paginate: false
})).then(result => Array.isArray(result) ? result : [result]).then(mapIds);
// Run the query
return ids.then(idList => {
// Create a new query that re-queries all ids that
// were originally changed
const findParams = Object.assign({}, params, {
query: Object.assign({
[this.id]: { $in: idList }
}, query)
});
const updateData = Object.keys(data).reduce((result, key) => {
if (key.indexOf('$') === 0) {
result[key] = data[key];
} else if (key !== '_id' && key !== this.id) {
result.$set[key] = data[key];
}
return result;
}, { $set: {} });
return nfcall(this.getModel(params), 'update', query, updateData, options)
.then(() => this._findOrGet(id, findParams));
}).then(select(params, this.id));
}
_update (id, data, params = {}) {
const { query, options } = this.multiOptions(id, params);
const entry = _.omit(data, '_id');
if (this.id !== '_id' || (params.nedb && params.nedb.upsert)) {
entry[this.id] = id;
}
return nfcall(this.getModel(params), 'update', query, entry, options)
.then(() => this._findOrGet(id, params))
.then(select(params, this.id));
}
_remove (id, params = {}) {
const { query, options } = this.multiOptions(id, params);
return this._findOrGet(id, params).then(items =>
nfcall(this.getModel(params), 'remove', query, options)
.then(() => items)
).then(select(params, this.id));
}
}
module.exports = function init (options) {
return new Service(options);
};
module.exports.Service = Service;