blackhole.db 2.0.0 → 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 +11 -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,13 +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
|
|
|
47
|
+
blackhole.Schema.Types = blackhole.Types;
|
|
48
|
+
|
|
45
49
|
function normalizeProjection(p) {
|
|
46
50
|
if (!p) return null;
|
|
47
51
|
if (typeof p === 'string') {
|
|
@@ -152,6 +156,7 @@ class Document {
|
|
|
152
156
|
return o;
|
|
153
157
|
}
|
|
154
158
|
}
|
|
159
|
+
|
|
155
160
|
class Model {
|
|
156
161
|
constructor(name, schema) {
|
|
157
162
|
this.name = name; this.schema = schema; this._collection = name;
|
|
@@ -160,7 +165,7 @@ class Model {
|
|
|
160
165
|
_wrap(raw) { return new Document(raw, this); }
|
|
161
166
|
async _runHooks(p, e, d) { for (let f of this.schema._hooks[p][e] ?? []) await f.call(d); }
|
|
162
167
|
|
|
163
|
-
|
|
168
|
+
async create(data) {
|
|
164
169
|
if (Array.isArray(data)) return Promise.all(data.map(d => this.create(d)));
|
|
165
170
|
const doc = new Document(data, this);
|
|
166
171
|
return doc.save();
|
|
@@ -175,7 +180,8 @@ class Model {
|
|
|
175
180
|
const all = await this.find({}).lean();
|
|
176
181
|
return [...new Set(all.map(x => x[field]))];
|
|
177
182
|
}
|
|
178
|
-
|
|
183
|
+
|
|
184
|
+
async updateOne(f, u) {
|
|
179
185
|
const d = await this.findOne(f).exec();
|
|
180
186
|
if (!d) return { matchedCount: 0 };
|
|
181
187
|
Object.assign(d, u); await d.save();
|
|
@@ -193,7 +199,7 @@ class Model {
|
|
|
193
199
|
}
|
|
194
200
|
findByIdAndUpdate(id, u, o) { return this.findOneAndUpdate({ _id: id }, u, o); }
|
|
195
201
|
|
|
196
|
-
|
|
202
|
+
async deleteOne(f) {
|
|
197
203
|
const d = await this.findOne(f).exec();
|
|
198
204
|
if (!d) return { deletedCount: 0 };
|
|
199
205
|
await d.deleteOne(); return { deletedCount: 1 };
|
|
@@ -209,7 +215,7 @@ class Model {
|
|
|
209
215
|
}
|
|
210
216
|
findByIdAndDelete(id) { return this.findOneAndDelete({ _id: id }); }
|
|
211
217
|
|
|
212
|
-
|
|
218
|
+
async aggregate(pipeline) {
|
|
213
219
|
const raw = native.aggregate(this._collection, JSON.stringify(pipeline || []));
|
|
214
220
|
return JSON.parse(raw);
|
|
215
221
|
}
|
package/package.json
CHANGED