@rapidrest/service-core 1.0.0-beta.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/LICENSE +23 -0
- package/README.md +16 -0
- package/dist/lib/ApiErrors.js +41 -0
- package/dist/lib/ApiErrors.js.map +1 -0
- package/dist/lib/BackgroundService.js +16 -0
- package/dist/lib/BackgroundService.js.map +1 -0
- package/dist/lib/BackgroundServiceManager.js +135 -0
- package/dist/lib/BackgroundServiceManager.js.map +1 -0
- package/dist/lib/BulkError.js +24 -0
- package/dist/lib/BulkError.js.map +1 -0
- package/dist/lib/EventListenerManager.js +161 -0
- package/dist/lib/EventListenerManager.js.map +1 -0
- package/dist/lib/NetUtils.js +103 -0
- package/dist/lib/NetUtils.js.map +1 -0
- package/dist/lib/NotificationUtils.js +34 -0
- package/dist/lib/NotificationUtils.js.map +1 -0
- package/dist/lib/ObjectFactory.js +90 -0
- package/dist/lib/ObjectFactory.js.map +1 -0
- package/dist/lib/OpenApiSpec.js +748 -0
- package/dist/lib/OpenApiSpec.js.map +1 -0
- package/dist/lib/Server.js +549 -0
- package/dist/lib/Server.js.map +1 -0
- package/dist/lib/Types.js +2 -0
- package/dist/lib/Types.js.map +1 -0
- package/dist/lib/auth/AuthMiddleware.js +229 -0
- package/dist/lib/auth/AuthMiddleware.js.map +1 -0
- package/dist/lib/auth/AuthStrategy.js +2 -0
- package/dist/lib/auth/AuthStrategy.js.map +1 -0
- package/dist/lib/auth/BasicStrategy.js +106 -0
- package/dist/lib/auth/BasicStrategy.js.map +1 -0
- package/dist/lib/auth/JWTStrategy.js +161 -0
- package/dist/lib/auth/JWTStrategy.js.map +1 -0
- package/dist/lib/auth/index.js +5 -0
- package/dist/lib/auth/index.js.map +1 -0
- package/dist/lib/database/ConnectionKinds.js +14 -0
- package/dist/lib/database/ConnectionKinds.js.map +1 -0
- package/dist/lib/database/ConnectionManager.js +161 -0
- package/dist/lib/database/ConnectionManager.js.map +1 -0
- package/dist/lib/database/MongoConnection.js +86 -0
- package/dist/lib/database/MongoConnection.js.map +1 -0
- package/dist/lib/database/MongoRepository.js +136 -0
- package/dist/lib/database/MongoRepository.js.map +1 -0
- package/dist/lib/database/MongoSchemaSync.js +136 -0
- package/dist/lib/database/MongoSchemaSync.js.map +1 -0
- package/dist/lib/database/NamingUtils.js +52 -0
- package/dist/lib/database/NamingUtils.js.map +1 -0
- package/dist/lib/database/TypeOrmSupport.js +146 -0
- package/dist/lib/database/TypeOrmSupport.js.map +1 -0
- package/dist/lib/database/index.js +7 -0
- package/dist/lib/database/index.js.map +1 -0
- package/dist/lib/decorators/DatabaseDecorators.js +52 -0
- package/dist/lib/decorators/DatabaseDecorators.js.map +1 -0
- package/dist/lib/decorators/DocDecorators.js +120 -0
- package/dist/lib/decorators/DocDecorators.js.map +1 -0
- package/dist/lib/decorators/EventDecorators.js +24 -0
- package/dist/lib/decorators/EventDecorators.js.map +1 -0
- package/dist/lib/decorators/ModelDecorators.js +173 -0
- package/dist/lib/decorators/ModelDecorators.js.map +1 -0
- package/dist/lib/decorators/PersistenceDecorators.js +177 -0
- package/dist/lib/decorators/PersistenceDecorators.js.map +1 -0
- package/dist/lib/decorators/RouteDecorators.js +324 -0
- package/dist/lib/decorators/RouteDecorators.js.map +1 -0
- package/dist/lib/decorators/index.js +7 -0
- package/dist/lib/decorators/index.js.map +1 -0
- package/dist/lib/http/Adapters.js +230 -0
- package/dist/lib/http/Adapters.js.map +1 -0
- package/dist/lib/http/Router.js +403 -0
- package/dist/lib/http/Router.js.map +1 -0
- package/dist/lib/http/WebSocket.js +82 -0
- package/dist/lib/http/WebSocket.js.map +1 -0
- package/dist/lib/http/index.js +4 -0
- package/dist/lib/http/index.js.map +1 -0
- package/dist/lib/http/types.js +5 -0
- package/dist/lib/http/types.js.map +1 -0
- package/dist/lib/index.js +18 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/models/BaseEntity.js +83 -0
- package/dist/lib/models/BaseEntity.js.map +1 -0
- package/dist/lib/models/BaseMongoEntity.js +40 -0
- package/dist/lib/models/BaseMongoEntity.js.map +1 -0
- package/dist/lib/models/ModelUtils.js +645 -0
- package/dist/lib/models/ModelUtils.js.map +1 -0
- package/dist/lib/models/RecoverableBaseEntity.js +39 -0
- package/dist/lib/models/RecoverableBaseEntity.js.map +1 -0
- package/dist/lib/models/RecoverableBaseMongoEntity.js +40 -0
- package/dist/lib/models/RecoverableBaseMongoEntity.js.map +1 -0
- package/dist/lib/models/RepoUtils.js +717 -0
- package/dist/lib/models/RepoUtils.js.map +1 -0
- package/dist/lib/models/SimpleEntity.js +42 -0
- package/dist/lib/models/SimpleEntity.js.map +1 -0
- package/dist/lib/models/SimpleMongoEntity.js +38 -0
- package/dist/lib/models/SimpleMongoEntity.js.map +1 -0
- package/dist/lib/models/StatusExtraData.js +15 -0
- package/dist/lib/models/StatusExtraData.js.map +1 -0
- package/dist/lib/models/index.js +10 -0
- package/dist/lib/models/index.js.map +1 -0
- package/dist/lib/routes/AdminRoute.js +268 -0
- package/dist/lib/routes/AdminRoute.js.map +1 -0
- package/dist/lib/routes/MetricsRoute.js +86 -0
- package/dist/lib/routes/MetricsRoute.js.map +1 -0
- package/dist/lib/routes/ModelRoute.js +481 -0
- package/dist/lib/routes/ModelRoute.js.map +1 -0
- package/dist/lib/routes/OpenAPIRoute.js +115 -0
- package/dist/lib/routes/OpenAPIRoute.js.map +1 -0
- package/dist/lib/routes/RouteUtils.js +445 -0
- package/dist/lib/routes/RouteUtils.js.map +1 -0
- package/dist/lib/routes/StatusRoute.js +55 -0
- package/dist/lib/routes/StatusRoute.js.map +1 -0
- package/dist/lib/routes/index.js +7 -0
- package/dist/lib/routes/index.js.map +1 -0
- package/dist/lib/security/ACLRouteMongo.js +194 -0
- package/dist/lib/security/ACLRouteMongo.js.map +1 -0
- package/dist/lib/security/ACLRouteSQL.js +193 -0
- package/dist/lib/security/ACLRouteSQL.js.map +1 -0
- package/dist/lib/security/ACLUtils.js +457 -0
- package/dist/lib/security/ACLUtils.js.map +1 -0
- package/dist/lib/security/AccessControlList.js +18 -0
- package/dist/lib/security/AccessControlList.js.map +1 -0
- package/dist/lib/security/AccessControlListMongo.js +155 -0
- package/dist/lib/security/AccessControlListMongo.js.map +1 -0
- package/dist/lib/security/AccessControlListSQL.js +149 -0
- package/dist/lib/security/AccessControlListSQL.js.map +1 -0
- package/dist/lib/security/index.js +3 -0
- package/dist/lib/security/index.js.map +1 -0
- package/dist/lib/test/index.js +3 -0
- package/dist/lib/test/index.js.map +1 -0
- package/dist/lib/test/request.js +99 -0
- package/dist/lib/test/request.js.map +1 -0
- package/dist/lib/test/requestws.js +173 -0
- package/dist/lib/test/requestws.js.map +1 -0
- package/dist/types/ApiErrors.d.ts +38 -0
- package/dist/types/BackgroundService.d.ts +30 -0
- package/dist/types/BackgroundServiceManager.d.ts +66 -0
- package/dist/types/BulkError.d.ts +11 -0
- package/dist/types/EventListenerManager.d.ts +36 -0
- package/dist/types/NetUtils.d.ts +29 -0
- package/dist/types/NotificationUtils.d.ts +25 -0
- package/dist/types/ObjectFactory.d.ts +17 -0
- package/dist/types/OpenApiSpec.d.ts +114 -0
- package/dist/types/Server.d.ts +180 -0
- package/dist/types/Types.d.ts +8 -0
- package/dist/types/auth/AuthMiddleware.d.ts +42 -0
- package/dist/types/auth/AuthStrategy.d.ts +32 -0
- package/dist/types/auth/BasicStrategy.d.ts +33 -0
- package/dist/types/auth/JWTStrategy.d.ts +61 -0
- package/dist/types/auth/index.d.ts +4 -0
- package/dist/types/database/ConnectionKinds.d.ts +7 -0
- package/dist/types/database/ConnectionManager.d.ts +35 -0
- package/dist/types/database/MongoConnection.d.ts +54 -0
- package/dist/types/database/MongoRepository.d.ts +92 -0
- package/dist/types/database/MongoSchemaSync.d.ts +41 -0
- package/dist/types/database/NamingUtils.d.ts +24 -0
- package/dist/types/database/TypeOrmSupport.d.ts +20 -0
- package/dist/types/database/index.d.ts +6 -0
- package/dist/types/decorators/DatabaseDecorators.d.ts +18 -0
- package/dist/types/decorators/DocDecorators.d.ts +69 -0
- package/dist/types/decorators/EventDecorators.d.ts +12 -0
- package/dist/types/decorators/ModelDecorators.d.ts +80 -0
- package/dist/types/decorators/PersistenceDecorators.d.ts +117 -0
- package/dist/types/decorators/RouteDecorators.d.ts +172 -0
- package/dist/types/decorators/index.d.ts +6 -0
- package/dist/types/http/Adapters.d.ts +68 -0
- package/dist/types/http/Router.d.ts +99 -0
- package/dist/types/http/WebSocket.d.ts +56 -0
- package/dist/types/http/index.d.ts +6 -0
- package/dist/types/http/types.d.ts +54 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/models/BaseEntity.d.ts +29 -0
- package/dist/types/models/BaseMongoEntity.d.ts +13 -0
- package/dist/types/models/ModelUtils.d.ts +166 -0
- package/dist/types/models/RecoverableBaseEntity.d.ts +16 -0
- package/dist/types/models/RecoverableBaseMongoEntity.d.ts +16 -0
- package/dist/types/models/RepoUtils.d.ts +154 -0
- package/dist/types/models/SimpleEntity.d.ts +14 -0
- package/dist/types/models/SimpleMongoEntity.d.ts +15 -0
- package/dist/types/models/StatusExtraData.d.ts +6 -0
- package/dist/types/models/index.d.ts +9 -0
- package/dist/types/routes/AdminRoute.d.ts +47 -0
- package/dist/types/routes/MetricsRoute.d.ts +15 -0
- package/dist/types/routes/ModelRoute.d.ts +226 -0
- package/dist/types/routes/OpenAPIRoute.d.ts +17 -0
- package/dist/types/routes/RouteUtils.d.ts +55 -0
- package/dist/types/routes/StatusRoute.d.ts +11 -0
- package/dist/types/routes/index.d.ts +6 -0
- package/dist/types/security/ACLRouteMongo.d.ts +19 -0
- package/dist/types/security/ACLRouteSQL.d.ts +19 -0
- package/dist/types/security/ACLUtils.d.ts +94 -0
- package/dist/types/security/AccessControlList.d.ts +103 -0
- package/dist/types/security/AccessControlListMongo.d.ts +24 -0
- package/dist/types/security/AccessControlListSQL.d.ts +24 -0
- package/dist/types/security/index.d.ts +2 -0
- package/dist/types/test/index.d.ts +2 -0
- package/dist/types/test/request.d.ts +24 -0
- package/dist/types/test/requestws.d.ts +21 -0
- package/docs/Makefile +20 -0
- package/docs/conf.py +58 -0
- package/docs/index.rst +17 -0
- package/docs/make.bat +35 -0
- package/docs/reference/@rapidrest/namespaces/DatabaseDecorators/README.md +13 -0
- package/docs/reference/@rapidrest/namespaces/DatabaseDecorators/functions/MongoRepository.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DatabaseDecorators/functions/RedisConnection.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DatabaseDecorators/functions/Repository.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/README.md +23 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Default.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Description.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Document.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Example.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Format.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Returns.md +28 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Summary.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/Tags.md +25 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/functions/TypeInfo.md +28 -0
- package/docs/reference/@rapidrest/namespaces/DocDecorators/interfaces/DocumentsData.md +57 -0
- package/docs/reference/@rapidrest/namespaces/EventDecorators/README.md +12 -0
- package/docs/reference/@rapidrest/namespaces/EventDecorators/functions/EventListener.md +17 -0
- package/docs/reference/@rapidrest/namespaces/EventDecorators/functions/OnEvent.md +26 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/README.md +26 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/Cache.md +25 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/ChildEntity.md +18 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/DataStore.md +25 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/Identifier.md +27 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/Protect.md +35 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/Reference.md +25 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/Shard.md +27 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/functions/TrackChanges.md +26 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/interfaces/PendingTypeOrmColumn.md +45 -0
- package/docs/reference/@rapidrest/namespaces/ModelDecorators/variables/pendingTypeOrmColumns.md +14 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/README.md +25 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/Column.md +25 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/Entity.md +26 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/Index.md +119 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/PrimaryColumn.md +25 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/Unique.md +68 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/getColumnMetadata.md +26 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/getEntityName.md +26 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/functions/getIndexMetadata.md +27 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/interfaces/ColumnInfo.md +41 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/interfaces/ColumnOptions.md +51 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/interfaces/IndexInfo.md +41 -0
- package/docs/reference/@rapidrest/namespaces/PersistenceDecorators/interfaces/IndexOptions.md +51 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/README.md +36 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/After.md +26 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Auth.md +33 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/AuthResult.md +31 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Before.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/ContentType.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Delete.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Get.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Head.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Header.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Method.md +31 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Model.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Options.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Param.md +26 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Patch.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Post.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Protect.md +31 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Put.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Query.md +26 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Request.md +31 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/RequiresRole.md +26 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Response.md +31 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Route.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Socket.md +33 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/User.md +31 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/Validate.md +25 -0
- package/docs/reference/@rapidrest/namespaces/RouteDecorators/functions/WebSocket.md +25 -0
- package/docs/reference/README.md +20 -0
- package/docs/reference/classes/ACLUtils.md +251 -0
- package/docs/reference/classes/AdminRoute.md +51 -0
- package/docs/reference/classes/AuthMiddleware.md +131 -0
- package/docs/reference/classes/BackgroundService.md +117 -0
- package/docs/reference/classes/BackgroundServiceManager.md +172 -0
- package/docs/reference/classes/BaseEntity.md +82 -0
- package/docs/reference/classes/BaseMongoEntity.md +107 -0
- package/docs/reference/classes/BasicStrategy.md +89 -0
- package/docs/reference/classes/BasicStrategyOptions.md +91 -0
- package/docs/reference/classes/BulkError.md +271 -0
- package/docs/reference/classes/ConnectionManager.md +75 -0
- package/docs/reference/classes/EventListenerManager.md +88 -0
- package/docs/reference/classes/JWTStrategy.md +77 -0
- package/docs/reference/classes/JWTStrategyOptions.md +97 -0
- package/docs/reference/classes/MetricsRoute.md +27 -0
- package/docs/reference/classes/ModelRoute.md +527 -0
- package/docs/reference/classes/ModelUtils.md +448 -0
- package/docs/reference/classes/MongoConnection.md +218 -0
- package/docs/reference/classes/MongoRepository.md +354 -0
- package/docs/reference/classes/MongoSchemaSync.md +67 -0
- package/docs/reference/classes/NetUtils.md +90 -0
- package/docs/reference/classes/NotificationUtils.md +77 -0
- package/docs/reference/classes/ObjectFactory.md +336 -0
- package/docs/reference/classes/OpenAPIRoute.md +77 -0
- package/docs/reference/classes/OpenApiSpec.md +892 -0
- package/docs/reference/classes/RecoverableBaseEntity.md +114 -0
- package/docs/reference/classes/RecoverableBaseMongoEntity.md +124 -0
- package/docs/reference/classes/RedisTransport.md +2202 -0
- package/docs/reference/classes/RepoUtils.md +486 -0
- package/docs/reference/classes/RouteUtils.md +191 -0
- package/docs/reference/classes/Server.md +408 -0
- package/docs/reference/classes/SimpleEntity.md +48 -0
- package/docs/reference/classes/SimpleMongoEntity.md +66 -0
- package/docs/reference/classes/StatusExtraData.md +57 -0
- package/docs/reference/classes/StatusRoute.md +26 -0
- package/docs/reference/enumerations/ACLAction.md +63 -0
- package/docs/reference/enumerations/ApiErrorMessages.md +123 -0
- package/docs/reference/enumerations/ApiErrors.md +123 -0
- package/docs/reference/functions/isSqlDataSource.md +26 -0
- package/docs/reference/functions/resolveCollectionName.md +33 -0
- package/docs/reference/functions/snakeCase.md +28 -0
- package/docs/reference/globals.md +91 -0
- package/docs/reference/interfaces/ACLRecord.md +96 -0
- package/docs/reference/interfaces/AccessControlList.md +76 -0
- package/docs/reference/interfaces/AuthResult.md +55 -0
- package/docs/reference/interfaces/AuthStrategy.md +57 -0
- package/docs/reference/interfaces/CreateRequestOptions.md +121 -0
- package/docs/reference/interfaces/DeleteRequestOptions.md +147 -0
- package/docs/reference/interfaces/FindRequestOptions.md +133 -0
- package/docs/reference/interfaces/JWTAuthResult.md +84 -0
- package/docs/reference/interfaces/RepoCreateOptions.md +95 -0
- package/docs/reference/interfaces/RepoDeleteOptions.md +115 -0
- package/docs/reference/interfaces/RepoFindOptions.md +135 -0
- package/docs/reference/interfaces/RepoOperationOptions.md +69 -0
- package/docs/reference/interfaces/RepoUpdateOptions.md +111 -0
- package/docs/reference/interfaces/RequestOptions.md +112 -0
- package/docs/reference/interfaces/TruncateRequestOptions.md +175 -0
- package/docs/reference/interfaces/UpdateRequestOptions.md +149 -0
- package/docs/reference/type-aliases/OneOrMany.md +19 -0
- package/docs/reference/type-aliases/OneOrNull.md +19 -0
- package/docs/reference/type-aliases/PartialBaseEntity.md +17 -0
- package/docs/reference/type-aliases/PartialSimpleEntity.md +17 -0
- package/docs/reference/type-aliases/UpdateObject.md +19 -0
- package/package.json +125 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
11
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
12
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
13
|
+
import { ObjectDecorators } from "@rapidrest/core";
|
|
14
|
+
import { Redis } from "ioredis";
|
|
15
|
+
import { isSqlDataSource } from "./ConnectionKinds.js";
|
|
16
|
+
import { MongoConnection } from "./MongoConnection.js";
|
|
17
|
+
import { MongoSchemaSync } from "./MongoSchemaSync.js";
|
|
18
|
+
const { Destroy, Logger } = ObjectDecorators;
|
|
19
|
+
/**
|
|
20
|
+
* Provides database connection management.
|
|
21
|
+
*
|
|
22
|
+
* @author Jean-Philippe Steinmetz
|
|
23
|
+
*/
|
|
24
|
+
export class ConnectionManager {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.connections = new Map();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Builds a compatible connection URI for the database by the provided configuration.
|
|
30
|
+
*/
|
|
31
|
+
buildConnectionUri(config) {
|
|
32
|
+
// If a url is provided use that verbatim. We assume it's correct.
|
|
33
|
+
if (config.url) {
|
|
34
|
+
return config.url;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (!config.type || !config.host) {
|
|
38
|
+
throw new Error(`Invalid datastore config: ${JSON.stringify(config)}.`);
|
|
39
|
+
}
|
|
40
|
+
return `${config.protocol ? config.protocol : config.type}://${config.username && config.password ? `${config.username}:${config.password}@` : ""}${config.host}${config.port ? `:${config.port}` : ""}${config.database ? `/${config.database}` : ""}${config.options ? `?${config.options}` : ""}`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Dynamically imports the given optional peer dependency, throwing a helpful error if it is not installed.
|
|
45
|
+
*
|
|
46
|
+
* @param pkg The name of the package to import.
|
|
47
|
+
* @param datastoreName The name of the datastore requiring the package.
|
|
48
|
+
* @param datastoreType The type of the datastore requiring the package.
|
|
49
|
+
*/
|
|
50
|
+
async importOptionalDependency(pkg, datastoreName, datastoreType) {
|
|
51
|
+
try {
|
|
52
|
+
return await import(pkg);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
throw new Error(`Datastore '${datastoreName}' is of type '${datastoreType}' which requires the optional peer dependency '${pkg}'. Install it with: yarn add ${pkg}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Attempts to initiate all database connections as defined in the config.
|
|
60
|
+
*
|
|
61
|
+
* @param datastores A map of configured datastores to be passed to the underlying engine.
|
|
62
|
+
* @param models A map of model names and associated class definitions to establish database connections for.
|
|
63
|
+
*/
|
|
64
|
+
async connect(datastores, models) {
|
|
65
|
+
const processedModels = new Map();
|
|
66
|
+
// Go through each datastore in the configuration and attempt to make a connection
|
|
67
|
+
for (const name in datastores) {
|
|
68
|
+
const datastore = datastores[name];
|
|
69
|
+
// It's possible that the connection was already configured during a previous run. In that case we will
|
|
70
|
+
// attempt to reconnect instead of creating a new connection.
|
|
71
|
+
let connection = this.connections.get(name);
|
|
72
|
+
if (connection && isSqlDataSource(connection) && !connection.isInitialized) {
|
|
73
|
+
this.logger.info(`Reconnecting to database ${name}...`);
|
|
74
|
+
await connection.initialize();
|
|
75
|
+
}
|
|
76
|
+
if (!connection) {
|
|
77
|
+
datastore.name = name;
|
|
78
|
+
const url = this.buildConnectionUri(datastore);
|
|
79
|
+
this.logger.info(`Connecting to database ${name} [${url.replace(datastore.username, "****").replace(datastore.password, "****")}]...`);
|
|
80
|
+
if (datastore.type === "redis") {
|
|
81
|
+
connection = new Redis(url);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Make an array of all entities associated with this connection
|
|
85
|
+
const entities = [];
|
|
86
|
+
for (const className of models.keys()) {
|
|
87
|
+
// Get the class type
|
|
88
|
+
const clazz = models.get(className);
|
|
89
|
+
const ds = Reflect.getMetadata("rrst:datastore", clazz);
|
|
90
|
+
// Search for the associated datastore with the model via either config or @Model decorator
|
|
91
|
+
if (ds === name || (datastore.entities && datastore.entities.includes(className))) {
|
|
92
|
+
const processedDatastore = processedModels.get(clazz.name);
|
|
93
|
+
if (processedDatastore) {
|
|
94
|
+
throw new Error(`Model ${clazz.name} already defined as an entity for ${processedDatastore}`);
|
|
95
|
+
}
|
|
96
|
+
clazz.datastore = name;
|
|
97
|
+
entities.push(clazz);
|
|
98
|
+
processedModels.set(clazz.name, name);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (datastore.type === "mongodb" || datastore.type === "mongodb+srv") {
|
|
102
|
+
// Connect using the native MongoDB driver
|
|
103
|
+
const { MongoClient } = await this.importOptionalDependency("mongodb", name, datastore.type);
|
|
104
|
+
const client = new MongoClient(url, datastore.clientOptions);
|
|
105
|
+
await client.connect();
|
|
106
|
+
const db = client.db(datastore.database);
|
|
107
|
+
connection = new MongoConnection(name, client, db, entities);
|
|
108
|
+
// Perform structure synchronization when requested
|
|
109
|
+
if (datastore.synchronize) {
|
|
110
|
+
const schemaSync = new MongoSchemaSync(db, this.logger);
|
|
111
|
+
await schemaSync.synchronize(entities);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// Connect using TypeORM
|
|
116
|
+
await this.importOptionalDependency("typeorm", name, datastore.type);
|
|
117
|
+
const orm = await import("./TypeOrmSupport.js");
|
|
118
|
+
connection = await orm.connect(name, datastore, entities, url);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
this.connections.set(name, connection);
|
|
123
|
+
}
|
|
124
|
+
this.logger.info(`Successfully connected to all configured databases.`);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Attempts to disconnect all active database connections.
|
|
128
|
+
*/
|
|
129
|
+
async disconnect() {
|
|
130
|
+
for (const conn of this.connections.values()) {
|
|
131
|
+
if (conn) {
|
|
132
|
+
if (conn instanceof MongoConnection) {
|
|
133
|
+
if (conn.isConnected) {
|
|
134
|
+
await conn.close();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (conn instanceof Redis) {
|
|
138
|
+
if (conn.status === "ready") {
|
|
139
|
+
conn.disconnect();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (isSqlDataSource(conn) && conn.isInitialized) {
|
|
143
|
+
await conn.destroy();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
this.connections.clear();
|
|
148
|
+
this.logger.info(`Successfully disconnected from all configured databases.`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
__decorate([
|
|
152
|
+
Logger,
|
|
153
|
+
__metadata("design:type", Object)
|
|
154
|
+
], ConnectionManager.prototype, "logger", void 0);
|
|
155
|
+
__decorate([
|
|
156
|
+
Destroy,
|
|
157
|
+
__metadata("design:type", Function),
|
|
158
|
+
__metadata("design:paramtypes", []),
|
|
159
|
+
__metadata("design:returntype", Promise)
|
|
160
|
+
], ConnectionManager.prototype, "disconnect", null);
|
|
161
|
+
//# sourceMappingURL=ConnectionManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectionManager.js","sourceRoot":"","sources":["../../../src/database/ConnectionManager.ts"],"names":[],"mappings":";;;;;;;;;AAAA,gFAAgF;AAChF,kDAAkD;AAClD,+EAA+E;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,EAA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ClC,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;wBACnB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;oBACvB,CAAC;gBACL,CAAC;qBAAM,IAAI,IAAI,YAAY,KAAK,EAAE,CAAC;oBAC/B,IAAI,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;wBAC1B,IAAI,CAAC,UAAU,EAAE,CAAC;oBACtB,CAAC;gBACL,CAAC;qBAAM,IAAI,eAAe,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;oBACrD,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;gBACzB,CAAC;YACL,CAAC;QACL,CAAC;QAED,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,0DAA0D,CAAC,CAAC;IACjF,CAAC;CACJ;AAxIW;IADP,MAAM;;iDACa;AAoHP;IADZ,OAAO;;;;mDAoBP"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { MongoRepository } from "./MongoRepository.js";
|
|
2
|
+
import { resolveCollectionName } from "./NamingUtils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Represents a single named connection to a MongoDB database using the native `mongodb` driver, including the
|
|
5
|
+
* registry of all model classes assigned to the connection's datastore.
|
|
6
|
+
*
|
|
7
|
+
* Note that this class only references the `mongodb` package via type-only imports and is therefore safe to load
|
|
8
|
+
* when the optional `mongodb` peer dependency is not installed.
|
|
9
|
+
*
|
|
10
|
+
* @author Jean-Philippe Steinmetz <rapidrests@gmail.com>
|
|
11
|
+
*/
|
|
12
|
+
export class MongoConnection {
|
|
13
|
+
constructor(name, client, db, entities) {
|
|
14
|
+
this.connected = true;
|
|
15
|
+
this.entities = new Map();
|
|
16
|
+
this.repos = new Map();
|
|
17
|
+
this.name = name;
|
|
18
|
+
this.client = client;
|
|
19
|
+
this.db = db;
|
|
20
|
+
if (entities) {
|
|
21
|
+
for (const clazz of entities) {
|
|
22
|
+
this.entities.set(clazz.name, clazz);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Indicates whether or not the connection is currently active. */
|
|
27
|
+
get isConnected() {
|
|
28
|
+
return this.connected;
|
|
29
|
+
}
|
|
30
|
+
/** Returns the admin interface of the underlying database. */
|
|
31
|
+
admin() {
|
|
32
|
+
return this.db.admin();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Closes the connection to the database server.
|
|
36
|
+
*/
|
|
37
|
+
async close() {
|
|
38
|
+
this.connected = false;
|
|
39
|
+
await this.client.close();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the name of the collection that records of the given model class are stored in.
|
|
43
|
+
*
|
|
44
|
+
* @param clazz The model class to resolve the collection name for.
|
|
45
|
+
*/
|
|
46
|
+
collectionNameFor(clazz) {
|
|
47
|
+
return resolveCollectionName(clazz);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Returns the list of all model classes registered with this connection.
|
|
51
|
+
*/
|
|
52
|
+
get entityClasses() {
|
|
53
|
+
return Array.from(this.entities.values());
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns a repository for performing operations against the collection associated with the given model class.
|
|
57
|
+
*
|
|
58
|
+
* @param classOrName The model class, or the name of a registered model class, to retrieve a repository for.
|
|
59
|
+
*/
|
|
60
|
+
getRepository(classOrName) {
|
|
61
|
+
let clazz = classOrName;
|
|
62
|
+
if (typeof classOrName === "string") {
|
|
63
|
+
clazz = this.entities.get(classOrName);
|
|
64
|
+
if (!clazz) {
|
|
65
|
+
throw new Error(`No entity named '${classOrName}' is registered with datastore '${this.name}'.`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
let repo = this.repos.get(clazz);
|
|
69
|
+
if (!repo) {
|
|
70
|
+
const collectionName = this.collectionNameFor(clazz);
|
|
71
|
+
repo = new MongoRepository(this.db, this.db.collection(collectionName), clazz);
|
|
72
|
+
this.repos.set(clazz, repo);
|
|
73
|
+
}
|
|
74
|
+
return repo;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Returns a repository for performing operations against the collection associated with the given model class.
|
|
78
|
+
* This is an alias of `getRepository` provided for compatibility with TypeORM's `DataSource` interface.
|
|
79
|
+
*
|
|
80
|
+
* @param classOrName The model class, or the name of a registered model class, to retrieve a repository for.
|
|
81
|
+
*/
|
|
82
|
+
getMongoRepository(classOrName) {
|
|
83
|
+
return this.getRepository(classOrName);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=MongoConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongoConnection.js","sourceRoot":"","sources":["../../../src/database/MongoConnection.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAEzD;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAe;IAYxB,YAAY,IAAY,EAAE,MAAmB,EAAE,EAAM,EAAE,QAAwB;QAJvE,cAAS,GAAY,IAAI,CAAC;QAC1B,aAAQ,GAAqB,IAAI,GAAG,EAAE,CAAC;QACvC,UAAK,GAAmC,IAAI,GAAG,EAAE,CAAC;QAGtD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QAEb,IAAI,QAAQ,EAAE,CAAC;YACX,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;gBAC3B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACzC,CAAC;QACL,CAAC;IACL,CAAC;IAED,mEAAmE;IACnE,IAAW,WAAW;QAClB,OAAO,IAAI,CAAC,SAAS,CAAC;IAC1B,C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AAC;IACtC,CAAC;IAED;;;;;OAKG;IACI,kBAAkB,CAAyB,WAAyB;QACvE,OAAO,IAAI,CAAC,aAAa,CAAI,WAAW,CAAC,CAAC;IAC9C,CAAC;CACJ"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a lightweight repository for performing common operations against a single MongoDB collection using the
|
|
3
|
+
* native `mongodb` driver. Instances of this class are obtained via `MongoConnection.getRepository`.
|
|
4
|
+
*
|
|
5
|
+
* Note that this class only references the `mongodb` package via type-only imports and is therefore safe to load
|
|
6
|
+
* when the optional `mongodb` peer dependency is not installed.
|
|
7
|
+
*
|
|
8
|
+
* @author Jean-Philippe Steinmetz <rapidrests@gmail.com>
|
|
9
|
+
*/
|
|
10
|
+
export class MongoRepository {
|
|
11
|
+
constructor(db, collection, modelClass) {
|
|
12
|
+
this.db = db;
|
|
13
|
+
this.collection = collection;
|
|
14
|
+
this.modelClass = modelClass;
|
|
15
|
+
}
|
|
16
|
+
/** The name of the underlying MongoDB collection. */
|
|
17
|
+
get collectionName() {
|
|
18
|
+
return this.collection.collectionName;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Executes the given aggregation pipeline against the collection and returns the resulting cursor.
|
|
22
|
+
*
|
|
23
|
+
* @param pipeline The aggregation pipeline stages to execute.
|
|
24
|
+
*/
|
|
25
|
+
aggregate(pipeline) {
|
|
26
|
+
return this.collection.aggregate(pipeline);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Drops the entire collection from the database, removing all documents and indexes. Does nothing if the
|
|
30
|
+
* collection does not exist.
|
|
31
|
+
*/
|
|
32
|
+
async clear() {
|
|
33
|
+
try {
|
|
34
|
+
await this.collection.drop();
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
// Ignore "ns not found" errors for collections that don't exist yet
|
|
38
|
+
if (err.codeName !== "NamespaceNotFound" && !String(err.message).includes("ns not found")) {
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns the number of documents matching the given filter.
|
|
45
|
+
*
|
|
46
|
+
* @param filter The query filter to match documents against.
|
|
47
|
+
*/
|
|
48
|
+
async count(filter) {
|
|
49
|
+
return this.collection.countDocuments(filter ?? {});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Deletes all documents matching the given filter.
|
|
53
|
+
*
|
|
54
|
+
* @param filter The query filter to match documents against.
|
|
55
|
+
*/
|
|
56
|
+
async deleteMany(filter) {
|
|
57
|
+
return this.collection.deleteMany(filter);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Deletes the first document matching the given filter.
|
|
61
|
+
*
|
|
62
|
+
* @param filter The query filter to match documents against.
|
|
63
|
+
*/
|
|
64
|
+
async deleteOne(filter) {
|
|
65
|
+
return this.collection.deleteOne(filter);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Returns the list of distinct values of the given field for all documents matching the given filter.
|
|
69
|
+
*
|
|
70
|
+
* @param field The name of the document field to return distinct values of.
|
|
71
|
+
* @param filter The query filter to match documents against.
|
|
72
|
+
*/
|
|
73
|
+
async distinct(field, filter) {
|
|
74
|
+
return this.collection.distinct(field, filter ?? {});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Returns all documents matching the given filter.
|
|
78
|
+
*
|
|
79
|
+
* @param filter The query filter to match documents against.
|
|
80
|
+
*/
|
|
81
|
+
async find(filter) {
|
|
82
|
+
return this.collection.find(filter ?? {}).toArray();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns the first document matching the given filter.
|
|
86
|
+
*
|
|
87
|
+
* @param filter The query filter to match documents against.
|
|
88
|
+
*/
|
|
89
|
+
async findOne(filter) {
|
|
90
|
+
return this.collection.findOne(filter);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Saves the given document to the collection. If the document has an existing `_id` the stored document is
|
|
94
|
+
* replaced (inserting if missing), otherwise the document is inserted and its newly assigned `_id` is set on
|
|
95
|
+
* the returned object.
|
|
96
|
+
*
|
|
97
|
+
* @param doc The document to save.
|
|
98
|
+
* @returns The saved document.
|
|
99
|
+
*/
|
|
100
|
+
async save(doc) {
|
|
101
|
+
const copy = { ...doc };
|
|
102
|
+
// Strip any undefined properties so they are omitted from the stored document
|
|
103
|
+
for (const key of Object.keys(copy)) {
|
|
104
|
+
if (copy[key] === undefined) {
|
|
105
|
+
delete copy[key];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (doc._id !== undefined && doc._id !== null) {
|
|
109
|
+
await this.collection.replaceOne({ _id: doc._id }, copy, { upsert: true });
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const result = await this.collection.insertOne(copy);
|
|
113
|
+
doc._id = result.insertedId;
|
|
114
|
+
}
|
|
115
|
+
return doc;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Updates all documents matching the given filter with the provided update operations.
|
|
119
|
+
*
|
|
120
|
+
* @param filter The query filter to match documents against.
|
|
121
|
+
* @param update The update operations (e.g. `$set`) to apply.
|
|
122
|
+
*/
|
|
123
|
+
async updateMany(filter, update) {
|
|
124
|
+
return this.collection.updateMany(filter, update);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Updates the first document matching the given filter with the provided update operations.
|
|
128
|
+
*
|
|
129
|
+
* @param filter The query filter to match documents against.
|
|
130
|
+
* @param update The update operations (e.g. `$set`) to apply.
|
|
131
|
+
*/
|
|
132
|
+
async updateOne(filter, update) {
|
|
133
|
+
return this.collection.updateOne(filter, update);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=MongoRepository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongoRepository.js","sourceRoot":"","sources":["../../../src/database/MongoRepository.ts"],"names":[],"mappings":"AAYA;;;;;;;;GAQG;AACH,MAA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E,MAAW;QAC5C,OAAO,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtD,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,SAAS,CAAC,MAAW,EAAE,MAAW;QAC3C,OAAO,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrD,CAAC;CACJ"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { getIndexMetadata } from "../decorators/PersistenceDecorators.js";
|
|
2
|
+
import { resolveCollectionName } from "./NamingUtils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Performs structure synchronization of MongoDB collections based on the persistence metadata of model classes.
|
|
5
|
+
*
|
|
6
|
+
* For each collection associated with a model class the following operations are performed:
|
|
7
|
+
* - The collection is created if it does not already exist.
|
|
8
|
+
* - Indexes declared via the `@Index` and `@Unique` decorators are created if missing.
|
|
9
|
+
* - Existing declared indexes whose definition has changed are dropped and re-created.
|
|
10
|
+
*
|
|
11
|
+
* The synchronization process never drops collections or data, never modifies the built-in `_id_` index, and never
|
|
12
|
+
* drops indexes that are not declared by the model class (e.g. shard keys or manually created indexes).
|
|
13
|
+
*
|
|
14
|
+
* @author Jean-Philippe Steinmetz <rapidrests@gmail.com>
|
|
15
|
+
*/
|
|
16
|
+
export class MongoSchemaSync {
|
|
17
|
+
constructor(db, logger) {
|
|
18
|
+
this.db = db;
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Synchronizes the structure of all collections associated with the given model classes.
|
|
23
|
+
*
|
|
24
|
+
* @param entities The list of model classes to synchronize collections for.
|
|
25
|
+
*/
|
|
26
|
+
async synchronize(entities) {
|
|
27
|
+
// Group the model classes by their resolved collection name so that all classes stored in the same
|
|
28
|
+
// collection (e.g. @ChildEntity subclasses) contribute their indexes to a single specification.
|
|
29
|
+
const collections = new Map();
|
|
30
|
+
for (const clazz of entities) {
|
|
31
|
+
const name = resolveCollectionName(clazz);
|
|
32
|
+
const group = collections.get(name) ?? [];
|
|
33
|
+
group.push(clazz);
|
|
34
|
+
collections.set(name, group);
|
|
35
|
+
}
|
|
36
|
+
for (const [name, classes] of collections.entries()) {
|
|
37
|
+
await this.syncCollection(name, classes);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Synchronizes the structure of a single collection.
|
|
42
|
+
*
|
|
43
|
+
* @param name The name of the collection to synchronize.
|
|
44
|
+
* @param classes The list of model classes whose records are stored in the collection.
|
|
45
|
+
*/
|
|
46
|
+
async syncCollection(name, classes) {
|
|
47
|
+
// Create the collection if it doesn't already exist
|
|
48
|
+
const existing = await this.db.listCollections({ name }, { nameOnly: true }).toArray();
|
|
49
|
+
if (existing.length === 0) {
|
|
50
|
+
try {
|
|
51
|
+
await this.db.createCollection(name);
|
|
52
|
+
this.logger?.info(`Created collection: ${name}`);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
// Tolerate concurrent creation races
|
|
56
|
+
if (err.codeName !== "NamespaceExists") {
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Build the merged set of desired indexes from all classes stored in this collection
|
|
62
|
+
const desired = new Map();
|
|
63
|
+
for (const clazz of classes) {
|
|
64
|
+
for (const index of getIndexMetadata(clazz)) {
|
|
65
|
+
const spec = this.toDesiredIndex(index);
|
|
66
|
+
desired.set(JSON.stringify(spec.key), spec);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Retrieve the collection's current indexes
|
|
70
|
+
const collection = this.db.collection(name);
|
|
71
|
+
let currentIndexes = [];
|
|
72
|
+
try {
|
|
73
|
+
currentIndexes = await collection.listIndexes().toArray();
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
if (err.codeName !== "NamespaceNotFound") {
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Reconcile the desired indexes against the current state
|
|
81
|
+
for (const spec of desired.values()) {
|
|
82
|
+
const current = currentIndexes.find((idx) => JSON.stringify(idx.key) === JSON.stringify(spec.key));
|
|
83
|
+
if (current) {
|
|
84
|
+
if (this.optionsMatch(spec, current)) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
// The index definition has changed. Drop and re-create it.
|
|
88
|
+
this.logger?.info(`Re-creating index ${current.name} on collection ${name} due to changed definition.`);
|
|
89
|
+
await collection.dropIndex(current.name);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
// If an unrelated index already uses the desired name it must be dropped first to avoid a conflict
|
|
93
|
+
const conflict = currentIndexes.find((idx) => idx.name === spec.name);
|
|
94
|
+
if (conflict) {
|
|
95
|
+
this.logger?.info(`Dropping index ${spec.name} on collection ${name} due to changed key.`);
|
|
96
|
+
await collection.dropIndex(spec.name);
|
|
97
|
+
}
|
|
98
|
+
this.logger?.info(`Creating index ${spec.name} on collection ${name}.`);
|
|
99
|
+
}
|
|
100
|
+
await collection.createIndex(spec.key, {
|
|
101
|
+
name: spec.name,
|
|
102
|
+
unique: spec.unique,
|
|
103
|
+
sparse: spec.sparse,
|
|
104
|
+
...(spec.background !== undefined ? { background: spec.background } : {}),
|
|
105
|
+
...(spec.expireAfterSeconds !== undefined ? { expireAfterSeconds: spec.expireAfterSeconds } : {}),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Converts declared index metadata into a desired index specification.
|
|
111
|
+
*/
|
|
112
|
+
toDesiredIndex(index) {
|
|
113
|
+
const key = {};
|
|
114
|
+
for (const column of index.columns) {
|
|
115
|
+
key[column] = 1;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
name: index.name ?? index.columns.map((c) => `${c}_1`).join("_"),
|
|
119
|
+
key,
|
|
120
|
+
unique: index.options.unique ?? false,
|
|
121
|
+
sparse: index.options.sparse ?? false,
|
|
122
|
+
background: index.options.background,
|
|
123
|
+
expireAfterSeconds: index.options.expireAfterSeconds,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Determines whether an existing index satisfies the desired index specification. The `background` option is
|
|
128
|
+
* a build-time hint and is intentionally excluded from the comparison.
|
|
129
|
+
*/
|
|
130
|
+
optionsMatch(spec, current) {
|
|
131
|
+
return (spec.unique === (current.unique ?? false) &&
|
|
132
|
+
spec.sparse === (current.sparse ?? false) &&
|
|
133
|
+
spec.expireAfterSeconds === (current.expireAfterSeconds ?? undefined));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=MongoSchemaSync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongoSchemaSync.js","sourceRoot":"","sources":["../../../src/database/MongoSchemaSync.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAa,MAAM,wCAAwC,CAAC;AACrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAczD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,eAAe;IAIxB,YAAY,EAAM,EAAE,MAAY;QAC5B,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,WAAW,CAAC,QAAuB;QAC5C,mGAAmG;QACnG,gGAAgG;QAChG,MAAM,WAAW,GAAuB,IAAI,GAAG,EAAE,CAAC;QAClD,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC3B,MAAM,IAAI,GAAW,qBAAqB,CAAC,KAAK,CAAC,CAAC;YAClD,MAAM,KAAK,GAAU,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YACjD,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAClB,WAAW,CAAC,GAAG,CAAC,IAAI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,KAAK,CAAC,OAAO,CAAC,UAAU;YACpC,kBAAkB,EAAE,KAAK,CAAC,OAAO,CAAC,kBAAkB;SACvD,CAAC;IACN,CAAC;IAED;;;OAGG;IACK,YAAY,CAAC,IAAkB,EAAE,OAAiB;QACtD,OAAO,CACH,IAAI,CAAC,MAAM,KAAK,CAAC,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC;YACzC,IAAI,CAAC,MAAM,KAAK,CAAC,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC;YACzC,IAAI,CAAC,kBAAkB,KAAK,CAAC,OAAO,CAAC,kBAAkB,IAAI,SAAS,CAAC,CACxE,CAAC;IACN,CAAC;CACJ"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz. All rights reserved.
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
import "reflect-metadata";
|
|
5
|
+
/**
|
|
6
|
+
* Converts a string into snake_case.
|
|
7
|
+
*
|
|
8
|
+
* Note: This function is intentionally identical in behavior to TypeORM's `snakeCase` utility so that
|
|
9
|
+
* collection/table names derived from class names remain stable for existing deployments.
|
|
10
|
+
*
|
|
11
|
+
* @param str The string to convert.
|
|
12
|
+
*/
|
|
13
|
+
export function snakeCase(str) {
|
|
14
|
+
return (str
|
|
15
|
+
// ABc -> a_bc
|
|
16
|
+
.replace(/([A-Z])([A-Z])([a-z])/g, "$1_$2$3")
|
|
17
|
+
// aC -> a_c
|
|
18
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
19
|
+
.toLowerCase());
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolves the name of the database collection (or table) that records of the given model class are stored in.
|
|
23
|
+
*
|
|
24
|
+
* The name is resolved using the following rules:
|
|
25
|
+
* 1. The nearest class in the inheritance chain (starting with `clazz` itself) that specifies an explicit
|
|
26
|
+
* entity name via the `@Entity(name)` decorator.
|
|
27
|
+
* 2. Otherwise, the most ancestral class in the inheritance chain that declares its own `@DataStore` binding.
|
|
28
|
+
* This ensures that `@ChildEntity` subclasses are stored in the same collection as their parent entity
|
|
29
|
+
* (single collection inheritance).
|
|
30
|
+
* 3. Otherwise, the snake_case form of the class name.
|
|
31
|
+
*
|
|
32
|
+
* @param clazz The model class to resolve the collection name for.
|
|
33
|
+
*/
|
|
34
|
+
export function resolveCollectionName(clazz) {
|
|
35
|
+
// Rule 1: nearest explicit entity name
|
|
36
|
+
for (let c = clazz; c && c !== Function.prototype; c = Object.getPrototypeOf(c)) {
|
|
37
|
+
const entityName = Reflect.getOwnMetadata("rrst:entityName", c);
|
|
38
|
+
if (entityName) {
|
|
39
|
+
return entityName;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Rule 2: most ancestral class owning a datastore binding
|
|
43
|
+
let owner = undefined;
|
|
44
|
+
for (let c = clazz; c && c !== Function.prototype; c = Object.getPrototypeOf(c)) {
|
|
45
|
+
if (Reflect.getOwnMetadata("rrst:datastore", c)) {
|
|
46
|
+
owner = c;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Rule 3: fall back to the class itself
|
|
50
|
+
return snakeCase((owner ?? clazz).name);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=NamingUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NamingUtils.js","sourceRoot":"","sources":["../../../src/database/NamingUtils.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,wEAAwE;AACxE,+EAA+E;AAC/E,OAAO,kBAAkB,CAAC;AAE1B;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACjC,OAAO,CACH,GAAG;QACC,cAAc;SACb,OAAO,CAAC,wBAAwB,EAAE,SAAS,CAAC;QAC7C,YAAY;SACX,OAAO,CAAC,oBAAoB,EAAE,OAAO,CAAC;SACtC,WAAW,EAAE,CACrB,CA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