@abejarano/ts-mongodb-criteria 1.10.0 → 1.11.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/README.md
CHANGED
|
@@ -182,8 +182,9 @@ Use the `collection` argument inside `ensureIndexes` to avoid recursion.
|
|
|
182
182
|
MongoRepository provides ready-to-use public methods for repositories that extend it:
|
|
183
183
|
|
|
184
184
|
- `list(criteria, fieldsToExclude?)` for paginated queries
|
|
185
|
-
- `
|
|
186
|
-
- `
|
|
185
|
+
- `many(filter, transaction?)` to fetch multiple entities matching a filter
|
|
186
|
+
- `one(filter, transaction?)` to fetch a single entity
|
|
187
|
+
- `upsert(entity, transaction?)` to persist an aggregate
|
|
187
188
|
Internal helpers are private, so repositories should call these public methods
|
|
188
189
|
directly.
|
|
189
190
|
|
|
@@ -217,6 +218,7 @@ await MongoTransaction.run(async (tx) => {
|
|
|
217
218
|
|
|
218
219
|
// Reads can observe writes made earlier in this transaction.
|
|
219
220
|
const persistedUser = await userRepository.one({ id: user.getId() }, tx)
|
|
221
|
+
const activeUsers = await userRepository.many({ status: "active" }, tx)
|
|
220
222
|
})
|
|
221
223
|
```
|
|
222
224
|
|
|
@@ -236,7 +238,7 @@ async deactivateUser(id: string, tx: MongoTransaction): Promise<void> {
|
|
|
236
238
|
MongoDB transactions require a replica set or a sharded cluster; standalone
|
|
237
239
|
MongoDB instances do not support them. This initial API applies the transaction
|
|
238
240
|
context to `upsert`, `delete`, protected `updateOne`, `one`, and `list`.
|
|
239
|
-
Pass `tx` as the third argument to `list` after `fieldsToExclude`.
|
|
241
|
+
Pass `tx` as the third argument to `list` after `fieldsToExclude`, and as the second argument to `many`.
|
|
240
242
|
|
|
241
243
|
**Your First Query in 30 Seconds:**
|
|
242
244
|
|
|
@@ -25,6 +25,17 @@ class MongoRepository {
|
|
|
25
25
|
id: result._id.toString(),
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
+
async many(filter, transaction) {
|
|
29
|
+
const collection = await this.collection();
|
|
30
|
+
const session = MongoTransaction_1.MongoTransaction.sessionFor(transaction);
|
|
31
|
+
const documents = await collection
|
|
32
|
+
.find(filter, session === undefined ? undefined : { session })
|
|
33
|
+
.toArray();
|
|
34
|
+
return documents.map((document) => this.aggregateRootClass.fromPrimitives({
|
|
35
|
+
...document,
|
|
36
|
+
id: document._id.toString(),
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
28
39
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
29
40
|
async upsert(entity, transaction) {
|
|
30
41
|
await this.persist(entity.getId(), entity, transaction);
|
|
@@ -22,6 +22,17 @@ export class MongoRepository {
|
|
|
22
22
|
id: result._id.toString(),
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
|
+
async many(filter, transaction) {
|
|
26
|
+
const collection = await this.collection();
|
|
27
|
+
const session = MongoTransaction.sessionFor(transaction);
|
|
28
|
+
const documents = await collection
|
|
29
|
+
.find(filter, session === undefined ? undefined : { session })
|
|
30
|
+
.toArray();
|
|
31
|
+
return documents.map((document) => this.aggregateRootClass.fromPrimitives({
|
|
32
|
+
...document,
|
|
33
|
+
id: document._id.toString(),
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
25
36
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
26
37
|
async upsert(entity, transaction) {
|
|
27
38
|
await this.persist(entity.getId(), entity, transaction);
|
|
@@ -3,6 +3,7 @@ import { AggregateRoot } from "../AggregateRoot";
|
|
|
3
3
|
import { DeleteOptions } from "mongodb";
|
|
4
4
|
import { MongoTransaction } from "./MongoTransaction";
|
|
5
5
|
export interface IRepository<T extends AggregateRoot> {
|
|
6
|
+
many(filter: object, transaction?: MongoTransaction): Promise<T[]>;
|
|
6
7
|
one(filter: object, transaction?: MongoTransaction): Promise<T | null>;
|
|
7
8
|
list<D>(criteria: Criteria, fieldsToExclude?: string[], transaction?: MongoTransaction): Promise<Paginate<D>>;
|
|
8
9
|
upsert(entity: T, transaction?: MongoTransaction): Promise<void>;
|
|
@@ -12,6 +12,7 @@ export declare abstract class MongoRepository<T extends AggregateRoot> {
|
|
|
12
12
|
abstract collectionName(): string;
|
|
13
13
|
/** Finds a single entity and hydrates it via the aggregate's fromPrimitives. */
|
|
14
14
|
one(filter: object, transaction?: MongoTransaction): Promise<T | null>;
|
|
15
|
+
many(filter: object, transaction?: MongoTransaction): Promise<T[]>;
|
|
15
16
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
16
17
|
upsert(entity: T, transaction?: MongoTransaction): Promise<void>;
|
|
17
18
|
/** Lists entities by criteria and returns a paginated response. */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abejarano/ts-mongodb-criteria",
|
|
3
3
|
"author": "angel bejarano / angel.bejarano@jaspesoft.com",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.11.0",
|
|
5
5
|
"description": "Patrón Criteria para consultas MongoDB en TypeScript",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
7
7
|
"module": "dist/esm/index.js",
|