@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,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region src/lib/date-string.ts
|
|
3
|
+
var require_date_string = /* @__PURE__ */ require("../_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
|
|
4
|
+
const inspect = require("util").inspect;
|
|
5
|
+
module.exports = DateString;
|
|
6
|
+
/**
|
|
7
|
+
* A String whose value is a valid representation of a Date.
|
|
8
|
+
* Use this type if you need to preserve the format of the value and still
|
|
9
|
+
* check if it's valid.
|
|
10
|
+
* Example:
|
|
11
|
+
* ```js
|
|
12
|
+
* var loopback = require('loopback');
|
|
13
|
+
* var dt = new loopback.DateString('2001-01-01');
|
|
14
|
+
*
|
|
15
|
+
* dt.toString();
|
|
16
|
+
* // '2001-01-01'
|
|
17
|
+
* dt._date.toISOString();
|
|
18
|
+
* // '2001-01-01T00:00:00.000Z'
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* You can use this definition on your models as well:
|
|
22
|
+
* ```json
|
|
23
|
+
* {
|
|
24
|
+
* "name": "Person",
|
|
25
|
+
* "base": "PersistedModel",
|
|
26
|
+
* "properties": {
|
|
27
|
+
* "name": {
|
|
28
|
+
* "type": "string"
|
|
29
|
+
* },
|
|
30
|
+
* "dob": {
|
|
31
|
+
* "type": "DateString",
|
|
32
|
+
* "required": true
|
|
33
|
+
* },
|
|
34
|
+
* },
|
|
35
|
+
* "validations": [],
|
|
36
|
+
* "relations": {},
|
|
37
|
+
* "acls": [],
|
|
38
|
+
* "methods": {}
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
* @class DateString
|
|
42
|
+
* @param {String} value
|
|
43
|
+
* @constructor
|
|
44
|
+
*/
|
|
45
|
+
function DateString(value) {
|
|
46
|
+
if (!(this instanceof DateString)) return new DateString(value);
|
|
47
|
+
if (value instanceof DateString) value = value.when;
|
|
48
|
+
if (typeof value !== "string") throw new Error("Input must be a string");
|
|
49
|
+
Object.defineProperty(this, "when", {
|
|
50
|
+
get: () => {
|
|
51
|
+
return this._when;
|
|
52
|
+
},
|
|
53
|
+
set: (val) => {
|
|
54
|
+
const d = new Date(val);
|
|
55
|
+
if (isNaN(d.getTime())) throw new Error("Invalid date");
|
|
56
|
+
else {
|
|
57
|
+
this._when = val;
|
|
58
|
+
this._date = d;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
this.when = value;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns the value of DateString in its original form.
|
|
66
|
+
* @returns {String} The Date as a String.
|
|
67
|
+
*/
|
|
68
|
+
DateString.prototype.toString = function() {
|
|
69
|
+
return this.when;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Returns the JSON representation of the DateString object.
|
|
73
|
+
* @returns {String} A JSON string.
|
|
74
|
+
*/
|
|
75
|
+
DateString.prototype.toJSON = function() {
|
|
76
|
+
return JSON.stringify({ when: this.when });
|
|
77
|
+
};
|
|
78
|
+
DateString.prototype.inspect = function(_depth, _options) {
|
|
79
|
+
return "DateString " + inspect({
|
|
80
|
+
when: this.when,
|
|
81
|
+
_date: this._date
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
if (inspect.custom) DateString.prototype[inspect.custom] = DateString.prototype.inspect;
|
|
85
|
+
}));
|
|
86
|
+
//#endregion
|
|
87
|
+
module.exports = require_date_string();
|
package/dist/lib/geo.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region src/lib/geo.ts
|
|
3
|
+
var require_geo = /* @__PURE__ */ require("../_virtual/_rolldown/runtime.js").__commonJSMin(((exports) => {
|
|
4
|
+
const assert = require("assert");
|
|
5
|
+
/*!
|
|
6
|
+
* Get a near filter from a given where object. For connector use only.
|
|
7
|
+
*/
|
|
8
|
+
exports.nearFilter = function nearFilter(where) {
|
|
9
|
+
const nearResults = [];
|
|
10
|
+
nearSearch(where);
|
|
11
|
+
return !nearResults.length ? false : nearResults;
|
|
12
|
+
function nearSearch(clause, parentKeys) {
|
|
13
|
+
if (typeof clause !== "object") return false;
|
|
14
|
+
parentKeys = parentKeys || [];
|
|
15
|
+
Object.keys(clause).forEach(function(clauseKey) {
|
|
16
|
+
if (typeof clause[clauseKey] !== "object" || !clause[clauseKey]) return;
|
|
17
|
+
if (Array.isArray(clause[clauseKey])) clause[clauseKey].forEach(function(el, index) {
|
|
18
|
+
const ret = nearSearch(el, parentKeys.concat(clauseKey).concat(index));
|
|
19
|
+
if (ret) return ret;
|
|
20
|
+
});
|
|
21
|
+
else if (clause[clauseKey].hasOwnProperty("near")) {
|
|
22
|
+
const result = clause[clauseKey];
|
|
23
|
+
nearResults.push({
|
|
24
|
+
near: result.near,
|
|
25
|
+
maxDistance: result.maxDistance,
|
|
26
|
+
minDistance: result.minDistance,
|
|
27
|
+
unit: result.unit,
|
|
28
|
+
mongoKey: parentKeys.length ? parentKeys.concat(clauseKey) : clauseKey,
|
|
29
|
+
key: clauseKey
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/*!
|
|
36
|
+
* Filter a set of results using the given filters returned by `nearFilter()`.
|
|
37
|
+
* Can support multiple locations, but will include results from all of them.
|
|
38
|
+
*
|
|
39
|
+
* WARNING: "or" operator with GeoPoint does not work as expected, eg:
|
|
40
|
+
* {where: {or: [{location: {near: (29,-90)}},{name:'Sean'}]}}
|
|
41
|
+
* Will actually work as if you had used "and". This is because geo filtering
|
|
42
|
+
* takes place outside of the SQL query, so the result set of "name = Sean" is
|
|
43
|
+
* returned by the database, and then the location filtering happens in the app
|
|
44
|
+
* logic. So the "near" operator is always an "and" of the SQL filters, and "or"
|
|
45
|
+
* of other GeoPoint filters.
|
|
46
|
+
*
|
|
47
|
+
* Additionally, since this step occurs after the SQL result set is returned,
|
|
48
|
+
* if using GeoPoints with pagination the result set may be smaller than the
|
|
49
|
+
* page size. The page size is enforced at the DB level, and then we may
|
|
50
|
+
* remove results at the Geo-app level. If we "limit: 25", but 4 of those results
|
|
51
|
+
* do not have a matching geopoint field, the request will only return 21 results.
|
|
52
|
+
* This may make it erroneously look like a given page is the end of the result set.
|
|
53
|
+
*/
|
|
54
|
+
exports.filter = function(rawResults, filters) {
|
|
55
|
+
const distances = {};
|
|
56
|
+
const results = [];
|
|
57
|
+
filters.forEach(function(filter) {
|
|
58
|
+
const origin = filter.near;
|
|
59
|
+
const max = filter.maxDistance > 0 ? filter.maxDistance : false;
|
|
60
|
+
const min = filter.minDistance > 0 ? filter.minDistance : false;
|
|
61
|
+
const unit = filter.unit;
|
|
62
|
+
const key = filter.key;
|
|
63
|
+
rawResults.forEach(function(result) {
|
|
64
|
+
let loc = result[key];
|
|
65
|
+
if (!loc) return;
|
|
66
|
+
if (!(loc instanceof GeoPoint)) loc = GeoPoint(loc);
|
|
67
|
+
if (typeof loc.lat !== "number") return;
|
|
68
|
+
if (typeof loc.lng !== "number") return;
|
|
69
|
+
const d = GeoPoint.distanceBetween(origin, loc, { type: unit });
|
|
70
|
+
if (min && d < min || max && d > max) return;
|
|
71
|
+
distances[result.id] = d;
|
|
72
|
+
results.push(result);
|
|
73
|
+
});
|
|
74
|
+
results.sort(function(resA, resB) {
|
|
75
|
+
const a = resA[key];
|
|
76
|
+
const b = resB[key];
|
|
77
|
+
if (a && b) {
|
|
78
|
+
const da = distances[resA.id];
|
|
79
|
+
const db = distances[resB.id];
|
|
80
|
+
if (db === da) return 0;
|
|
81
|
+
return da > db ? 1 : -1;
|
|
82
|
+
} else return 0;
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return results;
|
|
86
|
+
};
|
|
87
|
+
exports.GeoPoint = GeoPoint;
|
|
88
|
+
/**
|
|
89
|
+
* The GeoPoint object represents a physical location.
|
|
90
|
+
*
|
|
91
|
+
* For example:
|
|
92
|
+
*
|
|
93
|
+
* ```js
|
|
94
|
+
* var loopback = require(‘loopback’);
|
|
95
|
+
* var here = new loopback.GeoPoint({lat: 10.32424, lng: 5.84978});
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* Embed a latitude / longitude point in a model.
|
|
99
|
+
*
|
|
100
|
+
* ```js
|
|
101
|
+
* var CoffeeShop = loopback.createModel('coffee-shop', {
|
|
102
|
+
* location: 'GeoPoint'
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* You can query LoopBack models with a GeoPoint property and an attached data source using geo-spatial filters and
|
|
107
|
+
* sorting. For example, the following code finds the three nearest coffee shops.
|
|
108
|
+
*
|
|
109
|
+
* ```js
|
|
110
|
+
* CoffeeShop.attachTo(oracle);
|
|
111
|
+
* var here = new GeoPoint({lat: 10.32424, lng: 5.84978});
|
|
112
|
+
* CoffeeShop.find( {where: {location: {near: here}}, limit:3}, function(err, nearbyShops) {
|
|
113
|
+
* console.info(nearbyShops); // [CoffeeShop, ...]
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
* @class GeoPoint
|
|
117
|
+
* @property {Number} lat The latitude in degrees.
|
|
118
|
+
* @property {Number} lng The longitude in degrees.
|
|
119
|
+
*
|
|
120
|
+
* @options {Object} Options Object with two Number properties: lat and long.
|
|
121
|
+
* @property {Number} lat The latitude point in degrees. Range: -90 to 90.
|
|
122
|
+
* @property {Number} lng The longitude point in degrees. Range: -180 to 180.
|
|
123
|
+
*
|
|
124
|
+
* @options {Array} Options Array with two Number entries: [lat,long].
|
|
125
|
+
* @property {Number} lat The latitude point in degrees. Range: -90 to 90.
|
|
126
|
+
* @property {Number} lng The longitude point in degrees. Range: -180 to 180.
|
|
127
|
+
*/
|
|
128
|
+
function GeoPoint(data) {
|
|
129
|
+
if (!(this instanceof GeoPoint)) return new GeoPoint(data);
|
|
130
|
+
if (arguments.length === 2) data = {
|
|
131
|
+
lat: arguments[0],
|
|
132
|
+
lng: arguments[1]
|
|
133
|
+
};
|
|
134
|
+
assert(Array.isArray(data) || typeof data === "object" || typeof data === "string", "must provide valid geo-coordinates array [lat, lng] or object or a \"lat, lng\" string");
|
|
135
|
+
if (typeof data === "string") try {
|
|
136
|
+
data = JSON.parse(data);
|
|
137
|
+
} catch {
|
|
138
|
+
data = data.split(/,\s*/);
|
|
139
|
+
assert(data.length === 2, "must provide a string \"lat,lng\" creating a GeoPoint with a string");
|
|
140
|
+
}
|
|
141
|
+
if (Array.isArray(data)) data = {
|
|
142
|
+
lat: Number(data[0]),
|
|
143
|
+
lng: Number(data[1])
|
|
144
|
+
};
|
|
145
|
+
else {
|
|
146
|
+
data.lng = Number(data.lng);
|
|
147
|
+
data.lat = Number(data.lat);
|
|
148
|
+
}
|
|
149
|
+
assert(typeof data === "object", "must provide a lat and lng object when creating a GeoPoint");
|
|
150
|
+
assert(typeof data.lat === "number" && !isNaN(data.lat), "lat must be a number when creating a GeoPoint");
|
|
151
|
+
assert(typeof data.lng === "number" && !isNaN(data.lng), "lng must be a number when creating a GeoPoint");
|
|
152
|
+
assert(data.lng <= 180, "lng must be <= 180");
|
|
153
|
+
assert(data.lng >= -180, "lng must be >= -180");
|
|
154
|
+
assert(data.lat <= 90, "lat must be <= 90");
|
|
155
|
+
assert(data.lat >= -90, "lat must be >= -90");
|
|
156
|
+
this.lat = data.lat;
|
|
157
|
+
this.lng = data.lng;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Determine the spherical distance between two GeoPoints.
|
|
161
|
+
*
|
|
162
|
+
* @param {GeoPoint} pointA Point A
|
|
163
|
+
* @param {GeoPoint} pointB Point B
|
|
164
|
+
* @options {Object} options Options object with one key, 'type'. See below.
|
|
165
|
+
* @property {String} type Unit of measurement, one of:
|
|
166
|
+
*
|
|
167
|
+
* - `miles` (default)
|
|
168
|
+
* - `radians`
|
|
169
|
+
* - `kilometers`
|
|
170
|
+
* - `meters`
|
|
171
|
+
* - `miles`
|
|
172
|
+
* - `feet`
|
|
173
|
+
* - `degrees`
|
|
174
|
+
*/
|
|
175
|
+
GeoPoint.distanceBetween = function distanceBetween(a, b, options) {
|
|
176
|
+
if (!(a instanceof GeoPoint)) a = GeoPoint(a);
|
|
177
|
+
if (!(b instanceof GeoPoint)) b = GeoPoint(b);
|
|
178
|
+
const x1 = a.lat;
|
|
179
|
+
const y1 = a.lng;
|
|
180
|
+
const x2 = b.lat;
|
|
181
|
+
const y2 = b.lng;
|
|
182
|
+
return geoDistance(x1, y1, x2, y2, options);
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Determine the spherical distance to the given point.
|
|
186
|
+
* Example:
|
|
187
|
+
* ```js
|
|
188
|
+
* var loopback = require(‘loopback’);
|
|
189
|
+
*
|
|
190
|
+
* var here = new loopback.GeoPoint({lat: 10, lng: 10});
|
|
191
|
+
* var there = new loopback.GeoPoint({lat: 5, lng: 5});
|
|
192
|
+
*
|
|
193
|
+
* loopback.GeoPoint.distanceBetween(here, there, {type: 'miles'}) // 438
|
|
194
|
+
* ```
|
|
195
|
+
* @param {Object} point GeoPoint object to which to measure distance.
|
|
196
|
+
* @options {Object} options Options object with one key, 'type'. See below.
|
|
197
|
+
* @property {String} type Unit of measurement, one of:
|
|
198
|
+
*
|
|
199
|
+
* - `miles` (default)
|
|
200
|
+
* - `radians`
|
|
201
|
+
* - `kilometers`
|
|
202
|
+
* - `meters`
|
|
203
|
+
* - `miles`
|
|
204
|
+
* - `feet`
|
|
205
|
+
* - `degrees`
|
|
206
|
+
*/
|
|
207
|
+
GeoPoint.prototype.distanceTo = function(point, options) {
|
|
208
|
+
return GeoPoint.distanceBetween(this, point, options);
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* Simple serialization.
|
|
212
|
+
*/
|
|
213
|
+
GeoPoint.prototype.toString = function() {
|
|
214
|
+
return this.lat + "," + this.lng;
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* @property {Number} DEG2RAD - Factor to convert degrees to radians.
|
|
218
|
+
* @property {Number} RAD2DEG - Factor to convert radians to degrees.
|
|
219
|
+
* @property {Object} EARTH_RADIUS - Radius of the earth.
|
|
220
|
+
*/
|
|
221
|
+
const DEG2RAD = .01745329252;
|
|
222
|
+
const EARTH_RADIUS = {
|
|
223
|
+
kilometers: 6370.99056,
|
|
224
|
+
meters: 6370990.56,
|
|
225
|
+
miles: 3958.75,
|
|
226
|
+
feet: 20902200,
|
|
227
|
+
radians: 1,
|
|
228
|
+
degrees: 57.29577951308
|
|
229
|
+
};
|
|
230
|
+
function geoDistance(x1, y1, x2, y2, options) {
|
|
231
|
+
const type = options && options.type || "miles";
|
|
232
|
+
x1 = x1 * DEG2RAD;
|
|
233
|
+
y1 = y1 * DEG2RAD;
|
|
234
|
+
x2 = x2 * DEG2RAD;
|
|
235
|
+
y2 = y2 * DEG2RAD;
|
|
236
|
+
const haversine = function(a) {
|
|
237
|
+
return Math.pow(Math.sin(a / 2), 2);
|
|
238
|
+
};
|
|
239
|
+
const f = Math.sqrt(haversine(x2 - x1) + Math.cos(x2) * Math.cos(x1) * haversine(y2 - y1));
|
|
240
|
+
return 2 * Math.asin(f) * EARTH_RADIUS[type];
|
|
241
|
+
}
|
|
242
|
+
}));
|
|
243
|
+
//#endregion
|
|
244
|
+
module.exports = require_geo();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region src/lib/globalize.ts
|
|
3
|
+
var require_globalize = /* @__PURE__ */ require("../_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
|
|
4
|
+
const util = require("util");
|
|
5
|
+
function normalizeTemplate(message) {
|
|
6
|
+
return String(message).replace(/\{\{([^}]+)\}\}/g, "$1");
|
|
7
|
+
}
|
|
8
|
+
function formatMessage(message, args) {
|
|
9
|
+
return util.format(normalizeTemplate(message), ...args);
|
|
10
|
+
}
|
|
11
|
+
function StrongGlobalize() {}
|
|
12
|
+
StrongGlobalize.prototype.f = function(message, ...args) {
|
|
13
|
+
return formatMessage(message, args);
|
|
14
|
+
};
|
|
15
|
+
StrongGlobalize.prototype.log = function(message, ...args) {
|
|
16
|
+
console.log(formatMessage(message, args));
|
|
17
|
+
};
|
|
18
|
+
StrongGlobalize.prototype.warn = function(message, ...args) {
|
|
19
|
+
console.warn(formatMessage(message, args));
|
|
20
|
+
};
|
|
21
|
+
StrongGlobalize.prototype.error = function(message, ...args) {
|
|
22
|
+
console.error(formatMessage(message, args));
|
|
23
|
+
};
|
|
24
|
+
const sharedGlobalize = new StrongGlobalize();
|
|
25
|
+
function createGlobalize() {
|
|
26
|
+
return sharedGlobalize;
|
|
27
|
+
}
|
|
28
|
+
createGlobalize.StrongGlobalize = StrongGlobalize;
|
|
29
|
+
createGlobalize.SetRootDir = function() {};
|
|
30
|
+
module.exports = createGlobalize;
|
|
31
|
+
}));
|
|
32
|
+
//#endregion
|
|
33
|
+
module.exports = require_globalize();
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_globalize = require("./globalize.js");
|
|
4
|
+
//#region src/lib/hooks.ts
|
|
5
|
+
var require_hooks = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const deprecated = require("depd")("loopback-datasource-juggler");
|
|
7
|
+
const g = require_lib_globalize();
|
|
8
|
+
/*!
|
|
9
|
+
* Module exports
|
|
10
|
+
*/
|
|
11
|
+
module.exports = Hookable;
|
|
12
|
+
function Hookable() {}
|
|
13
|
+
/**
|
|
14
|
+
* List of hooks available
|
|
15
|
+
*/
|
|
16
|
+
Hookable.afterInitialize = null;
|
|
17
|
+
Hookable.beforeValidate = null;
|
|
18
|
+
Hookable.afterValidate = null;
|
|
19
|
+
Hookable.beforeSave = null;
|
|
20
|
+
Hookable.afterSave = null;
|
|
21
|
+
Hookable.beforeCreate = null;
|
|
22
|
+
Hookable.afterCreate = null;
|
|
23
|
+
Hookable.beforeUpdate = null;
|
|
24
|
+
Hookable.afterUpdate = null;
|
|
25
|
+
Hookable.beforeDestroy = null;
|
|
26
|
+
Hookable.afterDestroy = null;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated
|
|
29
|
+
* Setup a Model-based hook to trigger when the specified action occurs.
|
|
30
|
+
* The trigger is broken up into three segments: `beforeHook`, `work` and
|
|
31
|
+
* `afterHook`.
|
|
32
|
+
* @param {string} actionName The name of the action that triggers the hook.
|
|
33
|
+
* @param {Function} work The 2nd phase of the trigger.
|
|
34
|
+
* @param {*} data The value(s) to provide to the 1st phase (`beforeHook`) call.
|
|
35
|
+
* @callback
|
|
36
|
+
* @param {Function} callback
|
|
37
|
+
*/
|
|
38
|
+
Hookable.prototype.trigger = function trigger(actionName, work, data, callback) {
|
|
39
|
+
const capitalizedName = capitalize(actionName);
|
|
40
|
+
let beforeHook = this.constructor["before" + capitalizedName] || this.constructor["pre" + capitalizedName];
|
|
41
|
+
let afterHook = this.constructor["after" + capitalizedName] || this.constructor["post" + capitalizedName];
|
|
42
|
+
if (actionName === "validate") {
|
|
43
|
+
beforeHook = beforeHook || this.constructor.beforeValidation;
|
|
44
|
+
afterHook = afterHook || this.constructor.afterValidation;
|
|
45
|
+
}
|
|
46
|
+
const inst = this;
|
|
47
|
+
if (actionName !== "initialize") {
|
|
48
|
+
if (beforeHook) deprecateHook(inst.constructor, ["before", "pre"], capitalizedName);
|
|
49
|
+
if (afterHook) deprecateHook(inst.constructor, ["after", "post"], capitalizedName);
|
|
50
|
+
}
|
|
51
|
+
if (work) if (beforeHook) beforeHook.call(inst, function() {
|
|
52
|
+
if (arguments.length) return callback && callback.apply(null, arguments);
|
|
53
|
+
work.call(inst, next);
|
|
54
|
+
}, data);
|
|
55
|
+
else work.call(inst, next);
|
|
56
|
+
else next();
|
|
57
|
+
function next(done) {
|
|
58
|
+
if (afterHook) afterHook.call(inst, done);
|
|
59
|
+
else if (done) done.call(this);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
function capitalize(string) {
|
|
63
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
64
|
+
}
|
|
65
|
+
function deprecateHook(ctor, prefixes, capitalizedName) {
|
|
66
|
+
const candidateNames = prefixes.map(function(p) {
|
|
67
|
+
return p + capitalizedName;
|
|
68
|
+
});
|
|
69
|
+
if (capitalizedName === "Validate") candidateNames.push(prefixes[0] + "Validation");
|
|
70
|
+
let hookName = candidateNames.filter(function(hook) {
|
|
71
|
+
return !!ctor[hook];
|
|
72
|
+
})[0];
|
|
73
|
+
if (!hookName) return;
|
|
74
|
+
if (ctor.modelName) hookName = ctor.modelName + "." + hookName;
|
|
75
|
+
deprecated(g.f("Model hook \"%s\" is deprecated, use Operation hooks instead. {{http://docs.strongloop.com/display/LB/Operation+hooks}}", hookName));
|
|
76
|
+
}
|
|
77
|
+
}));
|
|
78
|
+
//#endregion
|
|
79
|
+
module.exports = require_hooks();
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region src/lib/id-utils.ts
|
|
3
|
+
var require_id_utils = /* @__PURE__ */ require("../_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
|
|
4
|
+
const { randomBytes } = require("crypto");
|
|
5
|
+
const NANOID_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-";
|
|
6
|
+
const UUID_EPOCH_OFFSET = 12219292800000n;
|
|
7
|
+
const nodeId = Buffer.from(randomBytes(6));
|
|
8
|
+
const clockSeqSeed = randomBytes(2);
|
|
9
|
+
nodeId[0] |= 1;
|
|
10
|
+
let clockSeq = (clockSeqSeed[0] << 8 | clockSeqSeed[1]) & 16383;
|
|
11
|
+
let lastMSecs = 0;
|
|
12
|
+
let lastNSecs = 0;
|
|
13
|
+
function nanoid(size = 21) {
|
|
14
|
+
const bytes = randomBytes(size);
|
|
15
|
+
let id = "";
|
|
16
|
+
for (let i = 0; i < size; i++) id += NANOID_ALPHABET[bytes[i] % 64];
|
|
17
|
+
return id;
|
|
18
|
+
}
|
|
19
|
+
function uuidV4() {
|
|
20
|
+
const bytes = Buffer.from(randomBytes(16));
|
|
21
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
22
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
23
|
+
return stringifyUuid(bytes);
|
|
24
|
+
}
|
|
25
|
+
function uuidV1() {
|
|
26
|
+
let msecs = Date.now();
|
|
27
|
+
let nsecs = lastNSecs + 1;
|
|
28
|
+
const dt = msecs - lastMSecs + (nsecs - lastNSecs) / 1e4;
|
|
29
|
+
if (dt < 0) clockSeq = clockSeq + 1 & 16383;
|
|
30
|
+
if (dt < 0 || msecs > lastMSecs) nsecs = 0;
|
|
31
|
+
if (nsecs >= 1e4) {
|
|
32
|
+
msecs += 1;
|
|
33
|
+
nsecs = 0;
|
|
34
|
+
}
|
|
35
|
+
lastMSecs = msecs;
|
|
36
|
+
lastNSecs = nsecs;
|
|
37
|
+
const time = (BigInt(msecs) + UUID_EPOCH_OFFSET) * 10000n + BigInt(nsecs);
|
|
38
|
+
const bytes = Buffer.allocUnsafe(16);
|
|
39
|
+
const timeLow = Number(time & 4294967295n);
|
|
40
|
+
const timeMid = Number(time >> 32n & 65535n);
|
|
41
|
+
const timeHigh = Number(time >> 48n & 4095n);
|
|
42
|
+
bytes[0] = timeLow >>> 24 & 255;
|
|
43
|
+
bytes[1] = timeLow >>> 16 & 255;
|
|
44
|
+
bytes[2] = timeLow >>> 8 & 255;
|
|
45
|
+
bytes[3] = timeLow & 255;
|
|
46
|
+
bytes[4] = timeMid >>> 8 & 255;
|
|
47
|
+
bytes[5] = timeMid & 255;
|
|
48
|
+
bytes[6] = timeHigh >>> 8 & 15 | 16;
|
|
49
|
+
bytes[7] = timeHigh & 255;
|
|
50
|
+
bytes[8] = clockSeq >>> 8 & 63 | 128;
|
|
51
|
+
bytes[9] = clockSeq & 255;
|
|
52
|
+
nodeId.copy(bytes, 10);
|
|
53
|
+
return stringifyUuid(bytes);
|
|
54
|
+
}
|
|
55
|
+
function stringifyUuid(bytes) {
|
|
56
|
+
const hex = bytes.toString("hex");
|
|
57
|
+
return hex.slice(0, 8) + "-" + hex.slice(8, 12) + "-" + hex.slice(12, 16) + "-" + hex.slice(16, 20) + "-" + hex.slice(20, 32);
|
|
58
|
+
}
|
|
59
|
+
module.exports = {
|
|
60
|
+
nanoid,
|
|
61
|
+
uuidV1,
|
|
62
|
+
uuidV4
|
|
63
|
+
};
|
|
64
|
+
}));
|
|
65
|
+
//#endregion
|
|
66
|
+
module.exports = require_id_utils();
|