@abejarano/ts-mongodb-criteria 1.5.2 → 1.6.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
CHANGED
|
@@ -154,12 +154,21 @@ class UserRepository extends MongoRepository<User> {
|
|
|
154
154
|
collectionName(): string {
|
|
155
155
|
return "users"
|
|
156
156
|
}
|
|
157
|
+
|
|
158
|
+
// Create indexes the first time the collection is accessed
|
|
159
|
+
protected async ensureIndexes(collection: Collection): Promise<void> {
|
|
160
|
+
await collection.createIndex({ email: 1 }, { unique: true })
|
|
161
|
+
}
|
|
157
162
|
}
|
|
158
163
|
|
|
159
164
|
const userRepo = new UserRepository()
|
|
160
165
|
const { results } = await userRepo.list(criteria)
|
|
161
166
|
```
|
|
162
167
|
|
|
168
|
+
Indexes are created lazily the first time the repository accesses the collection
|
|
169
|
+
(via `list`, `one`, or `upsert`).
|
|
170
|
+
Use the `collection` argument inside `ensureIndexes` to avoid recursion.
|
|
171
|
+
|
|
163
172
|
MongoRepository provides ready-to-use public methods for repositories that extend it:
|
|
164
173
|
- `list(criteria, fieldsToExclude?)` for paginated queries
|
|
165
174
|
- `one(filter)` to fetch a single entity
|
|
@@ -9,6 +9,23 @@ class MongoRepository {
|
|
|
9
9
|
this.aggregateRootClass = aggregateRootClass;
|
|
10
10
|
this.criteriaConverter = new MongoCriteriaConverter_1.MongoCriteriaConverter();
|
|
11
11
|
}
|
|
12
|
+
async ensureIndexesOnce() {
|
|
13
|
+
const key = this.collectionName();
|
|
14
|
+
if (MongoRepository.indexRegistry.has(key))
|
|
15
|
+
return;
|
|
16
|
+
const collection = await this.collectionRaw();
|
|
17
|
+
await this.ensureIndexes(collection);
|
|
18
|
+
MongoRepository.indexRegistry.add(key);
|
|
19
|
+
}
|
|
20
|
+
async collection() {
|
|
21
|
+
await this.ensureIndexesOnce();
|
|
22
|
+
return this.collectionRaw();
|
|
23
|
+
}
|
|
24
|
+
async collectionRaw() {
|
|
25
|
+
return (await MongoClientFactory_1.MongoClientFactory.createClient())
|
|
26
|
+
.db()
|
|
27
|
+
.collection(this.collectionName());
|
|
28
|
+
}
|
|
12
29
|
/** Finds a single entity and hydrates it via the aggregate's fromPrimitives. */
|
|
13
30
|
async one(filter) {
|
|
14
31
|
const collection = await this.collection();
|
|
@@ -28,11 +45,6 @@ class MongoRepository {
|
|
|
28
45
|
const documents = await this.searchByCriteria(criteria, fieldsToExclude);
|
|
29
46
|
return this.paginate(documents);
|
|
30
47
|
}
|
|
31
|
-
async collection() {
|
|
32
|
-
return (await MongoClientFactory_1.MongoClientFactory.createClient())
|
|
33
|
-
.db()
|
|
34
|
-
.collection(this.collectionName());
|
|
35
|
-
}
|
|
36
48
|
async updateOne(filter, update) {
|
|
37
49
|
const collection = await this.collection();
|
|
38
50
|
await collection.updateOne(filter, update, {
|
|
@@ -100,3 +112,4 @@ class MongoRepository {
|
|
|
100
112
|
}
|
|
101
113
|
}
|
|
102
114
|
exports.MongoRepository = MongoRepository;
|
|
115
|
+
MongoRepository.indexRegistry = new Set();
|
|
@@ -6,6 +6,23 @@ export class MongoRepository {
|
|
|
6
6
|
this.aggregateRootClass = aggregateRootClass;
|
|
7
7
|
this.criteriaConverter = new MongoCriteriaConverter();
|
|
8
8
|
}
|
|
9
|
+
async ensureIndexesOnce() {
|
|
10
|
+
const key = this.collectionName();
|
|
11
|
+
if (MongoRepository.indexRegistry.has(key))
|
|
12
|
+
return;
|
|
13
|
+
const collection = await this.collectionRaw();
|
|
14
|
+
await this.ensureIndexes(collection);
|
|
15
|
+
MongoRepository.indexRegistry.add(key);
|
|
16
|
+
}
|
|
17
|
+
async collection() {
|
|
18
|
+
await this.ensureIndexesOnce();
|
|
19
|
+
return this.collectionRaw();
|
|
20
|
+
}
|
|
21
|
+
async collectionRaw() {
|
|
22
|
+
return (await MongoClientFactory.createClient())
|
|
23
|
+
.db()
|
|
24
|
+
.collection(this.collectionName());
|
|
25
|
+
}
|
|
9
26
|
/** Finds a single entity and hydrates it via the aggregate's fromPrimitives. */
|
|
10
27
|
async one(filter) {
|
|
11
28
|
const collection = await this.collection();
|
|
@@ -25,11 +42,6 @@ export class MongoRepository {
|
|
|
25
42
|
const documents = await this.searchByCriteria(criteria, fieldsToExclude);
|
|
26
43
|
return this.paginate(documents);
|
|
27
44
|
}
|
|
28
|
-
async collection() {
|
|
29
|
-
return (await MongoClientFactory.createClient())
|
|
30
|
-
.db()
|
|
31
|
-
.collection(this.collectionName());
|
|
32
|
-
}
|
|
33
45
|
async updateOne(filter, update) {
|
|
34
46
|
const collection = await this.collection();
|
|
35
47
|
await collection.updateOne(filter, update, {
|
|
@@ -96,3 +108,4 @@ export class MongoRepository {
|
|
|
96
108
|
};
|
|
97
109
|
}
|
|
98
110
|
}
|
|
111
|
+
MongoRepository.indexRegistry = new Set();
|
|
@@ -6,15 +6,19 @@ export declare abstract class MongoRepository<T extends AggregateRoot> {
|
|
|
6
6
|
private criteriaConverter;
|
|
7
7
|
private query;
|
|
8
8
|
private criteria;
|
|
9
|
+
private static indexRegistry;
|
|
9
10
|
protected constructor(aggregateRootClass: AggregateRootClass<T>);
|
|
10
11
|
abstract collectionName(): string;
|
|
12
|
+
protected abstract ensureIndexes(collection: Collection): Promise<void>;
|
|
13
|
+
protected ensureIndexesOnce(): Promise<void>;
|
|
14
|
+
protected collection<U extends Document>(): Promise<Collection<U>>;
|
|
15
|
+
private collectionRaw;
|
|
11
16
|
/** Finds a single entity and hydrates it via the aggregate's fromPrimitives. */
|
|
12
17
|
one(filter: object): Promise<T | null>;
|
|
13
18
|
/** Upserts an aggregate by delegating to persist with its id. */
|
|
14
19
|
upsert(entity: T): Promise<void>;
|
|
15
20
|
/** Lists entities by criteria and returns a paginated response. */
|
|
16
21
|
list<D>(criteria: Criteria, fieldsToExclude?: string[]): Promise<Paginate<D>>;
|
|
17
|
-
protected collection<T extends Document>(): Promise<Collection<T>>;
|
|
18
22
|
protected updateOne(filter: object, update: Document[] | UpdateFilter<any>): Promise<void>;
|
|
19
23
|
private persist;
|
|
20
24
|
private searchByCriteria;
|
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.6.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",
|