@vsaas/loopback-datasource-juggler 10.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/LICENSE +25 -0
- package/NOTICE +23 -0
- package/README.md +74 -0
- package/dist/_virtual/_rolldown/runtime.js +4 -0
- package/dist/index.js +26 -0
- package/dist/lib/browser.depd.js +13 -0
- package/dist/lib/case-utils.js +21 -0
- package/dist/lib/connectors/kv-memory.js +158 -0
- package/dist/lib/connectors/memory.js +810 -0
- package/dist/lib/connectors/transient.js +126 -0
- package/dist/lib/dao.js +2445 -0
- package/dist/lib/datasource.js +2215 -0
- package/dist/lib/date-string.js +87 -0
- package/dist/lib/geo.js +244 -0
- package/dist/lib/globalize.js +33 -0
- package/dist/lib/hooks.js +79 -0
- package/dist/lib/id-utils.js +66 -0
- package/dist/lib/include.js +795 -0
- package/dist/lib/include_utils.js +104 -0
- package/dist/lib/introspection.js +37 -0
- package/dist/lib/jutil.js +65 -0
- package/dist/lib/kvao/delete-all.js +57 -0
- package/dist/lib/kvao/delete.js +43 -0
- package/dist/lib/kvao/expire.js +35 -0
- package/dist/lib/kvao/get.js +34 -0
- package/dist/lib/kvao/index.js +28 -0
- package/dist/lib/kvao/iterate-keys.js +38 -0
- package/dist/lib/kvao/keys.js +55 -0
- package/dist/lib/kvao/set.js +39 -0
- package/dist/lib/kvao/ttl.js +35 -0
- package/dist/lib/list.js +101 -0
- package/dist/lib/mixins.js +58 -0
- package/dist/lib/model-builder.js +608 -0
- package/dist/lib/model-definition.js +231 -0
- package/dist/lib/model-utils.js +368 -0
- package/dist/lib/model.js +586 -0
- package/dist/lib/observer.js +235 -0
- package/dist/lib/relation-definition.js +2604 -0
- package/dist/lib/relations.js +587 -0
- package/dist/lib/scope.js +392 -0
- package/dist/lib/transaction.js +183 -0
- package/dist/lib/types.js +58 -0
- package/dist/lib/utils.js +625 -0
- package/dist/lib/validations.js +742 -0
- package/dist/package.js +93 -0
- package/package.json +85 -0
- package/types/common.d.ts +28 -0
- package/types/connector.d.ts +52 -0
- package/types/datasource.d.ts +324 -0
- package/types/date-string.d.ts +21 -0
- package/types/inclusion-mixin.d.ts +44 -0
- package/types/index.d.ts +36 -0
- package/types/kv-model.d.ts +201 -0
- package/types/model.d.ts +368 -0
- package/types/observer-mixin.d.ts +174 -0
- package/types/persisted-model.d.ts +505 -0
- package/types/query.d.ts +108 -0
- package/types/relation-mixin.d.ts +577 -0
- package/types/relation.d.ts +301 -0
- package/types/scope.d.ts +92 -0
- package/types/transaction-mixin.d.ts +47 -0
- package/types/types.d.ts +65 -0
- package/types/validation-mixin.d.ts +287 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_globalize = require("./globalize.js");
|
|
4
|
+
//#region src/lib/include_utils.ts
|
|
5
|
+
var require_include_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const g = require_lib_globalize();
|
|
7
|
+
module.exports.buildOneToOneIdentityMapWithOrigKeys = buildOneToOneIdentityMapWithOrigKeys;
|
|
8
|
+
module.exports.buildOneToManyIdentityMapWithOrigKeys = buildOneToManyIdentityMapWithOrigKeys;
|
|
9
|
+
module.exports.join = join;
|
|
10
|
+
module.exports.KVMap = KVMap;
|
|
11
|
+
function getId(obj, idName) {
|
|
12
|
+
const id = obj && obj[idName];
|
|
13
|
+
if (id == null) {
|
|
14
|
+
const msg = g.f("ID property \"%s\" is missing for included item: %j. Please make sure `fields` include \"%s\" if it's present in the `filter`", idName, obj, idName);
|
|
15
|
+
const err = new Error(msg);
|
|
16
|
+
err.statusCode = 400;
|
|
17
|
+
throw err;
|
|
18
|
+
}
|
|
19
|
+
return id;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Effectively builds associative map on id -> object relation and stores original keys.
|
|
23
|
+
* Map returned in form of object with ids in keys and object as values.
|
|
24
|
+
* @param objs array of objects to build from
|
|
25
|
+
* @param idName name of property to be used as id. Such property considered to be unique across array.
|
|
26
|
+
* In case of collisions last wins. For non-unique ids use buildOneToManyIdentityMap()
|
|
27
|
+
* @returns {} object where keys are ids and values are objects itself
|
|
28
|
+
*/
|
|
29
|
+
function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
|
|
30
|
+
const kvMap = new KVMap();
|
|
31
|
+
for (let i = 0; i < objs.length; i++) {
|
|
32
|
+
const obj = objs[i];
|
|
33
|
+
const id = getId(obj, idName);
|
|
34
|
+
kvMap.set(id, obj);
|
|
35
|
+
}
|
|
36
|
+
return kvMap;
|
|
37
|
+
}
|
|
38
|
+
function buildOneToManyIdentityMapWithOrigKeys(objs, idName) {
|
|
39
|
+
const kvMap = new KVMap();
|
|
40
|
+
for (let i = 0; i < objs.length; i++) {
|
|
41
|
+
const obj = objs[i];
|
|
42
|
+
const id = getId(obj, idName);
|
|
43
|
+
const value = kvMap.get(id) || [];
|
|
44
|
+
value.push(obj);
|
|
45
|
+
kvMap.set(id, value);
|
|
46
|
+
}
|
|
47
|
+
return kvMap;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Yeah, it joins. You need three things id -> obj1 map, id -> [obj2] map and merge function.
|
|
51
|
+
* This functions will take each obj1, locate all data to join in map2 and call merge function.
|
|
52
|
+
* @param oneToOneIdMap
|
|
53
|
+
* @param oneToManyIdMap
|
|
54
|
+
* @param mergeF function(obj, objectsToMergeIn)
|
|
55
|
+
*/
|
|
56
|
+
function join(oneToOneIdMap, oneToManyIdMap, mergeF) {
|
|
57
|
+
const ids = oneToOneIdMap.getKeys();
|
|
58
|
+
for (let i = 0; i < ids.length; i++) {
|
|
59
|
+
const id = ids[i];
|
|
60
|
+
mergeF(oneToOneIdMap.get(id), oneToManyIdMap.get(id) || []);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Map with arbitrary keys and values. User .set() and .get() to work with values instead of []
|
|
65
|
+
* @returns {{set: Function, get: Function, remove: Function, exist: Function, getKeys: Function}}
|
|
66
|
+
* @constructor
|
|
67
|
+
*/
|
|
68
|
+
function KVMap() {
|
|
69
|
+
const _originalKeyFieldName = "originalKey";
|
|
70
|
+
const _valueKeyFieldName = "value";
|
|
71
|
+
const _dict = {};
|
|
72
|
+
const keyToString = function(key) {
|
|
73
|
+
return key.toString();
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
set: function(key, value) {
|
|
77
|
+
const recordObj = {};
|
|
78
|
+
recordObj[_originalKeyFieldName] = key;
|
|
79
|
+
recordObj[_valueKeyFieldName] = value;
|
|
80
|
+
_dict[keyToString(key)] = recordObj;
|
|
81
|
+
return true;
|
|
82
|
+
},
|
|
83
|
+
get: function(key) {
|
|
84
|
+
const storeObj = _dict[keyToString(key)];
|
|
85
|
+
if (storeObj) return storeObj[_valueKeyFieldName];
|
|
86
|
+
else return;
|
|
87
|
+
},
|
|
88
|
+
remove: function(key) {
|
|
89
|
+
delete _dict[keyToString(key)];
|
|
90
|
+
return true;
|
|
91
|
+
},
|
|
92
|
+
exist: function(key) {
|
|
93
|
+
return _dict.hasOwnProperty(keyToString(key));
|
|
94
|
+
},
|
|
95
|
+
getKeys: function() {
|
|
96
|
+
const result = [];
|
|
97
|
+
for (const key in _dict) result.push(_dict[key][_originalKeyFieldName]);
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}));
|
|
103
|
+
//#endregion
|
|
104
|
+
module.exports = require_include_utils();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region src/lib/introspection.ts
|
|
3
|
+
var require_introspection = /* @__PURE__ */ require("../_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
|
|
4
|
+
module.exports = function getIntrospector(ModelBuilder) {
|
|
5
|
+
function introspectType(value) {
|
|
6
|
+
if (value === null || value === void 0) return ModelBuilder.Any;
|
|
7
|
+
for (const t in ModelBuilder.schemaTypes) {
|
|
8
|
+
const st = ModelBuilder.schemaTypes[t];
|
|
9
|
+
if (st !== Object && st !== Array && value instanceof st) return t;
|
|
10
|
+
}
|
|
11
|
+
const type = typeof value;
|
|
12
|
+
if (type === "string" || type === "number" || type === "boolean") return type;
|
|
13
|
+
if (value instanceof Date) return "date";
|
|
14
|
+
let itemType;
|
|
15
|
+
if (Array.isArray(value)) {
|
|
16
|
+
for (let i = 0; i < value.length; i++) {
|
|
17
|
+
if (value[i] === null || value[i] === void 0) continue;
|
|
18
|
+
itemType = introspectType(value[i]);
|
|
19
|
+
if (itemType) return [itemType];
|
|
20
|
+
}
|
|
21
|
+
return "array";
|
|
22
|
+
}
|
|
23
|
+
if (type === "function") return value.constructor.name;
|
|
24
|
+
const properties = {};
|
|
25
|
+
for (const p in value) {
|
|
26
|
+
itemType = introspectType(value[p]);
|
|
27
|
+
if (itemType) properties[p] = itemType;
|
|
28
|
+
}
|
|
29
|
+
if (Object.keys(properties).length === 0) return "object";
|
|
30
|
+
return properties;
|
|
31
|
+
}
|
|
32
|
+
ModelBuilder.introspect = introspectType;
|
|
33
|
+
return introspectType;
|
|
34
|
+
};
|
|
35
|
+
}));
|
|
36
|
+
//#endregion
|
|
37
|
+
module.exports = require_introspection();
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region src/lib/jutil.ts
|
|
3
|
+
var require_jutil = /* @__PURE__ */ require("../_virtual/_rolldown/runtime.js").__commonJSMin(((exports) => {
|
|
4
|
+
const util = require("util");
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param newClass
|
|
8
|
+
* @param baseClass
|
|
9
|
+
*/
|
|
10
|
+
exports.inherits = function(newClass, baseClass, options) {
|
|
11
|
+
util.inherits(newClass, baseClass);
|
|
12
|
+
options = options || {
|
|
13
|
+
staticProperties: true,
|
|
14
|
+
override: false
|
|
15
|
+
};
|
|
16
|
+
if (options.staticProperties) Object.keys(baseClass).forEach(function(classProp) {
|
|
17
|
+
if (classProp !== "super_" && (!newClass.hasOwnProperty(classProp) || options.override)) Object.defineProperty(newClass, classProp, Object.getOwnPropertyDescriptor(baseClass, classProp));
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Mix in the a class into the new class
|
|
22
|
+
* @param newClass The target class to receive the mixin
|
|
23
|
+
* @param mixinClass The class to be mixed in
|
|
24
|
+
* @param options
|
|
25
|
+
*/
|
|
26
|
+
exports.mixin = function(newClass, mixinClass, options) {
|
|
27
|
+
if (Array.isArray(newClass._mixins)) {
|
|
28
|
+
if (newClass._mixins.indexOf(mixinClass) !== -1) return;
|
|
29
|
+
newClass._mixins.push(mixinClass);
|
|
30
|
+
} else newClass._mixins = [mixinClass];
|
|
31
|
+
options = options || {
|
|
32
|
+
staticProperties: true,
|
|
33
|
+
instanceProperties: true,
|
|
34
|
+
override: false,
|
|
35
|
+
proxyFunctions: false
|
|
36
|
+
};
|
|
37
|
+
if (options.staticProperties === void 0) options.staticProperties = true;
|
|
38
|
+
if (options.instanceProperties === void 0) options.instanceProperties = true;
|
|
39
|
+
if (options.staticProperties) mixInto(mixinClass, newClass, options);
|
|
40
|
+
if (options.instanceProperties && mixinClass.prototype) mixInto(mixinClass.prototype, newClass.prototype, options);
|
|
41
|
+
return newClass;
|
|
42
|
+
};
|
|
43
|
+
function mixInto(sourceScope, targetScope, options) {
|
|
44
|
+
Object.keys(sourceScope).forEach(function(propertyName) {
|
|
45
|
+
const targetPropertyExists = targetScope.hasOwnProperty(propertyName);
|
|
46
|
+
const sourceProperty = Object.getOwnPropertyDescriptor(sourceScope, propertyName);
|
|
47
|
+
const targetProperty = targetPropertyExists && Object.getOwnPropertyDescriptor(targetScope, propertyName);
|
|
48
|
+
const isDelegate = targetPropertyExists && typeof targetProperty.value === "function" && targetProperty.value._delegate;
|
|
49
|
+
const shouldOverride = options.override || !targetPropertyExists || isDelegate;
|
|
50
|
+
if (propertyName == "_mixins") {
|
|
51
|
+
mergeMixins(sourceScope._mixins, targetScope._mixins);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (shouldOverride) Object.defineProperty(targetScope, propertyName, sourceProperty);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function mergeMixins(source, target) {
|
|
58
|
+
for (const ix in source) {
|
|
59
|
+
const mx = source[ix];
|
|
60
|
+
if (target.indexOf(mx) === -1) target.push(mx);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}));
|
|
64
|
+
//#endregion
|
|
65
|
+
module.exports = require_jutil();
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/delete-all.ts
|
|
5
|
+
var require_delete_all = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const debug = require("debug")("loopback:kvao:delete-all");
|
|
8
|
+
const utils = require_lib_utils;
|
|
9
|
+
/**
|
|
10
|
+
* Delete all keys (and values) associated to the current model.
|
|
11
|
+
*
|
|
12
|
+
* @options {Object} options Unused ATM, placeholder for future options.
|
|
13
|
+
* @callback {Function} callback
|
|
14
|
+
* @param {Error} err Error object.
|
|
15
|
+
* @promise
|
|
16
|
+
*
|
|
17
|
+
* @header KVAO.prototype.deleteAll([options, ]cb)
|
|
18
|
+
*/
|
|
19
|
+
module.exports = function deleteAll(options, callback) {
|
|
20
|
+
if (callback == void 0 && typeof options === "function") {
|
|
21
|
+
callback = options;
|
|
22
|
+
options = {};
|
|
23
|
+
} else if (!options) options = {};
|
|
24
|
+
assert(typeof options === "object", "options must be an object");
|
|
25
|
+
callback = callback || utils.createPromiseCallback();
|
|
26
|
+
const connector = this.getConnector();
|
|
27
|
+
if (typeof connector.deleteAll === "function") connector.deleteAll(this.modelName, options, callback);
|
|
28
|
+
else if (typeof connector.delete === "function") {
|
|
29
|
+
debug("Falling back to unoptimized key-value pair deletion");
|
|
30
|
+
iterateAndDelete(connector, this.modelName, options, callback);
|
|
31
|
+
} else {
|
|
32
|
+
const errMsg = "Connector does not support key-value pair deletion";
|
|
33
|
+
debug(errMsg);
|
|
34
|
+
process.nextTick(function() {
|
|
35
|
+
const err = new Error(errMsg);
|
|
36
|
+
err.statusCode = 501;
|
|
37
|
+
callback(err);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return callback.promise;
|
|
41
|
+
};
|
|
42
|
+
function iterateAndDelete(connector, modelName, options, callback) {
|
|
43
|
+
const iter = connector.iterateKeys(modelName, {});
|
|
44
|
+
iter.next(onNextKey);
|
|
45
|
+
function onNextKey(err, key) {
|
|
46
|
+
if (err) return callback(err);
|
|
47
|
+
if (key === void 0) return callback();
|
|
48
|
+
connector.delete(modelName, key, options, onDeleted);
|
|
49
|
+
}
|
|
50
|
+
function onDeleted(err) {
|
|
51
|
+
if (err) return callback(err);
|
|
52
|
+
iter.next(onNextKey);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}));
|
|
56
|
+
//#endregion
|
|
57
|
+
module.exports = require_delete_all();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/delete.ts
|
|
5
|
+
var require_delete = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const debug = require("debug")("loopback:kvao:delete");
|
|
8
|
+
const utils = require_lib_utils;
|
|
9
|
+
/**
|
|
10
|
+
* Delete the key-value pair associated to the given key.
|
|
11
|
+
*
|
|
12
|
+
* @param {String} key Key to use when searching the database.
|
|
13
|
+
* @options {Object} options
|
|
14
|
+
* @callback {Function} callback
|
|
15
|
+
* @param {Error} err Error object.
|
|
16
|
+
* @param {*} result Value associated with the given key.
|
|
17
|
+
* @promise
|
|
18
|
+
*
|
|
19
|
+
* @header KVAO.prototype.delete(key[, options], cb)
|
|
20
|
+
*/
|
|
21
|
+
module.exports = function keyValueDelete(key, options, callback) {
|
|
22
|
+
if (callback == void 0 && typeof options === "function") {
|
|
23
|
+
callback = options;
|
|
24
|
+
options = {};
|
|
25
|
+
} else if (!options) options = {};
|
|
26
|
+
assert(typeof key === "string" && key, "key must be a non-empty string");
|
|
27
|
+
callback = callback || utils.createPromiseCallback();
|
|
28
|
+
const connector = this.getConnector();
|
|
29
|
+
if (typeof connector.delete === "function") connector.delete(this.modelName, key, options, callback);
|
|
30
|
+
else {
|
|
31
|
+
const errMsg = "Connector does not support key-value pair deletion";
|
|
32
|
+
debug(errMsg);
|
|
33
|
+
process.nextTick(function() {
|
|
34
|
+
const err = new Error(errMsg);
|
|
35
|
+
err.statusCode = 501;
|
|
36
|
+
callback(err);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return callback.promise;
|
|
40
|
+
};
|
|
41
|
+
}));
|
|
42
|
+
//#endregion
|
|
43
|
+
module.exports = require_delete();
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/expire.ts
|
|
5
|
+
var require_expire = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const utils = require_lib_utils;
|
|
8
|
+
/**
|
|
9
|
+
* Set the TTL (time to live) in ms (milliseconds) for a given key. TTL is the
|
|
10
|
+
* remaining time before a key-value pair is discarded from the database.
|
|
11
|
+
*
|
|
12
|
+
* @param {String} key Key to use when searching the database.
|
|
13
|
+
* @param {Number} ttl TTL in ms to set for the key.
|
|
14
|
+
* @options {Object} options
|
|
15
|
+
* @callback {Function} callback
|
|
16
|
+
* @param {Error} err Error object.
|
|
17
|
+
* @promise
|
|
18
|
+
*
|
|
19
|
+
* @header KVAO.expire(key, ttl, cb)
|
|
20
|
+
*/
|
|
21
|
+
module.exports = function keyValueExpire(key, ttl, options, callback) {
|
|
22
|
+
if (callback == void 0 && typeof options === "function") {
|
|
23
|
+
callback = options;
|
|
24
|
+
options = {};
|
|
25
|
+
} else if (!options) options = {};
|
|
26
|
+
assert(typeof key === "string" && key, "key must be a non-empty string");
|
|
27
|
+
assert(typeof ttl === "number" && ttl > 0, "ttl must be a positive integer");
|
|
28
|
+
assert(typeof options === "object", "options must be an object");
|
|
29
|
+
callback = callback || utils.createPromiseCallback();
|
|
30
|
+
this.getConnector().expire(this.modelName, key, ttl, options, callback);
|
|
31
|
+
return callback.promise;
|
|
32
|
+
};
|
|
33
|
+
}));
|
|
34
|
+
//#endregion
|
|
35
|
+
module.exports = require_expire();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/get.ts
|
|
5
|
+
var require_get = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const utils = require_lib_utils;
|
|
8
|
+
/**
|
|
9
|
+
* Return the value associated with a given key.
|
|
10
|
+
*
|
|
11
|
+
* @param {String} key Key to use when searching the database.
|
|
12
|
+
* @options {Object} options
|
|
13
|
+
* @callback {Function} callback
|
|
14
|
+
* @param {Error} err Error object.
|
|
15
|
+
* @param {*} result Value associated with the given key.
|
|
16
|
+
* @promise
|
|
17
|
+
*
|
|
18
|
+
* @header KVAO.get(key, cb)
|
|
19
|
+
*/
|
|
20
|
+
module.exports = function keyValueGet(key, options, callback) {
|
|
21
|
+
if (callback == void 0 && typeof options === "function") {
|
|
22
|
+
callback = options;
|
|
23
|
+
options = {};
|
|
24
|
+
} else if (!options) options = {};
|
|
25
|
+
assert(typeof key === "string" && key, "key must be a non-empty string");
|
|
26
|
+
callback = callback || utils.createPromiseCallback();
|
|
27
|
+
this.getConnector().get(this.modelName, key, options, function(err, result) {
|
|
28
|
+
callback(err, result);
|
|
29
|
+
});
|
|
30
|
+
return callback.promise;
|
|
31
|
+
};
|
|
32
|
+
}));
|
|
33
|
+
//#endregion
|
|
34
|
+
module.exports = require_get();
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_kvao_delete = require("./delete.js");
|
|
4
|
+
const require_lib_kvao_delete_all = require("./delete-all.js");
|
|
5
|
+
const require_lib_kvao_get = require("./get.js");
|
|
6
|
+
const require_lib_kvao_set = require("./set.js");
|
|
7
|
+
const require_lib_kvao_expire = require("./expire.js");
|
|
8
|
+
const require_lib_kvao_ttl = require("./ttl.js");
|
|
9
|
+
const require_lib_kvao_iterate_keys = require("./iterate-keys.js");
|
|
10
|
+
const require_lib_kvao_keys = require("./keys.js");
|
|
11
|
+
//#region src/lib/kvao/index.ts
|
|
12
|
+
var require_kvao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
13
|
+
function KeyValueAccessObject() {}
|
|
14
|
+
module.exports = KeyValueAccessObject;
|
|
15
|
+
KeyValueAccessObject.delete = require_lib_kvao_delete;
|
|
16
|
+
KeyValueAccessObject.deleteAll = require_lib_kvao_delete_all;
|
|
17
|
+
KeyValueAccessObject.get = require_lib_kvao_get;
|
|
18
|
+
KeyValueAccessObject.set = require_lib_kvao_set;
|
|
19
|
+
KeyValueAccessObject.expire = require_lib_kvao_expire;
|
|
20
|
+
KeyValueAccessObject.ttl = require_lib_kvao_ttl;
|
|
21
|
+
KeyValueAccessObject.iterateKeys = require_lib_kvao_iterate_keys;
|
|
22
|
+
KeyValueAccessObject.keys = require_lib_kvao_keys;
|
|
23
|
+
KeyValueAccessObject.getConnector = function() {
|
|
24
|
+
return this.getDataSource().connector;
|
|
25
|
+
};
|
|
26
|
+
}));
|
|
27
|
+
//#endregion
|
|
28
|
+
module.exports = require_kvao();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/iterate-keys.ts
|
|
5
|
+
var require_iterate_keys = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const utils = require_lib_utils;
|
|
8
|
+
/**
|
|
9
|
+
* Asynchronously iterate all keys in the database. Similar to `.keys()` but
|
|
10
|
+
* instead allows for iteration over large data sets without having to load
|
|
11
|
+
* everything into memory at once.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} filter An optional filter object with the following
|
|
14
|
+
* @param {String} filter.match Glob string to use to filter returned
|
|
15
|
+
* keys (i.e. `userid.*`). All connectors are required to support `*` and
|
|
16
|
+
* `?`. They may also support additional special characters that are
|
|
17
|
+
* specific to the backing database.
|
|
18
|
+
* @param {Object} options
|
|
19
|
+
* @returns {AsyncIterator} An Object implementing `next(cb) -> Promise`
|
|
20
|
+
* function that can be used to iterate all keys.
|
|
21
|
+
*
|
|
22
|
+
* @header KVAO.iterateKeys(filter)
|
|
23
|
+
*/
|
|
24
|
+
module.exports = function keyValueIterateKeys(filter, options) {
|
|
25
|
+
filter = filter || {};
|
|
26
|
+
options = options || {};
|
|
27
|
+
assert(typeof filter === "object", "filter must be an object");
|
|
28
|
+
assert(typeof options === "object", "options must be an object");
|
|
29
|
+
const iter = this.getConnector().iterateKeys(this.modelName, filter, options);
|
|
30
|
+
return { next: function(callback) {
|
|
31
|
+
callback = callback || utils.createPromiseCallback();
|
|
32
|
+
iter.next(callback);
|
|
33
|
+
return callback.promise;
|
|
34
|
+
} };
|
|
35
|
+
};
|
|
36
|
+
}));
|
|
37
|
+
//#endregion
|
|
38
|
+
module.exports = require_iterate_keys();
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/keys.ts
|
|
5
|
+
var require_keys = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const utils = require_lib_utils;
|
|
8
|
+
/**
|
|
9
|
+
* Return all keys in the database.
|
|
10
|
+
*
|
|
11
|
+
* **WARNING**: This method is not suitable for large data sets as all
|
|
12
|
+
* key-values pairs are loaded into memory at once. For large data sets,
|
|
13
|
+
* use `iterateKeys()` instead.
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} filter An optional filter object with the following
|
|
16
|
+
* @param {String} filter.match Glob string used to filter returned
|
|
17
|
+
* keys (i.e. `userid.*`). All connectors are required to support `*` and
|
|
18
|
+
* `?`, but may also support additional special characters specific to the
|
|
19
|
+
* database.
|
|
20
|
+
* @param {Object} options
|
|
21
|
+
* @callback {Function} callback
|
|
22
|
+
* @promise
|
|
23
|
+
*
|
|
24
|
+
*
|
|
25
|
+
* @header KVAO.keys(filter, callback)
|
|
26
|
+
*/
|
|
27
|
+
module.exports = function keyValueKeys(filter, options, callback) {
|
|
28
|
+
if (callback === void 0) {
|
|
29
|
+
if (typeof options === "function") {
|
|
30
|
+
callback = options;
|
|
31
|
+
options = void 0;
|
|
32
|
+
} else if (options === void 0 && typeof filter === "function") {
|
|
33
|
+
callback = filter;
|
|
34
|
+
filter = void 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
filter = filter || {};
|
|
38
|
+
options = options || {};
|
|
39
|
+
assert(typeof filter === "object", "filter must be an object");
|
|
40
|
+
assert(typeof options === "object", "options must be an object");
|
|
41
|
+
callback = callback || utils.createPromiseCallback();
|
|
42
|
+
const iter = this.iterateKeys(filter, options);
|
|
43
|
+
const keys = [];
|
|
44
|
+
iter.next(onNextKey);
|
|
45
|
+
function onNextKey(err, key) {
|
|
46
|
+
if (err) return callback(err);
|
|
47
|
+
if (key === void 0) return callback(null, keys);
|
|
48
|
+
keys.push(key);
|
|
49
|
+
iter.next(onNextKey);
|
|
50
|
+
}
|
|
51
|
+
return callback.promise;
|
|
52
|
+
};
|
|
53
|
+
}));
|
|
54
|
+
//#endregion
|
|
55
|
+
module.exports = require_keys();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/set.ts
|
|
5
|
+
var require_set = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const utils = require_lib_utils;
|
|
8
|
+
/**
|
|
9
|
+
* Persist a value and associate it with the given key.
|
|
10
|
+
*
|
|
11
|
+
* @param {String} key Key to associate with the given value.
|
|
12
|
+
* @param {*} value Value to persist.
|
|
13
|
+
* @options {Number|Object} options Optional settings for the key-value
|
|
14
|
+
* pair. If a Number is provided, it is set as the TTL (time to live) in ms
|
|
15
|
+
* (milliseconds) for the key-value pair.
|
|
16
|
+
* @property {Number} ttl TTL for the key-value pair in ms.
|
|
17
|
+
* @callback {Function} callback
|
|
18
|
+
* @param {Error} err Error object.
|
|
19
|
+
* @promise
|
|
20
|
+
*
|
|
21
|
+
* @header KVAO.set(key, value, cb)
|
|
22
|
+
*/
|
|
23
|
+
module.exports = function keyValueSet(key, value, options, callback) {
|
|
24
|
+
if (callback == void 0 && typeof options === "function") {
|
|
25
|
+
callback = options;
|
|
26
|
+
options = {};
|
|
27
|
+
} else if (typeof options === "number") options = { ttl: options };
|
|
28
|
+
else if (!options) options = {};
|
|
29
|
+
assert(typeof key === "string" && key, "key must be a non-empty string");
|
|
30
|
+
assert(value != null, "value must be defined and not null");
|
|
31
|
+
assert(typeof options === "object", "options must be an object");
|
|
32
|
+
if (options && "ttl" in options) assert(typeof options.ttl === "number" && options.ttl > 0, "options.ttl must be a positive number");
|
|
33
|
+
callback = callback || utils.createPromiseCallback();
|
|
34
|
+
this.getConnector().set(this.modelName, key, value, options, callback);
|
|
35
|
+
return callback.promise;
|
|
36
|
+
};
|
|
37
|
+
}));
|
|
38
|
+
//#endregion
|
|
39
|
+
module.exports = require_set();
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("../utils.js");
|
|
4
|
+
//#region src/lib/kvao/ttl.ts
|
|
5
|
+
var require_ttl = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const utils = require_lib_utils;
|
|
8
|
+
/**
|
|
9
|
+
* Return the TTL (time to live) for a given key. TTL is the remaining time
|
|
10
|
+
* before a key-value pair is discarded from the database.
|
|
11
|
+
*
|
|
12
|
+
* @param {String} key Key to use when searching the database.
|
|
13
|
+
* @options {Object} options
|
|
14
|
+
* @callback {Function} callback
|
|
15
|
+
* @param {Error} error
|
|
16
|
+
* @param {Number} ttl Expiration time for the key-value pair. `undefined` if
|
|
17
|
+
* TTL was not initially set.
|
|
18
|
+
* @promise
|
|
19
|
+
*
|
|
20
|
+
* @header KVAO.ttl(key, cb)
|
|
21
|
+
*/
|
|
22
|
+
module.exports = function keyValueTtl(key, options, callback) {
|
|
23
|
+
if (callback == void 0 && typeof options === "function") {
|
|
24
|
+
callback = options;
|
|
25
|
+
options = {};
|
|
26
|
+
} else if (!options) options = {};
|
|
27
|
+
assert(typeof key === "string" && key, "key must be a non-empty string");
|
|
28
|
+
assert(typeof options === "object", "options must be an object");
|
|
29
|
+
callback = callback || utils.createPromiseCallback();
|
|
30
|
+
this.getConnector().ttl(this.modelName, key, options, callback);
|
|
31
|
+
return callback.promise;
|
|
32
|
+
};
|
|
33
|
+
}));
|
|
34
|
+
//#endregion
|
|
35
|
+
module.exports = require_ttl();
|
package/dist/lib/list.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_globalize = require("./globalize.js");
|
|
4
|
+
const require_lib_types = require("./types.js");
|
|
5
|
+
const require_lib_utils = require("./utils.js");
|
|
6
|
+
//#region src/lib/list.ts
|
|
7
|
+
var require_list = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
8
|
+
const g = require_lib_globalize();
|
|
9
|
+
const util = require("util");
|
|
10
|
+
const Any = require_lib_types.Types.Any;
|
|
11
|
+
const { applyParentProperty } = require_lib_utils;
|
|
12
|
+
module.exports = List;
|
|
13
|
+
function List(items, itemType, parent) {
|
|
14
|
+
if (!(this instanceof List)) return new List(items, itemType, parent);
|
|
15
|
+
if (typeof items === "string") try {
|
|
16
|
+
items = JSON.parse(items);
|
|
17
|
+
} catch {
|
|
18
|
+
const err = new Error(g.f("could not create List from JSON string: %j", items));
|
|
19
|
+
err.statusCode = 400;
|
|
20
|
+
throw err;
|
|
21
|
+
}
|
|
22
|
+
if (typeof items === "number") items = Array.from({ length: items });
|
|
23
|
+
const arr = [];
|
|
24
|
+
arr.__proto__ = List.prototype;
|
|
25
|
+
items = items || [];
|
|
26
|
+
if (!Array.isArray(items)) {
|
|
27
|
+
const err = new Error(g.f("Items must be an array: %j", items));
|
|
28
|
+
err.statusCode = 400;
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
if (!itemType) itemType = items[0] && items[0].constructor;
|
|
32
|
+
if (Array.isArray(itemType)) itemType = itemType[0];
|
|
33
|
+
if (itemType === Array) itemType = Any;
|
|
34
|
+
Object.defineProperty(arr, "itemType", {
|
|
35
|
+
writable: true,
|
|
36
|
+
enumerable: false,
|
|
37
|
+
value: itemType
|
|
38
|
+
});
|
|
39
|
+
if (parent) Object.defineProperty(arr, "parent", {
|
|
40
|
+
writable: true,
|
|
41
|
+
enumerable: false,
|
|
42
|
+
value: parent
|
|
43
|
+
});
|
|
44
|
+
items.forEach(function(item, i) {
|
|
45
|
+
if (itemType && !(item instanceof itemType)) arr[i] = arr.toItem(item);
|
|
46
|
+
else arr[i] = item;
|
|
47
|
+
if (parent && arr[i] && typeof arr[i] === "object") applyParentProperty(arr[i], parent);
|
|
48
|
+
});
|
|
49
|
+
return arr;
|
|
50
|
+
}
|
|
51
|
+
util.inherits(List, Array);
|
|
52
|
+
const _push = List.prototype.push;
|
|
53
|
+
List.prototype.toItem = function(item) {
|
|
54
|
+
if (isClass(this.itemType)) return new this.itemType(item);
|
|
55
|
+
else if (Array.isArray(item)) return item;
|
|
56
|
+
else if (this.itemType === Date) {
|
|
57
|
+
if (item === null) return null;
|
|
58
|
+
return new Date(item);
|
|
59
|
+
} else return this.itemType(item);
|
|
60
|
+
};
|
|
61
|
+
List.prototype.push = function(obj) {
|
|
62
|
+
const item = this.itemType && obj instanceof this.itemType ? obj : this.toItem(obj);
|
|
63
|
+
if (item && typeof item === "object" && this.parent) applyParentProperty(item, this.parent);
|
|
64
|
+
_push.call(this, item);
|
|
65
|
+
return item;
|
|
66
|
+
};
|
|
67
|
+
List.prototype.toObject = function(onlySchema, removeHidden, removeProtected) {
|
|
68
|
+
const items = [];
|
|
69
|
+
this.forEach(function(item) {
|
|
70
|
+
if (item && Array.isArray(item) && item.toArray) {
|
|
71
|
+
const subArray = item.toArray();
|
|
72
|
+
items.push(subArray);
|
|
73
|
+
} else if (item && typeof item === "object" && item.toObject) items.push(item.toObject(onlySchema, removeHidden, removeProtected));
|
|
74
|
+
else items.push(item);
|
|
75
|
+
});
|
|
76
|
+
return items;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Convert itself to a plain array.
|
|
80
|
+
*
|
|
81
|
+
* Some modules such as `should` checks prototype for comparison
|
|
82
|
+
*/
|
|
83
|
+
List.prototype.toArray = function() {
|
|
84
|
+
const items = [];
|
|
85
|
+
this.forEach(function(item) {
|
|
86
|
+
items.push(item);
|
|
87
|
+
});
|
|
88
|
+
return items;
|
|
89
|
+
};
|
|
90
|
+
List.prototype.toJSON = function() {
|
|
91
|
+
return this.toObject(true);
|
|
92
|
+
};
|
|
93
|
+
List.prototype.toString = function() {
|
|
94
|
+
return JSON.stringify(this.toJSON());
|
|
95
|
+
};
|
|
96
|
+
function isClass(fn) {
|
|
97
|
+
return fn && fn.toString().indexOf("class ") === 0;
|
|
98
|
+
}
|
|
99
|
+
}));
|
|
100
|
+
//#endregion
|
|
101
|
+
module.exports = require_list();
|