@opra/mongodb 1.1.0 → 1.2.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/cjs/mongo-adapter.js +10 -0
- package/cjs/mongo-entity-service.js +33 -33
- package/esm/mongo-adapter.js +10 -0
- package/esm/mongo-entity-service.js +33 -33
- package/package.json +4 -4
- package/types/mongo-adapter.d.ts +1 -1
- package/types/mongo-collection-service.d.ts +2 -2
- package/types/mongo-entity-service.d.ts +2 -0
package/cjs/mongo-adapter.js
CHANGED
|
@@ -64,6 +64,16 @@ var MongoAdapter;
|
|
|
64
64
|
};
|
|
65
65
|
return { method: 'get', key, options };
|
|
66
66
|
}
|
|
67
|
+
case 'Entity.Replace': {
|
|
68
|
+
const data = await ctx.getBody();
|
|
69
|
+
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
70
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
71
|
+
const options = {
|
|
72
|
+
projection: ctx.queryParams.projection,
|
|
73
|
+
filter: ctx.queryParams.filter,
|
|
74
|
+
};
|
|
75
|
+
return { method: 'replace', key, data, options };
|
|
76
|
+
}
|
|
67
77
|
case 'Entity.Update': {
|
|
68
78
|
const data = await ctx.getBody();
|
|
69
79
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
@@ -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
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
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)
|
|
@@ -319,7 +319,7 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
319
319
|
const input = command.input;
|
|
320
320
|
(0, valgen_1.isNotNullish)(input, { label: 'input' });
|
|
321
321
|
(0, valgen_1.isNotNullish)(input._id, { label: 'input._id' });
|
|
322
|
-
const inputCodec = this._getInputCodec('
|
|
322
|
+
const inputCodec = this._getInputCodec('replace');
|
|
323
323
|
const document = inputCodec(input);
|
|
324
324
|
const { options } = command;
|
|
325
325
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
@@ -336,7 +336,7 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
336
336
|
session: options?.session ?? this.getSession(),
|
|
337
337
|
projection: mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection),
|
|
338
338
|
});
|
|
339
|
-
const outputCodec = this._getOutputCodec('
|
|
339
|
+
const outputCodec = this._getOutputCodec('replace');
|
|
340
340
|
if (out)
|
|
341
341
|
return outputCodec(out);
|
|
342
342
|
}
|
package/esm/mongo-adapter.js
CHANGED
|
@@ -60,6 +60,16 @@ export var MongoAdapter;
|
|
|
60
60
|
};
|
|
61
61
|
return { method: 'get', key, options };
|
|
62
62
|
}
|
|
63
|
+
case 'Entity.Replace': {
|
|
64
|
+
const data = await ctx.getBody();
|
|
65
|
+
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
66
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
67
|
+
const options = {
|
|
68
|
+
projection: ctx.queryParams.projection,
|
|
69
|
+
filter: ctx.queryParams.filter,
|
|
70
|
+
};
|
|
71
|
+
return { method: 'replace', key, data, options };
|
|
72
|
+
}
|
|
63
73
|
case 'Entity.Update': {
|
|
64
74
|
const data = await ctx.getBody();
|
|
65
75
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
@@ -125,21 +125,17 @@ export class MongoEntityService extends MongoService {
|
|
|
125
125
|
command.options?.filter,
|
|
126
126
|
]);
|
|
127
127
|
const { options } = command;
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
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)
|
|
@@ -315,7 +315,7 @@ export class MongoEntityService extends MongoService {
|
|
|
315
315
|
const input = command.input;
|
|
316
316
|
isNotNullish(input, { label: 'input' });
|
|
317
317
|
isNotNullish(input._id, { label: 'input._id' });
|
|
318
|
-
const inputCodec = this._getInputCodec('
|
|
318
|
+
const inputCodec = this._getInputCodec('replace');
|
|
319
319
|
const document = inputCodec(input);
|
|
320
320
|
const { options } = command;
|
|
321
321
|
const filter = MongoAdapter.prepareFilter([
|
|
@@ -332,7 +332,7 @@ export class MongoEntityService extends MongoService {
|
|
|
332
332
|
session: options?.session ?? this.getSession(),
|
|
333
333
|
projection: MongoAdapter.prepareProjection(this.dataType, options?.projection),
|
|
334
334
|
});
|
|
335
|
-
const outputCodec = this._getOutputCodec('
|
|
335
|
+
const outputCodec = this._getOutputCodec('replace');
|
|
336
336
|
if (out)
|
|
337
337
|
return outputCodec(out);
|
|
338
338
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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.
|
|
15
|
-
"@opra/core": "^1.
|
|
16
|
-
"@opra/http": "^1.
|
|
14
|
+
"@opra/common": "^1.2.0",
|
|
15
|
+
"@opra/core": "^1.2.0",
|
|
16
|
+
"@opra/http": "^1.2.0",
|
|
17
17
|
"mongodb": ">= 6.0.0"
|
|
18
18
|
},
|
|
19
19
|
"type": "module",
|
package/types/mongo-adapter.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export declare namespace MongoAdapter {
|
|
|
15
15
|
const prepareProjection: typeof _prepareProjection;
|
|
16
16
|
const prepareSort: typeof _prepareSort;
|
|
17
17
|
interface TransformedRequest {
|
|
18
|
-
method: 'create' | 'delete' | 'deleteMany' | 'get' | 'findMany' | 'update' | 'updateMany';
|
|
18
|
+
method: 'create' | 'delete' | 'deleteMany' | 'get' | 'findMany' | 'replace' | 'update' | 'updateMany';
|
|
19
19
|
key?: any;
|
|
20
20
|
data?: any;
|
|
21
21
|
options: any;
|
|
@@ -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
|
|
59
|
-
create(input: PartialDTO<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
|
}
|