@wisemen/nestjs-typeorm 0.0.9 → 0.0.11
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.
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { type EntityManager, type EntityTarget, ObjectLiteral, Repository } from 'typeorm';
|
|
1
|
+
import { type EntityManager, type EntityTarget, FindOneOptions, FindOptionsWhere, ObjectLiteral, Repository } from 'typeorm';
|
|
2
2
|
export declare class TypeOrmRepository<T extends ObjectLiteral> extends Repository<T> {
|
|
3
3
|
constructor(entity: EntityTarget<T>, manager: EntityManager);
|
|
4
|
+
findInBatches(options: FindOneOptions<T>, batchSize: number): AsyncGenerator<T[], void, void>;
|
|
5
|
+
findByInBatches(where: FindOptionsWhere<T> | FindOptionsWhere<T>[], batchSize: number): AsyncGenerator<T[], void, void>;
|
|
4
6
|
}
|
|
@@ -1,8 +1,37 @@
|
|
|
1
|
-
import { Repository } from 'typeorm';
|
|
1
|
+
import { MoreThan, Repository } from 'typeorm';
|
|
2
2
|
import { createTransactionManagerProxy } from './transaction.js';
|
|
3
3
|
export class TypeOrmRepository extends Repository {
|
|
4
4
|
constructor(entity, manager) {
|
|
5
5
|
super(entity, createTransactionManagerProxy(manager));
|
|
6
6
|
}
|
|
7
|
+
async *findInBatches(options, batchSize) {
|
|
8
|
+
if (this.metadata.primaryColumns.length !== 1) {
|
|
9
|
+
throw new Error(`Entity ${this.metadata.name} has a composite primary key and cannot be fetched in batches`);
|
|
10
|
+
}
|
|
11
|
+
const primaryKey = this.metadata.primaryColumns[0].propertyName;
|
|
12
|
+
let lastPrimaryKeyValue = undefined;
|
|
13
|
+
let entities = [];
|
|
14
|
+
do {
|
|
15
|
+
const where = lastPrimaryKeyValue !== undefined
|
|
16
|
+
? { ...options.where, [primaryKey]: MoreThan(lastPrimaryKeyValue) }
|
|
17
|
+
: options.where;
|
|
18
|
+
const order = options.order !== undefined && primaryKey in options.order
|
|
19
|
+
? options.order
|
|
20
|
+
: { ...options.order, [primaryKey]: 'ASC' };
|
|
21
|
+
entities = await this.find({
|
|
22
|
+
...options,
|
|
23
|
+
where,
|
|
24
|
+
order,
|
|
25
|
+
take: batchSize,
|
|
26
|
+
});
|
|
27
|
+
if (entities.length === 0)
|
|
28
|
+
return;
|
|
29
|
+
yield entities;
|
|
30
|
+
lastPrimaryKeyValue = entities.at(-1)?.[primaryKey];
|
|
31
|
+
} while (lastPrimaryKeyValue !== undefined);
|
|
32
|
+
}
|
|
33
|
+
findByInBatches(where, batchSize) {
|
|
34
|
+
return this.findInBatches({ where }, batchSize);
|
|
35
|
+
}
|
|
7
36
|
}
|
|
8
37
|
//# sourceMappingURL=repository.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../lib/extensions/repository.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../lib/extensions/repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoH,QAAQ,EAAiB,UAAU,EAAE,MAAM,SAAS,CAAA;AAC/K,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAA;AAEhE,MAAM,OAAO,iBAA2C,SAAQ,UAAc;IAC5E,YAAa,MAAuB,EAAE,OAAsB;QAC1D,KAAK,CAAC,MAAM,EAAE,6BAA6B,CAAC,OAAO,CAAC,CAAC,CAAA;IACvD,CAAC;IAED,KAAK,CAAC,CAAC,aAAa,CAClB,OAA0B,EAC1B,SAAiB;QAEjB,IAAI,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9C,MAAM,IAAI,KAAK,CAAC,UAAU,IAAI,CAAC,QAAQ,CAAC,IAAI,+DAA+D,CAAC,CAAA;QAC9G,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,YAAY,CAAA;QA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