blackhole.db 2.0.1 → 3.0.0
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/collection.js +299 -0
- package/document.js +476 -0
- package/index.js +191 -210
- package/lib/engine.js +861 -0
- package/lib/errors.js +94 -0
- package/lib/objectid.js +50 -0
- package/lib/schema.js +555 -0
- package/model.js +260 -0
- package/package.json +1 -1
- package/query.js +551 -0
package/lib/errors.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class blackholeError extends Error {
|
|
4
|
+
constructor(msg) { super(msg); this.name = 'blackholeError'; }
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
class CastError extends blackholeError {
|
|
8
|
+
constructor(type, value, path, reason) {
|
|
9
|
+
const shown = typeof value === 'object' && value !== null ? safeJson(value) : String(value);
|
|
10
|
+
super(`Cast to ${type} failed for value ${typeof value === 'string' ? JSON.stringify(value) : shown} (type ${typeOf(value)}) at path "${path}"${reason ? ` because of "${reason.name}"` : ''}`);
|
|
11
|
+
this.name = 'CastError';
|
|
12
|
+
this.stringValue = typeof value === 'string' ? JSON.stringify(value) : shown;
|
|
13
|
+
this.messageFormat = undefined;
|
|
14
|
+
this.kind = type;
|
|
15
|
+
this.value = value;
|
|
16
|
+
this.path = path;
|
|
17
|
+
this.reason = reason;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class ValidatorError extends blackholeError {
|
|
22
|
+
constructor(properties) {
|
|
23
|
+
super(properties.message);
|
|
24
|
+
this.name = 'ValidatorError';
|
|
25
|
+
this.properties = properties;
|
|
26
|
+
this.kind = properties.type;
|
|
27
|
+
this.path = properties.path;
|
|
28
|
+
this.value = properties.value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class ValidationError extends blackholeError {
|
|
33
|
+
constructor(doc) {
|
|
34
|
+
super(doc && doc.$__name ? `${doc.$__name} validation failed` : 'Validation failed');
|
|
35
|
+
this.name = 'ValidationError';
|
|
36
|
+
this.errors = {};
|
|
37
|
+
}
|
|
38
|
+
addError(path, error) {
|
|
39
|
+
this.errors[path] = error;
|
|
40
|
+
this.message = `${this.message.split(': ')[0]}: ${Object.values(this.errors).map((e) => `${e.path}: ${e.message}`).join(', ')}`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class DocumentNotFoundError extends blackholeError {
|
|
45
|
+
constructor(filter, model) {
|
|
46
|
+
super(`No document found for query "${safeJson(filter)}" on model "${model}"`);
|
|
47
|
+
this.name = 'DocumentNotFoundError';
|
|
48
|
+
this.filter = filter;
|
|
49
|
+
this.query = filter;
|
|
50
|
+
this.numAffected = 0;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
class DuplicateKeyError extends Error {
|
|
55
|
+
constructor(collection, keyPattern, keyValue) {
|
|
56
|
+
const keys = Object.keys(keyValue);
|
|
57
|
+
super(`E11000 duplicate key error collection: ${collection} index: ${keys.map((k) => `${k}_1`).join('_')} dup key: ${safeJson(keyValue)}`);
|
|
58
|
+
this.name = 'blackholeServerError';
|
|
59
|
+
this.code = 11000;
|
|
60
|
+
this.keyPattern = keyPattern;
|
|
61
|
+
this.keyValue = keyValue;
|
|
62
|
+
this.index = 0;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
class StrictModeError extends blackholeError {
|
|
67
|
+
constructor(path) { super(`Field \`${path}\` is not in schema and strict mode is set to throw.`); this.name = 'StrictModeError'; this.path = path; }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
class ObjectParameterError extends blackholeError {
|
|
71
|
+
constructor(value, paramName, fnName) {
|
|
72
|
+
super(`Parameter "${paramName}" to ${fnName}() must be an object, got "${String(value)}" (type ${typeOf(value)})`);
|
|
73
|
+
this.name = 'ObjectParameterError';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function typeOf(v) {
|
|
78
|
+
if (v === null) return 'null';
|
|
79
|
+
if (Array.isArray(v)) return 'Array';
|
|
80
|
+
if (v instanceof Date) return 'Date';
|
|
81
|
+
if (typeof v === 'object') return 'Object';
|
|
82
|
+
return typeof v === 'number' ? 'number' : typeof v === 'string' ? 'string' : typeof v;
|
|
83
|
+
}
|
|
84
|
+
function safeJson(v) { try { return JSON.stringify(v); } catch { return String(v); } }
|
|
85
|
+
|
|
86
|
+
blackholeError.CastError = CastError;
|
|
87
|
+
blackholeError.ValidationError = ValidationError;
|
|
88
|
+
blackholeError.ValidatorError = ValidatorError;
|
|
89
|
+
blackholeError.DocumentNotFoundError = DocumentNotFoundError;
|
|
90
|
+
blackholeError.StrictModeError = StrictModeError;
|
|
91
|
+
blackholeError.ObjectParameterError = ObjectParameterError;
|
|
92
|
+
blackholeError.blackholeError = blackholeError;
|
|
93
|
+
|
|
94
|
+
module.exports = { blackholeError, CastError, ValidatorError, ValidationError, DocumentNotFoundError, DuplicateKeyError, StrictModeError, ObjectParameterError };
|
package/lib/objectid.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
|
|
4
|
+
const PROCESS = crypto.randomBytes(5).toString('hex');
|
|
5
|
+
let counter = crypto.randomBytes(3).readUIntBE(0, 3);
|
|
6
|
+
const HEX24 = /^[0-9a-fA-F]{24}$/;
|
|
7
|
+
|
|
8
|
+
function generate(time) {
|
|
9
|
+
const secs = (time ?? Math.floor(Date.now() / 1000)) >>> 0;
|
|
10
|
+
counter = (counter + 1) & 0xffffff;
|
|
11
|
+
return secs.toString(16).padStart(8, '0') + PROCESS + counter.toString(16).padStart(6, '0');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class ObjectId {
|
|
15
|
+
constructor(value) {
|
|
16
|
+
if (value instanceof ObjectId) this._hex = value._hex;
|
|
17
|
+
else if (value === undefined || value === null) this._hex = generate();
|
|
18
|
+
else if (typeof value === 'number') this._hex = generate(value);
|
|
19
|
+
else if (typeof value === 'string' && HEX24.test(value)) this._hex = value.toLowerCase();
|
|
20
|
+
else if (Buffer.isBuffer(value) && value.length === 12) this._hex = value.toString('hex');
|
|
21
|
+
else if (value && typeof value.toHexString === 'function') this._hex = String(value.toHexString()).toLowerCase();
|
|
22
|
+
else if (value && typeof value === 'object' && typeof value._id !== 'undefined') this._hex = new ObjectId(value._id)._hex;
|
|
23
|
+
else throw new TypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
|
|
24
|
+
}
|
|
25
|
+
toString() { return this._hex; }
|
|
26
|
+
toHexString() { return this._hex; }
|
|
27
|
+
toJSON() { return this._hex; }
|
|
28
|
+
valueOf() { return this._hex; }
|
|
29
|
+
get id() { return Buffer.from(this._hex, 'hex'); }
|
|
30
|
+
equals(other) {
|
|
31
|
+
if (other === undefined || other === null) return false;
|
|
32
|
+
try { return this._hex === new ObjectId(other)._hex; } catch { return false; }
|
|
33
|
+
}
|
|
34
|
+
getTimestamp() { return new Date(parseInt(this._hex.slice(0, 8), 16) * 1000); }
|
|
35
|
+
inspect() { return `new ObjectId("${this._hex}")`; }
|
|
36
|
+
[Symbol.for('nodejs.util.inspect.custom')]() { return this.inspect(); }
|
|
37
|
+
static isValid(value) {
|
|
38
|
+
if (value === undefined || value === null) return false;
|
|
39
|
+
if (value instanceof ObjectId) return true;
|
|
40
|
+
if (typeof value === 'string') return HEX24.test(value);
|
|
41
|
+
if (typeof value === 'number') return Number.isInteger(value);
|
|
42
|
+
return Buffer.isBuffer(value) && value.length === 12;
|
|
43
|
+
}
|
|
44
|
+
static createFromHexString(hex) { return new ObjectId(hex); }
|
|
45
|
+
static createFromTime(secs) { return new ObjectId(secs); }
|
|
46
|
+
static generate() { return generate(); }
|
|
47
|
+
}
|
|
48
|
+
ObjectId.prototype._bsontype = 'ObjectId';
|
|
49
|
+
|
|
50
|
+
module.exports = ObjectId;
|