@typetron/framework 0.3.16 → 0.3.18
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/Cache/Cache.d.ts +9 -0
- package/Cache/Cache.d.ts.map +1 -0
- package/Cache/Cache.js +7 -0
- package/Cache/Cache.js.map +1 -0
- package/Cache/CacheLoader.d.ts +8 -0
- package/Cache/CacheLoader.d.ts.map +1 -0
- package/Cache/CacheLoader.js +55 -0
- package/Cache/CacheLoader.js.map +1 -0
- package/Cache/Consumers/Cache.d.ts +18 -0
- package/Cache/Consumers/Cache.d.ts.map +1 -0
- package/Cache/Consumers/Cache.js +99 -0
- package/Cache/Consumers/Cache.js.map +1 -0
- package/Cache/Contracts/CacheInterface.d.ts +14 -0
- package/Cache/Contracts/CacheInterface.d.ts.map +1 -0
- package/Cache/Contracts/CacheInterface.js +3 -0
- package/Cache/Contracts/CacheInterface.js.map +1 -0
- package/Cache/Contracts/EngineInterface.d.ts +8 -0
- package/Cache/Contracts/EngineInterface.d.ts.map +1 -0
- package/Cache/Contracts/EngineInterface.js +3 -0
- package/Cache/Contracts/EngineInterface.js.map +1 -0
- package/Cache/Contracts/ServiceInterface.d.ts +8 -0
- package/Cache/Contracts/ServiceInterface.d.ts.map +1 -0
- package/Cache/Contracts/ServiceInterface.js +3 -0
- package/Cache/Contracts/ServiceInterface.js.map +1 -0
- package/Cache/Engines/DatabaseCache.d.ts +11 -0
- package/Cache/Engines/DatabaseCache.d.ts.map +1 -0
- package/Cache/Engines/DatabaseCache.js +40 -0
- package/Cache/Engines/DatabaseCache.js.map +1 -0
- package/Cache/Engines/FileCache.d.ts +22 -0
- package/Cache/Engines/FileCache.d.ts.map +1 -0
- package/Cache/Engines/FileCache.js +107 -0
- package/Cache/Engines/FileCache.js.map +1 -0
- package/Cache/Services/DatabaseCacheService.d.ts +13 -0
- package/Cache/Services/DatabaseCacheService.d.ts.map +1 -0
- package/Cache/Services/DatabaseCacheService.js +74 -0
- package/Cache/Services/DatabaseCacheService.js.map +1 -0
- package/Cache/Services/FileCacheService.d.ts +13 -0
- package/Cache/Services/FileCacheService.d.ts.map +1 -0
- package/Cache/Services/FileCacheService.js +60 -0
- package/Cache/Services/FileCacheService.js.map +1 -0
- package/Cache/Stores/DatabaseStore.d.ts +16 -0
- package/Cache/Stores/DatabaseStore.d.ts.map +1 -0
- package/Cache/Stores/DatabaseStore.js +208 -0
- package/Cache/Stores/DatabaseStore.js.map +1 -0
- package/Cache/Stores/FileStore.d.ts +16 -0
- package/Cache/Stores/FileStore.d.ts.map +1 -0
- package/Cache/Stores/FileStore.js +63 -0
- package/Cache/Stores/FileStore.js.map +1 -0
- package/Cache/Stores/MemoryStore.d.ts +13 -0
- package/Cache/Stores/MemoryStore.d.ts.map +1 -0
- package/Cache/Stores/MemoryStore.js +60 -0
- package/Cache/Stores/MemoryStore.js.map +1 -0
- package/Cache/index.d.ts +6 -0
- package/Cache/index.d.ts.map +1 -0
- package/Cache/index.js +18 -0
- package/Cache/index.js.map +1 -0
- package/Cache/types.d.ts +5 -0
- package/Cache/types.d.ts.map +1 -0
- package/Cache/types.js +3 -0
- package/Cache/types.js.map +1 -0
- package/Container/ClassResolver.d.ts +8 -8
- package/Container/ClassResolver.d.ts.map +1 -1
- package/Container/ClassResolver.js +89 -87
- package/Container/ClassResolver.js.map +1 -1
- package/Container/Container.d.ts +24 -24
- package/Container/Container.d.ts.map +1 -1
- package/Container/Container.js +118 -118
- package/Container/Container.js.map +1 -1
- package/Container/Contracts.d.ts +2 -2
- package/Container/Contracts.js +2 -2
- package/Container/Decorators.d.ts +4 -4
- package/Container/Decorators.js +21 -21
- package/Container/Metadata.d.ts +20 -20
- package/Container/Metadata.js +18 -18
- package/Container/Resolver.d.ts +18 -18
- package/Container/Resolver.js +15 -15
- package/Container/index.d.ts +5 -5
- package/Container/index.js +17 -17
- package/Database/Clauses/Where.js +1 -1
- package/Database/Connection.d.ts +20 -20
- package/Database/Connection.js +42 -42
- package/Database/Decorators.d.ts +36 -36
- package/Database/Decorators.js +116 -116
- package/Database/Drivers/DatabaseDriver.d.ts +28 -28
- package/Database/Drivers/DatabaseDriver.js +6 -6
- package/Database/Drivers/MySQL/Alter/index.d.ts +33 -33
- package/Database/Drivers/MySQL/Alter/index.js +61 -61
- package/Database/Drivers/MySQL/ColumnDefinition.d.ts +6 -6
- package/Database/Drivers/MySQL/ColumnDefinition.js +17 -17
- package/Database/Drivers/MySQL/Create.d.ts +9 -9
- package/Database/Drivers/MySQL/Create.js +37 -37
- package/Database/Drivers/MySQL/MysqlDriver.d.ts +41 -41
- package/Database/Drivers/MySQL/MysqlDriver.js +76 -76
- package/Database/Drivers/MySQL/Schema.d.ts +15 -15
- package/Database/Drivers/MySQL/Schema.js +65 -65
- package/Database/Drivers/MySQL/index.d.ts +1 -1
- package/Database/Drivers/MySQL/index.js +15 -15
- package/Database/Drivers/SQL/Schema.d.ts +6 -6
- package/Database/Drivers/SQL/Schema.js +57 -57
- package/Database/Drivers/SQL/Statements/Alter.d.ts +7 -7
- package/Database/Drivers/SQL/Statements/Alter.js +17 -17
- package/Database/Drivers/SQL/Statements/BaseStatement.d.ts +12 -12
- package/Database/Drivers/SQL/Statements/BaseStatement.js +46 -46
- package/Database/Drivers/SQL/Statements/Delete.d.ts +5 -5
- package/Database/Drivers/SQL/Statements/Delete.js +16 -16
- package/Database/Drivers/SQL/Statements/Insert.d.ts +6 -6
- package/Database/Drivers/SQL/Statements/Insert.js +23 -23
- package/Database/Drivers/SQL/Statements/Select.d.ts +7 -7
- package/Database/Drivers/SQL/Statements/Select.js +44 -44
- package/Database/Drivers/SQL/Statements/Update.d.ts +6 -6
- package/Database/Drivers/SQL/Statements/Update.js +27 -27
- package/Database/Drivers/SQL/index.d.ts +8 -8
- package/Database/Drivers/SQL/index.js +2 -2
- package/Database/Drivers/SQLite/Create.d.ts +9 -9
- package/Database/Drivers/SQLite/Create.js +36 -36
- package/Database/Drivers/SQLite/Schema.d.ts +15 -15
- package/Database/Drivers/SQLite/Schema.js +44 -44
- package/Database/Drivers/SQLite/SqliteDriver.d.ts +42 -42
- package/Database/Drivers/SQLite/SqliteDriver.d.ts.map +1 -1
- package/Database/Drivers/SQLite/SqliteDriver.js +102 -102
- package/Database/Drivers/SQLite/SqliteDriver.js.map +1 -1
- package/Database/Drivers/SQLite/index.d.ts +2 -2
- package/Database/Drivers/SQLite/index.js +26 -26
- package/Database/Drivers/SchemaContract.d.ts +8 -8
- package/Database/Drivers/SchemaContract.js +9 -9
- package/Database/Drivers/Statement.d.ts +9 -9
- package/Database/Drivers/Statement.js +15 -15
- package/Database/Drivers/index.d.ts +4 -4
- package/Database/Drivers/index.js +16 -16
- package/Database/Entity.d.ts +64 -64
- package/Database/Entity.d.ts.map +1 -1
- package/Database/Entity.js +252 -252
- package/Database/Entity.js.map +1 -1
- package/Database/EntityNotFoundError.d.ts +5 -5
- package/Database/EntityNotFoundError.js +10 -10
- package/Database/EntityProxyHandler.d.ts +5 -5
- package/Database/EntityProxyHandler.js +30 -30
- package/Database/EntityQuery.d.ts +28 -28
- package/Database/EntityQuery.js +156 -156
- package/Database/Expression.d.ts +3 -3
- package/Database/Expression.js +6 -6
- package/Database/Fields.d.ts +155 -155
- package/Database/Fields.js +548 -548
- package/Database/Helpers.d.ts +1 -1
- package/Database/Helpers.js +16 -16
- package/Database/List.d.ts +24 -24
- package/Database/List.d.ts.map +1 -1
- package/Database/List.js +49 -49
- package/Database/List.js.map +1 -1
- package/Database/Migrations/Migration.d.ts +7 -7
- package/Database/Migrations/Migration.js +9 -9
- package/Database/Migrations/MigrationHistory.d.ts +8 -8
- package/Database/Migrations/MigrationHistory.js +38 -38
- package/Database/Migrations/Migrator.d.ts +20 -20
- package/Database/Migrations/Migrator.js +118 -118
- package/Database/Migrations/index.d.ts +3 -3
- package/Database/Migrations/index.js +15 -15
- package/Database/ORM/BaseRelationship.d.ts +7 -7
- package/Database/ORM/BaseRelationship.js +10 -10
- package/Database/Query.d.ts +62 -62
- package/Database/Query.d.ts.map +1 -1
- package/Database/Query.js +265 -264
- package/Database/Query.js.map +1 -1
- package/Database/Seeders/Seeder.d.ts +3 -3
- package/Database/Seeders/Seeder.js +6 -6
- package/Database/Seeders/SeederManager.d.ts +8 -8
- package/Database/Seeders/SeederManager.js +24 -24
- package/Database/Seeders/index.d.ts +2 -2
- package/Database/Seeders/index.js +14 -14
- package/Database/StringExpression.d.ts +6 -6
- package/Database/StringExpression.js +14 -14
- package/Database/Types.d.ts +117 -117
- package/Database/Types.d.ts.map +1 -1
- package/Database/Types.js +94 -94
- package/Database/Types.js.map +1 -1
- package/Database/index.d.ts +20 -20
- package/Database/index.js +20 -20
- package/Encryption/index.d.ts +15 -15
- package/Encryption/index.js +53 -53
- package/Forms/Decorators.d.ts +6 -6
- package/Forms/Decorators.js +28 -28
- package/Forms/Form.d.ts +17 -17
- package/Forms/Form.js +69 -69
- package/Forms/FormFields.d.ts +9 -9
- package/Forms/FormFields.js +35 -35
- package/Forms/index.d.ts +3 -3
- package/Forms/index.js +15 -15
- package/Framework/App.d.ts +6 -6
- package/Framework/App.js +9 -9
- package/Framework/Application.d.ts +17 -17
- package/Framework/Application.d.ts.map +1 -1
- package/Framework/Application.js +94 -93
- package/Framework/Application.js.map +1 -1
- package/Framework/Auth/Auth.d.ts +24 -24
- package/Framework/Auth/Auth.js +108 -108
- package/Framework/Auth/Authenticatable.d.ts +6 -6
- package/Framework/Auth/Authenticatable.js +2 -2
- package/Framework/Auth/User.d.ts +10 -10
- package/Framework/Auth/User.js +37 -37
- package/Framework/Auth/index.d.ts +5 -5
- package/Framework/Auth/index.d.ts.map +1 -1
- package/Framework/Auth/index.js +45 -45
- package/Framework/Auth/index.js.map +1 -1
- package/Framework/Config/AppConfig.d.ts +18 -18
- package/Framework/Config/AppConfig.js +11 -11
- package/Framework/Config/AuthConfig.d.ts +8 -8
- package/Framework/Config/AuthConfig.js +7 -7
- package/Framework/Config/BaseConfig.d.ts +6 -6
- package/Framework/Config/BaseConfig.js +12 -12
- package/Framework/Config/CacheConfig.d.ts +16 -0
- package/Framework/Config/CacheConfig.d.ts.map +1 -0
- package/Framework/Config/CacheConfig.js +23 -0
- package/Framework/Config/CacheConfig.js.map +1 -0
- package/Framework/Config/DatabaseConfig.d.ts +10 -10
- package/Framework/Config/DatabaseConfig.js +25 -25
- package/Framework/Config/index.d.ts +5 -4
- package/Framework/Config/index.d.ts.map +1 -1
- package/Framework/Config/index.js +17 -16
- package/Framework/Config/index.js.map +1 -1
- package/Framework/Middleware/AuthMiddleware.d.ts +7 -7
- package/Framework/Middleware/AuthMiddleware.js +63 -63
- package/Framework/Middleware/CorsMiddleware.d.ts +6 -6
- package/Framework/Middleware/CorsMiddleware.js +43 -43
- package/Framework/Middleware/StaticAssetsMiddleware.d.ts +14 -14
- package/Framework/Middleware/StaticAssetsMiddleware.js +104 -104
- package/Framework/Middleware/index.d.ts +2 -2
- package/Framework/Middleware/index.js +14 -14
- package/Framework/Provider.d.ts +5 -5
- package/Framework/Provider.js +20 -20
- package/Framework/Providers/CacheProvider.d.ts +7 -0
- package/Framework/Providers/CacheProvider.d.ts.map +1 -0
- package/Framework/Providers/CacheProvider.js +39 -0
- package/Framework/Providers/CacheProvider.js.map +1 -0
- package/Framework/Providers/DatabaseProvider.d.ts +9 -9
- package/Framework/Providers/DatabaseProvider.js +63 -63
- package/Framework/Providers/WebsocketsProvider.d.ts +12 -12
- package/Framework/Providers/WebsocketsProvider.js +122 -122
- package/Framework/Resolvers/AuthResolver.d.ts +5 -5
- package/Framework/Resolvers/AuthResolver.js +14 -14
- package/Framework/Resolvers/EntityResolver.d.ts +9 -9
- package/Framework/Resolvers/EntityResolver.js +40 -40
- package/Framework/Resolvers/FormResolver.d.ts +9 -9
- package/Framework/Resolvers/FormResolver.js +32 -32
- package/Framework/RootDir.d.ts +2 -2
- package/Framework/RootDir.js +6 -6
- package/Framework/index.d.ts +7 -6
- package/Framework/index.d.ts.map +1 -1
- package/Framework/index.js +19 -18
- package/Framework/index.js.map +1 -1
- package/Models/Model.d.ts +9 -9
- package/Models/Model.js +35 -35
- package/Models/index.d.ts +21 -21
- package/Models/index.d.ts.map +1 -1
- package/Models/index.js +58 -58
- package/Models/index.js.map +1 -1
- package/Router/BaseController.d.ts +4 -4
- package/Router/BaseController.js +20 -20
- package/Router/Decorators.d.ts +21 -21
- package/Router/Decorators.js +170 -170
- package/Router/Guard.d.ts +4 -4
- package/Router/Guard.js +10 -10
- package/Router/Http/Contracts.d.ts +3 -3
- package/Router/Http/Contracts.js +2 -2
- package/Router/Http/ErrorHandler.d.ts +9 -9
- package/Router/Http/ErrorHandler.js +51 -51
- package/Router/Http/ErrorHandlerInterface.d.ts +5 -5
- package/Router/Http/ErrorHandlerInterface.js +6 -6
- package/Router/Http/Errors/HttpError.d.ts +7 -7
- package/Router/Http/Errors/HttpError.js +15 -15
- package/Router/Http/Handler.d.ts +15 -15
- package/Router/Http/Handler.js +131 -131
- package/Router/Http/Request.d.ts +22 -22
- package/Router/Http/Request.js +151 -151
- package/Router/Http/Response.d.ts +17 -17
- package/Router/Http/Response.js +51 -51
- package/Router/Http/index.d.ts +83 -83
- package/Router/Http/index.js +99 -99
- package/Router/Metadata.d.ts +31 -31
- package/Router/Metadata.js +25 -25
- package/Router/Middleware.d.ts +5 -5
- package/Router/Middleware.js +2 -2
- package/Router/Route.d.ts +23 -23
- package/Router/Route.js +110 -110
- package/Router/RouteNotFoundError.d.ts +3 -3
- package/Router/RouteNotFoundError.js +9 -9
- package/Router/Router.d.ts +12 -12
- package/Router/Router.js +32 -32
- package/Router/Websockets/Handler.d.ts +12 -12
- package/Router/Websockets/Handler.js +59 -59
- package/Router/Websockets/WebsocketEvent.d.ts +15 -15
- package/Router/Websockets/WebsocketEvent.js +53 -53
- package/Router/Websockets/index.d.ts +16 -16
- package/Router/Websockets/index.d.ts.map +1 -1
- package/Router/Websockets/index.js +57 -57
- package/Router/Websockets/index.js.map +1 -1
- package/Router/Websockets/types.d.ts +20 -20
- package/Router/Websockets/types.js +8 -8
- package/Router/index.d.ts +6 -6
- package/Router/index.js +18 -18
- package/Storage/File.d.ts +11 -11
- package/Storage/File.js +46 -46
- package/Storage/Image.d.ts +3 -3
- package/Storage/Image.js +7 -7
- package/Storage/InvalidImage.d.ts +3 -3
- package/Storage/InvalidImage.js +7 -7
- package/Storage/Storage.d.ts +13 -12
- package/Storage/Storage.d.ts.map +1 -1
- package/Storage/Storage.js +104 -110
- package/Storage/Storage.js.map +1 -1
- package/Storage/index.d.ts +4 -4
- package/Storage/index.js +16 -16
- package/Storage/utils.d.ts +5 -5
- package/Storage/utils.js +20 -20
- package/Support/Array.d.ts +28 -28
- package/Support/Array.d.ts.map +1 -1
- package/Support/Array.js +108 -108
- package/Support/Array.js.map +1 -1
- package/Support/Math.d.ts +6 -6
- package/Support/Math.js +7 -7
- package/Support/Metadata.d.ts +14 -14
- package/Support/Metadata.js +20 -20
- package/Support/String.d.ts +11 -11
- package/Support/String.js +23 -23
- package/Support/index.d.ts +14 -13
- package/Support/index.d.ts.map +1 -1
- package/Support/index.js +6 -5
- package/Support/index.js.map +1 -1
- package/Support/utils.d.ts +2 -0
- package/Support/utils.d.ts.map +1 -0
- package/Support/utils.js +13 -0
- package/Support/utils.js.map +1 -0
- package/Testing/TestCase.d.ts +32 -32
- package/Testing/TestCase.js +130 -130
- package/Validation/Rule.d.ts +6 -6
- package/Validation/Rule.js +6 -6
- package/Validation/RuleInterface.d.ts +6 -6
- package/Validation/RuleInterface.js +2 -2
- package/Validation/Rules/Email.d.ts +3 -3
- package/Validation/Rules/Email.js +20 -20
- package/Validation/Rules/FileExtension.d.ts +3 -3
- package/Validation/Rules/FileExtension.js +22 -22
- package/Validation/Rules/MaxLength.d.ts +3 -3
- package/Validation/Rules/MaxLength.js +19 -19
- package/Validation/Rules/MinLength.d.ts +3 -3
- package/Validation/Rules/MinLength.js +19 -19
- package/Validation/Rules/Optional.d.ts +3 -3
- package/Validation/Rules/Optional.js +19 -19
- package/Validation/Rules/Required.d.ts +3 -3
- package/Validation/Rules/Required.js +26 -26
- package/Validation/Validator.d.ts +11 -11
- package/Validation/Validator.js +10 -10
- package/Validation/index.d.ts +9 -9
- package/Validation/index.js +21 -21
- package/package.json +8 -5
- package/tests/Cache/DatabaseStoreTest.d.ts +2 -0
- package/tests/Cache/DatabaseStoreTest.d.ts.map +1 -0
- package/tests/Cache/DatabaseStoreTest.js +138 -0
- package/tests/Cache/DatabaseStoreTest.js.map +1 -0
- package/tests/Cache/FileStoreTest.d.ts +2 -0
- package/tests/Cache/FileStoreTest.d.ts.map +1 -0
- package/tests/Cache/FileStoreTest.js +142 -0
- package/tests/Cache/FileStoreTest.js.map +1 -0
- package/tests/Cache/MemoryStoreTest.d.ts +2 -0
- package/tests/Cache/MemoryStoreTest.d.ts.map +1 -0
- package/tests/Cache/MemoryStoreTest.js +141 -0
- package/tests/Cache/MemoryStoreTest.js.map +1 -0
- package/tests/Container/ContainerTest.d.ts +1 -1
- package/tests/Container/ContainerTest.js +395 -395
- package/tests/Database/Drivers/MySQL/AlterTest.d.ts +1 -1
- package/tests/Database/Drivers/MySQL/AlterTest.js +135 -135
- package/tests/Database/Drivers/MySQL/CreateTest.d.ts +1 -1
- package/tests/Database/Drivers/MySQL/CreateTest.js +57 -57
- package/tests/Database/Drivers/MysqlDriverTest.d.ts +1 -1
- package/tests/Database/Drivers/MysqlDriverTest.js +149 -149
- package/tests/Database/Drivers/SqliteDriverTest.d.ts +1 -1
- package/tests/Database/Drivers/SqliteDriverTest.js +135 -135
- package/tests/Database/Entities/Article.d.ts +10 -10
- package/tests/Database/Entities/Article.js +42 -42
- package/tests/Database/Entities/Profile.d.ts +10 -10
- package/tests/Database/Entities/Profile.js +38 -38
- package/tests/Database/Entities/Role.d.ts +8 -8
- package/tests/Database/Entities/Role.js +30 -30
- package/tests/Database/Entities/User.d.ts +15 -15
- package/tests/Database/Entities/User.js +57 -57
- package/tests/Database/EntityRelationshipsTest.d.ts +1 -1
- package/tests/Database/EntityRelationshipsTest.js +552 -552
- package/tests/Database/EntityTest.d.ts +1 -1
- package/tests/Database/EntityTest.js +233 -233
- package/tests/Database/Migrations/RunnerTest.d.ts +1 -1
- package/tests/Database/Migrations/RunnerTest.js +111 -111
- package/tests/Database/Migrations/migrations/1.createUserTable.d.ts +5 -5
- package/tests/Database/Migrations/migrations/1.createUserTable.js +13 -13
- package/tests/Database/Migrations/migrations/2.createArticlesTable.d.ts +5 -5
- package/tests/Database/Migrations/migrations/2.createArticlesTable.js +13 -13
- package/tests/Database/QueryTest.d.ts +1 -1
- package/tests/Database/QueryTest.js +622 -622
- package/tests/Database/SchemaTest.d.ts +1 -1
- package/tests/Database/SchemaTest.js +45 -45
- package/tests/Database/Seeders/RunnerTest.d.ts +1 -1
- package/tests/Database/Seeders/RunnerTest.js +43 -43
- package/tests/Database/Seeders/seeders/RandomSeeder.d.ts +4 -4
- package/tests/Database/Seeders/seeders/RandomSeeder.js +13 -13
- package/tests/Database/utils.d.ts +5 -5
- package/tests/Database/utils.js +18 -18
- package/tests/Forms/FormsTest.d.ts +1 -1
- package/tests/Forms/FormsTest.js +79 -79
- package/tests/Framework/Middleware/StaticAssetsMiddlewareTest.d.ts +1 -1
- package/tests/Framework/Middleware/StaticAssetsMiddlewareTest.js +165 -165
- package/tests/Framework/Resolvers/EntityResolverTest.d.ts +1 -1
- package/tests/Framework/Resolvers/EntityResolverTest.js +59 -59
- package/tests/Model/ModelTest.d.ts +1 -1
- package/tests/Model/ModelTest.js +233 -233
- package/tests/Router/HttpHandlerTest.d.ts +1 -1
- package/tests/Router/HttpHandlerTest.js +36 -36
- package/tests/Router/WebSocketsHandlerTest.d.ts +1 -1
- package/tests/Router/WebSocketsHandlerTest.js +35 -35
- package/tests/Support/ArrayTest.d.ts +1 -1
- package/tests/Support/ArrayTest.js +258 -258
- package/tests/Support/StringTest.d.ts +1 -1
- package/tests/Support/StringTest.js +38 -38
- package/tests/Validation/RulesTest.d.ts +1 -1
- package/tests/Validation/RulesTest.js +67 -67
- package/Database/Drivers/SQL/Statements/Create.d.ts +0 -7
- package/Database/Drivers/SQL/Statements/Create.d.ts.map +0 -1
- package/Database/Drivers/SQL/Statements/Create.js +0 -22
- package/Database/Drivers/SQL/Statements/Create.js.map +0 -1
- package/LICENSE.md +0 -21
- package/Readme.md +0 -123
package/Database/Fields.js
CHANGED
|
@@ -1,549 +1,549 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BelongsToMany = exports.HasMany = exports.HasOne = exports.BelongsTo = exports.BelongsToManyField = exports.BelongsToField = exports.HasManyField = exports.HasOneField = exports.InverseRelationship = exports.RelationshipField = exports.PrimaryField = exports.InverseField = exports.JSONField = exports.ColumnField = void 0;
|
|
4
|
-
const Entity_1 = require("./Entity");
|
|
5
|
-
const index_1 = require("./index");
|
|
6
|
-
const List_1 = require("./List");
|
|
7
|
-
const BaseRelationship_1 = require("./ORM/BaseRelationship");
|
|
8
|
-
const Helpers_1 = require("./Helpers");
|
|
9
|
-
class ColumnField {
|
|
10
|
-
constructor(entity, property, type, column) {
|
|
11
|
-
this.entity = entity;
|
|
12
|
-
this.property = property;
|
|
13
|
-
this.type = type;
|
|
14
|
-
this.column = column;
|
|
15
|
-
}
|
|
16
|
-
value(entity, key) {
|
|
17
|
-
return entity[key];
|
|
18
|
-
}
|
|
19
|
-
set(target, value) {
|
|
20
|
-
target[this.property] = value;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.ColumnField = ColumnField;
|
|
24
|
-
class JSONField extends ColumnField {
|
|
25
|
-
value(entity, key) {
|
|
26
|
-
return JSON.stringify(entity[key]);
|
|
27
|
-
}
|
|
28
|
-
set(target, value) {
|
|
29
|
-
target[this.property] = typeof value === 'string' ? JSON.parse(value) : undefined;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.JSONField = JSONField;
|
|
33
|
-
class InverseField {
|
|
34
|
-
constructor(entity, property, type) {
|
|
35
|
-
this.entity = entity;
|
|
36
|
-
this.property = property;
|
|
37
|
-
this.type = type;
|
|
38
|
-
}
|
|
39
|
-
value(entity, key) {
|
|
40
|
-
return entity[key];
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
exports.InverseField = InverseField;
|
|
44
|
-
class PrimaryField extends ColumnField {
|
|
45
|
-
}
|
|
46
|
-
exports.PrimaryField = PrimaryField;
|
|
47
|
-
class RelationshipField extends ColumnField {
|
|
48
|
-
constructor(entity, property, type, inverseBy, column) {
|
|
49
|
-
super(entity, property, type, column);
|
|
50
|
-
this.type = type;
|
|
51
|
-
this.inverseBy = inverseBy;
|
|
52
|
-
}
|
|
53
|
-
get related() {
|
|
54
|
-
return this.type();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.RelationshipField = RelationshipField;
|
|
58
|
-
class InverseRelationship extends InverseField {
|
|
59
|
-
constructor(entity, property, type, inverseBy) {
|
|
60
|
-
super(entity, property, type);
|
|
61
|
-
this.type = type;
|
|
62
|
-
this.inverseBy = inverseBy;
|
|
63
|
-
}
|
|
64
|
-
get related() {
|
|
65
|
-
return this.type();
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
exports.InverseRelationship = InverseRelationship;
|
|
69
|
-
class HasOneField extends InverseRelationship {
|
|
70
|
-
constructor(entity, property, type, inverseBy // TODO make it key of R
|
|
71
|
-
) {
|
|
72
|
-
super(entity, property, type, inverseBy);
|
|
73
|
-
this.relationClass = HasOne;
|
|
74
|
-
}
|
|
75
|
-
set(target, value) {
|
|
76
|
-
target[this.property].set(value);
|
|
77
|
-
}
|
|
78
|
-
async getRelatedValue(relatedEntities, eagerLoad, customQuery) {
|
|
79
|
-
const parentIds = relatedEntities.pluck(this.entity.getPrimaryKey());
|
|
80
|
-
const inverseByField = this.related.metadata.relationships[this.inverseBy];
|
|
81
|
-
const query = this.related.with(...eagerLoad).whereIn(inverseByField.column, parentIds);
|
|
82
|
-
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
83
|
-
return await query.get();
|
|
84
|
-
}
|
|
85
|
-
async getRelatedCount(relatedEntities, customQuery) {
|
|
86
|
-
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
87
|
-
const query = this.related.newQuery()
|
|
88
|
-
.whereIn(relatedField.column, relatedEntities.pluck(this.entity.getPrimaryKey()))
|
|
89
|
-
.select(relatedField.column)
|
|
90
|
-
.groupBy(relatedField.column);
|
|
91
|
-
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
92
|
-
return await query
|
|
93
|
-
.selectCount()
|
|
94
|
-
.get();
|
|
95
|
-
}
|
|
96
|
-
match(entities, relatedEntities) {
|
|
97
|
-
entities.forEach(entity => {
|
|
98
|
-
const inverseField = this.related.metadata.relationships[this.inverseBy];
|
|
99
|
-
const relatedEntity = relatedEntities.find(related => related.original[inverseField.column] === entity[this.entity.getPrimaryKey()]);
|
|
100
|
-
// TODO fix these weird types. Get rid of `unknown`
|
|
101
|
-
entity[this.property].set(relatedEntity);
|
|
102
|
-
});
|
|
103
|
-
return entities;
|
|
104
|
-
}
|
|
105
|
-
matchCounts(entities, counts) {
|
|
106
|
-
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
107
|
-
entities.forEach(entity => {
|
|
108
|
-
var _a;
|
|
109
|
-
const count = counts.find(item => item.original[relatedField.column] === entity[this.entity.getPrimaryKey()]);
|
|
110
|
-
entity[this.property + 'Count'] = ((_a = count === null || count === void 0 ? void 0 : count.original.aggregate) !== null && _a !== void 0 ? _a : 0);
|
|
111
|
-
});
|
|
112
|
-
return entities;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
exports.HasOneField = HasOneField;
|
|
116
|
-
class HasManyField extends InverseRelationship {
|
|
117
|
-
constructor(entity, property, type, inverseBy) {
|
|
118
|
-
super(entity, property, type, inverseBy);
|
|
119
|
-
this.inverseBy = inverseBy;
|
|
120
|
-
this.relationClass = HasMany;
|
|
121
|
-
}
|
|
122
|
-
set(target, value) {
|
|
123
|
-
}
|
|
124
|
-
async getRelatedValue(relatedEntities, eagerLoad, customQuery) {
|
|
125
|
-
const parentIds = relatedEntities.pluck(this.entity.getPrimaryKey());
|
|
126
|
-
const inverseByField = this.related.metadata.relationships[this.inverseBy];
|
|
127
|
-
const query = this.related.with(...eagerLoad).whereIn(inverseByField.column, parentIds);
|
|
128
|
-
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
129
|
-
return await query.get();
|
|
130
|
-
}
|
|
131
|
-
async getRelatedCount(relatedEntities, customQuery) {
|
|
132
|
-
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
133
|
-
const query = this.related.newQuery()
|
|
134
|
-
.whereIn(relatedField.column, relatedEntities.pluck(this.entity.getPrimaryKey()))
|
|
135
|
-
.select(relatedField.column)
|
|
136
|
-
.groupBy(relatedField.column);
|
|
137
|
-
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
138
|
-
return await query
|
|
139
|
-
.selectCount()
|
|
140
|
-
.get();
|
|
141
|
-
}
|
|
142
|
-
match(entities, relatedEntities) {
|
|
143
|
-
entities.forEach(entity => {
|
|
144
|
-
const inverseField = this.related.metadata.relationships[this.inverseBy];
|
|
145
|
-
const relatedEntity = relatedEntities.filter(related => related.original[inverseField.column] === entity[this.entity.getPrimaryKey()]);
|
|
146
|
-
// TODO fix these weird types. Get rid of `unknown`
|
|
147
|
-
entity[this.property].items = relatedEntity;
|
|
148
|
-
});
|
|
149
|
-
return entities;
|
|
150
|
-
}
|
|
151
|
-
matchCounts(entities, counts) {
|
|
152
|
-
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
153
|
-
entities.forEach(entity => {
|
|
154
|
-
var _a;
|
|
155
|
-
const count = counts.find(item => item.original[relatedField.column] === entity[this.entity.getPrimaryKey()]);
|
|
156
|
-
entity[this.property + 'Count'] = ((_a = count === null || count === void 0 ? void 0 : count.original.aggregate) !== null && _a !== void 0 ? _a : 0);
|
|
157
|
-
});
|
|
158
|
-
return entities;
|
|
159
|
-
}
|
|
160
|
-
async save(items, parent) {
|
|
161
|
-
const entities = [];
|
|
162
|
-
for await (const item of items) {
|
|
163
|
-
const entity = item instanceof Entity_1.Entity ? item : this.type().new(item);
|
|
164
|
-
entity.fill({
|
|
165
|
-
[this.inverseBy]: parent
|
|
166
|
-
});
|
|
167
|
-
await entity.save();
|
|
168
|
-
entities.push(entity);
|
|
169
|
-
}
|
|
170
|
-
return entities;
|
|
171
|
-
}
|
|
172
|
-
getQuery(parent) {
|
|
173
|
-
const inverseField = this.related.metadata.relationships[this.inverseBy];
|
|
174
|
-
return this.type().newQuery().where(inverseField.column, parent[parent.getPrimaryKey()]);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.HasManyField = HasManyField;
|
|
178
|
-
class BelongsToField extends RelationshipField {
|
|
179
|
-
constructor(entity, property, type, inverseBy, column) {
|
|
180
|
-
super(entity, property, type, inverseBy, column || property + entity.getPrimaryKey().capitalize());
|
|
181
|
-
this.relationClass = BelongsTo;
|
|
182
|
-
}
|
|
183
|
-
set(target, value) {
|
|
184
|
-
target[this.property].set(value);
|
|
185
|
-
}
|
|
186
|
-
update(target) {
|
|
187
|
-
target[this.property].update();
|
|
188
|
-
}
|
|
189
|
-
value(entity, key) {
|
|
190
|
-
const field = entity[key];
|
|
191
|
-
const instance = field.get();
|
|
192
|
-
if (instance) {
|
|
193
|
-
return instance[this.related.getPrimaryKey()];
|
|
194
|
-
}
|
|
195
|
-
// return new this.related({
|
|
196
|
-
// [this.related.getPrimaryKey()]: entity[key as K]
|
|
197
|
-
// }) as unknown as T[K]; // TODO get rid of `unknown`
|
|
198
|
-
// if (key === this.column) {
|
|
199
|
-
// return new this.related({
|
|
200
|
-
// [this.related.getPrimaryKey()]: entity[key as K]
|
|
201
|
-
// }) as unknown as T[K]; // TODO get rid of `unknown`
|
|
202
|
-
// }
|
|
203
|
-
// return entity[this.property as keyof T] as unknown as T[K];
|
|
204
|
-
}
|
|
205
|
-
async getRelatedValue(parents, eagerLoad, customQuery) {
|
|
206
|
-
const relationIds = parents
|
|
207
|
-
.map(parent => {
|
|
208
|
-
const relation = parent[this.property].get();
|
|
209
|
-
return relation === null || relation === void 0 ? void 0 : relation[relation.getPrimaryKey()];
|
|
210
|
-
})
|
|
211
|
-
.filter(Boolean);
|
|
212
|
-
if (relationIds.length) {
|
|
213
|
-
const query = this.related.with(...eagerLoad).whereIn(this.related.getPrimaryKey(), relationIds);
|
|
214
|
-
if (customQuery) {
|
|
215
|
-
customQuery(query);
|
|
216
|
-
}
|
|
217
|
-
return await query.get();
|
|
218
|
-
}
|
|
219
|
-
return [];
|
|
220
|
-
}
|
|
221
|
-
async getRelatedCount(relatedEntities, customQuery) {
|
|
222
|
-
return [];
|
|
223
|
-
}
|
|
224
|
-
match(entities, relatedEntities) {
|
|
225
|
-
return entities.map(entity => {
|
|
226
|
-
// TODO fix these weird types. Get rid of `unknown`
|
|
227
|
-
const related = entity[this.property].get();
|
|
228
|
-
const value = related === null || related === void 0 ? void 0 : related[related.getPrimaryKey()];
|
|
229
|
-
if (value) {
|
|
230
|
-
const instance = relatedEntities.findWhere(this.related.getPrimaryKey(), value);
|
|
231
|
-
if (instance) {
|
|
232
|
-
this.set(entity, instance);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return entity;
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
matchCounts(entities, counts) {
|
|
239
|
-
return entities;
|
|
240
|
-
}
|
|
241
|
-
getQuery(parent) {
|
|
242
|
-
return this.entity.newQuery().where(this.column, parent[parent.getPrimaryKey()]);
|
|
243
|
-
}
|
|
244
|
-
async save(items, parent) {
|
|
245
|
-
const entities = [];
|
|
246
|
-
for await (const item of items) {
|
|
247
|
-
const entity = item instanceof Entity_1.Entity ? item : this.entity.new(item);
|
|
248
|
-
entity.fill({
|
|
249
|
-
[this.property]: parent
|
|
250
|
-
});
|
|
251
|
-
await entity.save();
|
|
252
|
-
entities.push(entity);
|
|
253
|
-
}
|
|
254
|
-
return entities;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
exports.BelongsToField = BelongsToField;
|
|
258
|
-
class BelongsToManyField extends InverseRelationship {
|
|
259
|
-
constructor(entity, property, type, inverseBy, table, parentColumn, relatedColumn) {
|
|
260
|
-
super(entity, property, type, inverseBy);
|
|
261
|
-
this.table = table;
|
|
262
|
-
this.parentColumn = parentColumn;
|
|
263
|
-
this.relatedColumn = relatedColumn;
|
|
264
|
-
this.relationClass = BelongsToMany;
|
|
265
|
-
}
|
|
266
|
-
set(target, value) {
|
|
267
|
-
}
|
|
268
|
-
async getRelatedValue(parents, eagerLoad, customQuery) {
|
|
269
|
-
const parentIds = parents.pluck(this.entity.getPrimaryKey());
|
|
270
|
-
const relatedForeignKey = `${Helpers_1.wrap(this.getPivotTable())}.${this.getRelatedForeignKey()}`;
|
|
271
|
-
const parentForeignKey = `${Helpers_1.wrap(this.getPivotTable())}.${this.getParentForeignKey()}`;
|
|
272
|
-
const relatedKey = `${Helpers_1.wrap(this.related.getTable())}.${this.related.getPrimaryKey()}`;
|
|
273
|
-
const query = this.related
|
|
274
|
-
.with(...eagerLoad)
|
|
275
|
-
.addSelect(new index_1.StringExpression(`${Helpers_1.wrap(this.related.getTable())}.*`), new index_1.StringExpression(relatedForeignKey), new index_1.StringExpression(parentForeignKey))
|
|
276
|
-
.join(this.getPivotTable(), relatedForeignKey, '=', relatedKey)
|
|
277
|
-
.whereIn(parentForeignKey, parentIds.unique());
|
|
278
|
-
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
279
|
-
const results = await query.get();
|
|
280
|
-
return results.map(entity => {
|
|
281
|
-
entity.original.pivot = {
|
|
282
|
-
[this.getParentForeignKey()]: entity.original[this.getParentForeignKey()],
|
|
283
|
-
[this.getRelatedForeignKey()]: entity.original[this.getRelatedForeignKey()],
|
|
284
|
-
};
|
|
285
|
-
delete entity.original[this.getParentForeignKey()];
|
|
286
|
-
delete entity.original[this.getRelatedForeignKey()];
|
|
287
|
-
return entity;
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
getParentForeignKey() {
|
|
291
|
-
return this.parentColumn || `${this.entity.name.toLocaleLowerCase()}${this.entity.getPrimaryKey().capitalize()}`;
|
|
292
|
-
}
|
|
293
|
-
getRelatedForeignKey() {
|
|
294
|
-
return this.relatedColumn || `${this.related.name.toLocaleLowerCase()}${this.related.getPrimaryKey().capitalize()}`;
|
|
295
|
-
}
|
|
296
|
-
async getRelatedCount(relatedEntities, customQuery) {
|
|
297
|
-
const query = index_1.Query.table(this.getPivotTable())
|
|
298
|
-
.select(this.getParentForeignKey())
|
|
299
|
-
.groupBy(this.getParentForeignKey())
|
|
300
|
-
.whereIn(this.getParentForeignKey(), relatedEntities.pluck(this.related.getPrimaryKey()));
|
|
301
|
-
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
302
|
-
return await query.selectCount().get();
|
|
303
|
-
}
|
|
304
|
-
match(entities, relatedEntities) {
|
|
305
|
-
return entities.map(entity => {
|
|
306
|
-
const entityPrimaryKey = entity[this.entity.getPrimaryKey()];
|
|
307
|
-
entity[this.property].items = relatedEntities
|
|
308
|
-
.filter(related => related.original.pivot[this.getParentForeignKey()] === entityPrimaryKey);
|
|
309
|
-
return entity;
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
matchCounts(entities, counts) {
|
|
313
|
-
entities.forEach(entity => {
|
|
314
|
-
const foreignKey = this.getParentForeignKey();
|
|
315
|
-
const count = counts.find(item => { var _a; return (((_a = item === null || item === void 0 ? void 0 : item.original) === null || _a === void 0 ? void 0 : _a[foreignKey]) || item[foreignKey]) === entity[this.entity.getPrimaryKey()]; });
|
|
316
|
-
// @ts-ignore
|
|
317
|
-
const aggregateValue = (count === null || count === void 0 ? void 0 : count.aggregate) || (count === null || count === void 0 ? void 0 : count.original.aggregate);
|
|
318
|
-
entity[this.property + 'Count'] = (aggregateValue !== null && aggregateValue !== void 0 ? aggregateValue : 0);
|
|
319
|
-
});
|
|
320
|
-
return entities;
|
|
321
|
-
}
|
|
322
|
-
getPivotTable() {
|
|
323
|
-
return this.table || [this.entity.getTable(), this.related.getTable()].sort().join('_');
|
|
324
|
-
}
|
|
325
|
-
value(entity, key) {
|
|
326
|
-
const value = entity[key];
|
|
327
|
-
if (value instanceof Array) {
|
|
328
|
-
return value.map(related => {
|
|
329
|
-
if (related instanceof Entity_1.Entity) {
|
|
330
|
-
return related;
|
|
331
|
-
}
|
|
332
|
-
const instance = new this.related();
|
|
333
|
-
instance[instance.getPrimaryKey()] = related;
|
|
334
|
-
return instance;
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
return value;
|
|
338
|
-
}
|
|
339
|
-
async add(items, parent) {
|
|
340
|
-
const entities = [];
|
|
341
|
-
// tslint:disable-next-line:no-any
|
|
342
|
-
const dataToInsert = [];
|
|
343
|
-
for await (const item of items) {
|
|
344
|
-
dataToInsert.push({
|
|
345
|
-
[this.getRelatedForeignKey()]: item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item,
|
|
346
|
-
[this.getParentForeignKey()]: parent.getPrimaryKeyValue(),
|
|
347
|
-
});
|
|
348
|
-
entities.push(this.related.new({ [this.related.getPrimaryKey()]: item }));
|
|
349
|
-
}
|
|
350
|
-
if (dataToInsert.length) {
|
|
351
|
-
await index_1.Query.table(this.getPivotTable()).insert(dataToInsert);
|
|
352
|
-
}
|
|
353
|
-
return entities;
|
|
354
|
-
}
|
|
355
|
-
getQuery(parent) {
|
|
356
|
-
return this.related.newQuery().whereIn(this.related.getPrimaryKey(), query => {
|
|
357
|
-
query.select(this.getRelatedForeignKey())
|
|
358
|
-
.table(this.getPivotTable())
|
|
359
|
-
.where(this.getParentForeignKey(), parent[parent.getPrimaryKey()]);
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
exports.BelongsToManyField = BelongsToManyField;
|
|
364
|
-
class BelongsTo extends BaseRelationship_1.BaseRelationship {
|
|
365
|
-
constructor(relationship, parent) {
|
|
366
|
-
super(relationship, parent);
|
|
367
|
-
this.update();
|
|
368
|
-
}
|
|
369
|
-
update() {
|
|
370
|
-
const parentId = this.parent.original[this.relationship.column];
|
|
371
|
-
if (parentId && !this.instance) {
|
|
372
|
-
this.setById(parentId);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
376
|
-
metadata.relationships[property] = new BelongsToField(entityClass, property, type, inverseBy, options === null || options === void 0 ? void 0 : options.column);
|
|
377
|
-
return metadata;
|
|
378
|
-
}
|
|
379
|
-
get() {
|
|
380
|
-
return this.instance;
|
|
381
|
-
}
|
|
382
|
-
set(instance) {
|
|
383
|
-
if (instance instanceof Entity_1.Entity) {
|
|
384
|
-
this.instance = instance;
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
this.setById(instance);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
setById(id) {
|
|
391
|
-
this.set(this.relationship.related.new({ [this.relationship.related.getPrimaryKey()]: id }));
|
|
392
|
-
}
|
|
393
|
-
async save(data = {}) {
|
|
394
|
-
if (!this.instance) {
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
this.instance.fill({
|
|
398
|
-
...data,
|
|
399
|
-
});
|
|
400
|
-
await this.instance.save();
|
|
401
|
-
return this.instance;
|
|
402
|
-
}
|
|
403
|
-
toJSON() {
|
|
404
|
-
var _a;
|
|
405
|
-
return (_a = this.instance) === null || _a === void 0 ? void 0 : _a.toJSON();
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
exports.BelongsTo = BelongsTo;
|
|
409
|
-
class HasOne extends BaseRelationship_1.BaseRelationship {
|
|
410
|
-
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
411
|
-
metadata.inverseRelationships[property] = new HasOneField(entityClass, property, type, inverseBy);
|
|
412
|
-
return metadata;
|
|
413
|
-
}
|
|
414
|
-
get() {
|
|
415
|
-
return this.instance;
|
|
416
|
-
}
|
|
417
|
-
set(instance) {
|
|
418
|
-
if (!instance) {
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
if (instance instanceof Entity_1.Entity) {
|
|
422
|
-
this.instance = instance;
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
this.setById(instance);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
setById(id) {
|
|
429
|
-
this.set(this.relationship.related.new({ [this.relationship.related.getPrimaryKey()]: id }));
|
|
430
|
-
}
|
|
431
|
-
async save(instance = this.instance) {
|
|
432
|
-
this.set(instance);
|
|
433
|
-
if (instance) {
|
|
434
|
-
instance[this.relationship.inverseBy].set(this.parent);
|
|
435
|
-
await instance.save();
|
|
436
|
-
}
|
|
437
|
-
return this.instance;
|
|
438
|
-
}
|
|
439
|
-
toJSON() {
|
|
440
|
-
var _a;
|
|
441
|
-
return (_a = this.instance) === null || _a === void 0 ? void 0 : _a.toJSON();
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
exports.HasOne = HasOne;
|
|
445
|
-
class HasMany extends List_1.List {
|
|
446
|
-
constructor(relationship, parent) {
|
|
447
|
-
super(relationship, parent);
|
|
448
|
-
this.relationship = relationship;
|
|
449
|
-
}
|
|
450
|
-
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
451
|
-
metadata.inverseRelationships[property] = new HasManyField(entityClass, property, type, inverseBy);
|
|
452
|
-
return metadata;
|
|
453
|
-
}
|
|
454
|
-
async save(item) {
|
|
455
|
-
const instances = await this.saveMany(item);
|
|
456
|
-
return instances.first();
|
|
457
|
-
}
|
|
458
|
-
async saveMany(...items) {
|
|
459
|
-
if (!this.parent.exists) {
|
|
460
|
-
await this.parent.save();
|
|
461
|
-
}
|
|
462
|
-
const instances = await this.relationship.save(items, this.parent);
|
|
463
|
-
this.items.push(...instances);
|
|
464
|
-
return instances;
|
|
465
|
-
}
|
|
466
|
-
async clear() {
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
exports.HasMany = HasMany;
|
|
470
|
-
class BelongsToMany extends List_1.List {
|
|
471
|
-
constructor(relationship, parent) {
|
|
472
|
-
super(relationship, parent);
|
|
473
|
-
this.relationship = relationship;
|
|
474
|
-
}
|
|
475
|
-
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
476
|
-
// options.column = options.column || (property as string) + (entityClass.getPrimaryKey() as string).capitalize();
|
|
477
|
-
const inverseField = metadata.inverseRelationships[inverseBy];
|
|
478
|
-
metadata.inverseRelationships[property] = new BelongsToManyField(entityClass, property, type, inverseBy, (options === null || options === void 0 ? void 0 : options.table) || (inverseField === null || inverseField === void 0 ? void 0 : inverseField.table), (options === null || options === void 0 ? void 0 : options.foreignColumn) || (inverseField === null || inverseField === void 0 ? void 0 : inverseField.parentColumn), (options === null || options === void 0 ? void 0 : options.column) || (inverseField === null || inverseField === void 0 ? void 0 : inverseField.relatedColumn));
|
|
479
|
-
return metadata;
|
|
480
|
-
}
|
|
481
|
-
async has(item) {
|
|
482
|
-
if (item instanceof Entity_1.Entity) {
|
|
483
|
-
item = item.getPrimaryKeyValue();
|
|
484
|
-
}
|
|
485
|
-
const relatedForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getRelatedForeignKey()}`;
|
|
486
|
-
const parentForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getParentForeignKey()}`;
|
|
487
|
-
return Boolean(await index_1.Query.table(this.relationship.getPivotTable())
|
|
488
|
-
.where(parentForeignKey, this.parent[this.parent.getPrimaryKey()])
|
|
489
|
-
.andWhere(relatedForeignKey, item)
|
|
490
|
-
.first());
|
|
491
|
-
}
|
|
492
|
-
async add(...items) {
|
|
493
|
-
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
494
|
-
if (!this.parent.exists) {
|
|
495
|
-
await this.parent.save();
|
|
496
|
-
}
|
|
497
|
-
for await (const id of ids) {
|
|
498
|
-
if (!(await this.has(id))) {
|
|
499
|
-
this.items = this.items.concat(await this.relationship.add(items, this.parent));
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
return this.items;
|
|
503
|
-
}
|
|
504
|
-
async remove(...items) {
|
|
505
|
-
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
506
|
-
const relatedForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getRelatedForeignKey()}`;
|
|
507
|
-
const parentForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getParentForeignKey()}`;
|
|
508
|
-
await index_1.Query.table(this.relationship.getPivotTable())
|
|
509
|
-
.where(parentForeignKey, this.parent[this.parent.getPrimaryKey()])
|
|
510
|
-
.whereIn(relatedForeignKey, ids)
|
|
511
|
-
.delete();
|
|
512
|
-
this.items = this.items.filter(item => !ids.includes(item.getPrimaryKeyValue()));
|
|
513
|
-
}
|
|
514
|
-
async toggle(...items) {
|
|
515
|
-
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
516
|
-
const attachedEntities = await this.relationship.getQuery(this.parent).get();
|
|
517
|
-
const entitiesToDetach = ids.filter(item => attachedEntities.some(entity => entity.getPrimaryKeyValue() === item));
|
|
518
|
-
const entitiesToAttach = ids.filter(item => attachedEntities.some(entity => entity.getPrimaryKeyValue() !== item));
|
|
519
|
-
await this.add(...entitiesToAttach);
|
|
520
|
-
await this.remove(...entitiesToDetach);
|
|
521
|
-
}
|
|
522
|
-
async clear() {
|
|
523
|
-
const relatedForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getRelatedForeignKey()}`;
|
|
524
|
-
const parentForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getParentForeignKey()}`;
|
|
525
|
-
await index_1.Query.table(this.relationship.getPivotTable())
|
|
526
|
-
.where(parentForeignKey, this.parent[this.parent.getPrimaryKey()])
|
|
527
|
-
.whereIn(relatedForeignKey, this.items.pluck(this.relationship.related.getPrimaryKey()))
|
|
528
|
-
.delete();
|
|
529
|
-
this.items = [];
|
|
530
|
-
}
|
|
531
|
-
async sync(...items) {
|
|
532
|
-
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
533
|
-
const existingItems = await this.get();
|
|
534
|
-
const primaryKey = this.relationship.type().getPrimaryKey();
|
|
535
|
-
const itemsToRemove = existingItems.filter(item => !ids.includes(item[primaryKey])).pluck(primaryKey);
|
|
536
|
-
const restOfItemsToAdd = ids.filter(item => !existingItems.findWhere(primaryKey, item));
|
|
537
|
-
await this.remove(...itemsToRemove);
|
|
538
|
-
await this.add(...restOfItemsToAdd);
|
|
539
|
-
}
|
|
540
|
-
async syncWithoutDetaching(...items) {
|
|
541
|
-
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
542
|
-
const existingItems = await this.get();
|
|
543
|
-
const primaryKey = this.relationship.type().getPrimaryKey();
|
|
544
|
-
const restOfItemsToAdd = ids.filter(item => !existingItems.findWhere(primaryKey, item));
|
|
545
|
-
await this.add(...restOfItemsToAdd);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
exports.BelongsToMany = BelongsToMany;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BelongsToMany = exports.HasMany = exports.HasOne = exports.BelongsTo = exports.BelongsToManyField = exports.BelongsToField = exports.HasManyField = exports.HasOneField = exports.InverseRelationship = exports.RelationshipField = exports.PrimaryField = exports.InverseField = exports.JSONField = exports.ColumnField = void 0;
|
|
4
|
+
const Entity_1 = require("./Entity");
|
|
5
|
+
const index_1 = require("./index");
|
|
6
|
+
const List_1 = require("./List");
|
|
7
|
+
const BaseRelationship_1 = require("./ORM/BaseRelationship");
|
|
8
|
+
const Helpers_1 = require("./Helpers");
|
|
9
|
+
class ColumnField {
|
|
10
|
+
constructor(entity, property, type, column) {
|
|
11
|
+
this.entity = entity;
|
|
12
|
+
this.property = property;
|
|
13
|
+
this.type = type;
|
|
14
|
+
this.column = column;
|
|
15
|
+
}
|
|
16
|
+
value(entity, key) {
|
|
17
|
+
return entity[key];
|
|
18
|
+
}
|
|
19
|
+
set(target, value) {
|
|
20
|
+
target[this.property] = value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.ColumnField = ColumnField;
|
|
24
|
+
class JSONField extends ColumnField {
|
|
25
|
+
value(entity, key) {
|
|
26
|
+
return JSON.stringify(entity[key]);
|
|
27
|
+
}
|
|
28
|
+
set(target, value) {
|
|
29
|
+
target[this.property] = typeof value === 'string' ? JSON.parse(value) : undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.JSONField = JSONField;
|
|
33
|
+
class InverseField {
|
|
34
|
+
constructor(entity, property, type) {
|
|
35
|
+
this.entity = entity;
|
|
36
|
+
this.property = property;
|
|
37
|
+
this.type = type;
|
|
38
|
+
}
|
|
39
|
+
value(entity, key) {
|
|
40
|
+
return entity[key];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.InverseField = InverseField;
|
|
44
|
+
class PrimaryField extends ColumnField {
|
|
45
|
+
}
|
|
46
|
+
exports.PrimaryField = PrimaryField;
|
|
47
|
+
class RelationshipField extends ColumnField {
|
|
48
|
+
constructor(entity, property, type, inverseBy, column) {
|
|
49
|
+
super(entity, property, type, column);
|
|
50
|
+
this.type = type;
|
|
51
|
+
this.inverseBy = inverseBy;
|
|
52
|
+
}
|
|
53
|
+
get related() {
|
|
54
|
+
return this.type();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.RelationshipField = RelationshipField;
|
|
58
|
+
class InverseRelationship extends InverseField {
|
|
59
|
+
constructor(entity, property, type, inverseBy) {
|
|
60
|
+
super(entity, property, type);
|
|
61
|
+
this.type = type;
|
|
62
|
+
this.inverseBy = inverseBy;
|
|
63
|
+
}
|
|
64
|
+
get related() {
|
|
65
|
+
return this.type();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.InverseRelationship = InverseRelationship;
|
|
69
|
+
class HasOneField extends InverseRelationship {
|
|
70
|
+
constructor(entity, property, type, inverseBy // TODO make it key of R
|
|
71
|
+
) {
|
|
72
|
+
super(entity, property, type, inverseBy);
|
|
73
|
+
this.relationClass = HasOne;
|
|
74
|
+
}
|
|
75
|
+
set(target, value) {
|
|
76
|
+
target[this.property].set(value);
|
|
77
|
+
}
|
|
78
|
+
async getRelatedValue(relatedEntities, eagerLoad, customQuery) {
|
|
79
|
+
const parentIds = relatedEntities.pluck(this.entity.getPrimaryKey());
|
|
80
|
+
const inverseByField = this.related.metadata.relationships[this.inverseBy];
|
|
81
|
+
const query = this.related.with(...eagerLoad).whereIn(inverseByField.column, parentIds);
|
|
82
|
+
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
83
|
+
return await query.get();
|
|
84
|
+
}
|
|
85
|
+
async getRelatedCount(relatedEntities, customQuery) {
|
|
86
|
+
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
87
|
+
const query = this.related.newQuery()
|
|
88
|
+
.whereIn(relatedField.column, relatedEntities.pluck(this.entity.getPrimaryKey()))
|
|
89
|
+
.select(relatedField.column)
|
|
90
|
+
.groupBy(relatedField.column);
|
|
91
|
+
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
92
|
+
return await query
|
|
93
|
+
.selectCount()
|
|
94
|
+
.get();
|
|
95
|
+
}
|
|
96
|
+
match(entities, relatedEntities) {
|
|
97
|
+
entities.forEach(entity => {
|
|
98
|
+
const inverseField = this.related.metadata.relationships[this.inverseBy];
|
|
99
|
+
const relatedEntity = relatedEntities.find(related => related.original[inverseField.column] === entity[this.entity.getPrimaryKey()]);
|
|
100
|
+
// TODO fix these weird types. Get rid of `unknown`
|
|
101
|
+
entity[this.property].set(relatedEntity);
|
|
102
|
+
});
|
|
103
|
+
return entities;
|
|
104
|
+
}
|
|
105
|
+
matchCounts(entities, counts) {
|
|
106
|
+
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
107
|
+
entities.forEach(entity => {
|
|
108
|
+
var _a;
|
|
109
|
+
const count = counts.find(item => item.original[relatedField.column] === entity[this.entity.getPrimaryKey()]);
|
|
110
|
+
entity[this.property + 'Count'] = ((_a = count === null || count === void 0 ? void 0 : count.original.aggregate) !== null && _a !== void 0 ? _a : 0);
|
|
111
|
+
});
|
|
112
|
+
return entities;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.HasOneField = HasOneField;
|
|
116
|
+
class HasManyField extends InverseRelationship {
|
|
117
|
+
constructor(entity, property, type, inverseBy) {
|
|
118
|
+
super(entity, property, type, inverseBy);
|
|
119
|
+
this.inverseBy = inverseBy;
|
|
120
|
+
this.relationClass = HasMany;
|
|
121
|
+
}
|
|
122
|
+
set(target, value) {
|
|
123
|
+
}
|
|
124
|
+
async getRelatedValue(relatedEntities, eagerLoad, customQuery) {
|
|
125
|
+
const parentIds = relatedEntities.pluck(this.entity.getPrimaryKey());
|
|
126
|
+
const inverseByField = this.related.metadata.relationships[this.inverseBy];
|
|
127
|
+
const query = this.related.with(...eagerLoad).whereIn(inverseByField.column, parentIds);
|
|
128
|
+
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
129
|
+
return await query.get();
|
|
130
|
+
}
|
|
131
|
+
async getRelatedCount(relatedEntities, customQuery) {
|
|
132
|
+
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
133
|
+
const query = this.related.newQuery()
|
|
134
|
+
.whereIn(relatedField.column, relatedEntities.pluck(this.entity.getPrimaryKey()))
|
|
135
|
+
.select(relatedField.column)
|
|
136
|
+
.groupBy(relatedField.column);
|
|
137
|
+
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
138
|
+
return await query
|
|
139
|
+
.selectCount()
|
|
140
|
+
.get();
|
|
141
|
+
}
|
|
142
|
+
match(entities, relatedEntities) {
|
|
143
|
+
entities.forEach(entity => {
|
|
144
|
+
const inverseField = this.related.metadata.relationships[this.inverseBy];
|
|
145
|
+
const relatedEntity = relatedEntities.filter(related => related.original[inverseField.column] === entity[this.entity.getPrimaryKey()]);
|
|
146
|
+
// TODO fix these weird types. Get rid of `unknown`
|
|
147
|
+
entity[this.property].items = relatedEntity;
|
|
148
|
+
});
|
|
149
|
+
return entities;
|
|
150
|
+
}
|
|
151
|
+
matchCounts(entities, counts) {
|
|
152
|
+
const relatedField = this.related.metadata.relationships[this.inverseBy];
|
|
153
|
+
entities.forEach(entity => {
|
|
154
|
+
var _a;
|
|
155
|
+
const count = counts.find(item => item.original[relatedField.column] === entity[this.entity.getPrimaryKey()]);
|
|
156
|
+
entity[this.property + 'Count'] = ((_a = count === null || count === void 0 ? void 0 : count.original.aggregate) !== null && _a !== void 0 ? _a : 0);
|
|
157
|
+
});
|
|
158
|
+
return entities;
|
|
159
|
+
}
|
|
160
|
+
async save(items, parent) {
|
|
161
|
+
const entities = [];
|
|
162
|
+
for await (const item of items) {
|
|
163
|
+
const entity = item instanceof Entity_1.Entity ? item : this.type().new(item);
|
|
164
|
+
entity.fill({
|
|
165
|
+
[this.inverseBy]: parent
|
|
166
|
+
});
|
|
167
|
+
await entity.save();
|
|
168
|
+
entities.push(entity);
|
|
169
|
+
}
|
|
170
|
+
return entities;
|
|
171
|
+
}
|
|
172
|
+
getQuery(parent) {
|
|
173
|
+
const inverseField = this.related.metadata.relationships[this.inverseBy];
|
|
174
|
+
return this.type().newQuery().where(inverseField.column, parent[parent.getPrimaryKey()]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.HasManyField = HasManyField;
|
|
178
|
+
class BelongsToField extends RelationshipField {
|
|
179
|
+
constructor(entity, property, type, inverseBy, column) {
|
|
180
|
+
super(entity, property, type, inverseBy, column || property + entity.getPrimaryKey().capitalize());
|
|
181
|
+
this.relationClass = BelongsTo;
|
|
182
|
+
}
|
|
183
|
+
set(target, value) {
|
|
184
|
+
target[this.property].set(value);
|
|
185
|
+
}
|
|
186
|
+
update(target) {
|
|
187
|
+
target[this.property].update();
|
|
188
|
+
}
|
|
189
|
+
value(entity, key) {
|
|
190
|
+
const field = entity[key];
|
|
191
|
+
const instance = field.get();
|
|
192
|
+
if (instance) {
|
|
193
|
+
return instance[this.related.getPrimaryKey()];
|
|
194
|
+
}
|
|
195
|
+
// return new this.related({
|
|
196
|
+
// [this.related.getPrimaryKey()]: entity[key as K]
|
|
197
|
+
// }) as unknown as T[K]; // TODO get rid of `unknown`
|
|
198
|
+
// if (key === this.column) {
|
|
199
|
+
// return new this.related({
|
|
200
|
+
// [this.related.getPrimaryKey()]: entity[key as K]
|
|
201
|
+
// }) as unknown as T[K]; // TODO get rid of `unknown`
|
|
202
|
+
// }
|
|
203
|
+
// return entity[this.property as keyof T] as unknown as T[K];
|
|
204
|
+
}
|
|
205
|
+
async getRelatedValue(parents, eagerLoad, customQuery) {
|
|
206
|
+
const relationIds = parents
|
|
207
|
+
.map(parent => {
|
|
208
|
+
const relation = parent[this.property].get();
|
|
209
|
+
return relation === null || relation === void 0 ? void 0 : relation[relation.getPrimaryKey()];
|
|
210
|
+
})
|
|
211
|
+
.filter(Boolean);
|
|
212
|
+
if (relationIds.length) {
|
|
213
|
+
const query = this.related.with(...eagerLoad).whereIn(this.related.getPrimaryKey(), relationIds);
|
|
214
|
+
if (customQuery) {
|
|
215
|
+
customQuery(query);
|
|
216
|
+
}
|
|
217
|
+
return await query.get();
|
|
218
|
+
}
|
|
219
|
+
return [];
|
|
220
|
+
}
|
|
221
|
+
async getRelatedCount(relatedEntities, customQuery) {
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
match(entities, relatedEntities) {
|
|
225
|
+
return entities.map(entity => {
|
|
226
|
+
// TODO fix these weird types. Get rid of `unknown`
|
|
227
|
+
const related = entity[this.property].get();
|
|
228
|
+
const value = related === null || related === void 0 ? void 0 : related[related.getPrimaryKey()];
|
|
229
|
+
if (value) {
|
|
230
|
+
const instance = relatedEntities.findWhere(this.related.getPrimaryKey(), value);
|
|
231
|
+
if (instance) {
|
|
232
|
+
this.set(entity, instance);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return entity;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
matchCounts(entities, counts) {
|
|
239
|
+
return entities;
|
|
240
|
+
}
|
|
241
|
+
getQuery(parent) {
|
|
242
|
+
return this.entity.newQuery().where(this.column, parent[parent.getPrimaryKey()]);
|
|
243
|
+
}
|
|
244
|
+
async save(items, parent) {
|
|
245
|
+
const entities = [];
|
|
246
|
+
for await (const item of items) {
|
|
247
|
+
const entity = item instanceof Entity_1.Entity ? item : this.entity.new(item);
|
|
248
|
+
entity.fill({
|
|
249
|
+
[this.property]: parent
|
|
250
|
+
});
|
|
251
|
+
await entity.save();
|
|
252
|
+
entities.push(entity);
|
|
253
|
+
}
|
|
254
|
+
return entities;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
exports.BelongsToField = BelongsToField;
|
|
258
|
+
class BelongsToManyField extends InverseRelationship {
|
|
259
|
+
constructor(entity, property, type, inverseBy, table, parentColumn, relatedColumn) {
|
|
260
|
+
super(entity, property, type, inverseBy);
|
|
261
|
+
this.table = table;
|
|
262
|
+
this.parentColumn = parentColumn;
|
|
263
|
+
this.relatedColumn = relatedColumn;
|
|
264
|
+
this.relationClass = BelongsToMany;
|
|
265
|
+
}
|
|
266
|
+
set(target, value) {
|
|
267
|
+
}
|
|
268
|
+
async getRelatedValue(parents, eagerLoad, customQuery) {
|
|
269
|
+
const parentIds = parents.pluck(this.entity.getPrimaryKey());
|
|
270
|
+
const relatedForeignKey = `${Helpers_1.wrap(this.getPivotTable())}.${this.getRelatedForeignKey()}`;
|
|
271
|
+
const parentForeignKey = `${Helpers_1.wrap(this.getPivotTable())}.${this.getParentForeignKey()}`;
|
|
272
|
+
const relatedKey = `${Helpers_1.wrap(this.related.getTable())}.${this.related.getPrimaryKey()}`;
|
|
273
|
+
const query = this.related
|
|
274
|
+
.with(...eagerLoad)
|
|
275
|
+
.addSelect(new index_1.StringExpression(`${Helpers_1.wrap(this.related.getTable())}.*`), new index_1.StringExpression(relatedForeignKey), new index_1.StringExpression(parentForeignKey))
|
|
276
|
+
.join(this.getPivotTable(), relatedForeignKey, '=', relatedKey)
|
|
277
|
+
.whereIn(parentForeignKey, parentIds.unique());
|
|
278
|
+
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
279
|
+
const results = await query.get();
|
|
280
|
+
return results.map(entity => {
|
|
281
|
+
entity.original.pivot = {
|
|
282
|
+
[this.getParentForeignKey()]: entity.original[this.getParentForeignKey()],
|
|
283
|
+
[this.getRelatedForeignKey()]: entity.original[this.getRelatedForeignKey()],
|
|
284
|
+
};
|
|
285
|
+
delete entity.original[this.getParentForeignKey()];
|
|
286
|
+
delete entity.original[this.getRelatedForeignKey()];
|
|
287
|
+
return entity;
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
getParentForeignKey() {
|
|
291
|
+
return this.parentColumn || `${this.entity.name.toLocaleLowerCase()}${this.entity.getPrimaryKey().capitalize()}`;
|
|
292
|
+
}
|
|
293
|
+
getRelatedForeignKey() {
|
|
294
|
+
return this.relatedColumn || `${this.related.name.toLocaleLowerCase()}${this.related.getPrimaryKey().capitalize()}`;
|
|
295
|
+
}
|
|
296
|
+
async getRelatedCount(relatedEntities, customQuery) {
|
|
297
|
+
const query = index_1.Query.table(this.getPivotTable())
|
|
298
|
+
.select(this.getParentForeignKey())
|
|
299
|
+
.groupBy(this.getParentForeignKey())
|
|
300
|
+
.whereIn(this.getParentForeignKey(), relatedEntities.pluck(this.related.getPrimaryKey()));
|
|
301
|
+
customQuery === null || customQuery === void 0 ? void 0 : customQuery(query);
|
|
302
|
+
return await query.selectCount().get();
|
|
303
|
+
}
|
|
304
|
+
match(entities, relatedEntities) {
|
|
305
|
+
return entities.map(entity => {
|
|
306
|
+
const entityPrimaryKey = entity[this.entity.getPrimaryKey()];
|
|
307
|
+
entity[this.property].items = relatedEntities
|
|
308
|
+
.filter(related => related.original.pivot[this.getParentForeignKey()] === entityPrimaryKey);
|
|
309
|
+
return entity;
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
matchCounts(entities, counts) {
|
|
313
|
+
entities.forEach(entity => {
|
|
314
|
+
const foreignKey = this.getParentForeignKey();
|
|
315
|
+
const count = counts.find(item => { var _a; return (((_a = item === null || item === void 0 ? void 0 : item.original) === null || _a === void 0 ? void 0 : _a[foreignKey]) || item[foreignKey]) === entity[this.entity.getPrimaryKey()]; });
|
|
316
|
+
// @ts-ignore
|
|
317
|
+
const aggregateValue = (count === null || count === void 0 ? void 0 : count.aggregate) || (count === null || count === void 0 ? void 0 : count.original.aggregate);
|
|
318
|
+
entity[this.property + 'Count'] = (aggregateValue !== null && aggregateValue !== void 0 ? aggregateValue : 0);
|
|
319
|
+
});
|
|
320
|
+
return entities;
|
|
321
|
+
}
|
|
322
|
+
getPivotTable() {
|
|
323
|
+
return this.table || [this.entity.getTable(), this.related.getTable()].sort().join('_');
|
|
324
|
+
}
|
|
325
|
+
value(entity, key) {
|
|
326
|
+
const value = entity[key];
|
|
327
|
+
if (value instanceof Array) {
|
|
328
|
+
return value.map(related => {
|
|
329
|
+
if (related instanceof Entity_1.Entity) {
|
|
330
|
+
return related;
|
|
331
|
+
}
|
|
332
|
+
const instance = new this.related();
|
|
333
|
+
instance[instance.getPrimaryKey()] = related;
|
|
334
|
+
return instance;
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
async add(items, parent) {
|
|
340
|
+
const entities = [];
|
|
341
|
+
// tslint:disable-next-line:no-any
|
|
342
|
+
const dataToInsert = [];
|
|
343
|
+
for await (const item of items) {
|
|
344
|
+
dataToInsert.push({
|
|
345
|
+
[this.getRelatedForeignKey()]: item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item,
|
|
346
|
+
[this.getParentForeignKey()]: parent.getPrimaryKeyValue(),
|
|
347
|
+
});
|
|
348
|
+
entities.push(this.related.new({ [this.related.getPrimaryKey()]: item }));
|
|
349
|
+
}
|
|
350
|
+
if (dataToInsert.length) {
|
|
351
|
+
await index_1.Query.table(this.getPivotTable()).insert(dataToInsert);
|
|
352
|
+
}
|
|
353
|
+
return entities;
|
|
354
|
+
}
|
|
355
|
+
getQuery(parent) {
|
|
356
|
+
return this.related.newQuery().whereIn(this.related.getPrimaryKey(), query => {
|
|
357
|
+
query.select(this.getRelatedForeignKey())
|
|
358
|
+
.table(this.getPivotTable())
|
|
359
|
+
.where(this.getParentForeignKey(), parent[parent.getPrimaryKey()]);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
exports.BelongsToManyField = BelongsToManyField;
|
|
364
|
+
class BelongsTo extends BaseRelationship_1.BaseRelationship {
|
|
365
|
+
constructor(relationship, parent) {
|
|
366
|
+
super(relationship, parent);
|
|
367
|
+
this.update();
|
|
368
|
+
}
|
|
369
|
+
update() {
|
|
370
|
+
const parentId = this.parent.original[this.relationship.column];
|
|
371
|
+
if (parentId && !this.instance) {
|
|
372
|
+
this.setById(parentId);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
376
|
+
metadata.relationships[property] = new BelongsToField(entityClass, property, type, inverseBy, options === null || options === void 0 ? void 0 : options.column);
|
|
377
|
+
return metadata;
|
|
378
|
+
}
|
|
379
|
+
get() {
|
|
380
|
+
return this.instance;
|
|
381
|
+
}
|
|
382
|
+
set(instance) {
|
|
383
|
+
if (instance instanceof Entity_1.Entity) {
|
|
384
|
+
this.instance = instance;
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
this.setById(instance);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
setById(id) {
|
|
391
|
+
this.set(this.relationship.related.new({ [this.relationship.related.getPrimaryKey()]: id }));
|
|
392
|
+
}
|
|
393
|
+
async save(data = {}) {
|
|
394
|
+
if (!this.instance) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
this.instance.fill({
|
|
398
|
+
...data,
|
|
399
|
+
});
|
|
400
|
+
await this.instance.save();
|
|
401
|
+
return this.instance;
|
|
402
|
+
}
|
|
403
|
+
toJSON() {
|
|
404
|
+
var _a;
|
|
405
|
+
return (_a = this.instance) === null || _a === void 0 ? void 0 : _a.toJSON();
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
exports.BelongsTo = BelongsTo;
|
|
409
|
+
class HasOne extends BaseRelationship_1.BaseRelationship {
|
|
410
|
+
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
411
|
+
metadata.inverseRelationships[property] = new HasOneField(entityClass, property, type, inverseBy);
|
|
412
|
+
return metadata;
|
|
413
|
+
}
|
|
414
|
+
get() {
|
|
415
|
+
return this.instance;
|
|
416
|
+
}
|
|
417
|
+
set(instance) {
|
|
418
|
+
if (!instance) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (instance instanceof Entity_1.Entity) {
|
|
422
|
+
this.instance = instance;
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
this.setById(instance);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
setById(id) {
|
|
429
|
+
this.set(this.relationship.related.new({ [this.relationship.related.getPrimaryKey()]: id }));
|
|
430
|
+
}
|
|
431
|
+
async save(instance = this.instance) {
|
|
432
|
+
this.set(instance);
|
|
433
|
+
if (instance) {
|
|
434
|
+
instance[this.relationship.inverseBy].set(this.parent);
|
|
435
|
+
await instance.save();
|
|
436
|
+
}
|
|
437
|
+
return this.instance;
|
|
438
|
+
}
|
|
439
|
+
toJSON() {
|
|
440
|
+
var _a;
|
|
441
|
+
return (_a = this.instance) === null || _a === void 0 ? void 0 : _a.toJSON();
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
exports.HasOne = HasOne;
|
|
445
|
+
class HasMany extends List_1.List {
|
|
446
|
+
constructor(relationship, parent) {
|
|
447
|
+
super(relationship, parent);
|
|
448
|
+
this.relationship = relationship;
|
|
449
|
+
}
|
|
450
|
+
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
451
|
+
metadata.inverseRelationships[property] = new HasManyField(entityClass, property, type, inverseBy);
|
|
452
|
+
return metadata;
|
|
453
|
+
}
|
|
454
|
+
async save(item) {
|
|
455
|
+
const instances = await this.saveMany(item);
|
|
456
|
+
return instances.first();
|
|
457
|
+
}
|
|
458
|
+
async saveMany(...items) {
|
|
459
|
+
if (!this.parent.exists) {
|
|
460
|
+
await this.parent.save();
|
|
461
|
+
}
|
|
462
|
+
const instances = await this.relationship.save(items, this.parent);
|
|
463
|
+
this.items.push(...instances);
|
|
464
|
+
return instances;
|
|
465
|
+
}
|
|
466
|
+
async clear() {
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
exports.HasMany = HasMany;
|
|
470
|
+
class BelongsToMany extends List_1.List {
|
|
471
|
+
constructor(relationship, parent) {
|
|
472
|
+
super(relationship, parent);
|
|
473
|
+
this.relationship = relationship;
|
|
474
|
+
}
|
|
475
|
+
static relationship(metadata, entityClass, property, type, inverseBy, options) {
|
|
476
|
+
// options.column = options.column || (property as string) + (entityClass.getPrimaryKey() as string).capitalize();
|
|
477
|
+
const inverseField = metadata.inverseRelationships[inverseBy];
|
|
478
|
+
metadata.inverseRelationships[property] = new BelongsToManyField(entityClass, property, type, inverseBy, (options === null || options === void 0 ? void 0 : options.table) || (inverseField === null || inverseField === void 0 ? void 0 : inverseField.table), (options === null || options === void 0 ? void 0 : options.foreignColumn) || (inverseField === null || inverseField === void 0 ? void 0 : inverseField.parentColumn), (options === null || options === void 0 ? void 0 : options.column) || (inverseField === null || inverseField === void 0 ? void 0 : inverseField.relatedColumn));
|
|
479
|
+
return metadata;
|
|
480
|
+
}
|
|
481
|
+
async has(item) {
|
|
482
|
+
if (item instanceof Entity_1.Entity) {
|
|
483
|
+
item = item.getPrimaryKeyValue();
|
|
484
|
+
}
|
|
485
|
+
const relatedForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getRelatedForeignKey()}`;
|
|
486
|
+
const parentForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getParentForeignKey()}`;
|
|
487
|
+
return Boolean(await index_1.Query.table(this.relationship.getPivotTable())
|
|
488
|
+
.where(parentForeignKey, this.parent[this.parent.getPrimaryKey()])
|
|
489
|
+
.andWhere(relatedForeignKey, item)
|
|
490
|
+
.first());
|
|
491
|
+
}
|
|
492
|
+
async add(...items) {
|
|
493
|
+
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
494
|
+
if (!this.parent.exists) {
|
|
495
|
+
await this.parent.save();
|
|
496
|
+
}
|
|
497
|
+
for await (const id of ids) {
|
|
498
|
+
if (!(await this.has(id))) {
|
|
499
|
+
this.items = this.items.concat(await this.relationship.add(items, this.parent));
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return this.items;
|
|
503
|
+
}
|
|
504
|
+
async remove(...items) {
|
|
505
|
+
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
506
|
+
const relatedForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getRelatedForeignKey()}`;
|
|
507
|
+
const parentForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getParentForeignKey()}`;
|
|
508
|
+
await index_1.Query.table(this.relationship.getPivotTable())
|
|
509
|
+
.where(parentForeignKey, this.parent[this.parent.getPrimaryKey()])
|
|
510
|
+
.whereIn(relatedForeignKey, ids)
|
|
511
|
+
.delete();
|
|
512
|
+
this.items = this.items.filter(item => !ids.includes(item.getPrimaryKeyValue()));
|
|
513
|
+
}
|
|
514
|
+
async toggle(...items) {
|
|
515
|
+
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
516
|
+
const attachedEntities = await this.relationship.getQuery(this.parent).get();
|
|
517
|
+
const entitiesToDetach = ids.filter(item => attachedEntities.some(entity => entity.getPrimaryKeyValue() === item));
|
|
518
|
+
const entitiesToAttach = ids.filter(item => attachedEntities.some(entity => entity.getPrimaryKeyValue() !== item));
|
|
519
|
+
await this.add(...entitiesToAttach);
|
|
520
|
+
await this.remove(...entitiesToDetach);
|
|
521
|
+
}
|
|
522
|
+
async clear() {
|
|
523
|
+
const relatedForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getRelatedForeignKey()}`;
|
|
524
|
+
const parentForeignKey = `${this.relationship.getPivotTable()}.${this.relationship.getParentForeignKey()}`;
|
|
525
|
+
await index_1.Query.table(this.relationship.getPivotTable())
|
|
526
|
+
.where(parentForeignKey, this.parent[this.parent.getPrimaryKey()])
|
|
527
|
+
.whereIn(relatedForeignKey, this.items.pluck(this.relationship.related.getPrimaryKey()))
|
|
528
|
+
.delete();
|
|
529
|
+
this.items = [];
|
|
530
|
+
}
|
|
531
|
+
async sync(...items) {
|
|
532
|
+
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
533
|
+
const existingItems = await this.get();
|
|
534
|
+
const primaryKey = this.relationship.type().getPrimaryKey();
|
|
535
|
+
const itemsToRemove = existingItems.filter(item => !ids.includes(item[primaryKey])).pluck(primaryKey);
|
|
536
|
+
const restOfItemsToAdd = ids.filter(item => !existingItems.findWhere(primaryKey, item));
|
|
537
|
+
await this.remove(...itemsToRemove);
|
|
538
|
+
await this.add(...restOfItemsToAdd);
|
|
539
|
+
}
|
|
540
|
+
async syncWithoutDetaching(...items) {
|
|
541
|
+
const ids = items.map(item => item instanceof Entity_1.Entity ? item.getPrimaryKeyValue() : item);
|
|
542
|
+
const existingItems = await this.get();
|
|
543
|
+
const primaryKey = this.relationship.type().getPrimaryKey();
|
|
544
|
+
const restOfItemsToAdd = ids.filter(item => !existingItems.findWhere(primaryKey, item));
|
|
545
|
+
await this.add(...restOfItemsToAdd);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
exports.BelongsToMany = BelongsToMany;
|
|
549
549
|
//# sourceMappingURL=Fields.js.map
|