fib-orm 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -6
- package/index.d.ts +11 -0
- package/index.js +15 -0
- package/package.json +36 -34
- package/{lib/cli/commands/ddlFrom.js → src/cli/commands/ddlFrom.ts} +40 -26
- package/{lib/cli/commands/dumpModel.js → src/cli/commands/dumpModel.ts} +78 -31
- package/{lib/cli/commands/escape.js → src/cli/commands/escape.ts} +51 -28
- package/{lib/cli/commands/spinner.js → src/cli/commands/spinner.ts} +20 -10
- package/{lib/cli/commands/upgrade.js → src/cli/commands/upgrade.ts} +53 -33
- package/src/cli/helpers/ddl.ts +84 -0
- package/src/cli/helpers/package.ts +81 -0
- package/src/cli/index.ts +24 -0
- package/src/cli/wrapSubcommand.ts +66 -0
- package/src/db-driver/Typo.d.ts +141 -0
- package/src/db-driver/built-ins/base.class.ts +260 -0
- package/src/db-driver/built-ins/driver-dm.ts +62 -0
- package/src/db-driver/built-ins/driver-mssql.ts +67 -0
- package/src/db-driver/built-ins/driver-mysql.ts +44 -0
- package/src/db-driver/built-ins/driver-postgresql.ts +94 -0
- package/src/db-driver/built-ins/driver-redis.ts +45 -0
- package/src/db-driver/built-ins/driver-sqlite.ts +41 -0
- package/src/db-driver/index.ts +6 -0
- package/src/db-driver/parseQueryString.ts +76 -0
- package/src/db-driver/utils.ts +204 -0
- package/src/knex/deps/tarn/PendingOperation.ts +56 -0
- package/src/knex/deps/tarn/Pool.ts +610 -0
- package/src/knex/deps/tarn/PromiseInspection.ts +29 -0
- package/src/knex/deps/tarn/Resource.ts +22 -0
- package/src/knex/deps/tarn/TimeoutError.ts +1 -0
- package/src/knex/deps/tarn/tarn.ts +7 -0
- package/src/knex/deps/tarn/utils.ts +62 -0
- package/src/knex/knex.ts +11 -0
- package/src/knex/result.d.ts +27 -0
- package/src/knex/sandbox.ts +45 -0
- package/src/knex/tables.d.ts +4 -0
- package/src/knex/types.d.ts +3228 -0
- package/src/orm/Adapters.ts +53 -0
- package/src/orm/AggregateFunctions.ts +254 -0
- package/src/orm/Associations/Extend.ts +405 -0
- package/src/orm/Associations/Many.ts +784 -0
- package/src/orm/Associations/One.ts +494 -0
- package/src/orm/Associations/_utils.ts +72 -0
- package/src/orm/ChainFind.ts +427 -0
- package/src/orm/ChainInstance.ts +97 -0
- package/src/orm/Debug.ts +21 -0
- package/src/orm/Drivers/DB/base.ts +7 -0
- package/src/orm/Drivers/DB/dm.ts +35 -0
- package/src/orm/Drivers/DB/mssql.ts +35 -0
- package/src/orm/Drivers/DB/mysql.ts +35 -0
- package/src/orm/Drivers/DB/postgres.ts +35 -0
- package/src/orm/Drivers/DB/sqlite3.ts +60 -0
- package/src/orm/Drivers/DML/_ddl-sql.ts +85 -0
- package/src/orm/Drivers/DML/_dml-helpers.ts +66 -0
- package/src/orm/Drivers/DML/_shared.ts +61 -0
- package/src/orm/Drivers/DML/_utils.ts +112 -0
- package/{lib/orm/Drivers/DML/dm.js → src/orm/Drivers/DML/dm.ts} +178 -85
- package/src/orm/Drivers/DML/index.ts +10 -0
- package/src/orm/Drivers/DML/mssql.ts +366 -0
- package/src/orm/Drivers/DML/mysql.ts +347 -0
- package/src/orm/Drivers/DML/postgres.ts +382 -0
- package/src/orm/Drivers/DML/sqlite.ts +361 -0
- package/src/orm/Error.ts +49 -0
- package/src/orm/Helpers.ts +297 -0
- package/src/orm/Hook.ts +125 -0
- package/src/orm/Instance.ts +944 -0
- package/src/orm/LazyLoad.ts +117 -0
- package/src/orm/Model.ts +1235 -0
- package/src/orm/ORM.ts +301 -0
- package/src/orm/Patch/fib-cache.ts +7 -0
- package/src/orm/Patch/plugin.ts +25 -0
- package/src/orm/Property.ts +86 -0
- package/src/orm/Settings.ts +134 -0
- package/src/orm/Singleton.ts +97 -0
- package/src/orm/Typo/Adapter.d.ts +5 -0
- package/src/orm/Typo/DMLDriver.d.ts +260 -0
- package/src/orm/Typo/Db.d.ts +100 -0
- package/src/orm/Typo/Error.d.ts +36 -0
- package/src/orm/Typo/ORM.d.ts +315 -0
- package/src/orm/Typo/Validators.d.ts +39 -0
- package/src/orm/Typo/_common.d.ts +44 -0
- package/src/orm/Typo/assoc.d.ts +280 -0
- package/src/orm/Typo/hook.d.ts +68 -0
- package/src/orm/Typo/instance.d.ts +214 -0
- package/{lib → src}/orm/Typo/model.d.ts +297 -176
- package/src/orm/Typo/patch.d.ts +39 -0
- package/src/orm/Typo/property.d.ts +79 -0
- package/src/orm/Typo/query.d.ts +266 -0
- package/{lib → src}/orm/Typo/settings.d.ts +14 -7
- package/{lib → src}/orm/Typo/synchronous.d.ts +51 -26
- package/src/orm/Utilities.ts +1313 -0
- package/src/orm/Validators.ts +127 -0
- package/src/orm/Where/common.ts +8 -0
- package/src/orm/Where/filterDate.ts +74 -0
- package/src/orm/cloneDeep.ts +45 -0
- package/src/orm/index.ts +231 -0
- package/src/orm-core/callback.d.ts +15 -0
- package/{lib → src}/orm-core/dialect.d.ts +75 -52
- package/src/orm-core/error.ts +71 -0
- package/{lib/orm-core/index.d.ts → src/orm-core/index.ts} +8 -2
- package/src/orm-core/sync.d.ts +14 -0
- package/src/orm-core/utils.ts +4 -0
- package/src/orm-property/Property.ts +110 -0
- package/src/orm-property/Utils.ts +26 -0
- package/src/orm-property/customTypes.ts +13 -0
- package/src/orm-property/index.ts +34 -0
- package/src/orm-property/transformer.ts +16 -0
- package/{lib/orm-property/transformers/dm.js → src/orm-property/transformers/dm.ts} +128 -68
- package/src/orm-property/transformers/index.ts +10 -0
- package/src/orm-property/transformers/mssql.ts +285 -0
- package/src/orm-property/transformers/mysql.ts +395 -0
- package/src/orm-property/transformers/postgresql.ts +267 -0
- package/src/orm-property/transformers/sqlite.ts +217 -0
- package/src/plugins/orm-plugin-uacl/Typo.d.ts +173 -0
- package/src/plugins/orm-plugin-uacl/_utils.ts +237 -0
- package/src/plugins/orm-plugin-uacl/acl-tree.ts +440 -0
- package/src/plugins/orm-plugin-uacl/config-acl-tree.ts +592 -0
- package/src/plugins/orm-plugin-uacl/index.ts +180 -0
- package/src/plugins/orm-plugin-uacl/tree.ts +331 -0
- package/src/sql-ddl-sync/Dialects/dm.ts +493 -0
- package/{lib/sql-query/Dialects/index.d.ts → src/sql-ddl-sync/Dialects/index.ts} +3 -3
- package/src/sql-ddl-sync/Dialects/mssql.ts +441 -0
- package/src/sql-ddl-sync/Dialects/mysql.ts +414 -0
- package/src/sql-ddl-sync/Dialects/postgresql.ts +474 -0
- package/src/sql-ddl-sync/Dialects/sqlite.ts +439 -0
- package/src/sql-ddl-sync/SQL.ts +157 -0
- package/src/sql-ddl-sync/Typo/Collection.d.ts +19 -0
- package/src/sql-ddl-sync/Typo/DbIndex.d.ts +11 -0
- package/{lib → src}/sql-ddl-sync/Typo/Dialect.d.ts +67 -52
- package/src/sql-ddl-sync/Typo/Driver.d.ts +67 -0
- package/src/sql-ddl-sync/Typo/Query.d.ts +8 -0
- package/src/sql-ddl-sync/Typo/SQL.d.ts +45 -0
- package/src/sql-ddl-sync/Typo/_common.d.ts +59 -0
- package/src/sql-ddl-sync/Utils.ts +291 -0
- package/src/sql-ddl-sync/index.ts +599 -0
- package/src/sql-query/Comparators.ts +115 -0
- package/src/sql-query/Create.ts +87 -0
- package/{lib/sql-query/Dialects/dm.js → src/sql-query/Dialects/dm.ts} +42 -21
- package/src/sql-query/Dialects/index.ts +14 -0
- package/src/sql-query/Dialects/mssql.ts +91 -0
- package/src/sql-query/Dialects/mysql.ts +120 -0
- package/src/sql-query/Dialects/postgresql.ts +99 -0
- package/src/sql-query/Dialects/sqlite.ts +80 -0
- package/src/sql-query/Helpers.ts +303 -0
- package/src/sql-query/Insert.ts +41 -0
- package/src/sql-query/Query.ts +140 -0
- package/src/sql-query/Remove.ts +111 -0
- package/src/sql-query/Select.ts +719 -0
- package/src/sql-query/Set.ts +25 -0
- package/src/sql-query/Typo/Aggregation.d.ts +34 -0
- package/src/sql-query/Typo/Comparators.d.ts +156 -0
- package/src/sql-query/Typo/Dialect.d.ts +51 -0
- package/src/sql-query/Typo/Field.d.ts +10 -0
- package/src/sql-query/Typo/Helper.d.ts +4 -0
- package/src/sql-query/Typo/Query-ChainBuilder.d.ts +110 -0
- package/src/sql-query/Typo/Query.d.ts +65 -0
- package/src/sql-query/Typo/Sql.d.ts +192 -0
- package/src/sql-query/Typo/SubQuery.d.ts +52 -0
- package/{lib/sql-query/Typo/index.d.ts → src/sql-query/Typo/index.ts} +5 -2
- package/src/sql-query/Update.ts +50 -0
- package/src/sql-query/Where.ts +435 -0
- package/{lib/orm/Patch → src/typings}/fib-cache.d.ts +4 -3
- package/lib/cli/commands/ddlFrom.d.ts +0 -2
- package/lib/cli/commands/dumpModel.d.ts +0 -2
- package/lib/cli/commands/escape.d.ts +0 -2
- package/lib/cli/commands/spinner.d.ts +0 -2
- package/lib/cli/commands/upgrade.d.ts +0 -2
- package/lib/cli/helpers/ddl.d.ts +0 -17
- package/lib/cli/helpers/ddl.js +0 -55
- package/lib/cli/helpers/package.d.ts +0 -26
- package/lib/cli/helpers/package.js +0 -56
- package/lib/cli/index.d.ts +0 -2
- package/lib/cli/index.js +0 -18
- package/lib/db-driver/Typo.d.ts +0 -112
- package/lib/db-driver/Typo.js +0 -1
- package/lib/db-driver/built-ins/base.class.d.ts +0 -96
- package/lib/db-driver/built-ins/base.class.js +0 -186
- package/lib/db-driver/built-ins/driver-dm.d.ts +0 -18
- package/lib/db-driver/built-ins/driver-dm.js +0 -53
- package/lib/db-driver/built-ins/driver-mongodb.d.ts +0 -24
- package/lib/db-driver/built-ins/driver-mongodb.js +0 -46
- package/lib/db-driver/built-ins/driver-mysql.d.ts +0 -18
- package/lib/db-driver/built-ins/driver-mysql.js +0 -37
- package/lib/db-driver/built-ins/driver-postgresql.d.ts +0 -22
- package/lib/db-driver/built-ins/driver-postgresql.js +0 -76
- package/lib/db-driver/built-ins/driver-redis.d.ts +0 -12
- package/lib/db-driver/built-ins/driver-redis.js +0 -35
- package/lib/db-driver/built-ins/driver-sqlite.d.ts +0 -17
- package/lib/db-driver/built-ins/driver-sqlite.js +0 -34
- package/lib/db-driver/index.d.ts +0 -3
- package/lib/db-driver/index.js +0 -4
- package/lib/db-driver/utils.d.ts +0 -14
- package/lib/db-driver/utils.js +0 -187
- package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +0 -12
- package/lib/knex/deps/tarn/dist/PendingOperation.js +0 -48
- package/lib/knex/deps/tarn/dist/Pool.d.ts +0 -97
- package/lib/knex/deps/tarn/dist/Pool.js +0 -516
- package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +0 -16
- package/lib/knex/deps/tarn/dist/PromiseInspection.js +0 -21
- package/lib/knex/deps/tarn/dist/Resource.d.ts +0 -9
- package/lib/knex/deps/tarn/dist/Resource.js +0 -19
- package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +0 -2
- package/lib/knex/deps/tarn/dist/TimeoutError.js +0 -5
- package/lib/knex/deps/tarn/dist/tarn.d.ts +0 -3
- package/lib/knex/deps/tarn/dist/tarn.js +0 -10
- package/lib/knex/deps/tarn/dist/utils.d.ts +0 -14
- package/lib/knex/deps/tarn/dist/utils.js +0 -60
- package/lib/knex/knex.d.ts +0 -2
- package/lib/knex/knex.js +0 -3
- package/lib/knex/sandbox.d.ts +0 -4
- package/lib/knex/sandbox.js +0 -22
- package/lib/orm/Adapters.d.ts +0 -3
- package/lib/orm/Adapters.js +0 -44
- package/lib/orm/AggregateFunctions.d.ts +0 -3
- package/lib/orm/AggregateFunctions.js +0 -176
- package/lib/orm/Associations/Extend.d.ts +0 -22
- package/lib/orm/Associations/Extend.js +0 -274
- package/lib/orm/Associations/Many.d.ts +0 -17
- package/lib/orm/Associations/Many.js +0 -569
- package/lib/orm/Associations/One.d.ts +0 -16
- package/lib/orm/Associations/One.js +0 -310
- package/lib/orm/Associations/_utils.d.ts +0 -15
- package/lib/orm/Associations/_utils.js +0 -52
- package/lib/orm/ChainFind.d.ts +0 -3
- package/lib/orm/ChainFind.js +0 -327
- package/lib/orm/ChainInstance.d.ts +0 -6
- package/lib/orm/ChainInstance.js +0 -73
- package/lib/orm/Debug.d.ts +0 -2
- package/lib/orm/Debug.js +0 -17
- package/lib/orm/Drivers/DB/base.d.ts +0 -3
- package/lib/orm/Drivers/DB/base.js +0 -3
- package/lib/orm/Drivers/DB/dm.d.ts +0 -11
- package/lib/orm/Drivers/DB/dm.js +0 -23
- package/lib/orm/Drivers/DB/mysql.d.ts +0 -11
- package/lib/orm/Drivers/DB/mysql.js +0 -23
- package/lib/orm/Drivers/DB/postgres.d.ts +0 -11
- package/lib/orm/Drivers/DB/postgres.js +0 -22
- package/lib/orm/Drivers/DB/sqlite3.d.ts +0 -16
- package/lib/orm/Drivers/DB/sqlite3.js +0 -39
- package/lib/orm/Drivers/DML/_ddl-sql.d.ts +0 -3
- package/lib/orm/Drivers/DML/_ddl-sql.js +0 -51
- package/lib/orm/Drivers/DML/_dml-helpers.d.ts +0 -19
- package/lib/orm/Drivers/DML/_dml-helpers.js +0 -51
- package/lib/orm/Drivers/DML/_shared.d.ts +0 -9
- package/lib/orm/Drivers/DML/_shared.js +0 -38
- package/lib/orm/Drivers/DML/_utils.d.ts +0 -8
- package/lib/orm/Drivers/DML/_utils.js +0 -67
- package/lib/orm/Drivers/DML/constants.d.ts +0 -3
- package/lib/orm/Drivers/DML/constants.js +0 -2
- package/lib/orm/Drivers/DML/dm.d.ts +0 -2
- package/lib/orm/Drivers/DML/index.d.ts +0 -4
- package/lib/orm/Drivers/DML/index.js +0 -6
- package/lib/orm/Drivers/DML/mysql.d.ts +0 -2
- package/lib/orm/Drivers/DML/mysql.js +0 -251
- package/lib/orm/Drivers/DML/postgres.d.ts +0 -3
- package/lib/orm/Drivers/DML/postgres.js +0 -276
- package/lib/orm/Drivers/DML/sqlite.d.ts +0 -2
- package/lib/orm/Drivers/DML/sqlite.js +0 -266
- package/lib/orm/Error.d.ts +0 -11
- package/lib/orm/Error.js +0 -40
- package/lib/orm/Helpers.d.ts +0 -35
- package/lib/orm/Helpers.js +0 -233
- package/lib/orm/Hook.d.ts +0 -13
- package/lib/orm/Hook.js +0 -99
- package/lib/orm/Instance.d.ts +0 -2
- package/lib/orm/Instance.js +0 -697
- package/lib/orm/LazyLoad.d.ts +0 -4
- package/lib/orm/LazyLoad.js +0 -76
- package/lib/orm/Model.d.ts +0 -7
- package/lib/orm/Model.js +0 -971
- package/lib/orm/ORM.d.ts +0 -37
- package/lib/orm/ORM.js +0 -241
- package/lib/orm/Patch/fib-cache.js +0 -2
- package/lib/orm/Patch/plugin.d.ts +0 -4
- package/lib/orm/Patch/plugin.js +0 -13
- package/lib/orm/Property.d.ts +0 -9
- package/lib/orm/Property.js +0 -71
- package/lib/orm/Settings.d.ts +0 -42
- package/lib/orm/Settings.js +0 -119
- package/lib/orm/Singleton.d.ts +0 -5
- package/lib/orm/Singleton.js +0 -72
- package/lib/orm/Typo/Adapter.d.ts +0 -5
- package/lib/orm/Typo/Adapter.js +0 -1
- package/lib/orm/Typo/DMLDriver.d.ts +0 -210
- package/lib/orm/Typo/DMLDriver.js +0 -1
- package/lib/orm/Typo/Db.d.ts +0 -38
- package/lib/orm/Typo/Db.js +0 -1
- package/lib/orm/Typo/Error.d.ts +0 -31
- package/lib/orm/Typo/Error.js +0 -1
- package/lib/orm/Typo/ORM.d.ts +0 -233
- package/lib/orm/Typo/ORM.js +0 -3
- package/lib/orm/Typo/Validators.d.ts +0 -32
- package/lib/orm/Typo/Validators.js +0 -2
- package/lib/orm/Typo/_common.d.ts +0 -38
- package/lib/orm/Typo/_common.js +0 -1
- package/lib/orm/Typo/assoc.d.ts +0 -221
- package/lib/orm/Typo/assoc.js +0 -1
- package/lib/orm/Typo/hook.d.ts +0 -42
- package/lib/orm/Typo/hook.js +0 -1
- package/lib/orm/Typo/instance.d.ts +0 -157
- package/lib/orm/Typo/instance.js +0 -1
- package/lib/orm/Typo/model.js +0 -6
- package/lib/orm/Typo/patch.d.ts +0 -31
- package/lib/orm/Typo/patch.js +0 -1
- package/lib/orm/Typo/property.d.ts +0 -57
- package/lib/orm/Typo/property.js +0 -1
- package/lib/orm/Typo/query.d.ts +0 -216
- package/lib/orm/Typo/query.js +0 -1
- package/lib/orm/Typo/settings.js +0 -5
- package/lib/orm/Typo/synchronous.js +0 -1
- package/lib/orm/Utilities.d.ts +0 -179
- package/lib/orm/Utilities.js +0 -975
- package/lib/orm/Validators.d.ts +0 -34
- package/lib/orm/Validators.js +0 -100
- package/lib/orm/Where/common.d.ts +0 -1
- package/lib/orm/Where/common.js +0 -7
- package/lib/orm/Where/filterDate.d.ts +0 -11
- package/lib/orm/Where/filterDate.js +0 -58
- package/lib/orm/index.d.ts +0 -68
- package/lib/orm/index.js +0 -157
- package/lib/orm-core/callback.d.ts +0 -12
- package/lib/orm-core/callback.js +0 -1
- package/lib/orm-core/dialect.js +0 -1
- package/lib/orm-core/error.d.ts +0 -16
- package/lib/orm-core/error.js +0 -29
- package/lib/orm-core/index.js +0 -6
- package/lib/orm-core/sync.d.ts +0 -12
- package/lib/orm-core/sync.js +0 -1
- package/lib/orm-core/utils.d.ts +0 -1
- package/lib/orm-core/utils.js +0 -5
- package/lib/orm-property/Property.d.ts +0 -78
- package/lib/orm-property/Property.js +0 -1
- package/lib/orm-property/Utils.d.ts +0 -9
- package/lib/orm-property/Utils.js +0 -13
- package/lib/orm-property/customTypes.d.ts +0 -2
- package/lib/orm-property/customTypes.js +0 -13
- package/lib/orm-property/index.d.ts +0 -10
- package/lib/orm-property/index.js +0 -22
- package/lib/orm-property/transformer.d.ts +0 -5
- package/lib/orm-property/transformer.js +0 -10
- package/lib/orm-property/transformers/dm.d.ts +0 -29
- package/lib/orm-property/transformers/index.d.ts +0 -4
- package/lib/orm-property/transformers/index.js +0 -5
- package/lib/orm-property/transformers/mysql.d.ts +0 -102
- package/lib/orm-property/transformers/mysql.js +0 -261
- package/lib/orm-property/transformers/postgresql.d.ts +0 -54
- package/lib/orm-property/transformers/postgresql.js +0 -189
- package/lib/orm-property/transformers/sqlite.d.ts +0 -19
- package/lib/orm-property/transformers/sqlite.js +0 -171
- package/lib/sql-ddl-sync/Dialects/dm.d.ts +0 -40
- package/lib/sql-ddl-sync/Dialects/dm.js +0 -296
- package/lib/sql-ddl-sync/Dialects/index.d.ts +0 -7
- package/lib/sql-ddl-sync/Dialects/index.js +0 -7
- package/lib/sql-ddl-sync/Dialects/mysql.d.ts +0 -39
- package/lib/sql-ddl-sync/Dialects/mysql.js +0 -240
- package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +0 -40
- package/lib/sql-ddl-sync/Dialects/postgresql.js +0 -286
- package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +0 -44
- package/lib/sql-ddl-sync/Dialects/sqlite.js +0 -256
- package/lib/sql-ddl-sync/SQL.d.ts +0 -12
- package/lib/sql-ddl-sync/SQL.js +0 -108
- package/lib/sql-ddl-sync/Typo/Collection.d.ts +0 -13
- package/lib/sql-ddl-sync/Typo/Collection.js +0 -1
- package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +0 -13
- package/lib/sql-ddl-sync/Typo/DbIndex.js +0 -1
- package/lib/sql-ddl-sync/Typo/Dialect.js +0 -1
- package/lib/sql-ddl-sync/Typo/Driver.d.ts +0 -53
- package/lib/sql-ddl-sync/Typo/Driver.js +0 -1
- package/lib/sql-ddl-sync/Typo/Query.d.ts +0 -7
- package/lib/sql-ddl-sync/Typo/Query.js +0 -1
- package/lib/sql-ddl-sync/Typo/SQL.d.ts +0 -31
- package/lib/sql-ddl-sync/Typo/SQL.js +0 -1
- package/lib/sql-ddl-sync/Typo/_common.d.ts +0 -29
- package/lib/sql-ddl-sync/Typo/_common.js +0 -1
- package/lib/sql-ddl-sync/Utils.d.ts +0 -40
- package/lib/sql-ddl-sync/Utils.js +0 -217
- package/lib/sql-ddl-sync/index.d.ts +0 -87
- package/lib/sql-ddl-sync/index.js +0 -371
- package/lib/sql-query/Comparators.d.ts +0 -3
- package/lib/sql-query/Comparators.js +0 -94
- package/lib/sql-query/Create.d.ts +0 -35
- package/lib/sql-query/Create.js +0 -77
- package/lib/sql-query/Dialects/dm.d.ts +0 -3
- package/lib/sql-query/Dialects/index.js +0 -12
- package/lib/sql-query/Dialects/mssql.d.ts +0 -3
- package/lib/sql-query/Dialects/mssql.js +0 -73
- package/lib/sql-query/Dialects/mysql.d.ts +0 -3
- package/lib/sql-query/Dialects/mysql.js +0 -100
- package/lib/sql-query/Dialects/postgresql.d.ts +0 -3
- package/lib/sql-query/Dialects/postgresql.js +0 -76
- package/lib/sql-query/Dialects/sqlite.d.ts +0 -3
- package/lib/sql-query/Dialects/sqlite.js +0 -61
- package/lib/sql-query/Helpers.d.ts +0 -38
- package/lib/sql-query/Helpers.js +0 -259
- package/lib/sql-query/Insert.d.ts +0 -12
- package/lib/sql-query/Insert.js +0 -33
- package/lib/sql-query/Query.d.ts +0 -29
- package/lib/sql-query/Query.js +0 -119
- package/lib/sql-query/Remove.d.ts +0 -16
- package/lib/sql-query/Remove.js +0 -93
- package/lib/sql-query/Select.d.ts +0 -62
- package/lib/sql-query/Select.js +0 -533
- package/lib/sql-query/Set.d.ts +0 -4
- package/lib/sql-query/Set.js +0 -16
- package/lib/sql-query/Typo/Aggregation.d.ts +0 -32
- package/lib/sql-query/Typo/Aggregation.js +0 -1
- package/lib/sql-query/Typo/Comparators.d.ts +0 -140
- package/lib/sql-query/Typo/Comparators.js +0 -1
- package/lib/sql-query/Typo/Dialect.d.ts +0 -39
- package/lib/sql-query/Typo/Dialect.js +0 -1
- package/lib/sql-query/Typo/Field.d.ts +0 -8
- package/lib/sql-query/Typo/Field.js +0 -1
- package/lib/sql-query/Typo/Helper.d.ts +0 -4
- package/lib/sql-query/Typo/Helper.js +0 -1
- package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +0 -80
- package/lib/sql-query/Typo/Query-ChainBuilder.js +0 -1
- package/lib/sql-query/Typo/Query.d.ts +0 -34
- package/lib/sql-query/Typo/Query.js +0 -16
- package/lib/sql-query/Typo/Sql.d.ts +0 -143
- package/lib/sql-query/Typo/Sql.js +0 -1
- package/lib/sql-query/Typo/SubQuery.d.ts +0 -39
- package/lib/sql-query/Typo/SubQuery.js +0 -1
- package/lib/sql-query/Typo/index.js +0 -1
- package/lib/sql-query/Update.d.ts +0 -15
- package/lib/sql-query/Update.js +0 -38
- package/lib/sql-query/Where.d.ts +0 -4
- package/lib/sql-query/Where.js +0 -280
- /package/{lib → src}/codepoints-map.json +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { FxOrmDMLDriver } from './Typo/DMLDriver';
|
|
2
|
+
|
|
3
|
+
import * as DMLs from "./Drivers/DML";
|
|
4
|
+
|
|
5
|
+
const adapters = {} as {
|
|
6
|
+
[key: string]: FxOrmDMLDriver.DMLDriverConstructor
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function addAdapter(name: string, constructor: FxOrmDMLDriver.DMLDriverConstructor) {
|
|
10
|
+
if (adapters.hasOwnProperty(name)) {
|
|
11
|
+
throw new Error(`[addAdapter] adapter '${name}' already exists`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Object.defineProperty(adapters, name, {
|
|
15
|
+
get () {
|
|
16
|
+
return constructor
|
|
17
|
+
},
|
|
18
|
+
configurable: false
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
addAdapter('mysql', DMLs.mysql.Driver);
|
|
23
|
+
addAdapter('sqlite', DMLs.sqlite.Driver);
|
|
24
|
+
addAdapter('postgres', DMLs.postgres.Driver);
|
|
25
|
+
addAdapter('dm', DMLs.dm.Driver);
|
|
26
|
+
addAdapter('mssql', DMLs.mssql.Driver);
|
|
27
|
+
|
|
28
|
+
export function getAdapter(name: string): FxOrmDMLDriver.DMLDriverConstructor {
|
|
29
|
+
switch (name) {
|
|
30
|
+
case 'mysql':
|
|
31
|
+
case 'sqlite':
|
|
32
|
+
break;
|
|
33
|
+
case 'pg':
|
|
34
|
+
case 'psql':
|
|
35
|
+
case 'postgres':
|
|
36
|
+
case 'postgresql':
|
|
37
|
+
case 'kingbase':
|
|
38
|
+
name = 'postgres';
|
|
39
|
+
break;
|
|
40
|
+
case 'dm':
|
|
41
|
+
case 'mssql':
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// TODO: support install dml driver from node_modules
|
|
45
|
+
if (!(name in adapters)) {
|
|
46
|
+
// trigger error which could be detected by `isDriverNotSupportedError`
|
|
47
|
+
const err = new Error(`No adapter named '${name}' found.`);
|
|
48
|
+
(err as any).code = 'MODULE_NOT_FOUND';
|
|
49
|
+
throw err;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return adapters[name];
|
|
53
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import ORMError from './Error';
|
|
2
|
+
|
|
3
|
+
import type { FxOrmDb } from "./Typo/Db";
|
|
4
|
+
import type { FxOrmQuery } from "./Typo/query";
|
|
5
|
+
import type { FxOrmCommon } from "./Typo/_common";
|
|
6
|
+
import * as Utilities from './Utilities';
|
|
7
|
+
|
|
8
|
+
const AggregateFunctions = function (
|
|
9
|
+
this: FxOrmQuery.IAggregated,
|
|
10
|
+
opts: FxOrmQuery.AggregateConstructorOptions
|
|
11
|
+
) {
|
|
12
|
+
if (typeof opts.driver.getQuery !== "function") {
|
|
13
|
+
throw new ORMError("This driver does not support aggregate functions", 'NO_SUPPORT');
|
|
14
|
+
}
|
|
15
|
+
if (!Array.isArray(opts.driver.aggregate_functions)) {
|
|
16
|
+
throw new ORMError("This driver does not support aggregate functions", 'NO_SUPPORT');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const aggregates: FxOrmQuery.SqlSelectFieldsDescriptor[][] = [ [] ];
|
|
20
|
+
let group_by: string[] = null as any;
|
|
21
|
+
let used_distinct = false;
|
|
22
|
+
|
|
23
|
+
const aggregationFunc = function (fun: string) {
|
|
24
|
+
fun = fun.toLocaleLowerCase()
|
|
25
|
+
|
|
26
|
+
return function () {
|
|
27
|
+
// select 1st array as args
|
|
28
|
+
var args: string[] = (arguments.length && Array.isArray(arguments[0]) ? arguments[0] : Array.prototype.slice.apply(arguments));
|
|
29
|
+
|
|
30
|
+
if (args.length > 0) {
|
|
31
|
+
aggregates[aggregates.length - 1].push({ func_name: fun, args: args, as: aggregateAlias(fun, args) });
|
|
32
|
+
// aggregates.push([]);
|
|
33
|
+
} else {
|
|
34
|
+
aggregates[aggregates.length - 1].push({ func_name: fun, as: aggregateAlias(fun, args) });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (fun === "distinct") {
|
|
38
|
+
used_distinct = true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return proto;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const distinct_to_one_row = function () {
|
|
46
|
+
return used_distinct && aggregates.length === 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const proto: FxOrmQuery.IAggregated = {
|
|
50
|
+
groupBy: function (...columns: string[]) {
|
|
51
|
+
group_by = columns;
|
|
52
|
+
return this;
|
|
53
|
+
},
|
|
54
|
+
limit: function (offset: number, limit?: number) {
|
|
55
|
+
if (typeof limit === "number") {
|
|
56
|
+
opts.limit = [ offset, limit ];
|
|
57
|
+
} else {
|
|
58
|
+
opts.limit = [ 0, offset ]; // offset = limit
|
|
59
|
+
}
|
|
60
|
+
return this;
|
|
61
|
+
},
|
|
62
|
+
order: function (...orders: FxOrmQuery.OrderSeqRawTuple) {
|
|
63
|
+
if (!Array.isArray(opts.order)) {
|
|
64
|
+
opts.order = [];
|
|
65
|
+
}
|
|
66
|
+
opts.order = Utilities.standardizeOrder(orders);
|
|
67
|
+
return this;
|
|
68
|
+
},
|
|
69
|
+
select: function (...columns: (string|string[])[]) {
|
|
70
|
+
if (columns.length === 0) {
|
|
71
|
+
throw new ORMError("When using append you must at least define one property", 'PARAM_MISMATCH');
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(columns[0])) {
|
|
74
|
+
opts.propertyList = (opts.propertyList || []).concat(columns[0] as string[]);
|
|
75
|
+
} else {
|
|
76
|
+
opts.propertyList = (opts.propertyList || []).concat(Array.prototype.slice.apply(arguments));
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
},
|
|
80
|
+
as: function (alias) {
|
|
81
|
+
if (!aggregates.length || (aggregates.length === 1 && aggregates[0].length === 0)) {
|
|
82
|
+
throw new ORMError("No aggregate functions defined yet", 'PARAM_MISMATCH');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
var len = aggregates.length;
|
|
86
|
+
|
|
87
|
+
aggregates[len - 1][
|
|
88
|
+
aggregates[len - 1].length - 1
|
|
89
|
+
].as = alias;
|
|
90
|
+
|
|
91
|
+
return this;
|
|
92
|
+
},
|
|
93
|
+
call: function (fun, args) {
|
|
94
|
+
if (args && args.length > 0) {
|
|
95
|
+
aggregates[aggregates.length - 1].push({ func_name: fun, args: args, as: aggregateAlias(fun, args) });
|
|
96
|
+
} else {
|
|
97
|
+
aggregates[aggregates.length - 1].push({ func_name: fun, as: aggregateAlias(fun, args) });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (fun.toLowerCase() === "distinct") {
|
|
101
|
+
used_distinct = true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return this;
|
|
105
|
+
},
|
|
106
|
+
getSync: function <T>() {
|
|
107
|
+
if (aggregates[aggregates.length - 1].length === 0) {
|
|
108
|
+
aggregates.length -= 1;
|
|
109
|
+
}
|
|
110
|
+
if (!aggregates.length) {
|
|
111
|
+
throw new ORMError("Missing aggregate functions", 'PARAM_MISMATCH');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const query = opts.driver.getQuery()
|
|
115
|
+
.select()
|
|
116
|
+
.from(opts.table! as string)
|
|
117
|
+
.select((opts.propertyList || []) as any);
|
|
118
|
+
|
|
119
|
+
for (let i = 0; i < aggregates.length; i++) {
|
|
120
|
+
for (let j = 0; j < aggregates[i].length; j++) {
|
|
121
|
+
query.fun(aggregates[i][j].func_name as any, aggregates[i][j].args, aggregates[i][j].as as string);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
query.where(opts.conditions || {});
|
|
126
|
+
|
|
127
|
+
if (group_by !== null) {
|
|
128
|
+
query.groupBy.apply(query, group_by);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (opts.order) {
|
|
132
|
+
for (let i = 0; i < opts.order.length; i++) {
|
|
133
|
+
query.order(opts.order[i][0], opts.order[i][1]);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (opts.limit) {
|
|
137
|
+
query.offset(opts.limit[0]).limit(opts.limit[1]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const q_str = query.build();
|
|
141
|
+
|
|
142
|
+
const data: any = (opts.driver.execQuery as any)(q_str);
|
|
143
|
+
|
|
144
|
+
if (group_by !== null) {
|
|
145
|
+
return data;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
var items = [];
|
|
149
|
+
|
|
150
|
+
if (distinct_to_one_row()) {
|
|
151
|
+
for (let i = 0; i < data.length; i++) {
|
|
152
|
+
items.push(
|
|
153
|
+
data[i][
|
|
154
|
+
Object.keys(data[i]).pop() as string
|
|
155
|
+
]
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return items;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (let i = 0; i < aggregates.length; i++) {
|
|
163
|
+
for (let j = 0; j < aggregates[i].length; j++) {
|
|
164
|
+
items.push(
|
|
165
|
+
pickAggregateValue(data[0], aggregates[i][j].as as string)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return items;
|
|
171
|
+
},
|
|
172
|
+
get: function <T>(cb?: FxOrmCommon.ExecutionCallback<T[]>) {
|
|
173
|
+
if (typeof cb !== "function") {
|
|
174
|
+
throw new ORMError("You must pass a callback to Model.aggregate().get()", 'MISSING_CALLBACK');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const syncReponse = Utilities.catchBlocking(proto.getSync, [], { thisArg: proto })
|
|
178
|
+
if (syncReponse.error instanceof ORMError && syncReponse.error.code === ORMError.codes['PARAM_MISMATCH']) {
|
|
179
|
+
throw syncReponse.error;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
Utilities.takeAwayResult(
|
|
183
|
+
syncReponse,
|
|
184
|
+
{
|
|
185
|
+
callback: cb && function (err, results) {
|
|
186
|
+
if (err)
|
|
187
|
+
return cb(err)
|
|
188
|
+
|
|
189
|
+
if (distinct_to_one_row())
|
|
190
|
+
return cb(null, results)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* when not used_distinct, support to spread results as callback's arguments
|
|
195
|
+
*/
|
|
196
|
+
results.unshift(null)
|
|
197
|
+
return cb.apply(null, results)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
opts.driver.aggregate_functions.forEach(aggregate_function => {
|
|
205
|
+
addAggregate(
|
|
206
|
+
proto,
|
|
207
|
+
aggregate_function as any,
|
|
208
|
+
aggregationFunc as any
|
|
209
|
+
);
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
return proto;
|
|
213
|
+
} as any as FxOrmQuery.AggregateConstructor;
|
|
214
|
+
|
|
215
|
+
export default AggregateFunctions
|
|
216
|
+
|
|
217
|
+
function addAggregate(
|
|
218
|
+
proto: FxOrmQuery.IAggregated,
|
|
219
|
+
fun: FxOrmDb.AGGREGATION_METHOD_COMPLEX | FxOrmDb.AGGREGATION_METHOD_TUPLE__COMMON,
|
|
220
|
+
builder: {
|
|
221
|
+
(fun: FxOrmQuery.KeyOfIAggregated): FxOrmQuery.AggregationMethod
|
|
222
|
+
}
|
|
223
|
+
) {
|
|
224
|
+
let k: FxOrmQuery.KeyOfIAggregated
|
|
225
|
+
if (Array.isArray(fun)) {
|
|
226
|
+
k = fun[0].toLowerCase()
|
|
227
|
+
proto[k] = builder( fun[1] || fun[0] );
|
|
228
|
+
} else {
|
|
229
|
+
k = fun.toLowerCase()
|
|
230
|
+
proto[k] = builder( fun );
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function aggregateAlias(fun: string, fields?: string[]) {
|
|
235
|
+
return fun + (fields && fields.length ? "_" + fields.join("_") : "");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* ODBC-based engines (psql / KingbaseES / MSSQL) lower-case an all-uppercase
|
|
240
|
+
* alias, while `aggregateAlias()` keeps the function name as written. Resolve
|
|
241
|
+
* the column case-insensitively so `.call('COUNT')` works on those drivers too.
|
|
242
|
+
*/
|
|
243
|
+
function pickAggregateValue(row: any, key: string) {
|
|
244
|
+
if (!row || typeof row !== 'object') return null;
|
|
245
|
+
|
|
246
|
+
if (Object.prototype.hasOwnProperty.call(row, key)) return row[key];
|
|
247
|
+
|
|
248
|
+
const lowered = key.toLowerCase();
|
|
249
|
+
if (Object.prototype.hasOwnProperty.call(row, lowered)) return row[lowered];
|
|
250
|
+
|
|
251
|
+
const matched = Object.keys(row).find(name => name.toLowerCase() === lowered);
|
|
252
|
+
|
|
253
|
+
return matched === undefined ? null : row[matched];
|
|
254
|
+
}
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
const util = require('util');
|
|
4
|
+
import * as Hook from '../Hook';
|
|
5
|
+
import cloneDeep from '../cloneDeep';
|
|
6
|
+
import ORMError from "../Error";
|
|
7
|
+
import * as Singleton from '../Singleton';
|
|
8
|
+
import * as Utilities from '../Utilities';
|
|
9
|
+
import * as Helpers from '../Helpers';
|
|
10
|
+
|
|
11
|
+
import { listFindByChainOrRunSync } from '../Model';
|
|
12
|
+
import { defineDefaultExtendsToTableName, defineAssociationAccessorMethodName, ACCESSOR_KEYS, addAssociationInfoToModel } from "./_utils";
|
|
13
|
+
|
|
14
|
+
import type { FxOrmInstance } from '../Typo/instance';
|
|
15
|
+
import type { FxOrmAssociation } from '../Typo/assoc';
|
|
16
|
+
import type { FxOrmError } from '../Typo/Error';
|
|
17
|
+
import type { FxOrmNS } from '../Typo/ORM';
|
|
18
|
+
import type { FxOrmModel } from '../Typo/model';
|
|
19
|
+
import type { FxOrmProperty } from '../Typo/property';
|
|
20
|
+
import type { FxOrmDMLDriver } from '../Typo/DMLDriver';
|
|
21
|
+
import type { FxOrmCommon } from '../Typo/_common';
|
|
22
|
+
import type { FxOrmQuery } from '../Typo/query';
|
|
23
|
+
|
|
24
|
+
function noOperation (...args: any[]) {};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param db orm instance
|
|
29
|
+
* @param Model model
|
|
30
|
+
* @param associations association definitions
|
|
31
|
+
*/
|
|
32
|
+
export function prepare (
|
|
33
|
+
Model: FxOrmModel.Model,
|
|
34
|
+
assocs: {
|
|
35
|
+
one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[],
|
|
36
|
+
many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[],
|
|
37
|
+
extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[],
|
|
38
|
+
},
|
|
39
|
+
opts: {
|
|
40
|
+
db: FxOrmNS.ORM
|
|
41
|
+
}
|
|
42
|
+
) {
|
|
43
|
+
const { extend_associations } = assocs;
|
|
44
|
+
const { db } = opts
|
|
45
|
+
|
|
46
|
+
Model.extendsTo = function (
|
|
47
|
+
name: string,
|
|
48
|
+
properties: Record<string, FxOrmModel.ModelPropertyDefinition>,
|
|
49
|
+
assoc_options?: FxOrmAssociation.AssociationDefinitionOptions_ExtendsTo
|
|
50
|
+
) {
|
|
51
|
+
Utilities.disAllowOpForVModel(Model, 'model.extendsTo');
|
|
52
|
+
|
|
53
|
+
assoc_options = assoc_options || {};
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i < db.plugins.length; i++) {
|
|
56
|
+
if (typeof db.plugins[i].beforeExtendsTo === "function") {
|
|
57
|
+
(db.plugins[i].beforeExtendsTo as (...args: any[]) => any)(Model, {
|
|
58
|
+
association_name: name,
|
|
59
|
+
properties: properties,
|
|
60
|
+
assoc_options
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const associationSemanticNameCore = assoc_options.name || Utilities.formatNameFor("assoc:extendsTo", name);
|
|
66
|
+
const association = {
|
|
67
|
+
name : name,
|
|
68
|
+
model : null,
|
|
69
|
+
table : assoc_options.table || defineDefaultExtendsToTableName(Model.table, name),
|
|
70
|
+
reverse : assoc_options.reverse,
|
|
71
|
+
// reversed : assoc_options.reversed,
|
|
72
|
+
autoFetch : assoc_options.autoFetch || false,
|
|
73
|
+
autoFetchLimit : assoc_options.autoFetchLimit || 2,
|
|
74
|
+
field : Utilities.wrapFieldObject({
|
|
75
|
+
field: assoc_options.field as any,
|
|
76
|
+
model: Model,
|
|
77
|
+
altName: Model.table
|
|
78
|
+
}) || Utilities.formatAssociatedField(
|
|
79
|
+
Model,
|
|
80
|
+
Model.table,
|
|
81
|
+
false,
|
|
82
|
+
false
|
|
83
|
+
),
|
|
84
|
+
|
|
85
|
+
getAccessor : assoc_options.getAccessor || defineAssociationAccessorMethodName(ACCESSOR_KEYS.get, associationSemanticNameCore),
|
|
86
|
+
setAccessor : assoc_options.setAccessor || defineAssociationAccessorMethodName(ACCESSOR_KEYS.set, associationSemanticNameCore),
|
|
87
|
+
hasAccessor : assoc_options.hasAccessor || defineAssociationAccessorMethodName(ACCESSOR_KEYS.has, associationSemanticNameCore),
|
|
88
|
+
delAccessor : assoc_options.delAccessor || defineAssociationAccessorMethodName(ACCESSOR_KEYS.del, associationSemanticNameCore),
|
|
89
|
+
modelFindByAccessor: assoc_options.modelFindByAccessor || defineAssociationAccessorMethodName(ACCESSOR_KEYS.modelFindBy, associationSemanticNameCore),
|
|
90
|
+
|
|
91
|
+
hooks: {...assoc_options.hooks},
|
|
92
|
+
} as unknown as FxOrmAssociation.InstanceAssociationItem_ExtendTos;
|
|
93
|
+
Utilities.fillSyncVersionAccessorForAssociation(association);
|
|
94
|
+
Utilities.addHookPatchHelperForAssociation(association);
|
|
95
|
+
|
|
96
|
+
const newProperties = cloneDeep(properties) as Record<string, FxOrmModel.ModelPropertyDefinition>;
|
|
97
|
+
const assoc_field = association.field as Record<string, FxOrmProperty.NormalizedProperty>
|
|
98
|
+
|
|
99
|
+
for (let k in assoc_field) {
|
|
100
|
+
assoc_field[k].klass = 'extendsTo';
|
|
101
|
+
if (assoc_field[k].type === 'serial') {
|
|
102
|
+
assoc_field[k].type = 'integer';
|
|
103
|
+
assoc_field[k].key = true;
|
|
104
|
+
}
|
|
105
|
+
newProperties[k] = assoc_field[k];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const modelOpts: FxOrmModel.ModelDefineOptions = util.extend(
|
|
109
|
+
util.pick(assoc_options, 'identityCache', 'autoSave', 'cascadeRemove', 'hooks', 'methods', 'validations'),
|
|
110
|
+
{
|
|
111
|
+
id : Object.keys(assoc_field),
|
|
112
|
+
__for_extension : true,
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
association.model = db.define(association.table, newProperties, modelOpts);
|
|
117
|
+
|
|
118
|
+
association.model.hasOne(association.reverse || Model.table, Model, {
|
|
119
|
+
__for_extension: true,
|
|
120
|
+
field: assoc_field,
|
|
121
|
+
reverse: null as any,
|
|
122
|
+
reversed: false,
|
|
123
|
+
hooks: assoc_options.reverseHooks
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
extend_associations.push(association);
|
|
127
|
+
|
|
128
|
+
const findByAccessorChainOrRunSync = function (is_sync: boolean = false) {
|
|
129
|
+
return function () {
|
|
130
|
+
var cb: FxOrmModel.ModelMethodCallback__Find | null = null,
|
|
131
|
+
conditions: FxOrmQuery.QueryConditions__Find | null = null,
|
|
132
|
+
options: FxOrmAssociation.ModelAssociationMethod__FindOptions = {};
|
|
133
|
+
|
|
134
|
+
const args = Array.prototype.slice.apply(arguments)
|
|
135
|
+
Helpers.selectArgs(args, (arg_type, arg) => {
|
|
136
|
+
switch (arg_type) {
|
|
137
|
+
case "function":
|
|
138
|
+
cb = arg;
|
|
139
|
+
break;
|
|
140
|
+
case "object":
|
|
141
|
+
if (conditions === null) {
|
|
142
|
+
conditions = arg;
|
|
143
|
+
} else {
|
|
144
|
+
options = arg;
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (conditions === null) {
|
|
151
|
+
throw new ORMError(`.${association.modelFindByAccessor}() is missing a conditions object`, 'PARAM_MISMATCH');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return listFindByChainOrRunSync(Model,
|
|
155
|
+
{},
|
|
156
|
+
[
|
|
157
|
+
{
|
|
158
|
+
association_name: association.name,
|
|
159
|
+
conditions: conditions!
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
options,
|
|
163
|
+
{ callback: cb || undefined, is_sync }
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
Model[association.modelFindByAccessor!] = findByAccessorChainOrRunSync();
|
|
169
|
+
Model[association.modelFindBySyncAccessor!] = findByAccessorChainOrRunSync(true);
|
|
170
|
+
|
|
171
|
+
addAssociationInfoToModel(Model, name, {
|
|
172
|
+
type: 'extendsTo',
|
|
173
|
+
association: association
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
return association.model;
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export function extend (
|
|
181
|
+
Model: FxOrmModel.Model,
|
|
182
|
+
Instance: FxOrmInstance.Instance,
|
|
183
|
+
Driver: FxOrmDMLDriver.DMLDriver,
|
|
184
|
+
associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[],
|
|
185
|
+
cfg: {
|
|
186
|
+
assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasOne,
|
|
187
|
+
}
|
|
188
|
+
) {
|
|
189
|
+
for (let i = 0; i < associations.length; i++) {
|
|
190
|
+
extendInstance(Model, Instance, Driver, associations[i], cfg);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export function autoFetch (
|
|
195
|
+
Instance: FxOrmInstance.Instance,
|
|
196
|
+
associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[],
|
|
197
|
+
opts: FxOrmNS.ModelAutoFetchOptions,
|
|
198
|
+
parallel: boolean = false
|
|
199
|
+
) {
|
|
200
|
+
if (associations.length === 0) {
|
|
201
|
+
return ;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
Utilities.parallelQueryIfPossible(
|
|
205
|
+
parallel,
|
|
206
|
+
associations,
|
|
207
|
+
(item) => autoFetchInstance(Instance, item, opts)
|
|
208
|
+
)
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
function extendInstance(
|
|
212
|
+
Model: FxOrmModel.Model,
|
|
213
|
+
Instance: FxOrmInstance.Instance,
|
|
214
|
+
Driver: FxOrmDMLDriver.DMLDriver,
|
|
215
|
+
association: FxOrmAssociation.InstanceAssociationItem_ExtendTos,
|
|
216
|
+
cfg: {
|
|
217
|
+
assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasOne
|
|
218
|
+
}
|
|
219
|
+
) {
|
|
220
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.hasSyncAccessor, function () {
|
|
221
|
+
if (!Instance[Model.id + ''])
|
|
222
|
+
throw new ORMError("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table });
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
return !!association.model.getSync(Utilities.values(Instance, Model.id));
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (!Model.settings.get('extendsTo.throwWhenNotFound') && (error as FxOrmError.ExtendedError).literalCode === 'NOT_FOUND') return false
|
|
228
|
+
throw error
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.hasAccessor, function (this: any, cb: FxOrmCommon.GenericCallback<boolean>) {
|
|
233
|
+
process.nextTick(() => {
|
|
234
|
+
const syncResponse = Utilities.catchBlocking<boolean>(Instance[association.hasSyncAccessor]);
|
|
235
|
+
syncResponse.result = !!syncResponse.result;
|
|
236
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb })
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
return this;
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.getSyncAccessor, function (opts: FxOrmModel.ModelOptions__Get = {}) {
|
|
243
|
+
if (!Instance[Model.id + ''])
|
|
244
|
+
throw new ORMError("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table });
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
return association.model.getSync(Utilities.values(Instance, Model.id), opts);
|
|
248
|
+
} catch (error) {
|
|
249
|
+
if (!Model.settings.get('extendsTo.throwWhenNotFound') && (error as FxOrmError.ExtendedError).literalCode === 'NOT_FOUND') return null
|
|
250
|
+
throw error
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.getAccessor, function (this: any) {
|
|
255
|
+
let opts: FxOrmModel.ModelOptions__Get = {};
|
|
256
|
+
let cb: FxOrmCommon.ExecutionCallback<FxOrmInstance.Instance>
|
|
257
|
+
|
|
258
|
+
const args = Array.prototype.slice.apply(arguments)
|
|
259
|
+
Helpers.selectArgs(args, (arg_type, arg) => {
|
|
260
|
+
switch (arg_type) {
|
|
261
|
+
case "function":
|
|
262
|
+
cb = arg;
|
|
263
|
+
break;
|
|
264
|
+
case "object":
|
|
265
|
+
opts = arg;
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
process.nextTick(() => {
|
|
271
|
+
const syncResponse = Utilities.catchBlocking<FxOrmInstance.Instance>(Instance[association.getSyncAccessor], [ opts ]);
|
|
272
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb })
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
return this;
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.setSyncAccessor, function (
|
|
279
|
+
Extension: FxOrmInstance.Instance | FxOrmInstance.InstanceDataPayload
|
|
280
|
+
) {
|
|
281
|
+
const $ref = {
|
|
282
|
+
instance: Instance,
|
|
283
|
+
association: Extension,
|
|
284
|
+
useChannel: Utilities.reusableChannelGenerator()
|
|
285
|
+
} as Record<string, any>;
|
|
286
|
+
Hook.wait(
|
|
287
|
+
Instance,
|
|
288
|
+
association.hooks[`beforeSet`],
|
|
289
|
+
Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
|
|
290
|
+
let Extension = $ref.association
|
|
291
|
+
Instance.$emit(`before:set:${association.name}`, Extension);
|
|
292
|
+
|
|
293
|
+
Instance.saveSync();
|
|
294
|
+
|
|
295
|
+
Instance.$emit(`before-del-extension:${association.setAccessor}`);
|
|
296
|
+
Instance[association.delSyncAccessor]();
|
|
297
|
+
Instance.$emit(`after-del-extension:${association.setAccessor}`);
|
|
298
|
+
|
|
299
|
+
const fields = Object.keys(association.field);
|
|
300
|
+
|
|
301
|
+
if (!Extension.isInstance) {
|
|
302
|
+
$ref.association = Extension = new association.model(Extension);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
for (let i = 0; i < Model.id.length; i++) {
|
|
306
|
+
Extension[fields[i]] = Instance[Model.id[i]];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
Instance.$emit(`before-save-extension:${association.setAccessor}`, Extension);
|
|
310
|
+
Extension.saveSync();
|
|
311
|
+
Instance.$emit(`after-save-extension:${association.setAccessor}`, Extension);
|
|
312
|
+
|
|
313
|
+
Instance.$emit(`after:set:${association.name}`, Extension);
|
|
314
|
+
}),
|
|
315
|
+
Utilities.buildAssocHooksContext('beforeSet', { $ref })
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
Hook.trigger(Instance, association.hooks['afterSet'], Utilities.buildAssocHooksContext('afterSet', { $ref }));
|
|
319
|
+
|
|
320
|
+
return $ref.association;
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.setAccessor, function (this: any,
|
|
324
|
+
Extension: FxOrmInstance.Instance | FxOrmInstance.InstanceDataPayload,
|
|
325
|
+
cb: FxOrmCommon.ExecutionCallback<FxOrmInstance.Instance>
|
|
326
|
+
) {
|
|
327
|
+
process.nextTick(() => {
|
|
328
|
+
const syncResponse = Utilities.catchBlocking(Instance[association.setSyncAccessor], [ Extension ]);
|
|
329
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb })
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
return this;
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.delSyncAccessor, function () {
|
|
336
|
+
if (!Instance[Model.id + ''])
|
|
337
|
+
throw new ORMError("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table });
|
|
338
|
+
|
|
339
|
+
const conditions: {[k: string]: any} = {};
|
|
340
|
+
const fields = Object.keys(association.field);
|
|
341
|
+
|
|
342
|
+
for (let i = 0; i < Model.id.length; i++) {
|
|
343
|
+
conditions[fields[i]] = Instance[Model.id[i]];
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const $ref = {
|
|
347
|
+
instance: Instance,
|
|
348
|
+
removeConditions: conditions,
|
|
349
|
+
useChannel: Utilities.reusableChannelGenerator()
|
|
350
|
+
} as Record<string, any>;
|
|
351
|
+
Hook.wait(
|
|
352
|
+
Instance,
|
|
353
|
+
association.hooks[`beforeRemove`],
|
|
354
|
+
Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
|
|
355
|
+
const extensions = association.model.findSync($ref.removeConditions)
|
|
356
|
+
|
|
357
|
+
Instance.$emit(`before:del:${association.name}`, extensions);
|
|
358
|
+
for (let i = 0; i < extensions.length; i++) {
|
|
359
|
+
Singleton.clear(extensions[i].__singleton_uid() + '');
|
|
360
|
+
extensions[i].removeSync();
|
|
361
|
+
}
|
|
362
|
+
Instance.$emit(`after:del:${association.name}`, extensions);
|
|
363
|
+
}),
|
|
364
|
+
Utilities.buildAssocHooksContext('beforeRemove', { $ref })
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
Hook.trigger(Instance, association.hooks['afterRemove'], Utilities.buildAssocHooksContext('afterRemove', { $ref }));
|
|
368
|
+
|
|
369
|
+
return ;
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
Utilities.addHiddenPropertyToInstance(Instance, association.delAccessor, function (this: any, cb: FxOrmCommon.ExecutionCallback<FxOrmInstance.Instance, FxOrmInstance.Instance>) {
|
|
373
|
+
process.nextTick(() => {
|
|
374
|
+
const syncResponse = Utilities.catchBlocking<FxOrmInstance.Instance>(Instance[association.delSyncAccessor]);
|
|
375
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb })
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
return this;
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function autoFetchInstance(
|
|
383
|
+
Instance: FxOrmInstance.Instance,
|
|
384
|
+
association: FxOrmAssociation.InstanceAssociationItem_ExtendTos,
|
|
385
|
+
opts: FxOrmNS.InstanceAutoFetchOptions,
|
|
386
|
+
) {
|
|
387
|
+
if (!Instance.saved())
|
|
388
|
+
return ;
|
|
389
|
+
|
|
390
|
+
if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
|
|
391
|
+
opts.autoFetchLimit = association.autoFetchLimit;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch))
|
|
395
|
+
return ;
|
|
396
|
+
|
|
397
|
+
if (Instance.isPersisted()) {
|
|
398
|
+
try {
|
|
399
|
+
const Assoc = Instance[association.getSyncAccessor]({ autoFetchLimit: opts.autoFetchLimit - 1 })
|
|
400
|
+
Instance[association.name] = Assoc;
|
|
401
|
+
} catch (err) {}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return ;
|
|
405
|
+
}
|