@opra/mongodb 0.32.6 → 0.33.1

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.
@@ -30,11 +30,11 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
30
30
  * Asserts the existence of a resource based on the given options.
31
31
  *
32
32
  * @returns {Promise<void>} A Promise that resolves when the resource exists.
33
- * @throws {ResourceNotFoundError} If the resource does not exist.
33
+ * @throws {ResourceNotAvailableError} If the resource does not exist.
34
34
  */
35
35
  async assert(options) {
36
36
  if (!(await this.exists(options)))
37
- throw new common_1.ResourceNotFoundError(this.getResourceName());
37
+ throw new common_1.ResourceNotAvailableError(this.getResourceName());
38
38
  }
39
39
  /**
40
40
  * Creates the document in the database.
@@ -45,13 +45,15 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
45
45
  * @throws {Error} Throws an error if an unknown error occurs while creating the document.
46
46
  */
47
47
  async create(input, options) {
48
- if (this.$interceptor && !options?.__interceptor__)
49
- return this.$interceptor(() => this.create(input, { ...options, __interceptor__: true }), {
50
- crud: 'create',
51
- method: 'create',
52
- input,
53
- options,
54
- }, this);
48
+ const info = {
49
+ crud: 'create',
50
+ method: 'create',
51
+ input,
52
+ options,
53
+ };
54
+ return this._intercept(() => this._create(input, options), info);
55
+ }
56
+ async _create(input, options) {
55
57
  const encode = this.getEncoder('create');
56
58
  const doc = encode(input);
57
59
  doc._id = this._id;
@@ -59,16 +61,14 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
59
61
  if (r.insertedId) {
60
62
  if (!options)
61
63
  return doc;
62
- try {
63
- return this.get(options);
64
- }
65
- catch (e) {
66
- Error.captureStackTrace(e);
67
- throw e;
68
- }
64
+ const out = await this._findOne(options);
65
+ if (out)
66
+ return out;
67
+ if (!out)
68
+ throw new common_1.ResourceNotAvailableError(this.getResourceName());
69
69
  }
70
70
  /* istanbul ignore next */
71
- throw new Error('Unknown error while creating document');
71
+ throw new common_1.InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
72
72
  }
73
73
  /**
74
74
  * Deletes a record from the database
@@ -77,15 +77,22 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
77
77
  * @returns {Promise<number>} The number of records deleted
78
78
  */
79
79
  async delete(options) {
80
- if (this.$interceptor && !options?.__interceptor__)
81
- return this.$interceptor(() => this.delete({ ...options, __interceptor__: true }), {
82
- crud: 'delete',
83
- method: 'delete',
84
- options,
85
- }, this);
80
+ const info = {
81
+ crud: 'delete',
82
+ method: 'delete',
83
+ options,
84
+ };
85
+ return this._intercept(async () => {
86
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
87
+ await this._getDocumentFilter(info),
88
+ options?.filter
89
+ ]);
90
+ return this._delete({ ...options, filter });
91
+ }, info);
92
+ }
93
+ async _delete(options) {
86
94
  const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
87
95
  { _id: this._id },
88
- await this._getDocumentFilter(),
89
96
  options?.filter
90
97
  ]);
91
98
  const r = await this.__deleteOne(filter, options);
@@ -97,13 +104,7 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
97
104
  * @return {Promise<boolean>} - A promise that resolves to a boolean value indicating if the document exists.
98
105
  */
99
106
  async exists(options) {
100
- if (this.$interceptor && !options?.__interceptor__)
101
- return this.$interceptor(() => this.exists({ ...options, __interceptor__: true }), {
102
- crud: 'read',
103
- method: 'exists',
104
- options,
105
- }, this);
106
- return !!(await this.findOne({ ...options, pick: ['_id'] }));
107
+ return !!(await this.findOne({ ...options, pick: ['_id'], omit: undefined, include: undefined }));
107
108
  }
108
109
  /**
109
110
  * Fetches the document if it exists. Returns undefined if not found.
@@ -112,15 +113,22 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
112
113
  * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
113
114
  */
114
115
  async findOne(options) {
115
- if (this.$interceptor && !options?.__interceptor__)
116
- return this.$interceptor(() => this.findOne({ ...options, __interceptor__: true }), {
117
- crud: 'read',
118
- method: 'find',
119
- options,
120
- }, this);
116
+ const info = {
117
+ crud: 'read',
118
+ method: 'findOne',
119
+ options,
120
+ };
121
+ return this._intercept(async () => {
122
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
123
+ await this._getDocumentFilter(info),
124
+ options?.filter
125
+ ]);
126
+ return this._findOne({ ...options, filter });
127
+ }, info);
128
+ }
129
+ async _findOne(options) {
121
130
  const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
122
131
  { _id: this._id },
123
- await this._getDocumentFilter(),
124
132
  options?.filter
125
133
  ]);
126
134
  const mongoOptions = {
@@ -139,12 +147,12 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
139
147
  *
140
148
  * @param {MongoCollectionService.FindOneOptions} options - The options to customize the query.
141
149
  * @return {Promise<PartialDTO<T>>} - A promise that resolves to the fetched document.
142
- * @throws {ResourceNotFoundError} - If the document is not found in the collection.
150
+ * @throws {ResourceNotAvailableError} - If the document is not found in the collection.
143
151
  */
144
152
  async get(options) {
145
153
  const out = await this.findOne(options);
146
154
  if (!out)
147
- throw new common_1.ResourceNotFoundError(this.getResourceName());
155
+ throw new common_1.ResourceNotAvailableError(this.getResourceName());
148
156
  return out;
149
157
  }
150
158
  /**
@@ -156,13 +164,21 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
156
164
  * @return {Promise<number>} - A promise that resolves to the updated document or undefined if not found.
157
165
  */
158
166
  async updateOnly(input, options) {
159
- if (this.$interceptor && !options?.__interceptor__)
160
- return this.$interceptor(() => this.updateOnly(input, { ...options, __interceptor__: true }), {
161
- crud: 'update',
162
- method: 'update',
163
- input,
164
- options,
165
- }, this);
167
+ const info = {
168
+ crud: 'update',
169
+ method: 'update',
170
+ input,
171
+ options,
172
+ };
173
+ return this._intercept(async () => {
174
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
175
+ await this._getDocumentFilter(info),
176
+ options?.filter
177
+ ]);
178
+ return this._updateOnly(input, { ...options, filter });
179
+ }, info);
180
+ }
181
+ async _updateOnly(input, options) {
166
182
  const encode = this.getEncoder('update');
167
183
  const doc = encode(input);
168
184
  const patch = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
@@ -174,7 +190,6 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
174
190
  };
175
191
  const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
176
192
  { _id: this._id },
177
- await this._getDocumentFilter(),
178
193
  options?.filter
179
194
  ]);
180
195
  const r = await this.__updateOne(filter, patch, mongoOptions);
@@ -189,13 +204,21 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
189
204
  * @return {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
190
205
  */
191
206
  async update(input, options) {
192
- if (this.$interceptor && !options?.__interceptor__)
193
- return this.$interceptor(() => this.update(input, { ...options, __interceptor__: true }), {
194
- crud: 'update',
195
- method: 'update',
196
- input,
197
- options,
198
- }, this);
207
+ const info = {
208
+ crud: 'update',
209
+ method: 'update',
210
+ input,
211
+ options,
212
+ };
213
+ return this._intercept(async () => {
214
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
215
+ await this._getDocumentFilter(info),
216
+ options?.filter
217
+ ]);
218
+ return this._update(input, { ...options, filter });
219
+ }, info);
220
+ }
221
+ async _update(input, options) {
199
222
  const encode = this.getEncoder('update');
200
223
  const doc = encode(input);
201
224
  const patch = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
@@ -207,7 +230,6 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
207
230
  };
208
231
  const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
209
232
  { _id: this._id },
210
- await this._getDocumentFilter(),
211
233
  options?.filter
212
234
  ]);
213
235
  const decoder = this.getDecoder();
@@ -222,9 +244,14 @@ class MongoSingletonService extends mongo_service_js_1.MongoService {
222
244
  * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
223
245
  * that resolves to the common filter, or undefined if not available.
224
246
  */
225
- _getDocumentFilter() {
247
+ _getDocumentFilter(args) {
226
248
  return typeof this.$documentFilter === 'function' ?
227
- this.$documentFilter(this) : this.$documentFilter;
249
+ this.$documentFilter(args, this) : this.$documentFilter;
250
+ }
251
+ async _intercept(callback, args) {
252
+ if (this.$interceptor)
253
+ return this.$interceptor(callback, args, this);
254
+ return callback();
228
255
  }
229
256
  }
230
257
  exports.MongoSingletonService = MongoSingletonService;