blackhole.db 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +10 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const native = require('./index.node');
|
|
|
2
2
|
|
|
3
3
|
const _internal = new WeakMap();
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
const blackhole = {
|
|
6
7
|
models: {},
|
|
7
8
|
connect: async (path = 'blackhole.db') => {
|
|
@@ -35,14 +36,16 @@ const blackhole = {
|
|
|
35
36
|
set(fn) { this.setter = fn; return this; }
|
|
36
37
|
};
|
|
37
38
|
}
|
|
39
|
+
index(fields, opts) { (this._indexes ??= []).push({ fields, opts }); return this; }
|
|
38
40
|
},
|
|
39
|
-
Types: { ObjectId: (v) => (v != null ? String(v) : undefined) },
|
|
41
|
+
Types: { ObjectId: (v) => (v != null ? String(v) : undefined), Mixed: 'Mixed' },
|
|
40
42
|
connection: { on: () => {}, once: () => {} },
|
|
41
43
|
set: () => {},
|
|
42
44
|
pluralize: (v) => v
|
|
43
45
|
};
|
|
44
46
|
|
|
45
47
|
blackhole.Schema.Types = blackhole.Types;
|
|
48
|
+
|
|
46
49
|
function normalizeProjection(p) {
|
|
47
50
|
if (!p) return null;
|
|
48
51
|
if (typeof p === 'string') {
|
|
@@ -153,6 +156,7 @@ class Document {
|
|
|
153
156
|
return o;
|
|
154
157
|
}
|
|
155
158
|
}
|
|
159
|
+
|
|
156
160
|
class Model {
|
|
157
161
|
constructor(name, schema) {
|
|
158
162
|
this.name = name; this.schema = schema; this._collection = name;
|
|
@@ -161,7 +165,7 @@ class Model {
|
|
|
161
165
|
_wrap(raw) { return new Document(raw, this); }
|
|
162
166
|
async _runHooks(p, e, d) { for (let f of this.schema._hooks[p][e] ?? []) await f.call(d); }
|
|
163
167
|
|
|
164
|
-
|
|
168
|
+
async create(data) {
|
|
165
169
|
if (Array.isArray(data)) return Promise.all(data.map(d => this.create(d)));
|
|
166
170
|
const doc = new Document(data, this);
|
|
167
171
|
return doc.save();
|
|
@@ -176,7 +180,8 @@ class Model {
|
|
|
176
180
|
const all = await this.find({}).lean();
|
|
177
181
|
return [...new Set(all.map(x => x[field]))];
|
|
178
182
|
}
|
|
179
|
-
|
|
183
|
+
|
|
184
|
+
async updateOne(f, u) {
|
|
180
185
|
const d = await this.findOne(f).exec();
|
|
181
186
|
if (!d) return { matchedCount: 0 };
|
|
182
187
|
Object.assign(d, u); await d.save();
|
|
@@ -194,7 +199,7 @@ class Model {
|
|
|
194
199
|
}
|
|
195
200
|
findByIdAndUpdate(id, u, o) { return this.findOneAndUpdate({ _id: id }, u, o); }
|
|
196
201
|
|
|
197
|
-
|
|
202
|
+
async deleteOne(f) {
|
|
198
203
|
const d = await this.findOne(f).exec();
|
|
199
204
|
if (!d) return { deletedCount: 0 };
|
|
200
205
|
await d.deleteOne(); return { deletedCount: 1 };
|
|
@@ -210,7 +215,7 @@ class Model {
|
|
|
210
215
|
}
|
|
211
216
|
findByIdAndDelete(id) { return this.findOneAndDelete({ _id: id }); }
|
|
212
217
|
|
|
213
|
-
|
|
218
|
+
async aggregate(pipeline) {
|
|
214
219
|
const raw = native.aggregate(this._collection, JSON.stringify(pipeline || []));
|
|
215
220
|
return JSON.parse(raw);
|
|
216
221
|
}
|
package/package.json
CHANGED