@abejarano/ts-mongodb-criteria 1.4.0 → 1.5.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.
- package/README.md +21 -1
- package/dist/cjs/mongo/IRepository.js +2 -0
- package/dist/cjs/mongo/MongoRepository.js +3 -4
- package/dist/cjs/mongo/index.js +1 -0
- package/dist/esm/mongo/IRepository.js +1 -0
- package/dist/esm/mongo/MongoRepository.js +3 -4
- package/dist/esm/mongo/index.js +1 -0
- package/dist/types/mongo/IRepository.d.ts +7 -0
- package/dist/types/mongo/MongoRepository.d.ts +3 -3
- package/dist/types/mongo/index.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -147,19 +147,39 @@ const criteria = new Criteria(
|
|
|
147
147
|
|
|
148
148
|
// 3. Use with your MongoDB repository
|
|
149
149
|
class UserRepository extends MongoRepository<User> {
|
|
150
|
+
constructor() {
|
|
151
|
+
super(User)
|
|
152
|
+
}
|
|
153
|
+
|
|
150
154
|
collectionName(): string {
|
|
151
155
|
return "users"
|
|
152
156
|
}
|
|
153
157
|
}
|
|
154
158
|
|
|
155
159
|
const userRepo = new UserRepository()
|
|
156
|
-
const
|
|
160
|
+
const { results } = await userRepo.list(criteria)
|
|
157
161
|
```
|
|
158
162
|
|
|
159
163
|
MongoRepository provides ready-to-use public methods for repositories that extend it:
|
|
160
164
|
- `list(criteria, fieldsToExclude?)` for paginated queries
|
|
161
165
|
- `one(filter)` to fetch a single entity
|
|
162
166
|
- `upsert(entity)` to persist an aggregate
|
|
167
|
+
Internal helpers are private, so repositories should call these public methods
|
|
168
|
+
directly.
|
|
169
|
+
|
|
170
|
+
If you need a repository interface in your app, extend `IRepository<T>` so your
|
|
171
|
+
custom interfaces stay aligned with the library return types:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import { IRepository } from "@abejarano/ts-mongodb-criteria"
|
|
175
|
+
|
|
176
|
+
export interface IUserRepository extends IRepository<User> {
|
|
177
|
+
// Add domain-specific methods here
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The goal of `IRepository` is to prevent signature drift (e.g. `upsert` returning
|
|
182
|
+
`void` in your app while the base repository returns `ObjectId | null`).
|
|
163
183
|
|
|
164
184
|
**Your First Query in 30 Seconds:**
|
|
165
185
|
|
|
@@ -21,7 +21,7 @@ class MongoRepository {
|
|
|
21
21
|
}
|
|
22
22
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
23
23
|
async upsert(entity) {
|
|
24
|
-
|
|
24
|
+
await this.persist(entity.getId(), entity);
|
|
25
25
|
}
|
|
26
26
|
/** Lists entities by criteria and returns a paginated response. */
|
|
27
27
|
async list(criteria, fieldsToExclude = []) {
|
|
@@ -35,10 +35,9 @@ class MongoRepository {
|
|
|
35
35
|
}
|
|
36
36
|
async updateOne(filter, update) {
|
|
37
37
|
const collection = await this.collection();
|
|
38
|
-
|
|
38
|
+
await collection.updateOne(filter, update, {
|
|
39
39
|
upsert: true,
|
|
40
40
|
});
|
|
41
|
-
return result.upsertedId;
|
|
42
41
|
}
|
|
43
42
|
async persist(id, aggregateRoot) {
|
|
44
43
|
let primitives;
|
|
@@ -48,7 +47,7 @@ class MongoRepository {
|
|
|
48
47
|
else {
|
|
49
48
|
primitives = aggregateRoot.toPrimitives();
|
|
50
49
|
}
|
|
51
|
-
|
|
50
|
+
await this.updateOne({ _id: new mongodb_1.ObjectId(id) }, {
|
|
52
51
|
$set: {
|
|
53
52
|
...primitives,
|
|
54
53
|
id: id,
|
package/dist/cjs/mongo/index.js
CHANGED
|
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./MongoCriteriaConverter"), exports);
|
|
18
18
|
__exportStar(require("./MongoRepository"), exports);
|
|
19
|
+
__exportStar(require("./IRepository"), exports);
|
|
19
20
|
__exportStar(require("./MongoClientFactory"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -18,7 +18,7 @@ export class MongoRepository {
|
|
|
18
18
|
}
|
|
19
19
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
20
20
|
async upsert(entity) {
|
|
21
|
-
|
|
21
|
+
await this.persist(entity.getId(), entity);
|
|
22
22
|
}
|
|
23
23
|
/** Lists entities by criteria and returns a paginated response. */
|
|
24
24
|
async list(criteria, fieldsToExclude = []) {
|
|
@@ -32,10 +32,9 @@ export class MongoRepository {
|
|
|
32
32
|
}
|
|
33
33
|
async updateOne(filter, update) {
|
|
34
34
|
const collection = await this.collection();
|
|
35
|
-
|
|
35
|
+
await collection.updateOne(filter, update, {
|
|
36
36
|
upsert: true,
|
|
37
37
|
});
|
|
38
|
-
return result.upsertedId;
|
|
39
38
|
}
|
|
40
39
|
async persist(id, aggregateRoot) {
|
|
41
40
|
let primitives;
|
|
@@ -45,7 +44,7 @@ export class MongoRepository {
|
|
|
45
44
|
else {
|
|
46
45
|
primitives = aggregateRoot.toPrimitives();
|
|
47
46
|
}
|
|
48
|
-
|
|
47
|
+
await this.updateOne({ _id: new ObjectId(id) }, {
|
|
49
48
|
$set: {
|
|
50
49
|
...primitives,
|
|
51
50
|
id: id,
|
package/dist/esm/mongo/index.js
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Criteria, Paginate } from "../criteria";
|
|
2
|
+
import { AggregateRoot } from "../AggregateRoot";
|
|
3
|
+
export interface IRepository<T extends AggregateRoot> {
|
|
4
|
+
one(filter: object): Promise<T | null>;
|
|
5
|
+
list<D>(criteria: Criteria, fieldsToExclude?: string[]): Promise<Paginate<D>>;
|
|
6
|
+
upsert(entity: T): Promise<void>;
|
|
7
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Criteria, Paginate } from "../criteria";
|
|
2
2
|
import { AggregateRoot, AggregateRootClass } from "../AggregateRoot";
|
|
3
|
-
import { Collection,
|
|
3
|
+
import { Collection, UpdateFilter } from "mongodb";
|
|
4
4
|
export declare abstract class MongoRepository<T extends AggregateRoot> {
|
|
5
5
|
private readonly aggregateRootClass;
|
|
6
6
|
private criteriaConverter;
|
|
@@ -11,11 +11,11 @@ export declare abstract class MongoRepository<T extends AggregateRoot> {
|
|
|
11
11
|
/** Finds a single entity and hydrates it via the aggregate's fromPrimitives. */
|
|
12
12
|
one(filter: object): Promise<T | null>;
|
|
13
13
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
14
|
-
upsert(entity: T): Promise<
|
|
14
|
+
upsert(entity: T): Promise<void>;
|
|
15
15
|
/** Lists entities by criteria and returns a paginated response. */
|
|
16
16
|
list<D>(criteria: Criteria, fieldsToExclude?: string[]): Promise<Paginate<D>>;
|
|
17
17
|
protected collection<T extends Document>(): Promise<Collection<T>>;
|
|
18
|
-
protected updateOne(filter: object, update: Document[] | UpdateFilter<any>): Promise<
|
|
18
|
+
protected updateOne(filter: object, update: Document[] | UpdateFilter<any>): Promise<void>;
|
|
19
19
|
private persist;
|
|
20
20
|
private searchByCriteria;
|
|
21
21
|
private paginate;
|
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.5.1",
|
|
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",
|