blackhole.db 1.1.1 → 2.0.1
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 +51 -52
- package/index.node +0 -0
- package/package.json +2 -5
package/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
const native = require('./index.node');
|
|
2
|
-
const { v4: uuidv4 } = require('uuid');
|
|
3
2
|
|
|
4
3
|
const _internal = new WeakMap();
|
|
5
4
|
|
|
6
|
-
/**
|
|
7
|
-
* 1. GLOBAL BLACKHOLE FUNCTIONS
|
|
8
|
-
*/
|
|
9
5
|
const blackhole = {
|
|
10
6
|
models: {},
|
|
11
7
|
connect: async (path = 'blackhole.db') => {
|
|
@@ -13,7 +9,7 @@ const blackhole = {
|
|
|
13
9
|
return blackhole.connection;
|
|
14
10
|
},
|
|
15
11
|
createConnection: (path) => blackhole.connect(path),
|
|
16
|
-
disconnect: async () => {},
|
|
12
|
+
disconnect: async () => {},
|
|
17
13
|
model: (name, schema) => {
|
|
18
14
|
if (!schema) return blackhole.models[name];
|
|
19
15
|
return blackhole.models[name] = new Model(name, schema);
|
|
@@ -40,36 +36,48 @@ const blackhole = {
|
|
|
40
36
|
};
|
|
41
37
|
}
|
|
42
38
|
},
|
|
43
|
-
Types: { ObjectId: () =>
|
|
39
|
+
Types: { ObjectId: (v) => (v != null ? String(v) : undefined) },
|
|
44
40
|
connection: { on: () => {}, once: () => {} },
|
|
45
41
|
set: () => {},
|
|
46
42
|
pluralize: (v) => v
|
|
47
43
|
};
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
blackhole.Schema.Types = blackhole.Types;
|
|
46
|
+
function normalizeProjection(p) {
|
|
47
|
+
if (!p) return null;
|
|
48
|
+
if (typeof p === 'string') {
|
|
49
|
+
const obj = {};
|
|
50
|
+
for (const f of p.split(/\s+/).filter(Boolean)) {
|
|
51
|
+
if (f.startsWith('-')) obj[f.slice(1)] = 0;
|
|
52
|
+
else obj[f] = 1;
|
|
53
|
+
}
|
|
54
|
+
return obj;
|
|
55
|
+
}
|
|
56
|
+
return p;
|
|
57
|
+
}
|
|
58
|
+
|
|
52
59
|
class Query {
|
|
53
|
-
constructor(model, filter, op) {
|
|
54
|
-
this.model = model; this._filter = filter
|
|
55
|
-
this._limit =
|
|
60
|
+
constructor(model, filter, op, projection) {
|
|
61
|
+
this.model = model; this._filter = filter; this._op = op;
|
|
62
|
+
this._limit = -1; this._skip = -1; this._sort = null; this._isLean = false;
|
|
63
|
+
this._projection = normalizeProjection(projection);
|
|
56
64
|
}
|
|
65
|
+
select(p) { this._projection = normalizeProjection(p); return this; }
|
|
57
66
|
limit(n) { this._limit = n; return this; }
|
|
58
67
|
skip(n) { this._skip = n; return this; }
|
|
59
68
|
sort(s) { this._sort = s; return this; }
|
|
60
69
|
lean(v = true) { this._isLean = v; return this; }
|
|
61
70
|
|
|
62
71
|
async exec() {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
72
|
+
const filter = this._op === 'findById' ? { _id: this._filter } : (this._filter || {});
|
|
73
|
+
const raw = JSON.parse(native.find(
|
|
74
|
+
this.model._collection,
|
|
75
|
+
JSON.stringify(filter),
|
|
76
|
+
JSON.stringify(this._projection || {}),
|
|
77
|
+
JSON.stringify(this._sort || {}),
|
|
78
|
+
this._skip,
|
|
79
|
+
this._limit,
|
|
80
|
+
));
|
|
73
81
|
|
|
74
82
|
if (this._op === 'findOne' || this._op === 'findById') {
|
|
75
83
|
const res = raw[0] || null;
|
|
@@ -79,31 +87,26 @@ class Query {
|
|
|
79
87
|
return this._isLean ? raw : raw.map(r => this.model._wrap(r));
|
|
80
88
|
}
|
|
81
89
|
then(res, rej) { return this.exec().then(res, rej); }
|
|
90
|
+
catch(rej) { return this.exec().catch(rej); }
|
|
82
91
|
}
|
|
83
92
|
|
|
84
|
-
/**
|
|
85
|
-
* 4. DOCUMENT FUNCTIONS (Instance)
|
|
86
|
-
*/
|
|
87
93
|
class Document {
|
|
88
94
|
constructor(data, model) {
|
|
89
|
-
const id = data._id
|
|
90
|
-
const key = data._key || model._keyFor(id);
|
|
95
|
+
const id = data._id != null ? String(data._id) : null;
|
|
91
96
|
|
|
92
|
-
// Create Proxy first to avoid state binding issues
|
|
93
97
|
const proxy = new Proxy(this, {
|
|
94
98
|
set: (t, p, v) => {
|
|
95
99
|
const state = _internal.get(proxy);
|
|
96
|
-
if (p === 'id' || p === '_id') return true;
|
|
100
|
+
if (p === 'id' || p === '_id') return true;
|
|
97
101
|
if (state) state.modified.add(p);
|
|
98
102
|
t[p] = v; return true;
|
|
99
103
|
}
|
|
100
104
|
});
|
|
101
105
|
|
|
102
|
-
_internal.set(proxy, { model, id,
|
|
106
|
+
_internal.set(proxy, { model, id, modified: new Set(), isNew: id == null });
|
|
103
107
|
|
|
104
|
-
// Initialize values into proxy (filtering out getters)
|
|
105
108
|
for (let [k, v] of Object.entries(data)) {
|
|
106
|
-
if (k !== '_id' && k !== 'id'
|
|
109
|
+
if (k !== '_id' && k !== 'id') proxy[k] = v;
|
|
107
110
|
}
|
|
108
111
|
|
|
109
112
|
for (let [n, f] of Object.entries(model.schema.methods)) this[n] = f.bind(proxy);
|
|
@@ -117,15 +120,19 @@ class Document {
|
|
|
117
120
|
const state = _internal.get(this);
|
|
118
121
|
await state.model._runHooks('pre', 'save', this);
|
|
119
122
|
const data = this.toObject();
|
|
120
|
-
|
|
121
|
-
|
|
123
|
+
const id = native.save(state.model._collection, state.id || '', JSON.stringify(data));
|
|
124
|
+
state.id = id;
|
|
125
|
+
state.isNew = false;
|
|
122
126
|
state.modified.clear();
|
|
123
127
|
await state.model._runHooks('post', 'save', this);
|
|
124
128
|
return this;
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
async updateOne(u) { Object.assign(this, u); return this.save(); }
|
|
128
|
-
async deleteOne() {
|
|
132
|
+
async deleteOne() {
|
|
133
|
+
const state = _internal.get(this);
|
|
134
|
+
return native.delete_by_id(state.model._collection, state.id);
|
|
135
|
+
}
|
|
129
136
|
isModified(p) { return _internal.get(this).modified.has(p); }
|
|
130
137
|
markModified(p) { _internal.get(this).modified.add(p); }
|
|
131
138
|
validate() { return true; }
|
|
@@ -146,20 +153,14 @@ class Document {
|
|
|
146
153
|
return o;
|
|
147
154
|
}
|
|
148
155
|
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 3. MODEL FUNCTIONS (Statics)
|
|
152
|
-
*/
|
|
153
156
|
class Model {
|
|
154
157
|
constructor(name, schema) {
|
|
155
|
-
this.name = name; this.schema = schema; this.
|
|
158
|
+
this.name = name; this.schema = schema; this._collection = name;
|
|
156
159
|
Object.assign(this, schema.statics);
|
|
157
160
|
}
|
|
158
|
-
_keyFor(id) { return `${this._prefix}${id}`; }
|
|
159
161
|
_wrap(raw) { return new Document(raw, this); }
|
|
160
162
|
async _runHooks(p, e, d) { for (let f of this.schema._hooks[p][e] ?? []) await f.call(d); }
|
|
161
163
|
|
|
162
|
-
// Create
|
|
163
164
|
async create(data) {
|
|
164
165
|
if (Array.isArray(data)) return Promise.all(data.map(d => this.create(d)));
|
|
165
166
|
const doc = new Document(data, this);
|
|
@@ -167,17 +168,14 @@ class Model {
|
|
|
167
168
|
}
|
|
168
169
|
async insertMany(arr) { return this.create(arr); }
|
|
169
170
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
async countDocuments(f) { return (await this.find(f)).length; }
|
|
171
|
+
find(f, projection) { return new Query(this, f, 'find', projection); }
|
|
172
|
+
findOne(f, projection) { return new Query(this, f, 'findOne', projection); }
|
|
173
|
+
findById(id, projection) { return new Query(this, id, 'findById', projection); }
|
|
174
|
+
async countDocuments(f) { return native.count(this._collection, JSON.stringify(f || {})); }
|
|
175
175
|
async distinct(field) {
|
|
176
176
|
const all = await this.find({}).lean();
|
|
177
177
|
return [...new Set(all.map(x => x[field]))];
|
|
178
178
|
}
|
|
179
|
-
|
|
180
|
-
// Update
|
|
181
179
|
async updateOne(f, u) {
|
|
182
180
|
const d = await this.findOne(f).exec();
|
|
183
181
|
if (!d) return { matchedCount: 0 };
|
|
@@ -196,7 +194,6 @@ class Model {
|
|
|
196
194
|
}
|
|
197
195
|
findByIdAndUpdate(id, u, o) { return this.findOneAndUpdate({ _id: id }, u, o); }
|
|
198
196
|
|
|
199
|
-
// Delete
|
|
200
197
|
async deleteOne(f) {
|
|
201
198
|
const d = await this.findOne(f).exec();
|
|
202
199
|
if (!d) return { deletedCount: 0 };
|
|
@@ -213,8 +210,10 @@ class Model {
|
|
|
213
210
|
}
|
|
214
211
|
findByIdAndDelete(id) { return this.findOneAndDelete({ _id: id }); }
|
|
215
212
|
|
|
216
|
-
|
|
217
|
-
|
|
213
|
+
async aggregate(pipeline) {
|
|
214
|
+
const raw = native.aggregate(this._collection, JSON.stringify(pipeline || []));
|
|
215
|
+
return JSON.parse(raw);
|
|
216
|
+
}
|
|
218
217
|
async bulkWrite(ops) {
|
|
219
218
|
for (let o of ops) {
|
|
220
219
|
if (o.insertOne) await this.create(o.insertOne.document);
|
package/index.node
CHANGED
|
Binary file
|