@opra/mongodb 1.1.1 → 1.2.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.
@@ -129,21 +129,17 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
129
129
  command.options?.filter,
130
130
  ]);
131
131
  const { options } = command;
132
- const db = this.getDatabase();
133
- const collection = await this.getCollection(db);
134
- const projection = mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection);
135
- const out = await collection.findOne(filter || {}, {
136
- ...(0, lodash_omit_1.default)(options, 'filter'),
137
- session: options?.session ?? this.getSession(),
138
- projection,
139
- limit: undefined,
140
- skip: undefined,
141
- sort: undefined,
142
- });
143
- if (out) {
144
- const outputCodec = this._getOutputCodec('find');
145
- return outputCodec(out);
146
- }
132
+ const findManyCommand = {
133
+ ...command,
134
+ options: {
135
+ ...options,
136
+ filter,
137
+ limit: 1,
138
+ skip: undefined,
139
+ },
140
+ };
141
+ const rows = await this._findMany(findManyCommand);
142
+ return rows?.[0];
147
143
  }
148
144
  /**
149
145
  * Finds a document in the collection that matches the specified options.
@@ -152,20 +148,15 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
152
148
  */
153
149
  async _findOne(command) {
154
150
  const { options } = command;
155
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
156
- const db = this.getDatabase();
157
- const collection = await this.getCollection(db);
158
- const out = await collection.findOne(filter || {}, {
159
- ...(0, lodash_omit_1.default)(options, 'filter'),
160
- session: options?.session ?? this.getSession(),
161
- sort: options?.sort ? mongo_adapter_js_1.MongoAdapter.prepareSort(options.sort) : undefined,
162
- projection: mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection),
163
- limit: undefined,
164
- });
165
- if (out) {
166
- const outputCodec = this._getOutputCodec('find');
167
- return outputCodec(out);
168
- }
151
+ const findManyCommand = {
152
+ ...command,
153
+ options: {
154
+ ...options,
155
+ limit: 1,
156
+ },
157
+ };
158
+ const rows = await this._findMany(findManyCommand);
159
+ return rows?.[0];
169
160
  }
170
161
  /**
171
162
  * Finds multiple documents in the MongoDB collection
@@ -174,21 +165,30 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
174
165
  */
175
166
  async _findMany(command) {
176
167
  const { options } = command;
177
- const limit = options?.limit || 10;
178
168
  const stages = [];
169
+ /** Pre-Stages */
170
+ if (options?.preStages)
171
+ stages.push(...options.preStages);
172
+ /** "Filter" stage */
179
173
  let filter;
180
174
  if (options?.filter)
181
175
  filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
182
176
  if (filter)
183
177
  stages.push({ $match: filter });
178
+ /** "Skip" stage */
184
179
  if (options?.skip)
185
180
  stages.push({ $skip: options.skip });
181
+ /** "Sort" stage */
186
182
  if (options?.sort) {
187
183
  const sort = mongo_adapter_js_1.MongoAdapter.prepareSort(options.sort);
188
184
  if (sort)
189
185
  stages.push({ $sort: sort });
190
186
  }
191
- stages.push({ $limit: limit });
187
+ /** "Limit" stage */
188
+ stages.push({ $limit: options?.limit || 10 });
189
+ /** Post-Stages */
190
+ if (options?.postStages)
191
+ stages.push(...options.postStages);
192
192
  const dataType = this.dataType;
193
193
  const projection = mongo_adapter_js_1.MongoAdapter.prepareProjection(dataType, options?.projection);
194
194
  if (projection)
@@ -125,21 +125,17 @@ export class MongoEntityService extends MongoService {
125
125
  command.options?.filter,
126
126
  ]);
127
127
  const { options } = command;
128
- const db = this.getDatabase();
129
- const collection = await this.getCollection(db);
130
- const projection = MongoAdapter.prepareProjection(this.dataType, options?.projection);
131
- const out = await collection.findOne(filter || {}, {
132
- ...omit(options, 'filter'),
133
- session: options?.session ?? this.getSession(),
134
- projection,
135
- limit: undefined,
136
- skip: undefined,
137
- sort: undefined,
138
- });
139
- if (out) {
140
- const outputCodec = this._getOutputCodec('find');
141
- return outputCodec(out);
142
- }
128
+ const findManyCommand = {
129
+ ...command,
130
+ options: {
131
+ ...options,
132
+ filter,
133
+ limit: 1,
134
+ skip: undefined,
135
+ },
136
+ };
137
+ const rows = await this._findMany(findManyCommand);
138
+ return rows?.[0];
143
139
  }
144
140
  /**
145
141
  * Finds a document in the collection that matches the specified options.
@@ -148,20 +144,15 @@ export class MongoEntityService extends MongoService {
148
144
  */
149
145
  async _findOne(command) {
150
146
  const { options } = command;
151
- const filter = MongoAdapter.prepareFilter(options?.filter);
152
- const db = this.getDatabase();
153
- const collection = await this.getCollection(db);
154
- const out = await collection.findOne(filter || {}, {
155
- ...omit(options, 'filter'),
156
- session: options?.session ?? this.getSession(),
157
- sort: options?.sort ? MongoAdapter.prepareSort(options.sort) : undefined,
158
- projection: MongoAdapter.prepareProjection(this.dataType, options?.projection),
159
- limit: undefined,
160
- });
161
- if (out) {
162
- const outputCodec = this._getOutputCodec('find');
163
- return outputCodec(out);
164
- }
147
+ const findManyCommand = {
148
+ ...command,
149
+ options: {
150
+ ...options,
151
+ limit: 1,
152
+ },
153
+ };
154
+ const rows = await this._findMany(findManyCommand);
155
+ return rows?.[0];
165
156
  }
166
157
  /**
167
158
  * Finds multiple documents in the MongoDB collection
@@ -170,21 +161,30 @@ export class MongoEntityService extends MongoService {
170
161
  */
171
162
  async _findMany(command) {
172
163
  const { options } = command;
173
- const limit = options?.limit || 10;
174
164
  const stages = [];
165
+ /** Pre-Stages */
166
+ if (options?.preStages)
167
+ stages.push(...options.preStages);
168
+ /** "Filter" stage */
175
169
  let filter;
176
170
  if (options?.filter)
177
171
  filter = MongoAdapter.prepareFilter(options?.filter);
178
172
  if (filter)
179
173
  stages.push({ $match: filter });
174
+ /** "Skip" stage */
180
175
  if (options?.skip)
181
176
  stages.push({ $skip: options.skip });
177
+ /** "Sort" stage */
182
178
  if (options?.sort) {
183
179
  const sort = MongoAdapter.prepareSort(options.sort);
184
180
  if (sort)
185
181
  stages.push({ $sort: sort });
186
182
  }
187
- stages.push({ $limit: limit });
183
+ /** "Limit" stage */
184
+ stages.push({ $limit: options?.limit || 10 });
185
+ /** Post-Stages */
186
+ if (options?.postStages)
187
+ stages.push(...options.postStages);
188
188
  const dataType = this.dataType;
189
189
  const projection = MongoAdapter.prepareProjection(dataType, options?.projection);
190
190
  if (projection)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -11,9 +11,9 @@
11
11
  "valgen": "^5.12.0"
12
12
  },
13
13
  "peerDependencies": {
14
- "@opra/common": "^1.1.1",
15
- "@opra/core": "^1.1.1",
16
- "@opra/http": "^1.1.1",
14
+ "@opra/common": "^1.2.1",
15
+ "@opra/core": "^1.2.1",
16
+ "@opra/http": "^1.2.1",
17
17
  "mongodb": ">= 6.0.0"
18
18
  },
19
19
  "type": "module",
@@ -55,8 +55,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
55
55
  * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
56
56
  * @throws {Error} if an unknown error occurs while creating the document.
57
57
  */
58
- create(input: PartialDTO<T>, options: RequiredSome<MongoEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
59
- create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
58
+ create(input: PartialDTO<T> | T, options: RequiredSome<MongoEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
59
+ create(input: PartialDTO<T> | T, options?: MongoEntityService.CreateOptions): Promise<T>;
60
60
  /**
61
61
  * Returns the count of documents in the collection based on the provided options.
62
62
  *
@@ -31,6 +31,8 @@ export declare namespace MongoEntityService {
31
31
  interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
32
32
  }
33
33
  interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
34
+ preStages?: mongodb.Document[];
35
+ postStages?: mongodb.Document[];
34
36
  }
35
37
  interface ReplaceOptions<T> extends MongoService.ReplaceOptions<T> {
36
38
  }