@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.
Files changed (63) hide show
  1. package/LICENSE +25 -0
  2. package/NOTICE +23 -0
  3. package/README.md +74 -0
  4. package/dist/_virtual/_rolldown/runtime.js +4 -0
  5. package/dist/index.js +26 -0
  6. package/dist/lib/browser.depd.js +13 -0
  7. package/dist/lib/case-utils.js +21 -0
  8. package/dist/lib/connectors/kv-memory.js +158 -0
  9. package/dist/lib/connectors/memory.js +810 -0
  10. package/dist/lib/connectors/transient.js +126 -0
  11. package/dist/lib/dao.js +2445 -0
  12. package/dist/lib/datasource.js +2215 -0
  13. package/dist/lib/date-string.js +87 -0
  14. package/dist/lib/geo.js +244 -0
  15. package/dist/lib/globalize.js +33 -0
  16. package/dist/lib/hooks.js +79 -0
  17. package/dist/lib/id-utils.js +66 -0
  18. package/dist/lib/include.js +795 -0
  19. package/dist/lib/include_utils.js +104 -0
  20. package/dist/lib/introspection.js +37 -0
  21. package/dist/lib/jutil.js +65 -0
  22. package/dist/lib/kvao/delete-all.js +57 -0
  23. package/dist/lib/kvao/delete.js +43 -0
  24. package/dist/lib/kvao/expire.js +35 -0
  25. package/dist/lib/kvao/get.js +34 -0
  26. package/dist/lib/kvao/index.js +28 -0
  27. package/dist/lib/kvao/iterate-keys.js +38 -0
  28. package/dist/lib/kvao/keys.js +55 -0
  29. package/dist/lib/kvao/set.js +39 -0
  30. package/dist/lib/kvao/ttl.js +35 -0
  31. package/dist/lib/list.js +101 -0
  32. package/dist/lib/mixins.js +58 -0
  33. package/dist/lib/model-builder.js +608 -0
  34. package/dist/lib/model-definition.js +231 -0
  35. package/dist/lib/model-utils.js +368 -0
  36. package/dist/lib/model.js +586 -0
  37. package/dist/lib/observer.js +235 -0
  38. package/dist/lib/relation-definition.js +2604 -0
  39. package/dist/lib/relations.js +587 -0
  40. package/dist/lib/scope.js +392 -0
  41. package/dist/lib/transaction.js +183 -0
  42. package/dist/lib/types.js +58 -0
  43. package/dist/lib/utils.js +625 -0
  44. package/dist/lib/validations.js +742 -0
  45. package/dist/package.js +93 -0
  46. package/package.json +85 -0
  47. package/types/common.d.ts +28 -0
  48. package/types/connector.d.ts +52 -0
  49. package/types/datasource.d.ts +324 -0
  50. package/types/date-string.d.ts +21 -0
  51. package/types/inclusion-mixin.d.ts +44 -0
  52. package/types/index.d.ts +36 -0
  53. package/types/kv-model.d.ts +201 -0
  54. package/types/model.d.ts +368 -0
  55. package/types/observer-mixin.d.ts +174 -0
  56. package/types/persisted-model.d.ts +505 -0
  57. package/types/query.d.ts +108 -0
  58. package/types/relation-mixin.d.ts +577 -0
  59. package/types/relation.d.ts +301 -0
  60. package/types/scope.d.ts +92 -0
  61. package/types/transaction-mixin.d.ts +47 -0
  62. package/types/types.d.ts +65 -0
  63. package/types/validation-mixin.d.ts +287 -0
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ //#region src/lib/connectors/transient.ts
3
+ const g = require("../globalize.js")();
4
+ const util = require("util");
5
+ const Connector = require("@vsaas/loopback-connector").Connector;
6
+ const crypto = require("crypto");
7
+ /**
8
+ * Initialize the Transient connector against the given data source
9
+ *
10
+ * @param {DataSource} dataSource The loopback-datasource-juggler dataSource
11
+ * @param {Function} [callback] The callback function
12
+ */
13
+ exports.initialize = function initializeDataSource(dataSource, callback) {
14
+ dataSource.connector = new Transient(null, dataSource.settings);
15
+ dataSource.connector.connect(callback);
16
+ };
17
+ exports.Transient = Transient;
18
+ function Transient(m, settings) {
19
+ settings = settings || {};
20
+ if (typeof settings.generateId === "function") this.generateId = settings.generateId.bind(this);
21
+ this.defaultIdType = settings.defaultIdType || String;
22
+ if (m instanceof Transient) {
23
+ this.isTransaction = true;
24
+ this.constructor.super_.call(this, "transient", settings);
25
+ this._models = m._models;
26
+ } else {
27
+ this.isTransaction = false;
28
+ this.constructor.super_.call(this, "transient", settings);
29
+ }
30
+ }
31
+ util.inherits(Transient, Connector);
32
+ Transient.prototype.getDefaultIdType = function() {
33
+ return this.defaultIdType;
34
+ };
35
+ Transient.prototype.getTypes = function() {
36
+ return [
37
+ "db",
38
+ "nosql",
39
+ "transient"
40
+ ];
41
+ };
42
+ Transient.prototype.connect = function(callback) {
43
+ if (this.isTransaction) this.onTransactionExec = callback;
44
+ else process.nextTick(callback);
45
+ };
46
+ Transient.prototype.generateId = function(model, data, idName) {
47
+ let idType;
48
+ const props = this._models[model].properties;
49
+ if (idName) idType = props[idName] && props[idName].type;
50
+ idType = idType || this.getDefaultIdType();
51
+ if (idType === Number) return Math.floor(Math.random() * 1e4);
52
+ else return crypto.randomBytes(Math.ceil(24 / 2)).toString("hex").slice(0, 24);
53
+ };
54
+ Transient.prototype.exists = function exists(model, id, callback) {
55
+ process.nextTick(function() {
56
+ callback(null, false);
57
+ }.bind(this));
58
+ };
59
+ Transient.prototype.find = function find(model, id, callback) {
60
+ process.nextTick(function() {
61
+ callback(null, null);
62
+ }.bind(this));
63
+ };
64
+ Transient.prototype.all = function all(model, filter, callback) {
65
+ process.nextTick(function() {
66
+ callback(null, []);
67
+ });
68
+ };
69
+ Transient.prototype.count = function count(model, callback, _where) {
70
+ process.nextTick(function() {
71
+ callback(null, 0);
72
+ });
73
+ };
74
+ Transient.prototype.create = function create(model, data, callback) {
75
+ const props = this._models[model].properties;
76
+ const idName = this.idName(model);
77
+ let id = void 0;
78
+ if (idName && props[idName]) {
79
+ id = this.getIdValue(model, data) || this.generateId(model, data, idName);
80
+ id = props[idName] && props[idName].type && props[idName].type(id) || id;
81
+ this.setIdValue(model, data, id);
82
+ }
83
+ this.flush("create", id, callback);
84
+ };
85
+ Transient.prototype.save = function save(model, data, callback) {
86
+ this.flush("save", data, callback);
87
+ };
88
+ Transient.prototype.update = Transient.prototype.updateAll = function updateAll(model, _where, data, cb) {
89
+ this.flush("update", { count: 0 }, cb);
90
+ };
91
+ Transient.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {
92
+ if (!id) {
93
+ const err = new Error(g.f("You must provide an {{id}} when updating attributes!"));
94
+ if (cb) return cb(err);
95
+ else throw err;
96
+ }
97
+ this.setIdValue(model, data, id);
98
+ this.save(model, data, cb);
99
+ };
100
+ Transient.prototype.destroy = function destroy(model, id, callback) {
101
+ this.flush("destroy", null, callback);
102
+ };
103
+ Transient.prototype.destroyAll = function destroyAll(model, where, callback) {
104
+ if (!callback && "function" === typeof where) {
105
+ callback = where;
106
+ where = void 0;
107
+ }
108
+ this.flush("destroyAll", null, callback);
109
+ };
110
+ /*!
111
+ * Flush the cache - noop.
112
+ * @param {Function} callback
113
+ */
114
+ Transient.prototype.flush = function(action, result, callback) {
115
+ process.nextTick(function() {
116
+ if (callback) callback(null, result);
117
+ });
118
+ };
119
+ Transient.prototype.transaction = function() {
120
+ return new Transient(this);
121
+ };
122
+ Transient.prototype.exec = function(callback) {
123
+ this.onTransactionExec();
124
+ setTimeout(callback, 50);
125
+ };
126
+ //#endregion