@tstdl/base 0.92.167 → 0.93.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/application/application.d.ts +12 -18
- package/application/application.js +48 -69
- package/application/index.d.ts +1 -5
- package/application/index.js +1 -5
- package/application/providers.d.ts +10 -0
- package/application/providers.js +54 -0
- package/audit/audit.model.d.ts +15 -0
- package/audit/audit.model.js +73 -0
- package/audit/auditor.d.ts +42 -0
- package/audit/auditor.js +111 -0
- package/audit/index.d.ts +3 -0
- package/audit/index.js +3 -0
- package/audit/types.d.ts +22 -0
- package/audit/types.js +19 -0
- package/authentication/client/authentication.service.d.ts +1 -3
- package/authentication/client/authentication.service.js +4 -5
- package/browser/browser-context-controller.d.ts +2 -4
- package/browser/browser-context-controller.js +5 -6
- package/browser/browser-controller.d.ts +2 -4
- package/browser/browser-controller.js +3 -4
- package/browser/browser.service.d.ts +1 -3
- package/browser/browser.service.js +1 -2
- package/browser/page-controller.d.ts +2 -4
- package/browser/page-controller.js +7 -8
- package/browser/utils.js +3 -3
- package/cancellation/token.d.ts +104 -41
- package/cancellation/token.js +125 -54
- package/core.d.ts +1 -13
- package/core.js +1 -46
- package/disposable/disposable.d.ts +0 -8
- package/disposable/disposable.js +1 -3
- package/disposable/index.d.ts +0 -6
- package/disposable/index.js +0 -6
- package/disposable/using.d.ts +0 -1
- package/disposable/using.js +2 -3
- package/distributed-loop/distributed-loop.js +2 -2
- package/errors/utils.js +4 -1
- package/examples/api/authentication.js +11 -5
- package/examples/api/basic-overview.js +17 -12
- package/examples/api/custom-authentication.js +13 -7
- package/examples/api/streaming.js +15 -12
- package/examples/browser/basic.js +6 -3
- package/examples/document-management/main.js +6 -3
- package/examples/http/client.js +7 -3
- package/examples/mail/basic.js +9 -7
- package/examples/pdf/basic.js +8 -6
- package/examples/template/basic.js +7 -5
- package/http/client/http-client-request.d.ts +1 -2
- package/http/client/http-client-request.js +1 -2
- package/http/server/http-server.d.ts +1 -3
- package/http/server/http-server.js +0 -1
- package/http/server/node/node-http-server.d.ts +1 -2
- package/http/server/node/node-http-server.js +1 -2
- package/import.js +1 -1
- package/injector/injector.d.ts +1 -1
- package/injector/types.d.ts +3 -4
- package/lock/lock.d.ts +40 -21
- package/lock/lock.js +74 -1
- package/lock/postgres/drizzle/0000_busy_tattoo.sql +7 -0
- package/lock/postgres/drizzle/meta/0000_snapshot.json +65 -0
- package/lock/postgres/drizzle/meta/_journal.json +13 -0
- package/lock/postgres/drizzle.config.js +11 -0
- package/lock/postgres/index.d.ts +2 -0
- package/lock/postgres/index.js +2 -0
- package/lock/postgres/lock.d.ts +14 -0
- package/lock/postgres/lock.js +127 -0
- package/lock/postgres/models/index.d.ts +2 -0
- package/lock/postgres/models/index.js +2 -0
- package/lock/postgres/models/lock.model.d.ts +7 -0
- package/{examples/orm/user.model.js → lock/postgres/models/lock.model.js} +22 -30
- package/lock/postgres/models/schemas.d.ts +3 -0
- package/lock/postgres/models/schemas.js +4 -0
- package/lock/postgres/module.d.ts +6 -0
- package/lock/postgres/module.js +26 -0
- package/lock/postgres/provider.d.ts +6 -0
- package/lock/postgres/provider.js +29 -0
- package/lock/provider.d.ts +12 -2
- package/lock/provider.js +24 -1
- package/lock/web/web-lock.d.ts +4 -3
- package/lock/web/web-lock.js +49 -42
- package/lock/web/web-lock.provider.d.ts +0 -3
- package/lock/web/web-lock.provider.js +5 -22
- package/logger/formatter.d.ts +13 -0
- package/logger/formatter.js +3 -0
- package/logger/formatters/index.d.ts +2 -0
- package/logger/formatters/index.js +2 -0
- package/logger/formatters/json.d.ts +5 -0
- package/logger/formatters/json.js +33 -0
- package/logger/formatters/pretty-print.d.ts +5 -0
- package/logger/formatters/pretty-print.js +55 -0
- package/logger/index.d.ts +5 -2
- package/logger/index.js +5 -2
- package/logger/level.d.ts +10 -8
- package/logger/level.js +9 -9
- package/logger/logger.d.ts +21 -30
- package/logger/logger.js +98 -26
- package/logger/manager.d.ts +20 -0
- package/logger/manager.js +86 -0
- package/logger/tokens.d.ts +1 -1
- package/logger/tokens.js +1 -1
- package/logger/transport.d.ts +14 -0
- package/logger/transport.js +16 -0
- package/logger/transports/console.d.ts +14 -0
- package/logger/transports/console.js +36 -0
- package/logger/transports/index.d.ts +1 -0
- package/logger/transports/index.js +1 -0
- package/mail/clients/nodemailer.mail-client.d.ts +0 -1
- package/mail/clients/nodemailer.mail-client.js +9 -7
- package/message-bus/local/local-message-bus.js +2 -2
- package/message-bus/message-bus-base.d.ts +2 -3
- package/message-bus/message-bus-base.js +5 -6
- package/message-bus/message-bus.d.ts +1 -2
- package/message-bus/message-bus.js +1 -2
- package/module/index.d.ts +0 -2
- package/module/index.js +0 -2
- package/module/module.d.ts +17 -18
- package/module/module.js +47 -12
- package/module/modules/function.module.d.ts +6 -6
- package/module/modules/function.module.js +25 -9
- package/module/modules/web-server.module.d.ts +2 -10
- package/module/modules/web-server.module.js +3 -11
- package/openid-connect/index.d.ts +0 -2
- package/openid-connect/index.js +0 -2
- package/openid-connect/oidc-state.model.d.ts +4 -5
- package/openid-connect/oidc-state.model.js +51 -1
- package/openid-connect/oidc.service-model.d.ts +1 -1
- package/openid-connect/oidc.service.d.ts +2 -6
- package/openid-connect/oidc.service.js +24 -37
- package/orm/decorators.d.ts +10 -1
- package/orm/decorators.js +8 -0
- package/orm/server/repository.d.ts +3 -1
- package/orm/server/repository.js +32 -3
- package/package.json +19 -29
- package/pdf/pdf.service.js +9 -9
- package/pool/pool.d.ts +1 -3
- package/pool/pool.js +3 -4
- package/queue/postgres/job.model.d.ts +1 -2
- package/queue/postgres/job.model.js +1 -2
- package/queue/postgres/module.js +1 -1
- package/threading/thread-pool.d.ts +1 -3
- package/threading/thread-pool.js +7 -8
- package/utils/format-error.d.ts +7 -0
- package/utils/format-error.js +59 -17
- package/utils/function/memoize.d.ts +22 -7
- package/utils/function/memoize.js +82 -23
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/object/dereference.d.ts +51 -19
- package/utils/object/dereference.js +52 -43
- package/utils/timing.js +2 -2
- package/utils/try-chain.d.ts +22 -0
- package/utils/try-chain.js +46 -0
- package/database/entity-repository.d.ts +0 -50
- package/database/entity-repository.js +0 -3
- package/database/entity.d.ts +0 -7
- package/database/entity.js +0 -1
- package/database/id.d.ts +0 -1
- package/database/id.js +0 -9
- package/database/index.d.ts +0 -11
- package/database/index.js +0 -11
- package/database/module.d.ts +0 -8
- package/database/module.js +0 -11
- package/database/mongo/classes.d.ts +0 -21
- package/database/mongo/classes.js +0 -26
- package/database/mongo/index.d.ts +0 -15
- package/database/mongo/index.js +0 -15
- package/database/mongo/model/document.d.ts +0 -29
- package/database/mongo/model/document.js +0 -63
- package/database/mongo/model/index.d.ts +0 -1
- package/database/mongo/model/index.js +0 -1
- package/database/mongo/module.d.ts +0 -8
- package/database/mongo/module.js +0 -68
- package/database/mongo/mongo-base.repository.d.ts +0 -103
- package/database/mongo/mongo-base.repository.js +0 -263
- package/database/mongo/mongo-bulk.d.ts +0 -35
- package/database/mongo/mongo-bulk.js +0 -90
- package/database/mongo/mongo-entity-repository.d.ts +0 -98
- package/database/mongo/mongo-entity-repository.js +0 -278
- package/database/mongo/operations.d.ts +0 -10
- package/database/mongo/operations.js +0 -54
- package/database/mongo/query-converter.d.ts +0 -6
- package/database/mongo/query-converter.js +0 -83
- package/database/mongo/simple-entity-repository.d.ts +0 -7
- package/database/mongo/simple-entity-repository.js +0 -6
- package/database/mongo/types.d.ts +0 -50
- package/database/mongo/types.js +0 -3
- package/database/query.d.ts +0 -121
- package/database/query.js +0 -7
- package/database/utils.d.ts +0 -2
- package/database/utils.js +0 -3
- package/disposable/async-disposer.d.ts +0 -35
- package/disposable/async-disposer.js +0 -125
- package/examples/orm/drizzle.config.js +0 -6
- package/examples/orm/schemas.d.ts +0 -3
- package/examples/orm/schemas.js +0 -4
- package/examples/orm/test.d.ts +0 -1
- package/examples/orm/test.js +0 -11
- package/examples/orm/user.model.d.ts +0 -13
- package/key-value-store/mongo/index.d.ts +0 -6
- package/key-value-store/mongo/index.js +0 -6
- package/key-value-store/mongo/module.d.ts +0 -8
- package/key-value-store/mongo/module.js +0 -18
- package/key-value-store/mongo/mongo-key-value-store.provider.d.ts +0 -8
- package/key-value-store/mongo/mongo-key-value-store.provider.js +0 -26
- package/key-value-store/mongo/mongo-key-value.model.d.ts +0 -7
- package/key-value-store/mongo/mongo-key-value.model.js +0 -1
- package/key-value-store/mongo/mongo-key-value.repository.d.ts +0 -10
- package/key-value-store/mongo/mongo-key-value.repository.js +0 -31
- package/key-value-store/mongo/mongo-key-value.store.d.ts +0 -15
- package/key-value-store/mongo/mongo-key-value.store.js +0 -82
- package/key-value-store/mongo/tokens.d.ts +0 -3
- package/key-value-store/mongo/tokens.js +0 -2
- package/lock/mongo/index.d.ts +0 -5
- package/lock/mongo/index.js +0 -5
- package/lock/mongo/lock.d.ts +0 -14
- package/lock/mongo/lock.js +0 -125
- package/lock/mongo/model.d.ts +0 -6
- package/lock/mongo/model.js +0 -1
- package/lock/mongo/module.d.ts +0 -12
- package/lock/mongo/module.js +0 -20
- package/lock/mongo/mongo-lock-repository.d.ts +0 -14
- package/lock/mongo/mongo-lock-repository.js +0 -67
- package/lock/mongo/provider.d.ts +0 -8
- package/lock/mongo/provider.js +0 -36
- package/logger/console/index.d.ts +0 -1
- package/logger/console/index.js +0 -1
- package/logger/console/logger.d.ts +0 -11
- package/logger/console/logger.js +0 -64
- package/logger/noop/index.d.ts +0 -1
- package/logger/noop/index.js +0 -1
- package/logger/noop/logger.d.ts +0 -9
- package/logger/noop/logger.js +0 -21
- package/migration/index.d.ts +0 -9
- package/migration/index.js +0 -9
- package/migration/migration-state-repository.d.ts +0 -4
- package/migration/migration-state-repository.js +0 -3
- package/migration/migration-state.d.ts +0 -6
- package/migration/migration-state.js +0 -1
- package/migration/migrator.d.ts +0 -23
- package/migration/migrator.js +0 -76
- package/migration/mongo/index.d.ts +0 -2
- package/migration/mongo/index.js +0 -2
- package/migration/mongo/migration-state-repository.d.ts +0 -11
- package/migration/mongo/migration-state-repository.js +0 -32
- package/migration/mongo/module.d.ts +0 -12
- package/migration/mongo/module.js +0 -17
- package/module/module-base.d.ts +0 -18
- package/module/module-base.js +0 -40
- package/module/module-metric-reporter.d.ts +0 -29
- package/module/module-metric-reporter.js +0 -62
- package/openid-connect/mongo-oidc-state.repository.d.ts +0 -21
- package/openid-connect/mongo-oidc-state.repository.js +0 -52
- package/openid-connect/oidc-state.repository.d.ts +0 -4
- package/openid-connect/oidc-state.repository.js +0 -3
- package/process-shutdown.d.ts +0 -9
- package/process-shutdown.js +0 -65
- package/queue/mongo/index.d.ts +0 -4
- package/queue/mongo/index.js +0 -4
- package/queue/mongo/job.d.ts +0 -12
- package/queue/mongo/job.js +0 -1
- package/queue/mongo/mongo-job.repository.d.ts +0 -13
- package/queue/mongo/mongo-job.repository.js +0 -54
- package/queue/mongo/queue.d.ts +0 -38
- package/queue/mongo/queue.js +0 -266
- package/queue/mongo/queue.provider.d.ts +0 -18
- package/queue/mongo/queue.provider.js +0 -38
- package/search-index/elastic/config.d.ts +0 -8
- package/search-index/elastic/config.js +0 -26
- package/search-index/elastic/index.d.ts +0 -8
- package/search-index/elastic/index.js +0 -8
- package/search-index/elastic/keyword-rewriter.d.ts +0 -8
- package/search-index/elastic/keyword-rewriter.js +0 -18
- package/search-index/elastic/model/elastic-query.d.ts +0 -16
- package/search-index/elastic/model/elastic-query.js +0 -1
- package/search-index/elastic/model/index-mapping.d.ts +0 -26
- package/search-index/elastic/model/index-mapping.js +0 -4
- package/search-index/elastic/model/index.d.ts +0 -3
- package/search-index/elastic/model/index.js +0 -3
- package/search-index/elastic/model/sort.d.ts +0 -8
- package/search-index/elastic/model/sort.js +0 -1
- package/search-index/elastic/module.d.ts +0 -10
- package/search-index/elastic/module.js +0 -49
- package/search-index/elastic/query-builder/boolean-query-builder.d.ts +0 -11
- package/search-index/elastic/query-builder/boolean-query-builder.js +0 -52
- package/search-index/elastic/query-builder/index.d.ts +0 -1
- package/search-index/elastic/query-builder/index.js +0 -1
- package/search-index/elastic/query-converter.d.ts +0 -9
- package/search-index/elastic/query-converter.js +0 -183
- package/search-index/elastic/search-index.d.ts +0 -30
- package/search-index/elastic/search-index.js +0 -144
- package/search-index/elastic/sort-converter.d.ts +0 -4
- package/search-index/elastic/sort-converter.js +0 -14
- package/search-index/elastic/types.d.ts +0 -5
- package/search-index/elastic/types.js +0 -1
- package/search-index/error.d.ts +0 -10
- package/search-index/error.js +0 -14
- package/search-index/index.d.ts +0 -3
- package/search-index/index.js +0 -3
- package/search-index/memory/index.d.ts +0 -1
- package/search-index/memory/index.js +0 -1
- package/search-index/memory/memory-search-index.d.ts +0 -19
- package/search-index/memory/memory-search-index.js +0 -144
- package/search-index/search-index.d.ts +0 -46
- package/search-index/search-index.js +0 -31
- package/search-index/search-result.d.ts +0 -12
- package/search-index/search-result.js +0 -1
- package/theme/adapters/css-adapter.d.ts +0 -5
- package/theme/adapters/css-adapter.js +0 -29
- package/theme/adapters/index.d.ts +0 -2
- package/theme/adapters/index.js +0 -2
- package/theme/adapters/tailwind-adapter.d.ts +0 -18
- package/theme/adapters/tailwind-adapter.js +0 -32
- package/theme/index.d.ts +0 -1
- package/theme/index.js +0 -1
- package/theme/theme-service.d.ts +0 -43
- package/theme/theme-service.js +0 -128
- /package/{examples/orm → lock/postgres}/drizzle.config.d.ts +0 -0
package/database/mongo/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MongoDB implemenation for Database module
|
|
3
|
-
*
|
|
4
|
-
* @module Database - MongoDB
|
|
5
|
-
*/
|
|
6
|
-
export * from './classes.js';
|
|
7
|
-
export * from './model/index.js';
|
|
8
|
-
export * from './module.js';
|
|
9
|
-
export * from './mongo-base.repository.js';
|
|
10
|
-
export * from './mongo-bulk.js';
|
|
11
|
-
export * from './mongo-entity-repository.js';
|
|
12
|
-
export * from './operations.js';
|
|
13
|
-
export * from './query-converter.js';
|
|
14
|
-
export * from './simple-entity-repository.js';
|
|
15
|
-
export * from './types.js';
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { Entity, MaybeNewEntity, NewEntity } from '../../../database/index.js';
|
|
2
|
-
import type { OptionalId, OptionalUnlessRequiredId, WithId } from 'mongodb';
|
|
3
|
-
import type { ProjectedEntity, Projection } from '../mongo-base.repository.js';
|
|
4
|
-
import { ProjectionMode } from '../mongo-base.repository.js';
|
|
5
|
-
export type MongoDocument<T extends MaybeNewEntity> = OptionalUnlessRequiredId<Omit<T, 'id'> & {
|
|
6
|
-
_id: T['id'];
|
|
7
|
-
}>;
|
|
8
|
-
export type MongoDocumentWithPartialId<T extends MaybeNewEntity> = OptionalId<Omit<T, 'id'> & {
|
|
9
|
-
_id?: T['id'];
|
|
10
|
-
}>;
|
|
11
|
-
export type MongoDocumentWithoutId<T extends MaybeNewEntity> = Omit<T, 'id'>;
|
|
12
|
-
export declare function toEntity<T extends Entity>(document: WithId<MongoDocument<T>> | MongoDocument<T>): T;
|
|
13
|
-
export declare function toNewEntity<T extends MaybeNewEntity>(entity: T): NewEntity<T>;
|
|
14
|
-
export declare function toProjectedEntity<T extends Entity, M extends ProjectionMode, P extends Projection<T, M>>(document: MongoDocument<T> | MongoDocumentWithPartialId<T>): ProjectedEntity<T, M, P>;
|
|
15
|
-
export declare function toMongoProjection<T extends Entity, M extends ProjectionMode, P extends Projection<T, M>>(mode: M, projection: P): Projection<MongoDocument<T>, M>;
|
|
16
|
-
export declare function toMongoDocument<T extends Entity>(entity: T): MongoDocument<T>;
|
|
17
|
-
export declare function toMongoDocumentWithPartialId<T extends MaybeNewEntity>(entity: T): MongoDocumentWithPartialId<T>;
|
|
18
|
-
export declare function toMongoDocumentWithoutId<T extends Entity>(entity: MaybeNewEntity<T>): MongoDocumentWithoutId<T>;
|
|
19
|
-
export declare function mongoDocumentFromMaybeNewEntity<T extends Entity>(entity: MaybeNewEntity<T>): MongoDocument<T>;
|
|
20
|
-
export declare function renameIdPropertyToUnderscoreId<T extends {
|
|
21
|
-
id: any;
|
|
22
|
-
}>(object: T): Omit<T, 'id'> & {
|
|
23
|
-
_id: T['id'];
|
|
24
|
-
};
|
|
25
|
-
export declare function renameIdPropertyToUnderscoreId<T extends {
|
|
26
|
-
id?: any;
|
|
27
|
-
}>(object: T): Omit<T, 'id'> & {
|
|
28
|
-
_id?: T['id'];
|
|
29
|
-
};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { getNewId } from '../../../database/index.js';
|
|
2
|
-
import { isDefined } from '../../../utils/type-guards.js';
|
|
3
|
-
import { ProjectionMode } from '../mongo-base.repository.js';
|
|
4
|
-
export function toEntity(document) {
|
|
5
|
-
const { _id, ...entityRest } = document;
|
|
6
|
-
const entity = {
|
|
7
|
-
id: _id,
|
|
8
|
-
...entityRest
|
|
9
|
-
};
|
|
10
|
-
return entity;
|
|
11
|
-
}
|
|
12
|
-
export function toNewEntity(entity) {
|
|
13
|
-
const { id, ...rest } = entity;
|
|
14
|
-
return rest;
|
|
15
|
-
}
|
|
16
|
-
export function toProjectedEntity(document) {
|
|
17
|
-
const { _id, ...documentRest } = document;
|
|
18
|
-
const partialIdObject = (_id != undefined) ? { id: _id } : undefined;
|
|
19
|
-
const entity = {
|
|
20
|
-
...partialIdObject,
|
|
21
|
-
...documentRest
|
|
22
|
-
};
|
|
23
|
-
return entity;
|
|
24
|
-
}
|
|
25
|
-
export function toMongoProjection(mode, projection) {
|
|
26
|
-
const { id, ...projectionRest } = projection;
|
|
27
|
-
const partialIdObject = (id != undefined) ? { _id: id } : (mode == ProjectionMode.Include) ? { _id: false } : undefined;
|
|
28
|
-
const mongoProjection = {
|
|
29
|
-
...partialIdObject,
|
|
30
|
-
...projectionRest
|
|
31
|
-
};
|
|
32
|
-
return mongoProjection;
|
|
33
|
-
}
|
|
34
|
-
export function toMongoDocument(entity) {
|
|
35
|
-
return renameIdPropertyToUnderscoreId(entity);
|
|
36
|
-
}
|
|
37
|
-
export function toMongoDocumentWithPartialId(entity) {
|
|
38
|
-
return renameIdPropertyToUnderscoreId(entity);
|
|
39
|
-
}
|
|
40
|
-
export function toMongoDocumentWithoutId(entity) {
|
|
41
|
-
const { id, ...entityRest } = entity; // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
42
|
-
const document = {
|
|
43
|
-
...entityRest
|
|
44
|
-
};
|
|
45
|
-
return document;
|
|
46
|
-
}
|
|
47
|
-
export function mongoDocumentFromMaybeNewEntity(entity) {
|
|
48
|
-
const { id, ...entityRest } = entity;
|
|
49
|
-
const document = {
|
|
50
|
-
_id: id ?? getNewId(),
|
|
51
|
-
...entityRest
|
|
52
|
-
};
|
|
53
|
-
return document;
|
|
54
|
-
}
|
|
55
|
-
export function renameIdPropertyToUnderscoreId(object) {
|
|
56
|
-
const { id, ...rest } = object;
|
|
57
|
-
const partialIdObject = isDefined(id) ? { _id: id } : undefined;
|
|
58
|
-
const document = {
|
|
59
|
-
...partialIdObject,
|
|
60
|
-
...rest
|
|
61
|
-
};
|
|
62
|
-
return document;
|
|
63
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './document.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './document.js';
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { MongoConnection } from './types.js';
|
|
2
|
-
export type MongoModuleConfig = {
|
|
3
|
-
defaultConnection: MongoConnection;
|
|
4
|
-
defaultDatabase: string | undefined;
|
|
5
|
-
logPrefix: string;
|
|
6
|
-
};
|
|
7
|
-
export declare const mongoModuleConfig: MongoModuleConfig;
|
|
8
|
-
export declare function configureMongo(config: Partial<MongoModuleConfig>): void;
|
package/database/mongo/module.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { connect } from '../../core.js';
|
|
2
|
-
import { Injector } from '../../injector/injector.js';
|
|
3
|
-
import { Logger } from '../../logger/index.js';
|
|
4
|
-
import { assertDefined, isObject, isString } from '../../utils/type-guards.js';
|
|
5
|
-
import { Collection, Database, MongoClient } from './classes.js';
|
|
6
|
-
export const mongoModuleConfig = {
|
|
7
|
-
defaultConnection: { url: 'mongodb://localhost:27017/test-db' },
|
|
8
|
-
defaultDatabase: undefined,
|
|
9
|
-
logPrefix: 'Mongo',
|
|
10
|
-
};
|
|
11
|
-
export function configureMongo(config) {
|
|
12
|
-
mongoModuleConfig.defaultDatabase = config.defaultDatabase ?? mongoModuleConfig.defaultDatabase;
|
|
13
|
-
mongoModuleConfig.defaultConnection = config.defaultConnection ?? mongoModuleConfig.defaultConnection;
|
|
14
|
-
mongoModuleConfig.logPrefix = config.logPrefix ?? mongoModuleConfig.logPrefix;
|
|
15
|
-
}
|
|
16
|
-
Injector.registerSingleton(MongoClient, {
|
|
17
|
-
useFactory(argument, context) {
|
|
18
|
-
assertDefined(argument, 'mongo connection resolve argument missing');
|
|
19
|
-
const { url, ...options } = argument;
|
|
20
|
-
const logger = context.resolve(Logger, mongoModuleConfig.logPrefix);
|
|
21
|
-
const client = new MongoClient(url, options);
|
|
22
|
-
client
|
|
23
|
-
.on('fullsetup', () => logger.verbose('connection setup'))
|
|
24
|
-
.on('reconnect', () => logger.warn('reconnected'))
|
|
25
|
-
.on('timeout', () => logger.warn('connection timed out'))
|
|
26
|
-
.on('close', () => logger.verbose('connection closed'));
|
|
27
|
-
context.addDisposeHandler(async () => client.close());
|
|
28
|
-
context.data.logger = logger;
|
|
29
|
-
context.data.url = url;
|
|
30
|
-
return client;
|
|
31
|
-
},
|
|
32
|
-
async afterResolve(client, _argument, { cancellationSignal, data: { url, logger } }) {
|
|
33
|
-
await connect(`mongo at ${url}`, async () => client.connect(), logger, cancellationSignal);
|
|
34
|
-
},
|
|
35
|
-
}, {
|
|
36
|
-
defaultArgumentProvider: () => mongoModuleConfig.defaultConnection,
|
|
37
|
-
argumentIdentityProvider: JSON.stringify,
|
|
38
|
-
});
|
|
39
|
-
Injector.registerSingleton(Database, {
|
|
40
|
-
useFactory: (argument, context) => {
|
|
41
|
-
const connection = isObject(argument) ? argument.connection : mongoModuleConfig.defaultConnection;
|
|
42
|
-
const name = (isString(argument) ? argument : isObject(argument) ? argument.database : undefined) ?? mongoModuleConfig.defaultDatabase;
|
|
43
|
-
const client = context.resolve(MongoClient, connection);
|
|
44
|
-
return client.db(name);
|
|
45
|
-
},
|
|
46
|
-
defaultArgumentProvider: () => ({ database: mongoModuleConfig.defaultDatabase, connection: mongoModuleConfig.defaultConnection }),
|
|
47
|
-
}, {
|
|
48
|
-
argumentIdentityProvider: JSON.stringify,
|
|
49
|
-
});
|
|
50
|
-
Injector.registerSingleton(Collection, {
|
|
51
|
-
useFactory: (config, context) => {
|
|
52
|
-
assertDefined(config, 'mongo repository config resolve argument missing');
|
|
53
|
-
const database = context.resolve(Database, config);
|
|
54
|
-
context.data.database = database;
|
|
55
|
-
return database.collection(config.collection);
|
|
56
|
-
},
|
|
57
|
-
async afterResolve(_, config, { data: { database } }) {
|
|
58
|
-
const existingCollections = await database.collections();
|
|
59
|
-
for (const collection of existingCollections) {
|
|
60
|
-
if (collection.collectionName == config.collection) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
await database.createCollection(config.collection);
|
|
65
|
-
},
|
|
66
|
-
}, {
|
|
67
|
-
argumentIdentityProvider: JSON.stringify,
|
|
68
|
-
});
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import type { Entity, MaybeNewEntity } from '../../database/index.js';
|
|
2
|
-
import type { Record } from '../../types/index.js';
|
|
3
|
-
import type { FindOneAndUpdateOptions } from 'mongodb';
|
|
4
|
-
import type { Collection } from './classes.js';
|
|
5
|
-
import { MongoBulk } from './mongo-bulk.js';
|
|
6
|
-
import type { Filter, Sort, TypedIndexDescription, UpdateFilter } from './types.js';
|
|
7
|
-
export declare const enum ProjectionMode {
|
|
8
|
-
Include = 0,
|
|
9
|
-
Exclude = 1
|
|
10
|
-
}
|
|
11
|
-
export type Projection<T, M extends ProjectionMode> = {
|
|
12
|
-
[P in keyof T]?: M extends ProjectionMode.Include ? true : false;
|
|
13
|
-
};
|
|
14
|
-
export type ProjectedEntity<T, M extends ProjectionMode, P extends Projection<T, M>> = M extends ProjectionMode.Include ? {
|
|
15
|
-
[K in keyof T]: P[K] extends true ? T[K] : undefined;
|
|
16
|
-
} : {
|
|
17
|
-
[K in keyof T]: P[K] extends false ? undefined : T[K];
|
|
18
|
-
};
|
|
19
|
-
export type ReplaceOptions = {
|
|
20
|
-
upsert?: boolean;
|
|
21
|
-
};
|
|
22
|
-
export type UpdateOptions = {
|
|
23
|
-
upsert?: boolean;
|
|
24
|
-
};
|
|
25
|
-
export type UpdateResult = {
|
|
26
|
-
matchedCount: number;
|
|
27
|
-
modifiedCount: number;
|
|
28
|
-
};
|
|
29
|
-
export type LoadOptions<T extends Entity> = {
|
|
30
|
-
limit?: number;
|
|
31
|
-
skip?: number;
|
|
32
|
-
sort?: Sort<T>;
|
|
33
|
-
};
|
|
34
|
-
export type LoadAndDeleteOptions<T extends Entity> = LoadOptions<T>;
|
|
35
|
-
export type LoadManyOptions<T extends Entity> = LoadOptions<T>;
|
|
36
|
-
export type LoadAndUpdateOptions<T extends Entity> = LoadOptions<T> & {
|
|
37
|
-
upsert?: boolean;
|
|
38
|
-
returnDocument?: FindOneAndUpdateOptions['returnDocument'];
|
|
39
|
-
sort?: Sort<T>;
|
|
40
|
-
};
|
|
41
|
-
export type CountOptions = {
|
|
42
|
-
limit?: number;
|
|
43
|
-
skip?: number;
|
|
44
|
-
};
|
|
45
|
-
export type InsertIfNotExistsByFilterItem<T extends Entity> = {
|
|
46
|
-
filter: Filter<T>;
|
|
47
|
-
entity: MaybeNewEntity<T>;
|
|
48
|
-
};
|
|
49
|
-
export declare class MongoBaseRepository<T extends Entity> {
|
|
50
|
-
readonly collection: Collection<T>;
|
|
51
|
-
constructor(collection: Collection<T>);
|
|
52
|
-
createIndexes(indexes: TypedIndexDescription<T>[]): Promise<void>;
|
|
53
|
-
bulk(): MongoBulk<T>;
|
|
54
|
-
insert<U extends T>(entity: MaybeNewEntity<U>): Promise<U>;
|
|
55
|
-
insertMany<U extends T>(entities: MaybeNewEntity<U>[]): Promise<U[]>;
|
|
56
|
-
insertIfNotExists<U extends T>(entity: MaybeNewEntity<U>): Promise<U | undefined>;
|
|
57
|
-
insertManyIfNotExists<U extends T>(entities: MaybeNewEntity<U>[]): Promise<U[]>;
|
|
58
|
-
insertIfNotExistsByFilter<U extends T>(filter: Filter<T>, entity: MaybeNewEntity<U>): Promise<U | undefined>;
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
* @param items
|
|
62
|
-
* @returns inserted entities - entities which are already in the database are not returned
|
|
63
|
-
*/
|
|
64
|
-
insertManyIfNotExistsByFilter<U extends T>(items: InsertIfNotExistsByFilterItem<U>[]): Promise<U[]>;
|
|
65
|
-
load<U extends T = T>(id: string, options?: LoadOptions<U>): Promise<U>;
|
|
66
|
-
tryLoad<U extends T = T>(id: string, options?: LoadOptions<U>): Promise<U | undefined>;
|
|
67
|
-
loadAndUpdate<U extends T = T>(id: string, update: UpdateFilter<U>, options?: LoadAndUpdateOptions<U>): Promise<U>;
|
|
68
|
-
tryLoadAndUpdate<U extends T = T>(id: string, update: UpdateFilter<U>, options?: LoadAndUpdateOptions<U>): Promise<U | undefined>;
|
|
69
|
-
loadAndDelete<U extends T = T>(id: string, options?: LoadAndDeleteOptions<U>): Promise<U>;
|
|
70
|
-
loadByFilter<U extends T = T>(filter: Filter<U>, options?: LoadOptions<U>): Promise<U>;
|
|
71
|
-
tryLoadByFilter<U extends T = T>(filter: Filter<U>, options?: LoadOptions<U>): Promise<U | undefined>;
|
|
72
|
-
loadProjectedByFilter<U extends T = T, M extends ProjectionMode = ProjectionMode.Include, P extends Projection<U, M> = Record<string, never>>(filter: Filter<U>, mode: M, projection: P, options?: LoadOptions<U>): Promise<ProjectedEntity<U, M, P>>;
|
|
73
|
-
tryLoadProjectedByFilter<U extends T = T, M extends ProjectionMode = ProjectionMode.Include, P extends Projection<U, M> = Record<string, never>>(filter: Filter<U>, mode: M, projection: P, options?: LoadOptions<U>): Promise<ProjectedEntity<U, M, P> | undefined>;
|
|
74
|
-
loadByFilterAndDelete<U extends T = T>(filter: Filter<U>, options?: LoadAndDeleteOptions<U>): Promise<U>;
|
|
75
|
-
tryLoadAndDelete<U extends T = T>(id: string, options?: LoadAndDeleteOptions<U>): Promise<U | undefined>;
|
|
76
|
-
tryLoadByFilterAndDelete<U extends T = T>(filter: Filter<U>, options?: LoadAndDeleteOptions<U>): Promise<U | undefined>;
|
|
77
|
-
loadByFilterAndUpdate<U extends T = T>(filter: Filter<U>, update: UpdateFilter<U>, options?: LoadAndUpdateOptions<U>): Promise<U>;
|
|
78
|
-
tryLoadByFilterAndUpdate<U extends T = T>(filter: Filter<U>, update: UpdateFilter<U>, options?: LoadAndUpdateOptions<U>): Promise<U | undefined>;
|
|
79
|
-
loadManyById<U extends T = T>(ids: string[], options?: LoadManyOptions<U>): Promise<U[]>;
|
|
80
|
-
loadManyByFilter<U extends T = T>(filter: Filter<U>, options?: LoadManyOptions<U>): Promise<U[]>;
|
|
81
|
-
loadManyByIdWithCursor<U extends T = T>(ids: string[], options?: LoadManyOptions<U>): AsyncIterableIterator<U>;
|
|
82
|
-
loadManyByFilterWithCursor<U extends T = T>(filter: Filter<U>, options?: LoadManyOptions<U>): AsyncIterableIterator<U>;
|
|
83
|
-
loadManyProjectedById<U extends T = T, M extends ProjectionMode = ProjectionMode.Include, P extends Projection<U, M> = Record<string, never>>(ids: string[], mode: M, projection: P, options?: LoadManyOptions<U>): Promise<ProjectedEntity<U, M, P>[]>;
|
|
84
|
-
loadManyProjectedByFilter<U extends T = T, M extends ProjectionMode = ProjectionMode.Include, P extends Projection<U, M> = Record<string, never>>(filter: Filter<U>, mode: M, projection: P, options?: LoadManyOptions<U>): Promise<ProjectedEntity<U, M, P>[]>;
|
|
85
|
-
loadManyProjectedByFilterWithCursor<U extends T = T, M extends ProjectionMode = ProjectionMode.Include, P extends Projection<U, M> = Record<string, never>>(filter: Filter<U>, mode: M, projection: P, options?: LoadManyOptions<U>): AsyncIterableIterator<ProjectedEntity<U, M, P>>;
|
|
86
|
-
deleteById(id: string): Promise<boolean>;
|
|
87
|
-
deleteManyById(ids: string[]): Promise<number>;
|
|
88
|
-
deleteByFilter<U extends T = T>(filter: Filter<U>): Promise<boolean>;
|
|
89
|
-
deleteManyByFilter<U extends T = T>(filter: Filter<U>): Promise<number>;
|
|
90
|
-
replace<U extends T>(entity: U, options?: ReplaceOptions): Promise<boolean>;
|
|
91
|
-
replaceByFilter<U extends T>(filter: Filter<U>, entity: U, options?: ReplaceOptions): Promise<boolean>;
|
|
92
|
-
replaceMany<U extends T>(entities: U[], options?: ReplaceOptions): Promise<number>;
|
|
93
|
-
update<U extends T>(filter: Filter<U>, update: UpdateFilter<U>, options?: UpdateOptions): Promise<UpdateResult>;
|
|
94
|
-
updateMany<U extends T>(filter: Filter<U>, update: UpdateFilter<U>, options?: UpdateOptions): Promise<UpdateResult>;
|
|
95
|
-
has(id: string): Promise<boolean>;
|
|
96
|
-
hasByFilter<U extends T = T>(filter: Filter<U>): Promise<boolean>;
|
|
97
|
-
hasMany(ids: string[]): Promise<string[]>;
|
|
98
|
-
getIdsByFilter<U extends T>(filter: Filter<U>): Promise<string[]>;
|
|
99
|
-
hasAll(ids: string[]): Promise<boolean>;
|
|
100
|
-
countByFilter<U extends T = T>(filter: Filter<U>, { limit, skip }?: CountOptions): Promise<number>;
|
|
101
|
-
countByFilterEstimated(): Promise<number>;
|
|
102
|
-
drop(): Promise<void>;
|
|
103
|
-
}
|
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import { NotFoundError } from '../../errors/not-found.error.js';
|
|
2
|
-
import { objectKeys } from '../../utils/object/object.js';
|
|
3
|
-
import { assertDefined, isNull, isNullOrUndefined } from '../../utils/type-guards.js';
|
|
4
|
-
import { mongoDocumentFromMaybeNewEntity, toEntity, toMongoDocument, toMongoProjection, toNewEntity, toProjectedEntity } from './model/document.js';
|
|
5
|
-
import { MongoBulk } from './mongo-bulk.js';
|
|
6
|
-
import { replaceOneOperation, updateOneOperation } from './operations.js';
|
|
7
|
-
export var ProjectionMode;
|
|
8
|
-
(function (ProjectionMode) {
|
|
9
|
-
ProjectionMode[ProjectionMode["Include"] = 0] = "Include";
|
|
10
|
-
ProjectionMode[ProjectionMode["Exclude"] = 1] = "Exclude";
|
|
11
|
-
})(ProjectionMode || (ProjectionMode = {}));
|
|
12
|
-
export class MongoBaseRepository {
|
|
13
|
-
collection;
|
|
14
|
-
constructor(collection) {
|
|
15
|
-
this.collection = collection;
|
|
16
|
-
}
|
|
17
|
-
async createIndexes(indexes) {
|
|
18
|
-
await this.collection.createIndexes(indexes);
|
|
19
|
-
}
|
|
20
|
-
bulk() {
|
|
21
|
-
return new MongoBulk(this.collection);
|
|
22
|
-
}
|
|
23
|
-
async insert(entity) {
|
|
24
|
-
const document = mongoDocumentFromMaybeNewEntity(entity);
|
|
25
|
-
await this.collection.insertOne(document); // eslint-disable-line @typescript-eslint/no-unsafe-argument
|
|
26
|
-
return toEntity(document);
|
|
27
|
-
}
|
|
28
|
-
async insertMany(entities) {
|
|
29
|
-
if (entities.length == 0) {
|
|
30
|
-
return [];
|
|
31
|
-
}
|
|
32
|
-
const bulk = this.bulk();
|
|
33
|
-
const documents = bulk.insertMany(entities);
|
|
34
|
-
await bulk.execute();
|
|
35
|
-
return documents;
|
|
36
|
-
}
|
|
37
|
-
async insertIfNotExists(entity) {
|
|
38
|
-
const filter = toNewEntity(entity);
|
|
39
|
-
return this.insertIfNotExistsByFilter(filter, entity);
|
|
40
|
-
}
|
|
41
|
-
async insertManyIfNotExists(entities) {
|
|
42
|
-
const items = entities.map((entity) => ({ filter: toNewEntity(entity), entity }));
|
|
43
|
-
return this.insertManyIfNotExistsByFilter(items);
|
|
44
|
-
}
|
|
45
|
-
async insertIfNotExistsByFilter(filter, entity) {
|
|
46
|
-
const document = mongoDocumentFromMaybeNewEntity(entity);
|
|
47
|
-
const result = await this.collection.updateOne(filter, { $setOnInsert: document }, { upsert: true });
|
|
48
|
-
if (result.upsertedCount == 0) {
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
return toEntity(document);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
*
|
|
55
|
-
* @param items
|
|
56
|
-
* @returns inserted entities - entities which are already in the database are not returned
|
|
57
|
-
*/
|
|
58
|
-
async insertManyIfNotExistsByFilter(items) {
|
|
59
|
-
if (items.length == 0) {
|
|
60
|
-
return [];
|
|
61
|
-
}
|
|
62
|
-
const documents = items.map(({ filter, entity }) => ({ filter, document: mongoDocumentFromMaybeNewEntity(entity) }));
|
|
63
|
-
const documentOperations = documents.map(({ filter, document }) => ({ document, operation: updateOneOperation(filter, { $setOnInsert: document }, { upsert: true }) }));
|
|
64
|
-
const operations = documentOperations.map((o) => o.operation);
|
|
65
|
-
const result = await this.collection.bulkWrite(operations, { ordered: false });
|
|
66
|
-
assertDefined(result.upsertedIds);
|
|
67
|
-
const entities = objectKeys(result.upsertedIds).map((index) => toEntity(documentOperations[index].document));
|
|
68
|
-
return entities;
|
|
69
|
-
}
|
|
70
|
-
async load(id, options) {
|
|
71
|
-
const entity = await this.tryLoad(id, options);
|
|
72
|
-
return throwIfUndefinedElsePass(entity, this.collection.collectionName);
|
|
73
|
-
}
|
|
74
|
-
async tryLoad(id, options) {
|
|
75
|
-
return this.tryLoadByFilter({ _id: id }, options);
|
|
76
|
-
}
|
|
77
|
-
async loadAndUpdate(id, update, options) {
|
|
78
|
-
const entity = await this.tryLoadAndUpdate(id, update, options);
|
|
79
|
-
return throwIfUndefinedElsePass(entity, this.collection.collectionName);
|
|
80
|
-
}
|
|
81
|
-
async tryLoadAndUpdate(id, update, options) {
|
|
82
|
-
return this.tryLoadByFilterAndUpdate({ _id: id }, update, options);
|
|
83
|
-
}
|
|
84
|
-
async loadAndDelete(id, options) {
|
|
85
|
-
const entity = await this.tryLoadAndDelete(id, options);
|
|
86
|
-
return throwIfUndefinedElsePass(entity, this.collection.collectionName);
|
|
87
|
-
}
|
|
88
|
-
async loadByFilter(filter, options) {
|
|
89
|
-
const entity = await this.tryLoadByFilter(filter, options);
|
|
90
|
-
return throwIfUndefinedElsePass(entity, this.collection.collectionName);
|
|
91
|
-
}
|
|
92
|
-
async tryLoadByFilter(filter, options) {
|
|
93
|
-
const document = await this.collection.findOne(filter, options);
|
|
94
|
-
if (isNullOrUndefined(document)) {
|
|
95
|
-
return undefined;
|
|
96
|
-
}
|
|
97
|
-
return toEntity(document);
|
|
98
|
-
}
|
|
99
|
-
async loadProjectedByFilter(filter, mode, projection, options) {
|
|
100
|
-
const id = await this.tryLoadProjectedByFilter(filter, mode, projection, options);
|
|
101
|
-
return throwIfUndefinedElsePass(id, this.collection.collectionName);
|
|
102
|
-
}
|
|
103
|
-
async tryLoadProjectedByFilter(filter, mode, projection, options) {
|
|
104
|
-
const document = await this.collection.findOne(filter, { ...options, projection: toMongoProjection(mode, projection) });
|
|
105
|
-
if (isNullOrUndefined(document)) {
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
return toProjectedEntity(document);
|
|
109
|
-
}
|
|
110
|
-
async loadByFilterAndDelete(filter, options) {
|
|
111
|
-
const entity = await this.tryLoadByFilterAndDelete(filter, options);
|
|
112
|
-
return throwIfUndefinedElsePass(entity, this.collection.collectionName);
|
|
113
|
-
}
|
|
114
|
-
async tryLoadAndDelete(id, options) {
|
|
115
|
-
return this.tryLoadByFilterAndDelete({ _id: id }, options);
|
|
116
|
-
}
|
|
117
|
-
async tryLoadByFilterAndDelete(filter, options) {
|
|
118
|
-
const result = await this.collection.findOneAndDelete(filter, options);
|
|
119
|
-
if (isNull(result)) {
|
|
120
|
-
return undefined;
|
|
121
|
-
}
|
|
122
|
-
return toEntity(result);
|
|
123
|
-
}
|
|
124
|
-
async loadByFilterAndUpdate(filter, update, options) {
|
|
125
|
-
const entity = await this.tryLoadByFilterAndUpdate(filter, update, options);
|
|
126
|
-
return throwIfUndefinedElsePass(entity, this.collection.collectionName);
|
|
127
|
-
}
|
|
128
|
-
async tryLoadByFilterAndUpdate(filter, update, options) {
|
|
129
|
-
const document = await this.collection.findOneAndUpdate(filter, update, options);
|
|
130
|
-
if (isNull(document)) {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
return toEntity(document);
|
|
134
|
-
}
|
|
135
|
-
async loadManyById(ids, options) {
|
|
136
|
-
const filter = { _id: { $in: ids } };
|
|
137
|
-
return this.loadManyByFilter(filter, options);
|
|
138
|
-
}
|
|
139
|
-
async loadManyByFilter(filter, options) {
|
|
140
|
-
const documents = await this.collection.find(filter, options).toArray();
|
|
141
|
-
return documents.map(toEntity);
|
|
142
|
-
}
|
|
143
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
144
|
-
loadManyByIdWithCursor(ids, options) {
|
|
145
|
-
const filter = { _id: { $in: ids } };
|
|
146
|
-
return this.loadManyByFilterWithCursor(filter, options);
|
|
147
|
-
}
|
|
148
|
-
async *loadManyByFilterWithCursor(filter, options) {
|
|
149
|
-
const cursor = this.collection.find(filter, options);
|
|
150
|
-
for await (const document of cursor) {
|
|
151
|
-
if (isNullOrUndefined(document)) {
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
yield toEntity(document);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
async loadManyProjectedById(ids, mode, projection, options) {
|
|
158
|
-
const filter = { _id: { $in: ids } };
|
|
159
|
-
return this.loadManyProjectedByFilter(filter, mode, projection, options);
|
|
160
|
-
}
|
|
161
|
-
async loadManyProjectedByFilter(filter, mode, projection, options) {
|
|
162
|
-
const documents = await this.collection.find(filter, { ...options, projection: toMongoProjection(mode, projection) }).toArray();
|
|
163
|
-
return documents.map(toProjectedEntity);
|
|
164
|
-
}
|
|
165
|
-
async *loadManyProjectedByFilterWithCursor(filter, mode, projection, options) {
|
|
166
|
-
const cursor = this.collection.find(filter, { ...options, projection: toMongoProjection(mode, projection) });
|
|
167
|
-
for await (const document of cursor) {
|
|
168
|
-
if (isNullOrUndefined(document)) {
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
yield toProjectedEntity(document);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
async deleteById(id) {
|
|
175
|
-
return this.deleteByFilter({ _id: id });
|
|
176
|
-
}
|
|
177
|
-
async deleteManyById(ids) {
|
|
178
|
-
if (ids.length == 0) {
|
|
179
|
-
return 0;
|
|
180
|
-
}
|
|
181
|
-
const filter = { _id: { $in: ids } };
|
|
182
|
-
return this.deleteManyByFilter(filter);
|
|
183
|
-
}
|
|
184
|
-
async deleteByFilter(filter) {
|
|
185
|
-
const { deletedCount } = await this.collection.deleteOne(filter);
|
|
186
|
-
return deletedCount == 1;
|
|
187
|
-
}
|
|
188
|
-
async deleteManyByFilter(filter) {
|
|
189
|
-
const { deletedCount } = await this.collection.deleteMany(filter);
|
|
190
|
-
return deletedCount;
|
|
191
|
-
}
|
|
192
|
-
async replace(entity, options = {}) {
|
|
193
|
-
return this.replaceByFilter({ _id: entity.id }, entity, options);
|
|
194
|
-
}
|
|
195
|
-
async replaceByFilter(filter, entity, options = {}) {
|
|
196
|
-
const document = toMongoDocument(entity);
|
|
197
|
-
const result = await this.collection.replaceOne(filter, document, options);
|
|
198
|
-
return (result.matchedCount + result.upsertedCount) > 0;
|
|
199
|
-
}
|
|
200
|
-
async replaceMany(entities, options) {
|
|
201
|
-
if (entities.length == 0) {
|
|
202
|
-
return 0;
|
|
203
|
-
}
|
|
204
|
-
const documents = entities.map(toMongoDocument);
|
|
205
|
-
const operations = documents.map((document) => replaceOneOperation({ _id: document._id }, document, options));
|
|
206
|
-
const result = await this.collection.bulkWrite(operations);
|
|
207
|
-
if (result.matchedCount + result.upsertedCount != entities.length) {
|
|
208
|
-
throw new NotFoundError(`${entities.length - (result.matchedCount + result.upsertedCount)} entities in ${this.collection.collectionName} not found`);
|
|
209
|
-
}
|
|
210
|
-
return (result.matchedCount + result.upsertedCount);
|
|
211
|
-
}
|
|
212
|
-
async update(filter, update, options = {}) {
|
|
213
|
-
const { matchedCount, modifiedCount } = await this.collection.updateOne(filter, update, options);
|
|
214
|
-
const updateResult = {
|
|
215
|
-
matchedCount,
|
|
216
|
-
modifiedCount
|
|
217
|
-
};
|
|
218
|
-
return updateResult;
|
|
219
|
-
}
|
|
220
|
-
async updateMany(filter, update, options = {}) {
|
|
221
|
-
const { matchedCount, modifiedCount } = await this.collection.updateMany(filter, update, options);
|
|
222
|
-
const updateResult = {
|
|
223
|
-
matchedCount,
|
|
224
|
-
modifiedCount
|
|
225
|
-
};
|
|
226
|
-
return updateResult;
|
|
227
|
-
}
|
|
228
|
-
async has(id) {
|
|
229
|
-
return this.hasByFilter({ _id: id });
|
|
230
|
-
}
|
|
231
|
-
async hasByFilter(filter) {
|
|
232
|
-
const count = await this.countByFilter(filter, { limit: 1 });
|
|
233
|
-
return count > 0;
|
|
234
|
-
}
|
|
235
|
-
async hasMany(ids) {
|
|
236
|
-
const filter = { _id: { $in: ids } };
|
|
237
|
-
return this.getIdsByFilter(filter);
|
|
238
|
-
}
|
|
239
|
-
async getIdsByFilter(filter) {
|
|
240
|
-
const result = await this.collection.distinct('_id', filter);
|
|
241
|
-
return result;
|
|
242
|
-
}
|
|
243
|
-
async hasAll(ids) {
|
|
244
|
-
const filter = { _id: { $in: ids } };
|
|
245
|
-
const count = await this.countByFilter(filter);
|
|
246
|
-
return count == ids.length;
|
|
247
|
-
}
|
|
248
|
-
async countByFilter(filter, { limit, skip } = {}) {
|
|
249
|
-
return this.collection.countDocuments(filter, { limit, skip });
|
|
250
|
-
}
|
|
251
|
-
async countByFilterEstimated() {
|
|
252
|
-
return this.collection.estimatedDocumentCount();
|
|
253
|
-
}
|
|
254
|
-
async drop() {
|
|
255
|
-
await this.collection.drop();
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
function throwIfUndefinedElsePass(value, collectionName) {
|
|
259
|
-
if (value == undefined) {
|
|
260
|
-
throw new NotFoundError(`entity not found in ${collectionName}`);
|
|
261
|
-
}
|
|
262
|
-
return value;
|
|
263
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { Entity, MaybeNewEntity } from '../../database/index.js';
|
|
2
|
-
import type { BulkWriteResult } from 'mongodb';
|
|
3
|
-
import type { Collection } from './classes.js';
|
|
4
|
-
import type { ReplaceOptions, UpdateOptions } from './mongo-base.repository.js';
|
|
5
|
-
import type { Filter, UpdateFilter } from './types.js';
|
|
6
|
-
export type BulkResult = {
|
|
7
|
-
insertedCount: number;
|
|
8
|
-
matchedCount: number;
|
|
9
|
-
modifiedCount: number;
|
|
10
|
-
deletedCount: number;
|
|
11
|
-
upsertedCount: number;
|
|
12
|
-
raw: BulkWriteResult;
|
|
13
|
-
};
|
|
14
|
-
export declare class MongoBulk<T extends Entity> {
|
|
15
|
-
private readonly collection;
|
|
16
|
-
private readonly operations;
|
|
17
|
-
private executed;
|
|
18
|
-
constructor(collection: Collection<T>);
|
|
19
|
-
/**
|
|
20
|
-
* execute operations
|
|
21
|
-
* @param ordered whether operations must be executed in order (slower) (default: false)
|
|
22
|
-
*/
|
|
23
|
-
execute(ordered?: boolean): Promise<BulkResult>;
|
|
24
|
-
insert<U extends T>(entity: MaybeNewEntity<U>): U;
|
|
25
|
-
insertMany<U extends T>(entities: MaybeNewEntity<U>[]): U[];
|
|
26
|
-
update<U extends T>(filter: Filter<U>, update: UpdateFilter<U>, options?: UpdateOptions): this;
|
|
27
|
-
updateMany<U extends T>(filter: Filter<U>, update: UpdateFilter<U>, options?: UpdateOptions): this;
|
|
28
|
-
replace<U extends T>(entity: U, options?: ReplaceOptions): this;
|
|
29
|
-
replaceMany<U extends T>(entities: U[], options?: ReplaceOptions): this;
|
|
30
|
-
replaceByFilter<U extends T>(filter: Filter<U>, entity: U, options?: ReplaceOptions): this;
|
|
31
|
-
delete<U extends T>(entity: U): this;
|
|
32
|
-
deleteMany<U extends T>(entities: U[]): this;
|
|
33
|
-
deleteByFilter<U extends T>(filter: Filter<U>): this;
|
|
34
|
-
deleteManyByFilter<U extends T>(filter: Filter<U>): this;
|
|
35
|
-
}
|