nesoi 3.0.0 → 3.0.3

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 (185) hide show
  1. package/README.md +10 -0
  2. package/lib/adapters/postgres/src/migrator/bucket.d.ts +20 -0
  3. package/lib/adapters/postgres/src/migrator/bucket.js +184 -0
  4. package/lib/adapters/postgres/src/migrator/csv.d.ts +7 -0
  5. package/lib/adapters/postgres/src/migrator/csv.js +72 -0
  6. package/lib/adapters/postgres/src/migrator/migration.d.ts +2 -18
  7. package/lib/adapters/postgres/src/migrator/migration.js +10 -158
  8. package/lib/adapters/postgres/src/migrator/migrator.js +8 -5
  9. package/lib/adapters/postgres/src/migrator/runner.d.ts +16 -6
  10. package/lib/adapters/postgres/src/migrator/runner.js +103 -34
  11. package/lib/adapters/postgres/src/postgres.bucket_adapter.d.ts +19 -22
  12. package/lib/adapters/postgres/src/postgres.bucket_adapter.js +116 -100
  13. package/lib/adapters/postgres/src/postgres.cli.d.ts +23 -3
  14. package/lib/adapters/postgres/src/postgres.cli.js +70 -10
  15. package/lib/adapters/postgres/src/postgres.config.d.ts +5 -0
  16. package/lib/adapters/postgres/src/postgres.config.js +2 -0
  17. package/lib/adapters/postgres/src/postgres.nql.d.ts +7 -3
  18. package/lib/adapters/postgres/src/postgres.nql.js +86 -32
  19. package/lib/adapters/postgres/src/postgres.provider.d.ts +18 -0
  20. package/lib/adapters/postgres/src/postgres.provider.js +77 -0
  21. package/lib/adapters/postgres/test/postgres.bucket_adapter.test.js +76 -39
  22. package/lib/compiler/apps/monolyth/monolyth_compiler.d.ts +3 -0
  23. package/lib/compiler/apps/monolyth/monolyth_compiler.js +24 -0
  24. package/lib/compiler/apps/monolyth/stages/2_build_typescript_stage.js +2 -1
  25. package/lib/compiler/apps/monolyth/stages/5_dump_cli_stage.js +1 -1
  26. package/lib/compiler/elements/bucket.element.js +26 -11
  27. package/lib/compiler/elements/constants.element.js +1 -1
  28. package/lib/compiler/elements/element.d.ts +2 -0
  29. package/lib/compiler/elements/message.element.js +4 -4
  30. package/lib/compiler/helpers/dump_helpers.js +5 -2
  31. package/lib/compiler/stages/7_dump_stage.js +2 -0
  32. package/lib/compiler/treeshake.js +9 -37
  33. package/lib/compiler/typescript/bridge/extract.js +12 -0
  34. package/lib/compiler/typescript/bridge/inject.js +3 -0
  35. package/lib/compiler/typescript/bridge/organize.js +3 -3
  36. package/lib/elements/blocks/block.builder.js +4 -2
  37. package/lib/elements/blocks/job/internal/resource_job.builder.d.ts +22 -20
  38. package/lib/elements/blocks/job/internal/resource_job.d.ts +2 -1
  39. package/lib/elements/blocks/job/internal/resource_job.js +17 -4
  40. package/lib/elements/blocks/job/job.js +3 -0
  41. package/lib/elements/blocks/job/job.types.d.ts +7 -0
  42. package/lib/elements/blocks/job/job.types.js +2 -0
  43. package/lib/elements/blocks/machine/machine.js +3 -2
  44. package/lib/elements/blocks/resource/resource.builder.js +2 -4
  45. package/lib/elements/blocks/resource/resource.d.ts +5 -3
  46. package/lib/elements/blocks/resource/resource.js +26 -17
  47. package/lib/elements/edge/controller/adapters/controller_adapter.d.ts +2 -1
  48. package/lib/elements/edge/controller/adapters/controller_adapter.js +11 -2
  49. package/lib/elements/edge/controller/controller.builder.d.ts +4 -5
  50. package/lib/elements/edge/controller/controller.builder.js +7 -7
  51. package/lib/elements/edge/controller/controller.d.ts +2 -1
  52. package/lib/elements/edge/controller/controller.js +8 -6
  53. package/lib/elements/entities/bucket/adapters/bucket_adapter.d.ts +61 -23
  54. package/lib/elements/entities/bucket/adapters/bucket_adapter.js +22 -13
  55. package/lib/elements/entities/bucket/adapters/memory.bucket_adapter.d.ts +21 -22
  56. package/lib/elements/entities/bucket/adapters/memory.bucket_adapter.js +68 -2
  57. package/lib/elements/entities/bucket/adapters/memory.nql.d.ts +10 -6
  58. package/lib/elements/entities/bucket/adapters/memory.nql.js +38 -3
  59. package/lib/elements/entities/bucket/adapters/slow_memory.bucket_adapter.d.ts +0 -20
  60. package/lib/elements/entities/bucket/adapters/slow_memory.bucket_adapter.js +46 -30
  61. package/lib/elements/entities/bucket/bucket.builder.d.ts +8 -2
  62. package/lib/elements/entities/bucket/bucket.builder.js +13 -19
  63. package/lib/elements/entities/bucket/bucket.config.d.ts +5 -1
  64. package/lib/elements/entities/bucket/bucket.d.ts +180 -19
  65. package/lib/elements/entities/bucket/bucket.js +658 -48
  66. package/lib/elements/entities/bucket/bucket.schema.d.ts +7 -1
  67. package/lib/elements/entities/bucket/bucket.schema.js +2 -1
  68. package/lib/elements/entities/bucket/bucket.types.d.ts +2 -7
  69. package/lib/elements/entities/bucket/cache/bucket_cache.d.ts +6 -2
  70. package/lib/elements/entities/bucket/cache/bucket_cache.js +12 -12
  71. package/lib/elements/entities/bucket/graph/bucket_graph.d.ts +32 -5
  72. package/lib/elements/entities/bucket/graph/bucket_graph.js +80 -111
  73. package/lib/elements/entities/bucket/graph/bucket_graph.schema.d.ts +3 -6
  74. package/lib/elements/entities/bucket/graph/bucket_graph.schema.js +1 -4
  75. package/lib/elements/entities/bucket/graph/bucket_graph_link.builder.d.ts +3 -7
  76. package/lib/elements/entities/bucket/graph/bucket_graph_link.builder.js +6 -2
  77. package/lib/elements/entities/bucket/model/bucket_model.builder.js +1 -1
  78. package/lib/elements/entities/bucket/model/bucket_model.convert.js +3 -3
  79. package/lib/elements/entities/bucket/model/bucket_model.schema.d.ts +37 -8
  80. package/lib/elements/entities/bucket/model/bucket_model.schema.js +25 -4
  81. package/lib/elements/entities/bucket/model/bucket_model_field.builder.d.ts +33 -14
  82. package/lib/elements/entities/bucket/model/bucket_model_field.builder.js +56 -13
  83. package/lib/elements/entities/bucket/query/nql.schema.d.ts +1 -0
  84. package/lib/elements/entities/bucket/query/nql_compiler.js +13 -2
  85. package/lib/elements/entities/bucket/query/nql_engine.d.ts +11 -4
  86. package/lib/elements/entities/bucket/query/nql_engine.js +20 -11
  87. package/lib/elements/entities/bucket/view/bucket_view.js +63 -35
  88. package/lib/elements/entities/bucket/view/bucket_view.schema.d.ts +5 -2
  89. package/lib/elements/entities/bucket/view/bucket_view_field.builder.d.ts +6 -2
  90. package/lib/elements/entities/bucket/view/bucket_view_field.builder.js +21 -15
  91. package/lib/elements/entities/constants/constants.schema.d.ts +1 -1
  92. package/lib/elements/entities/drive/drive_adapter.d.ts +36 -0
  93. package/lib/elements/entities/drive/drive_adapter.js +10 -0
  94. package/lib/elements/entities/drive/local.drive_adapter.d.ts +8 -0
  95. package/lib/elements/entities/drive/local.drive_adapter.js +28 -0
  96. package/lib/elements/entities/message/message.schema.d.ts +1 -0
  97. package/lib/elements/entities/message/message.schema.js +33 -0
  98. package/lib/elements/entities/message/message_parser.d.ts +5 -1
  99. package/lib/elements/entities/message/message_parser.js +56 -35
  100. package/lib/elements/entities/message/template/message_template.schema.d.ts +10 -8
  101. package/lib/elements/entities/message/template/message_template_field.builder.d.ts +16 -6
  102. package/lib/elements/entities/message/template/message_template_field.builder.js +25 -0
  103. package/lib/elements/entities/message/template/message_template_parser.js +2 -1
  104. package/lib/engine/apps/app.config.d.ts +32 -11
  105. package/lib/engine/apps/app.config.js +12 -0
  106. package/lib/engine/apps/app.d.ts +2 -0
  107. package/lib/engine/apps/app.js +3 -0
  108. package/lib/engine/apps/inline.app.d.ts +5 -3
  109. package/lib/engine/apps/inline.app.js +27 -12
  110. package/lib/engine/apps/monolyth/monolyth.app.d.ts +4 -2
  111. package/lib/engine/apps/monolyth/monolyth.app.js +22 -10
  112. package/lib/engine/auth/authn.d.ts +5 -1
  113. package/lib/engine/auth/zero.authn_provider.d.ts +4 -2
  114. package/lib/engine/auth/zero.authn_provider.js +2 -2
  115. package/lib/engine/cli/cli.d.ts +3 -1
  116. package/lib/engine/cli/cli.js +22 -3
  117. package/lib/engine/cli/cli_adapter.d.ts +2 -1
  118. package/lib/engine/cli/cli_adapter.js +2 -1
  119. package/lib/engine/cli/cli_input.d.ts +19 -0
  120. package/lib/engine/cli/cli_input.js +207 -0
  121. package/lib/engine/cli/ui.d.ts +1 -1
  122. package/lib/engine/cli/ui.js +2 -2
  123. package/lib/engine/daemon.d.ts +3 -2
  124. package/lib/engine/daemon.js +14 -2
  125. package/lib/engine/data/date.js +2 -2
  126. package/lib/engine/data/datetime.d.ts +40 -4
  127. package/lib/engine/data/datetime.js +70 -11
  128. package/lib/engine/data/decimal.d.ts +1 -1
  129. package/lib/engine/data/decimal.js +3 -3
  130. package/lib/engine/data/error.d.ts +21 -4
  131. package/lib/engine/data/error.js +23 -7
  132. package/lib/engine/data/file.d.ts +23 -0
  133. package/lib/engine/data/file.js +53 -0
  134. package/lib/engine/data/json.d.ts +6 -0
  135. package/lib/engine/data/json.js +26 -0
  136. package/lib/engine/data/obj.d.ts +1 -1
  137. package/lib/engine/data/trash.d.ts +14 -0
  138. package/lib/engine/data/trash.js +2 -0
  139. package/lib/engine/data/tree.d.ts +7 -12
  140. package/lib/engine/data/tree.js +101 -49
  141. package/lib/engine/module.d.ts +2 -1
  142. package/lib/engine/module.js +2 -5
  143. package/lib/engine/space.d.ts +1 -0
  144. package/lib/engine/space.js +6 -0
  145. package/lib/engine/transaction/nodes/bucket.trx_node.d.ts +156 -24
  146. package/lib/engine/transaction/nodes/bucket.trx_node.js +297 -467
  147. package/lib/engine/transaction/nodes/bucket_query.trx_node.d.ts +4 -2
  148. package/lib/engine/transaction/nodes/bucket_query.trx_node.js +27 -15
  149. package/lib/engine/transaction/nodes/job.trx_node.d.ts +2 -1
  150. package/lib/engine/transaction/nodes/job.trx_node.js +6 -0
  151. package/lib/engine/transaction/trx.d.ts +5 -2
  152. package/lib/engine/transaction/trx.js +2 -2
  153. package/lib/engine/transaction/trx_engine.config.d.ts +1 -3
  154. package/lib/engine/transaction/trx_engine.d.ts +2 -2
  155. package/lib/engine/transaction/trx_engine.js +14 -11
  156. package/lib/engine/transaction/trx_node.d.ts +14 -4
  157. package/lib/engine/transaction/trx_node.js +50 -8
  158. package/lib/engine/tree.d.ts +1 -1
  159. package/lib/engine/util/crypto.d.ts +50 -0
  160. package/lib/engine/util/crypto.js +89 -0
  161. package/lib/engine/util/deep.d.ts +5 -0
  162. package/lib/engine/util/deep.js +46 -0
  163. package/lib/engine/util/dotenv.d.ts +2 -8
  164. package/lib/engine/util/dotenv.js +14 -36
  165. package/lib/engine/util/hash.d.ts +3 -0
  166. package/lib/engine/util/hash.js +23 -0
  167. package/lib/engine/util/log.js +1 -1
  168. package/lib/engine/util/mime.d.ts +10 -0
  169. package/lib/engine/util/mime.js +389 -0
  170. package/lib/engine/util/parse.d.ts +6 -5
  171. package/lib/engine/util/parse.js +16 -15
  172. package/lib/engine/util/path.d.ts +3 -0
  173. package/lib/engine/util/path.js +92 -0
  174. package/lib/engine/util/rules.d.ts +4 -0
  175. package/lib/engine/util/rules.js +12 -0
  176. package/package.json +1 -1
  177. package/tools/compile.js +2 -2
  178. package/tools/dotenv.d.ts +1 -0
  179. package/tools/dotenv.js +4 -0
  180. package/tools/joaquin/job.d.ts +5 -5
  181. package/tools/joaquin/mock.d.ts +23 -2
  182. package/tools/joaquin/mock.js +127 -21
  183. package/tsconfig.build.tsbuildinfo +1 -1
  184. package/lib/adapters/postgres/test/postgres.bucket_query.test.d.ts +0 -0
  185. package/lib/adapters/postgres/test/postgres.bucket_query.test.js +0 -136
@@ -1,522 +1,352 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BucketTrxNode = void 0;
4
- const error_1 = require("../../data/error");
3
+ exports.BucketUnsafeTrxNode = exports.BucketTrxNode = void 0;
5
4
  const trx_node_1 = require("../trx_node");
6
5
  const bucket_query_trx_node_1 = require("./bucket_query.trx_node");
7
6
  class BucketTrxNode {
8
7
  constructor(parentTrx, bucket) {
9
8
  this.parentTrx = parentTrx;
10
9
  this.bucket = bucket;
10
+ this.enableTenancy = true;
11
11
  }
12
- // Read/View One
13
- async readOne(id) {
12
+ /*
13
+ Modifiers
14
+ */
15
+ get no_tenancy() {
16
+ this.enableTenancy = false;
17
+ return this;
18
+ }
19
+ /*
20
+ Wrap
21
+ */
22
+ async wrap(action, input, fn, fmtTrxOut) {
14
23
  const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
15
- await trx_node_1.TrxNode.open(trx, 'readOne', { id });
16
- let obj;
24
+ await trx_node_1.TrxNode.open(trx, action, input);
25
+ let out;
17
26
  try {
18
- obj = await this.bucket.readOne(trx, id);
27
+ out = await fn(trx);
19
28
  }
20
29
  catch (e) {
21
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
30
+ await trx_node_1.TrxNode.error(trx, e);
22
31
  throw e;
23
32
  }
24
- await trx_node_1.TrxNode.ok(trx, obj);
25
- return obj;
33
+ await trx_node_1.TrxNode.ok(trx, fmtTrxOut ? fmtTrxOut(out) : out);
34
+ return out;
26
35
  }
36
+ /*
37
+ Read/View One
38
+ */
39
+ /**
40
+ * Returns one object by `id`, without pre-formatting,
41
+ * or `undefined` if the object was not found.
42
+ */
43
+ async readOne(id) {
44
+ return this.wrap('readOne', { id }, trx => this.bucket.readOne(trx, id, {
45
+ silent: true,
46
+ no_tenancy: !this.enableTenancy
47
+ }));
48
+ }
49
+ /**
50
+ * Returns one object by `id` formated with the specified view,
51
+ * or `undefined` if the object was not found.
52
+ * - The formating process can impact performance. If you just need
53
+ * the raw obj, it's recommended to use `readOne` instead.
54
+ */
27
55
  async viewOne(id, view = 'default') {
28
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
29
- await trx_node_1.TrxNode.open(trx, 'viewOne', { id, view });
30
- let obj;
31
- try {
32
- obj = await this.bucket.viewOne(trx, id, view);
33
- }
34
- catch (e) {
35
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
36
- throw e;
37
- }
38
- await trx_node_1.TrxNode.ok(trx, obj);
39
- return obj;
56
+ return this.wrap('viewOne', { id }, trx => this.bucket.viewOne(trx, id, view, {
57
+ silent: true,
58
+ no_tenancy: !this.enableTenancy
59
+ }));
40
60
  }
61
+ /**
62
+ * Returns one object by `id`, without pre-formatting,
63
+ * or **throws an exception** if the object was not found.
64
+ */
41
65
  async readOneOrFail(id) {
42
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
43
- await trx_node_1.TrxNode.open(trx, 'readOneOrFail', { id });
44
- let obj;
45
- try {
46
- obj = await this.bucket.readOne(trx, id);
47
- }
48
- catch (e) {
49
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
50
- throw e;
51
- }
52
- if (!obj) {
53
- const e = error_1.NesoiError.Bucket.ObjNotFound({ bucket: this.bucket.schema.alias, id: id });
54
- await trx_node_1.TrxNode.error(trx, e);
55
- throw e;
56
- }
57
- await trx_node_1.TrxNode.ok(trx, obj);
58
- return obj;
66
+ return this.wrap('readOneOrFail', { id }, trx => this.bucket.readOne(trx, id, {
67
+ no_tenancy: !this.enableTenancy
68
+ }));
59
69
  }
70
+ /**
71
+ * Returns one object by `id`, without pre-formatting,
72
+ * or **throws an exception** if the object was not found.
73
+ * - The formating process can impact performance. If you just need
74
+ * the raw obj, it's recommended to use `readOneOrFail` instead.
75
+ */
60
76
  async viewOneOrFail(id, view = 'default') {
61
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
62
- await trx_node_1.TrxNode.open(trx, 'viewOneOrFail', { id, view });
63
- let obj;
64
- try {
65
- obj = await this.bucket.viewOne(trx, id, view);
66
- }
67
- catch (e) {
68
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
69
- throw e;
70
- }
71
- if (!obj) {
72
- const e = error_1.NesoiError.Bucket.ObjNotFound({ bucket: this.bucket.schema.alias, id: id });
73
- await trx_node_1.TrxNode.error(trx, e);
74
- throw e;
75
- }
76
- await trx_node_1.TrxNode.ok(trx, obj);
77
- return obj;
77
+ return this.wrap('viewOneOrFail', { id }, trx => this.bucket.viewOne(trx, id, view, {
78
+ no_tenancy: !this.enableTenancy
79
+ }));
78
80
  }
79
- // All
80
- async readAll(pagination, order) {
81
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
82
- await trx_node_1.TrxNode.open(trx, 'readAll', { pagination, order });
83
- let objs;
84
- try {
85
- objs = await this.bucket.readAll(trx, pagination, order);
86
- }
87
- catch (e) {
88
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
89
- throw e;
90
- }
91
- await trx_node_1.TrxNode.ok(trx, { length: objs.length });
92
- return objs;
81
+ /*
82
+ Read/View All
83
+ */
84
+ /**
85
+ * Returns a list of all objects, without pre-formatting.
86
+ */
87
+ async readAll() {
88
+ return this.wrap('readAll', {}, trx => this.bucket.readAll(trx, {
89
+ no_tenancy: !this.enableTenancy
90
+ }), objs => ({ length: objs.length }));
93
91
  }
94
- async viewAll(view = 'default', pagination, order) {
95
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
96
- await trx_node_1.TrxNode.open(trx, 'viewAll', { view, pagination, order });
97
- let objs;
98
- try {
99
- objs = await this.bucket.viewAll(trx, view, pagination, order);
100
- }
101
- catch (e) {
102
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
103
- throw e;
104
- }
105
- await trx_node_1.TrxNode.ok(trx, { length: objs.length });
106
- return objs;
92
+ /**
93
+ * Returns a list of all objects formated with the specified view.
94
+ * - The formating process can impact performance. If you just need
95
+ * the raw obj, it's recommended to use `readAll` instead.
96
+ */
97
+ async viewAll(view = 'default') {
98
+ return this.wrap('viewAll', {}, trx => this.bucket.viewAll(trx, view, {
99
+ no_tenancy: !this.enableTenancy
100
+ }), objs => ({ length: objs.length }));
107
101
  }
108
- // Query
102
+ /*
103
+ Query
104
+ */
105
+ /**
106
+ * Returns a list containing the results of the query.
107
+ */
109
108
  query(query, view = 'default') {
110
109
  const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
111
- return new bucket_query_trx_node_1.BucketQueryTrxNode(trx, this.bucket, query, view);
110
+ return new bucket_query_trx_node_1.BucketQueryTrxNode(trx, this.bucket, query, view, this.enableTenancy);
112
111
  }
113
- // Graph Links
114
- async readLink(id, link, view = 'default') {
115
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
116
- await trx_node_1.TrxNode.open(trx, 'readLink', { id, link });
117
- let obj;
118
- let linkObj;
119
- try {
120
- obj = await this.bucket.readOne(trx, id);
121
- if (!obj) {
122
- const schema = this.bucket.schema.graph.links[link];
123
- if (schema.many) {
124
- return [];
125
- }
126
- return undefined;
127
- }
128
- linkObj = await this.bucket.graph.readLink(trx, link, obj, view);
129
- }
130
- catch (e) {
131
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
132
- throw e;
133
- }
134
- await trx_node_1.TrxNode.ok(trx, linkObj);
135
- return linkObj;
112
+ /*
113
+ Graph
114
+ */
115
+ /**
116
+ * Returns one or more objects referenced by the graph link,
117
+ * or `undefined` if the graph link doesn't resolve.
118
+ */
119
+ async readLink(id, link) {
120
+ return this.wrap('readLink', { id, link }, trx => this.bucket.readLink(trx, id, link, {
121
+ silent: true,
122
+ no_tenancy: !this.enableTenancy
123
+ }));
136
124
  }
137
- async readLinkOrFail(id, link, view = 'default') {
138
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
139
- await trx_node_1.TrxNode.open(trx, 'readLinkOrFail', { id, link });
140
- let obj;
141
- let linkObj;
142
- try {
143
- obj = await this.bucket.readOne(trx, id);
144
- if (!obj) {
145
- const e = error_1.NesoiError.Bucket.ObjNotFound({ bucket: this.bucket.schema.alias, id: id });
146
- await trx_node_1.TrxNode.error(trx, e);
147
- throw e;
148
- }
149
- linkObj = await this.bucket.graph.readLink(trx, link, obj, view);
150
- if (!linkObj) {
151
- const e = error_1.NesoiError.Bucket.Graph.LinkNotFound({ bucket: this.bucket.schema.alias, link: link });
152
- await trx_node_1.TrxNode.error(trx, e);
153
- throw e;
154
- }
155
- }
156
- catch (e) {
157
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
158
- throw e;
159
- }
160
- await trx_node_1.TrxNode.ok(trx);
161
- return linkObj;
125
+ /**
126
+ * Returns one or more objects referenced by the graph link built with a view,
127
+ * or `undefined` if the graph link doesn't resolve.
128
+ */
129
+ async viewLink(id, link, view = 'default') {
130
+ return this.wrap('viewLink', { id, link, view }, trx => this.bucket.viewLink(trx, id, link, view, {
131
+ silent: true,
132
+ no_tenancy: !this.enableTenancy
133
+ }));
162
134
  }
163
- async hasLink(id, link) {
164
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
165
- await trx_node_1.TrxNode.open(trx, 'hasLink', { id, link });
166
- let obj;
167
- let result;
168
- try {
169
- obj = await this.bucket.readOne(trx, id);
170
- if (!obj) {
171
- return undefined;
172
- }
173
- result = await this.bucket.graph.hasLink(trx, id, obj);
174
- }
175
- catch (e) {
176
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
177
- throw e;
178
- }
179
- await trx_node_1.TrxNode.ok(trx, { result });
180
- return result;
135
+ /**
136
+ * Returns one or more objects referenced by the graph link,
137
+ * or **throws an exception** if the graph link doesn't resolve.
138
+ */
139
+ async readLinkOrFail(id, link) {
140
+ return this.wrap('readLinkOrFail', { id, link }, trx => this.bucket.readLink(trx, id, link, {
141
+ no_tenancy: !this.enableTenancy
142
+ }));
181
143
  }
182
- // Put / Create / Update
183
144
  /**
184
- * Updates an object by passing it directly to the bucket adapter,
185
- * without checking if it exists. This creates an object if a new
186
- * id is passed.
145
+ * Returns one or more objects referenced by the graph link built with a view,
146
+ * or **throws an exception** if the graph link doesn't resolve.
187
147
  */
188
- async put(obj) {
189
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
190
- await trx_node_1.TrxNode.open(trx, 'put', { obj });
191
- let _obj;
192
- try {
193
- _obj = await this.bucket.put(trx, obj);
194
- }
195
- catch (e) {
196
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
197
- throw e;
198
- }
199
- await trx_node_1.TrxNode.ok(trx);
200
- return _obj;
148
+ async viewLinkOrFail(id, link, view = 'default') {
149
+ return this.wrap('viewLinkOrFail', { id, link, view }, trx => this.bucket.viewLink(trx, id, link, view, {
150
+ no_tenancy: !this.enableTenancy
151
+ }));
152
+ }
153
+ /**
154
+ * Returns `true` if the graph link resolves to at least 1 object.
155
+ */
156
+ async hasLink(id, link) {
157
+ return this.wrap('hasLink', { id, link }, trx => this.bucket.hasLink(trx, id, link, {
158
+ no_tenancy: !this.enableTenancy
159
+ }));
201
160
  }
161
+ /*
162
+ Create
163
+ */
202
164
  /**
203
- * Creates an object by passing it to the bucket adapter,
204
- * without an ID (it's removed if passed).
205
- * This is a safer version of `put`, which ensures no update will happen.
165
+ * Creates an object by passing it to the bucket adapter.
166
+ * Also creates the compositions of this bucket, from the
167
+ * `#composition` field passed in the message.
168
+ *
169
+ * - If `#composition` is wrong, this will throw an exception
206
170
  */
207
171
  async create(obj) {
208
- delete obj['id'];
209
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
210
- await trx_node_1.TrxNode.open(trx, 'create', { obj });
211
- const composition = obj['#composition'] || {};
212
- delete obj['#composition'];
213
- const input = Object.assign({}, this.bucket.schema.model.defaults, obj);
214
- let _obj;
215
- try {
216
- _obj = await this.bucket.put(trx, input);
217
- // Composition
218
- for (const link of Object.values(this.bucket.schema.graph.links)) {
219
- if (link.rel !== 'composition')
220
- continue;
221
- const linkObj = composition[link.name];
222
- if (!linkObj) {
223
- const e = error_1.NesoiError.Bucket.MissingComposition({ method: 'create', bucket: this.bucket.schema.name, link: link.name });
224
- await trx_node_1.TrxNode.error(trx, e);
225
- throw e;
226
- }
227
- if (link.many) {
228
- if (!Array.isArray(linkObj)) {
229
- const e = error_1.NesoiError.Bucket.CompositionValueShouldBeArray({ method: 'create', bucket: this.bucket.schema.name, link: link.name });
230
- await trx_node_1.TrxNode.error(trx, e);
231
- throw e;
232
- }
233
- for (const linkObjItem of linkObj) {
234
- await trx.bucket(link.bucket.refName).create(linkObjItem);
235
- }
236
- }
237
- else {
238
- await trx.bucket(link.bucket.refName).create(linkObj);
239
- }
240
- }
241
- }
242
- catch (e) {
243
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
244
- throw e;
245
- }
246
- await trx_node_1.TrxNode.ok(trx);
247
- return _obj;
172
+ return this.wrap('create', { obj }, trx => this.bucket.create(trx, obj), () => undefined);
248
173
  }
174
+ /*
175
+ Update (Patch, Replace)
176
+ */
249
177
  /**
250
- * Reads the id from the bucket adapter to ensure the object exists
251
- * before updating.
252
- * This is a safer version of `put`, suited for updating data.
178
+ * Reads one object by `id` and `patch` (modify) it based on the one passed as an argument.
179
+ * Also patches the compositions of this bucket, from the
180
+ * `#composition` field passed in the message.
181
+ *
182
+ * - If the object is not found, this will throw an exception
183
+ * - If `#composition` is wrong, this will throw an exception
184
+ * - The read query before updating might impact performance and be unnecessary
185
+ * when you're updating from code that's sure the object exists. In that case,
186
+ * you can use `unsafe.patch`, which doesn't read prior to writing.
187
+ */
188
+ async patch(obj) {
189
+ return this.wrap('patch', { obj }, trx => this.bucket.update(trx, obj, {
190
+ mode: 'patch',
191
+ no_tenancy: !this.enableTenancy
192
+ }), () => undefined);
193
+ }
194
+ /**
195
+ * Reads one object by `id` and `replace` it with the one passed as an argument.
196
+ * Also patches the compositions of this bucket, from the
197
+ * `#composition` field passed in the message.
198
+ *
199
+ * - If the object is not found, this will throw an exception
200
+ * - If `#composition` is wrong, this will throw an exception
201
+ * - The read query before updating might impact performance and be unnecessary
202
+ * when you're updating from code that's sure the object exists. In that case,
203
+ * you can use `unsafe.replace`, which doesn't read prior to writing.
253
204
  */
254
205
  async replace(obj) {
255
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
256
- await trx_node_1.TrxNode.open(trx, 'replace', { obj });
257
- const composition = obj['#composition'] || {};
258
- delete obj['#composition'];
259
- let newObj;
260
- try {
261
- const oldObj = await this.bucket.readOne(trx, obj['id']);
262
- if (!oldObj) {
263
- const e = error_1.NesoiError.Bucket.ObjNotFound({ bucket: this.bucket.schema.alias, id: obj['id'] });
264
- await trx_node_1.TrxNode.error(trx, e);
265
- throw e;
266
- }
267
- newObj = await this.bucket.put(trx, obj);
268
- // Composition
269
- for (const link of Object.values(this.bucket.schema.graph.links)) {
270
- if (link.rel !== 'composition')
271
- continue;
272
- const linkObj = composition[link.name];
273
- if (!linkObj) {
274
- const e = error_1.NesoiError.Bucket.MissingComposition({ method: 'replace', bucket: this.bucket.schema.name, link: link.name });
275
- await trx_node_1.TrxNode.error(trx, e);
276
- throw e;
277
- }
278
- if (link.many) {
279
- if (!Array.isArray(linkObj)) {
280
- const e = error_1.NesoiError.Bucket.CompositionValueShouldBeArray({ method: 'replace', bucket: this.bucket.schema.name, link: link.name });
281
- await trx_node_1.TrxNode.error(trx, e);
282
- throw e;
283
- }
284
- for (const linkObjItem of linkObj) {
285
- await trx.bucket(link.bucket.refName).replace(linkObjItem);
286
- }
287
- }
288
- else {
289
- await trx.bucket(link.bucket.refName).replace(linkObj);
290
- }
291
- }
292
- }
293
- catch (e) {
294
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
295
- throw e;
296
- }
297
- await trx_node_1.TrxNode.ok(trx);
298
- return newObj;
206
+ return this.wrap('replace', { obj }, trx => this.bucket.update(trx, obj, {
207
+ mode: 'replace',
208
+ no_tenancy: !this.enableTenancy
209
+ }), () => undefined);
299
210
  }
211
+ /*
212
+ Put
213
+ */
300
214
  /**
301
- * Reads the id from the bucket adapter to ensure the object exists
302
- * before updating. Applies changes to the object instead of replacing it.
215
+ * Creates or replaces (by `id`) the object passed as an argument.
216
+ * Does the same for compositions of this bucket, from the
217
+ * `#composition` field passed in the message.
218
+ *
219
+ * - If `#composition` is wrong, this will throw an exception.
220
+ * - This will **REPLACE** objects and it's compositions if they already exist,
221
+ * so there might be unexpected data loss, use it carefully.
222
+ *
223
+ * **WARNING** Tenancy currently not implemented for put.
303
224
  */
304
- async patch(obj) {
305
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
306
- await trx_node_1.TrxNode.open(trx, 'patch', { obj });
307
- const composition = obj['#composition'] || {};
308
- delete obj['#composition'];
309
- let newObj;
310
- try {
311
- const oldObj = await this.bucket.readOne(trx, obj['id']);
312
- if (!oldObj) {
313
- const e = error_1.NesoiError.Bucket.ObjNotFound({ bucket: this.bucket.schema.alias, id: obj['id'] });
314
- await trx_node_1.TrxNode.error(trx, e);
315
- throw e;
316
- }
317
- // TODO: deep merge
318
- const putObj = Object.assign({}, oldObj, obj);
319
- newObj = await this.bucket.put(trx, putObj);
320
- // Composition
321
- for (const link of Object.values(this.bucket.schema.graph.links)) {
322
- if (link.rel !== 'composition')
323
- continue;
324
- const linkObj = composition[link.name];
325
- if (!linkObj) {
326
- const e = error_1.NesoiError.Bucket.MissingComposition({ method: 'patch', bucket: this.bucket.schema.name, link: link.name });
327
- await trx_node_1.TrxNode.error(trx, e);
328
- throw e;
329
- }
330
- if (link.many) {
331
- if (!Array.isArray(linkObj)) {
332
- const e = error_1.NesoiError.Bucket.CompositionValueShouldBeArray({ method: 'patch', bucket: this.bucket.schema.name, link: link.name });
333
- await trx_node_1.TrxNode.error(trx, e);
334
- throw e;
335
- }
336
- for (const linkObjItem of linkObj) {
337
- await trx.bucket(link.bucket.refName).patch(linkObjItem);
338
- }
339
- }
340
- else {
341
- await trx.bucket(link.bucket.refName).patch(linkObj);
342
- }
343
- }
344
- }
345
- catch (e) {
346
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
347
- throw e;
348
- }
349
- await trx_node_1.TrxNode.ok(trx);
350
- return newObj;
225
+ async put(obj) {
226
+ return this.wrap('put', { obj }, trx => this.bucket.put(trx, obj), () => undefined);
351
227
  }
352
- // Delete
228
+ /*
229
+ Delete
230
+ */
231
+ /**
232
+ * Attempts to read an object by `id`, if found, deletes it.
233
+ *
234
+ * - If you want to skip the read query, use the `unsafe.delete` method,
235
+ * so the behavior depends on the bucket used.
236
+ */
353
237
  async delete(id) {
354
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
355
- await trx_node_1.TrxNode.open(trx, 'delete', { id });
356
- try {
357
- // Composition (with other key)
358
- for (const link of Object.values(this.bucket.schema.graph.links)) {
359
- if (link.rel !== 'composition')
360
- continue;
361
- if (link.keyOwner !== 'other')
362
- continue;
363
- const linked = await this.readLink(id, link.name);
364
- if (link.many) {
365
- for (const linkedItem of linked) {
366
- await trx.bucket(link.bucket.refName).delete(linkedItem.id);
367
- }
368
- }
369
- else {
370
- await trx.bucket(link.bucket.refName).delete(linked.id);
371
- }
372
- }
373
- // The object itself
374
- await this.bucket.delete(trx, id);
375
- // Composition (with self key)
376
- for (const link of Object.values(this.bucket.schema.graph.links)) {
377
- if (link.rel !== 'composition')
378
- continue;
379
- if (link.keyOwner !== 'self')
380
- continue;
381
- const linked = await this.readLink(id, link.name);
382
- if (link.many) {
383
- await trx.bucket(link.bucket.refName).deleteMany(linked.map((l) => l.id));
384
- }
385
- else {
386
- await trx.bucket(link.bucket.refName).delete(linked.id);
387
- }
388
- }
389
- }
390
- catch (e) {
391
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
392
- throw e;
393
- }
394
- await trx_node_1.TrxNode.ok(trx);
238
+ return this.wrap('delete', { id }, trx => this.bucket.delete(trx, id, {
239
+ no_tenancy: !this.enableTenancy
240
+ }), () => undefined);
395
241
  }
396
242
  /**
397
- * **WARNING**: This does not currently implement composition
243
+ * Attempts to read an object by `id`, if found, deletes it.
244
+ *
245
+ * - If you want to skip the read query, use the `unsafe.delete` method,
246
+ * so the behavior depends on the bucket used.
398
247
  */
399
248
  async deleteMany(ids) {
400
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
401
- await trx_node_1.TrxNode.open(trx, 'deleteMany', { ids });
402
- try {
403
- // Composition (with other key)
404
- for (const link of Object.values(this.bucket.schema.graph.links)) {
405
- if (link.rel !== 'composition')
406
- continue;
407
- if (link.keyOwner !== 'other')
408
- continue;
409
- for (const id of ids) {
410
- const linked = await this.readLink(id, link.name);
411
- if (link.many) {
412
- await trx.bucket(link.bucket.refName).deleteMany(linked.map((l) => l.id));
413
- }
414
- else {
415
- await trx.bucket(link.bucket.refName).delete(linked.id);
416
- }
417
- }
418
- }
419
- await this.bucket.deleteMany(trx, ids);
420
- // Composition (with self key)
421
- for (const link of Object.values(this.bucket.schema.graph.links)) {
422
- if (link.rel !== 'composition')
423
- continue;
424
- if (link.keyOwner !== 'self')
425
- continue;
426
- for (const id of ids) {
427
- const linked = await this.readLink(id, link.name);
428
- if (link.many) {
429
- await trx.bucket(link.bucket.refName).deleteMany(linked.map((l) => l.id));
430
- }
431
- else {
432
- await trx.bucket(link.bucket.refName).delete(linked.id);
433
- }
434
- }
435
- }
436
- }
437
- catch (e) {
438
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
439
- throw e;
440
- }
441
- await trx_node_1.TrxNode.ok(trx);
442
- }
443
- async deleteOrFail(id) {
444
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
445
- await trx_node_1.TrxNode.open(trx, 'deleteOrFail', { id });
446
- try {
447
- // Composition (with other key)
448
- for (const link of Object.values(this.bucket.schema.graph.links)) {
449
- if (link.rel !== 'composition')
450
- continue;
451
- if (link.keyOwner !== 'other')
452
- continue;
453
- const linked = await this.readLink(id, link.name);
454
- if (link.many) {
455
- for (const linkedItem of linked) {
456
- await trx.bucket(link.bucket.refName).deleteOrFail(linkedItem.id);
457
- }
458
- }
459
- else {
460
- await trx.bucket(link.bucket.refName).deleteOrFail(linked.id);
461
- }
462
- }
463
- const obj = await this.bucket.readOne(trx, id);
464
- if (!obj) {
465
- const e = error_1.NesoiError.Bucket.ObjNotFound({ bucket: this.bucket.schema.alias, id: id });
466
- await trx_node_1.TrxNode.error(trx, e);
467
- throw e;
468
- }
469
- await this.bucket.delete(trx, id);
470
- // Composition (with self key)
471
- for (const link of Object.values(this.bucket.schema.graph.links)) {
472
- if (link.rel !== 'composition')
473
- continue;
474
- if (link.keyOwner !== 'self')
475
- continue;
476
- const linked = await this.readLink(id, link.name);
477
- if (link.many) {
478
- for (const linkedItem of linked) {
479
- await trx.bucket(link.bucket.refName).deleteOrFail(linkedItem.id);
480
- }
481
- }
482
- else {
483
- await trx.bucket(link.bucket.refName).deleteOrFail(linked.id);
484
- }
485
- }
486
- }
487
- catch (e) {
488
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
489
- throw e;
490
- }
491
- await trx_node_1.TrxNode.ok(trx);
249
+ return this.wrap('deleteMany', { ids }, trx => this.bucket.deleteMany(trx, ids, {
250
+ no_tenancy: !this.enableTenancy
251
+ }), () => undefined);
492
252
  }
253
+ /*
254
+ Build
255
+ */
493
256
  async buildOne(obj, view) {
494
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
495
- await trx_node_1.TrxNode.open(trx, 'buildOne', { obj });
496
- let result;
497
- try {
498
- result = await this.bucket.buildOne(trx, obj, view);
499
- }
500
- catch (e) {
501
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
502
- throw e;
503
- }
504
- await trx_node_1.TrxNode.ok(trx);
505
- return result;
257
+ return this.wrap('buildOne', { obj }, trx => this.bucket.buildOne(trx, obj, view), () => undefined);
506
258
  }
507
- async buildAll(objs, view) {
508
- const trx = trx_node_1.TrxNode.makeChildNode(this.parentTrx, this.bucket.schema.module, 'bucket', this.bucket.schema.name);
509
- await trx_node_1.TrxNode.open(trx, 'buildAll', { objs });
510
- let result;
511
- try {
512
- result = await this.bucket.buildAll(trx, objs, view);
513
- }
514
- catch (e) {
515
- await trx_node_1.TrxNode.error(trx, e); // Bucket unexpected error
516
- throw e;
517
- }
518
- await trx_node_1.TrxNode.ok(trx);
519
- return result;
259
+ async buildMany(objs, view) {
260
+ return this.wrap('buildMany', { objs }, trx => this.bucket.buildMany(trx, objs, view), () => undefined);
261
+ }
262
+ /*
263
+ Unsafe
264
+ */
265
+ /**
266
+ * Unsafe versions of methods, which improve performance
267
+ * by avoiding some validations and queries.
268
+ *
269
+ * **Use it carefully.**
270
+ */
271
+ get unsafe() {
272
+ return new BucketUnsafeTrxNode(this, this.bucket, this.enableTenancy);
520
273
  }
521
274
  }
522
275
  exports.BucketTrxNode = BucketTrxNode;
276
+ class BucketUnsafeTrxNode {
277
+ constructor(bucketTrx, bucket, enableTenancy) {
278
+ this.bucketTrx = bucketTrx;
279
+ this.bucket = bucket;
280
+ this.enableTenancy = enableTenancy;
281
+ }
282
+ /*
283
+ Modifiers
284
+ */
285
+ get no_tenancy() {
286
+ this.enableTenancy = false;
287
+ return this;
288
+ }
289
+ /*
290
+ Update (Patch, Replace)
291
+ */
292
+ /**
293
+ * Directly `patch` (modify) an object based on the one passed as an argument.
294
+ * Also patches the compositions of this bucket, from the
295
+ * `#composition` field passed in the message.
296
+ *
297
+ * > This unsafe version does not check if the object exists prior to patching.
298
+ *
299
+ * - If the object is not found, this will throw an exception
300
+ * - If `#composition` is wrong, this will throw an exception
301
+ */
302
+ async patch(obj) {
303
+ return this.bucketTrx.wrap('patch', { obj }, trx => this.bucket.update(trx, obj, {
304
+ mode: 'patch',
305
+ unsafe: true,
306
+ no_tenancy: !this.enableTenancy
307
+ }), () => undefined);
308
+ }
309
+ /**
310
+ * Directly `replace` on object with the one passed as an argument.
311
+ * Also replaces the compositions of this bucket, from the
312
+ * `#composition` field passed in the message.
313
+ *
314
+ * > This unsafe version does not check if the object exists prior to replacing.
315
+ *
316
+ * - If the object is not found, this will throw an exception
317
+ * - If `#composition` is wrong, this will throw an exception
318
+ */
319
+ async replace(obj) {
320
+ return this.bucketTrx.wrap('replace', { obj }, trx => this.bucket.update(trx, obj, {
321
+ mode: 'replace',
322
+ unsafe: true,
323
+ no_tenancy: !this.enableTenancy
324
+ }), () => undefined);
325
+ }
326
+ /*
327
+ Delete
328
+ */
329
+ /**
330
+ * Deletes an object
331
+ *
332
+ * > This unsafe version does not check if the object exists prior to deleting.
333
+ */
334
+ async delete(id) {
335
+ return this.bucketTrx.wrap('delete', { id }, trx => this.bucket.delete(trx, id, {
336
+ unsafe: true,
337
+ no_tenancy: !this.enableTenancy
338
+ }), () => undefined);
339
+ }
340
+ /**
341
+ * Attempts to read an object by `id`, if found, deletes it.
342
+ *
343
+ * > This unsafe version does not check if the objects exist prior to deleting.
344
+ */
345
+ async deleteMany(ids) {
346
+ return this.bucketTrx.wrap('deleteMany', { ids }, trx => this.bucket.deleteMany(trx, ids, {
347
+ unsafe: true,
348
+ no_tenancy: !this.enableTenancy
349
+ }), () => undefined);
350
+ }
351
+ }
352
+ exports.BucketUnsafeTrxNode = BucketUnsafeTrxNode;