fib-orm 2.1.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 +37 -34
- package/{lib/cli/commands/ddlFrom.js → src/cli/commands/ddlFrom.ts} +40 -27
- package/{lib/cli/commands/dumpModel.js → src/cli/commands/dumpModel.ts} +78 -32
- package/{lib/cli/commands/escape.js → src/cli/commands/escape.ts} +51 -29
- package/{lib/cli/commands/spinner.js → src/cli/commands/spinner.ts} +20 -11
- package/{lib/cli/commands/upgrade.js → src/cli/commands/upgrade.ts} +53 -34
- 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 -86
- 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 -174
- 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 -69
- 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 -22
- 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 -2
- 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 -25
- package/lib/cli/helpers/package.js +0 -56
- package/lib/cli/index.d.ts +0 -2
- package/lib/cli/index.js +0 -19
- package/lib/db-driver/Typo.d.ts +0 -111
- package/lib/db-driver/Typo.js +0 -2
- package/lib/db-driver/built-ins/base.class.d.ts +0 -91
- package/lib/db-driver/built-ins/base.class.js +0 -187
- package/lib/db-driver/built-ins/driver-dm.d.ts +0 -17
- package/lib/db-driver/built-ins/driver-dm.js +0 -54
- package/lib/db-driver/built-ins/driver-mongodb.d.ts +0 -23
- package/lib/db-driver/built-ins/driver-mongodb.js +0 -47
- package/lib/db-driver/built-ins/driver-mysql.d.ts +0 -17
- package/lib/db-driver/built-ins/driver-mysql.js +0 -38
- package/lib/db-driver/built-ins/driver-postgresql.d.ts +0 -21
- package/lib/db-driver/built-ins/driver-postgresql.js +0 -77
- package/lib/db-driver/built-ins/driver-redis.d.ts +0 -11
- package/lib/db-driver/built-ins/driver-redis.js +0 -36
- package/lib/db-driver/built-ins/driver-sqlite.d.ts +0 -16
- package/lib/db-driver/built-ins/driver-sqlite.js +0 -35
- package/lib/db-driver/index.d.ts +0 -3
- package/lib/db-driver/index.js +0 -5
- 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 -4
- package/lib/knex/sandbox.d.ts +0 -4
- package/lib/knex/sandbox.js +0 -23
- 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 -177
- 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 -16
- 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 -14
- package/lib/orm/Associations/_utils.js +0 -53
- package/lib/orm/ChainFind.d.ts +0 -3
- package/lib/orm/ChainFind.js +0 -328
- package/lib/orm/ChainInstance.d.ts +0 -6
- package/lib/orm/ChainInstance.js +0 -74
- package/lib/orm/Debug.d.ts +0 -2
- package/lib/orm/Debug.js +0 -17
- package/lib/orm/Drivers/DB/base.d.ts +0 -2
- package/lib/orm/Drivers/DB/base.js +0 -4
- package/lib/orm/Drivers/DB/dm.d.ts +0 -9
- package/lib/orm/Drivers/DB/dm.js +0 -24
- package/lib/orm/Drivers/DB/mysql.d.ts +0 -9
- package/lib/orm/Drivers/DB/mysql.js +0 -24
- package/lib/orm/Drivers/DB/postgres.d.ts +0 -9
- package/lib/orm/Drivers/DB/postgres.js +0 -23
- package/lib/orm/Drivers/DB/sqlite3.d.ts +0 -14
- package/lib/orm/Drivers/DB/sqlite3.js +0 -40
- package/lib/orm/Drivers/DML/_ddl-sql.d.ts +0 -3
- package/lib/orm/Drivers/DML/_ddl-sql.js +0 -52
- 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 -39
- 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 -3
- 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 -7
- package/lib/orm/Drivers/DML/mysql.d.ts +0 -2
- package/lib/orm/Drivers/DML/mysql.js +0 -252
- package/lib/orm/Drivers/DML/postgres.d.ts +0 -2
- package/lib/orm/Drivers/DML/postgres.js +0 -277
- package/lib/orm/Drivers/DML/sqlite.d.ts +0 -2
- package/lib/orm/Drivers/DML/sqlite.js +0 -267
- package/lib/orm/Error.d.ts +0 -11
- package/lib/orm/Error.js +0 -41
- package/lib/orm/Helpers.d.ts +0 -35
- package/lib/orm/Helpers.js +0 -234
- package/lib/orm/Hook.d.ts +0 -13
- package/lib/orm/Hook.js +0 -100
- package/lib/orm/Instance.d.ts +0 -2
- package/lib/orm/Instance.js +0 -698
- package/lib/orm/LazyLoad.d.ts +0 -4
- package/lib/orm/LazyLoad.js +0 -76
- package/lib/orm/Model.d.ts +0 -6
- package/lib/orm/Model.js +0 -974
- package/lib/orm/ORM.d.ts +0 -37
- package/lib/orm/ORM.js +0 -242
- 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 -14
- 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 -120
- package/lib/orm/Singleton.d.ts +0 -5
- package/lib/orm/Singleton.js +0 -73
- package/lib/orm/Typo/Adapter.d.ts +0 -5
- package/lib/orm/Typo/Adapter.js +0 -2
- package/lib/orm/Typo/DMLDriver.d.ts +0 -209
- package/lib/orm/Typo/DMLDriver.js +0 -2
- package/lib/orm/Typo/Db.d.ts +0 -35
- package/lib/orm/Typo/Db.js +0 -2
- package/lib/orm/Typo/Error.d.ts +0 -29
- package/lib/orm/Typo/Error.js +0 -2
- package/lib/orm/Typo/ORM.d.ts +0 -231
- package/lib/orm/Typo/ORM.js +0 -4
- package/lib/orm/Typo/Validators.d.ts +0 -31
- package/lib/orm/Typo/Validators.js +0 -3
- package/lib/orm/Typo/_common.d.ts +0 -37
- package/lib/orm/Typo/_common.js +0 -2
- package/lib/orm/Typo/assoc.d.ts +0 -221
- package/lib/orm/Typo/assoc.js +0 -2
- package/lib/orm/Typo/hook.d.ts +0 -42
- package/lib/orm/Typo/hook.js +0 -2
- package/lib/orm/Typo/instance.d.ts +0 -156
- package/lib/orm/Typo/instance.js +0 -2
- package/lib/orm/Typo/model.js +0 -7
- package/lib/orm/Typo/patch.d.ts +0 -31
- package/lib/orm/Typo/patch.js +0 -2
- package/lib/orm/Typo/property.d.ts +0 -57
- package/lib/orm/Typo/property.js +0 -2
- package/lib/orm/Typo/query.d.ts +0 -216
- package/lib/orm/Typo/query.js +0 -2
- package/lib/orm/Typo/settings.js +0 -6
- package/lib/orm/Typo/synchronous.js +0 -2
- package/lib/orm/Utilities.d.ts +0 -178
- package/lib/orm/Utilities.js +0 -975
- package/lib/orm/Validators.d.ts +0 -33
- package/lib/orm/Validators.js +0 -101
- 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 -67
- package/lib/orm/index.js +0 -158
- package/lib/orm-core/callback.d.ts +0 -12
- package/lib/orm-core/callback.js +0 -2
- package/lib/orm-core/dialect.js +0 -2
- package/lib/orm-core/error.d.ts +0 -16
- package/lib/orm-core/error.js +0 -29
- package/lib/orm-core/index.js +0 -7
- package/lib/orm-core/sync.d.ts +0 -12
- package/lib/orm-core/sync.js +0 -2
- package/lib/orm-core/utils.d.ts +0 -1
- package/lib/orm-core/utils.js +0 -6
- package/lib/orm-property/Property.d.ts +0 -78
- package/lib/orm-property/Property.js +0 -2
- 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 -23
- 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 -6
- package/lib/orm-property/transformers/mysql.d.ts +0 -101
- package/lib/orm-property/transformers/mysql.js +0 -262
- package/lib/orm-property/transformers/postgresql.d.ts +0 -54
- package/lib/orm-property/transformers/postgresql.js +0 -190
- package/lib/orm-property/transformers/sqlite.d.ts +0 -19
- package/lib/orm-property/transformers/sqlite.js +0 -172
- package/lib/sql-ddl-sync/Dialects/dm.d.ts +0 -39
- package/lib/sql-ddl-sync/Dialects/dm.js +0 -297
- package/lib/sql-ddl-sync/Dialects/index.d.ts +0 -6
- package/lib/sql-ddl-sync/Dialects/index.js +0 -8
- package/lib/sql-ddl-sync/Dialects/mysql.d.ts +0 -38
- package/lib/sql-ddl-sync/Dialects/mysql.js +0 -241
- package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +0 -39
- package/lib/sql-ddl-sync/Dialects/postgresql.js +0 -287
- package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +0 -43
- package/lib/sql-ddl-sync/Dialects/sqlite.js +0 -257
- 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 -2
- package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +0 -13
- package/lib/sql-ddl-sync/Typo/DbIndex.js +0 -2
- package/lib/sql-ddl-sync/Typo/Dialect.js +0 -2
- package/lib/sql-ddl-sync/Typo/Driver.d.ts +0 -53
- package/lib/sql-ddl-sync/Typo/Driver.js +0 -2
- package/lib/sql-ddl-sync/Typo/Query.d.ts +0 -7
- package/lib/sql-ddl-sync/Typo/Query.js +0 -2
- package/lib/sql-ddl-sync/Typo/SQL.d.ts +0 -31
- package/lib/sql-ddl-sync/Typo/SQL.js +0 -2
- package/lib/sql-ddl-sync/Typo/_common.d.ts +0 -29
- package/lib/sql-ddl-sync/Typo/_common.js +0 -2
- package/lib/sql-ddl-sync/Utils.d.ts +0 -39
- 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 -372
- package/lib/sql-query/Comparators.d.ts +0 -3
- package/lib/sql-query/Comparators.js +0 -95
- package/lib/sql-query/Create.d.ts +0 -35
- package/lib/sql-query/Create.js +0 -78
- package/lib/sql-query/Dialects/dm.d.ts +0 -3
- package/lib/sql-query/Dialects/index.js +0 -13
- package/lib/sql-query/Dialects/mssql.d.ts +0 -3
- package/lib/sql-query/Dialects/mssql.js +0 -74
- package/lib/sql-query/Dialects/mysql.d.ts +0 -3
- package/lib/sql-query/Dialects/mysql.js +0 -101
- package/lib/sql-query/Dialects/postgresql.d.ts +0 -3
- package/lib/sql-query/Dialects/postgresql.js +0 -77
- package/lib/sql-query/Dialects/sqlite.d.ts +0 -3
- package/lib/sql-query/Dialects/sqlite.js +0 -62
- package/lib/sql-query/Helpers.d.ts +0 -37
- package/lib/sql-query/Helpers.js +0 -260
- package/lib/sql-query/Insert.d.ts +0 -12
- package/lib/sql-query/Insert.js +0 -34
- package/lib/sql-query/Query.d.ts +0 -28
- package/lib/sql-query/Query.js +0 -120
- package/lib/sql-query/Remove.d.ts +0 -16
- package/lib/sql-query/Remove.js +0 -94
- package/lib/sql-query/Select.d.ts +0 -62
- package/lib/sql-query/Select.js +0 -534
- 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 -2
- package/lib/sql-query/Typo/Comparators.d.ts +0 -140
- package/lib/sql-query/Typo/Comparators.js +0 -2
- package/lib/sql-query/Typo/Dialect.d.ts +0 -39
- package/lib/sql-query/Typo/Dialect.js +0 -2
- package/lib/sql-query/Typo/Field.d.ts +0 -8
- package/lib/sql-query/Typo/Field.js +0 -2
- package/lib/sql-query/Typo/Helper.d.ts +0 -4
- package/lib/sql-query/Typo/Helper.js +0 -2
- package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +0 -80
- package/lib/sql-query/Typo/Query-ChainBuilder.js +0 -2
- package/lib/sql-query/Typo/Query.d.ts +0 -34
- package/lib/sql-query/Typo/Query.js +0 -17
- package/lib/sql-query/Typo/Sql.d.ts +0 -143
- package/lib/sql-query/Typo/Sql.js +0 -2
- package/lib/sql-query/Typo/SubQuery.d.ts +0 -39
- package/lib/sql-query/Typo/SubQuery.js +0 -2
- package/lib/sql-query/Typo/index.js +0 -2
- package/lib/sql-query/Update.d.ts +0 -15
- package/lib/sql-query/Update.js +0 -39
- 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,1313 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
const util = require('util');
|
|
4
|
+
const uuid = require('uuid');
|
|
5
|
+
const coroutine = require('coroutine');
|
|
6
|
+
const events = require("events");
|
|
7
|
+
|
|
8
|
+
import cloneDeep from './cloneDeep';
|
|
9
|
+
import * as FxORMCore from '../orm-core/index';
|
|
10
|
+
import {Helpers as QueryHelpers, type FxSqlQuery, type FxSqlQuerySubQuery, type FxSqlQueryComparator, type FxSqlQuerySql, type FxSqlQueryDialect} from '../sql-query/Query';
|
|
11
|
+
import { selectArgs } from './Helpers';
|
|
12
|
+
|
|
13
|
+
import type { FxOrmInstance } from './Typo/instance';
|
|
14
|
+
import type { FxOrmModel } from './Typo/model';
|
|
15
|
+
import type { FxOrmQuery } from './Typo/query';
|
|
16
|
+
import type { FxOrmAssociation } from './Typo/assoc';
|
|
17
|
+
import type { FxOrmProperty } from './Typo/property';
|
|
18
|
+
import type { FxOrmCommon } from './Typo/_common';
|
|
19
|
+
import type { FxOrmNS } from './Typo/ORM';
|
|
20
|
+
import type { FxOrmError } from './Typo/Error';
|
|
21
|
+
import type { FxOrmHook } from './Typo/hook';
|
|
22
|
+
import { filterDate } from './Where/filterDate';
|
|
23
|
+
import type { FxOrmDMLDriver } from './Typo/DMLDriver';
|
|
24
|
+
import ORMError from './Error';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Order should be a String (with the property name assumed ascending)
|
|
28
|
+
* or an Array or property String names.
|
|
29
|
+
*
|
|
30
|
+
* Examples:
|
|
31
|
+
*
|
|
32
|
+
* 1. 'property1' (ORDER BY property1 ASC)
|
|
33
|
+
* 2. '-property1' (ORDER BY property1 DESC)
|
|
34
|
+
* 3. [ 'property1' ] (ORDER BY property1 ASC)
|
|
35
|
+
* 4. [ '-property1' ] (ORDER BY property1 DESC)
|
|
36
|
+
* 5. [ 'property1', 'A' ] (ORDER BY property1 ASC)
|
|
37
|
+
* 6. [ 'property1', 'Z' ] (ORDER BY property1 DESC)
|
|
38
|
+
* 7. [ '-property1', 'A' ] (ORDER BY property1 ASC)
|
|
39
|
+
* 8. [ 'property1', 'property2' ] (ORDER BY property1 ASC, property2 ASC)
|
|
40
|
+
* 9. [ 'property1', '-property2' ] (ORDER BY property1 ASC, property2 DESC)
|
|
41
|
+
* ...
|
|
42
|
+
*/
|
|
43
|
+
type IOrderFlag = FxSqlQuery.OrderNormalizedTuple[1];
|
|
44
|
+
const ZIS = [ "A", "Z" ] as IOrderFlag[]
|
|
45
|
+
export function standardizeOrder (
|
|
46
|
+
order: FxOrmModel.ModelOptions__Find['order']
|
|
47
|
+
): FxOrmQuery.OrderNormalizedTupleMixin {
|
|
48
|
+
if (typeof order === "string") {
|
|
49
|
+
let item: FxOrmQuery.OrderNormalizedTupleWithoutTable = [ order.substr(1), "Z" ];
|
|
50
|
+
if (order[0] === "-") {
|
|
51
|
+
return [ item ];
|
|
52
|
+
}
|
|
53
|
+
item = [ order, "A" ];
|
|
54
|
+
return [ item ];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// maybe `FxSqlQuery.OrderNormalizedResult`
|
|
58
|
+
if (
|
|
59
|
+
Array.isArray(order)
|
|
60
|
+
&& (Array.isArray(order[1]) || Array.isArray(order[0]))
|
|
61
|
+
) {
|
|
62
|
+
// is `FxSqlQuery.OrderSqlStyleTuple`
|
|
63
|
+
if (Array.isArray(order[1]))
|
|
64
|
+
return [order] as FxSqlQuery.OrderSqlStyleTuple[];
|
|
65
|
+
|
|
66
|
+
// is `FxSqlQuery.OrderNormalizedTuple`
|
|
67
|
+
return [order] as FxSqlQuery.OrderNormalizedTuple[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const new_order: FxOrmQuery.OrderNormalizedTupleWithoutTable[] = [];
|
|
71
|
+
let minus: boolean;
|
|
72
|
+
|
|
73
|
+
for (let i = 0, item: any; i < order!.length; i++) {
|
|
74
|
+
item = order![i];
|
|
75
|
+
|
|
76
|
+
// deprecate all non-string item
|
|
77
|
+
if (typeof item !== 'string') continue ;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* order from here would not be add table name afterwards
|
|
81
|
+
*/
|
|
82
|
+
minus = (item[0] === "-");
|
|
83
|
+
|
|
84
|
+
const next_one = order![i + 1]
|
|
85
|
+
const maybe_Z = typeof next_one === 'string' ? next_one.toUpperCase() as FxSqlQuery.OrderNormalizedTuple[1] : 'Z'
|
|
86
|
+
if (i < order!.length - 1 && ZIS.indexOf(maybe_Z) >= 0) {
|
|
87
|
+
new_order.push([
|
|
88
|
+
(minus ? item.substr(1) : item),
|
|
89
|
+
maybe_Z
|
|
90
|
+
]);
|
|
91
|
+
i += 1;
|
|
92
|
+
} else if (minus) {
|
|
93
|
+
new_order.push([ item.substr(1), "Z" ]);
|
|
94
|
+
} else {
|
|
95
|
+
new_order.push([ item, "A" ]);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return new_order;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export function addTableToStandardedOrder (
|
|
103
|
+
order: FxOrmQuery.OrderNormalizedTupleMixin,
|
|
104
|
+
table_alias: string | FxSqlQuerySql.SqlFromTableInput
|
|
105
|
+
): FxOrmQuery.ChainFindOptions['order'] {
|
|
106
|
+
const new_order: FxOrmQuery.ChainFindOptions['order'] = []
|
|
107
|
+
for (let i = 0, item: any; i < order!.length; i++) {
|
|
108
|
+
item = order[i]
|
|
109
|
+
// strange here, we support item[0] here as `FxOrmQuery.OrderNormalizedTuple` :)
|
|
110
|
+
if (Array.isArray(item[0])) {
|
|
111
|
+
new_order.push(item)
|
|
112
|
+
continue ;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
new_order.push([
|
|
116
|
+
[table_alias, item[0]],
|
|
117
|
+
item[1]
|
|
118
|
+
] as FxOrmQuery.OrderNormalizedTuple)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return new_order
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @description filtered out FxOrmInstance.Instance in mixed FxSqlQuerySubQuery.SubQueryConditions | { [k: string]: FxOrmInstance.Instance }
|
|
126
|
+
*/
|
|
127
|
+
export function checkConditions (
|
|
128
|
+
conditions: ( FxSqlQuerySubQuery.SubQueryConditions | { [k: string]: FxOrmInstance.Instance } ),
|
|
129
|
+
one_associations: ( FxOrmAssociation.InstanceAssociatedInstance | FxOrmAssociation.InstanceAssociationItem_HasOne )[]
|
|
130
|
+
): FxSqlQuerySubQuery.SubQueryConditions {
|
|
131
|
+
// A) Build an index of associations, with their name as the key
|
|
132
|
+
var associations = {} as {[k: string]: FxOrmAssociation.InstanceAssociatedInstance | FxOrmAssociation.InstanceAssociationItem_HasOne};
|
|
133
|
+
for (let i = 0; i < one_associations.length; i++) {
|
|
134
|
+
associations[one_associations[i].name] = one_associations[i];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (let k in conditions) {
|
|
138
|
+
// B) Check for any conditions with a key in the association index
|
|
139
|
+
if (!associations.hasOwnProperty(k)) continue;
|
|
140
|
+
|
|
141
|
+
// C) Ensure that our condition supports array values
|
|
142
|
+
var values = conditions[k] as (FxOrmAssociation.InstanceAssociatedInstance)[];
|
|
143
|
+
if (!Array.isArray(values))
|
|
144
|
+
values = [values] as any;
|
|
145
|
+
|
|
146
|
+
// D) Remove original condition (it's instance rather conditon, we would replace it later; not DB compatible)
|
|
147
|
+
delete conditions[k];
|
|
148
|
+
|
|
149
|
+
// E) Convert our association fields into an array, indexes are the same as model.id
|
|
150
|
+
const association_fields = Object.keys(associations[k].field);
|
|
151
|
+
const model: FxOrmModel.Model = (associations[k] as FxOrmAssociation.InstanceAssociationItem_HasOne).model;
|
|
152
|
+
|
|
153
|
+
// F) Iterate through values for the condition, only accept instances of the same type as the association
|
|
154
|
+
for (let i = 0; i < values.length; i++) {
|
|
155
|
+
const instance = (values[i].isInstance && values[i] as FxOrmAssociation.InstanceAssociatedInstance)
|
|
156
|
+
if (instance && instance.model().uid === model.uid) {
|
|
157
|
+
if (association_fields.length === 1) {
|
|
158
|
+
const cond_k = association_fields[0]
|
|
159
|
+
|
|
160
|
+
if (conditions[cond_k] === undefined) {
|
|
161
|
+
conditions[cond_k] = instance[model.id[0]];
|
|
162
|
+
} else if(Array.isArray(conditions[cond_k])) {
|
|
163
|
+
(conditions[cond_k] as FxSqlQueryComparator.SubQueryInput[]).push(instance[model.id[0]]);
|
|
164
|
+
} else {
|
|
165
|
+
conditions[cond_k] = [conditions[cond_k], instance[model.id[0]]];
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
var _conds = {} as FxSqlQueryComparator.SubQueryInput;
|
|
169
|
+
for (let j = 0; j < association_fields.length; i++) {
|
|
170
|
+
_conds[association_fields[j]] = instance[model.id[j]];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
conditions.or = conditions.or || [];
|
|
174
|
+
(conditions.or as FxSqlQueryComparator.SubQueryInput[]).push(_conds);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return conditions as FxSqlQuerySubQuery.SubQueryConditions;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Gets all the values within an object or array, optionally
|
|
185
|
+
* using a keys array to get only specific values
|
|
186
|
+
*/
|
|
187
|
+
export function values (obj: any[] | {[k: string]: any}, keys?: string[]) {
|
|
188
|
+
var vals: any[] = [];
|
|
189
|
+
|
|
190
|
+
if (keys) {
|
|
191
|
+
const non_arr = obj as {[k: string]: any}
|
|
192
|
+
for (let i = 0; i < keys.length; i++) {
|
|
193
|
+
vals.push(
|
|
194
|
+
non_arr[keys[i]]
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
} else if (Array.isArray(obj)) {
|
|
198
|
+
for (let i = 0; i < obj.length; i++) {
|
|
199
|
+
vals.push(obj[i]);
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
for (let k in obj) {
|
|
203
|
+
if (!/[0-9]+/.test(k)) {
|
|
204
|
+
vals.push(obj[k]);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return vals;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// Qn: is Zero a valid value for a FK column?
|
|
212
|
+
// Why? Well I've got a pre-existing database that started all its 'serial' IDs at zero...
|
|
213
|
+
// Answer: hasValues() is only used in hasOne association, so it's probably ok...
|
|
214
|
+
export function hasValues (obj: {[k: string]: any}, keys: string[]): boolean {
|
|
215
|
+
for (let i = 0; i < keys.length; i++) {
|
|
216
|
+
if (!obj[keys[i]] && obj[keys[i]] !== 0) return false; // 0 is also a good value...
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @description from from <source> object, copy specific ids(determined by <source_model>) to <target> object as
|
|
223
|
+
* correspoding <associacted_fields>, for example:
|
|
224
|
+
*
|
|
225
|
+
* if <source_model> id: id1, id2
|
|
226
|
+
* if <associacted_fields>: user_id, production_id
|
|
227
|
+
* source.id1 = 1, source.id2 = 2
|
|
228
|
+
* target.user_id = undefined, target.production_id = undefined
|
|
229
|
+
*
|
|
230
|
+
* Then, we will get:
|
|
231
|
+
*
|
|
232
|
+
* target.user_id = 1, target.production_id = 2
|
|
233
|
+
*
|
|
234
|
+
* if target.user_id is not undefined, it wouldn't be overwritten unless <overwrite> is true
|
|
235
|
+
*
|
|
236
|
+
* @param source_model
|
|
237
|
+
* @param associacted_fields
|
|
238
|
+
* @param source
|
|
239
|
+
* @param target
|
|
240
|
+
* @param overwrite
|
|
241
|
+
*/
|
|
242
|
+
export function populateModelIdKeysConditions (
|
|
243
|
+
source_model: FxOrmModel.Model,
|
|
244
|
+
associacted_fields: string[],
|
|
245
|
+
source: FxOrmInstance.InstanceDataPayload,
|
|
246
|
+
target: FxSqlQuerySubQuery.SubQueryConditions,
|
|
247
|
+
overwrite?: boolean
|
|
248
|
+
): void {
|
|
249
|
+
const mids = source_model.id;
|
|
250
|
+
for (let i = 0; i < mids.length; i++) {
|
|
251
|
+
if (typeof target[associacted_fields[i]] === 'undefined' || overwrite !== false) {
|
|
252
|
+
target[associacted_fields[i]] = source[mids[i]];
|
|
253
|
+
} else if (Array.isArray(target[associacted_fields[i]])) { // that might be conjunction query conditions
|
|
254
|
+
(target[associacted_fields[i]] as FxSqlQueryComparator.SubQueryInput[])
|
|
255
|
+
.push(
|
|
256
|
+
source[mids[i]] as FxSqlQueryComparator.SubQueryInput
|
|
257
|
+
);
|
|
258
|
+
} else {
|
|
259
|
+
target[associacted_fields[i]] = [target[associacted_fields[i]], source[mids[i]]];
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
export function getConditions (
|
|
265
|
+
model: FxOrmModel.Model,
|
|
266
|
+
fields: string[],
|
|
267
|
+
from: FxSqlQuerySubQuery.SubQueryConditions
|
|
268
|
+
): FxSqlQuerySubQuery.SubQueryConditions {
|
|
269
|
+
var conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
|
|
270
|
+
|
|
271
|
+
populateModelIdKeysConditions(model, fields, from, conditions);
|
|
272
|
+
|
|
273
|
+
return conditions;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* TODO: add comment for this method
|
|
278
|
+
*
|
|
279
|
+
* @description WIP
|
|
280
|
+
* @param params
|
|
281
|
+
* @returns
|
|
282
|
+
*/
|
|
283
|
+
export function wrapFieldObject (
|
|
284
|
+
params: {
|
|
285
|
+
field: FxOrmAssociation.InstanceAssociationItem['field']
|
|
286
|
+
model: FxOrmModel.Model
|
|
287
|
+
altName: string
|
|
288
|
+
mapsTo?: FxOrmModel.ModelPropertyDefinition['mapsTo']
|
|
289
|
+
}
|
|
290
|
+
): Record<string, FxOrmProperty.NormalizedProperty> {
|
|
291
|
+
if (!params.field) {
|
|
292
|
+
var assoc_key = params.model.settings.get("properties.association_key");
|
|
293
|
+
|
|
294
|
+
if (typeof assoc_key === "function") {
|
|
295
|
+
params.field = assoc_key(params.altName.toLowerCase(), params.model.id[0]);
|
|
296
|
+
} else {
|
|
297
|
+
params.field = assoc_key.replace("{name}", params.altName.toLowerCase())
|
|
298
|
+
.replace("{field}", params.model.id[0]);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (typeof params.field == 'object') {
|
|
303
|
+
for (let k in params.field) {
|
|
304
|
+
/* 1st self-own & non-array kv */
|
|
305
|
+
if (!/[0-9]+/.test(k) && params.field.hasOwnProperty(k)) {
|
|
306
|
+
return params.field as Record<string, FxOrmProperty.NormalizedProperty>;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const field_str = params.field as string
|
|
312
|
+
|
|
313
|
+
var newProp: FxOrmProperty.NormalizedProperty,
|
|
314
|
+
propPreDefined: FxOrmProperty.NormalizedProperty,
|
|
315
|
+
propFromKey: FxOrmProperty.NormalizedProperty;
|
|
316
|
+
|
|
317
|
+
propPreDefined = params.model.properties[field_str];
|
|
318
|
+
propFromKey = params.model.properties[params.model.id[0]];
|
|
319
|
+
newProp = { type: 'integer' } as FxOrmProperty.NormalizedProperty;
|
|
320
|
+
|
|
321
|
+
var prop: FxOrmProperty.NormalizedProperty = cloneDeep(propPreDefined || propFromKey || newProp);
|
|
322
|
+
|
|
323
|
+
if (!propPreDefined) {
|
|
324
|
+
util.extend(prop, {
|
|
325
|
+
name: field_str,
|
|
326
|
+
mapsTo: params.mapsTo || field_str
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
[field_str]: prop
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* TODO: add comment for this method
|
|
337
|
+
* @param model related Model
|
|
338
|
+
* @param name field name
|
|
339
|
+
* @param required is field required for relationship
|
|
340
|
+
* @param reversed is model is reversed in relationship
|
|
341
|
+
*/
|
|
342
|
+
export function formatAssociatedField (
|
|
343
|
+
model: FxOrmModel.Model,
|
|
344
|
+
name: string,
|
|
345
|
+
required: boolean,
|
|
346
|
+
reversed: boolean
|
|
347
|
+
): Record<string, FxOrmProperty.NormalizedProperty> {
|
|
348
|
+
let fields = {} as Record<string, FxOrmProperty.NormalizedProperty>,
|
|
349
|
+
field_opts: FxOrmProperty.NormalizedProperty,
|
|
350
|
+
field_name: string;
|
|
351
|
+
|
|
352
|
+
var keys = model.id;
|
|
353
|
+
var assoc_key: FxOrmAssociation.AssociationKeyComputation = model.settings.get("properties.association_key");
|
|
354
|
+
|
|
355
|
+
for (let i = 0; i < keys.length; i++) {
|
|
356
|
+
if (reversed) {
|
|
357
|
+
field_name = keys[i];
|
|
358
|
+
} else if (typeof assoc_key === "function") {
|
|
359
|
+
field_name = assoc_key(name.toLowerCase(), keys[i]);
|
|
360
|
+
} else {
|
|
361
|
+
field_name = assoc_key.replace("{name}", name.toLowerCase())
|
|
362
|
+
.replace("{field}", keys[i]);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (model.properties.hasOwnProperty(keys[i])) {
|
|
366
|
+
var p = model.properties[keys[i]];
|
|
367
|
+
|
|
368
|
+
field_opts = {
|
|
369
|
+
type : p.type || "integer",
|
|
370
|
+
// TODO: make 32 when p.type === 'text'
|
|
371
|
+
size : p.size || 4,
|
|
372
|
+
unsigned : p.unsigned || true,
|
|
373
|
+
time : p.time || false,
|
|
374
|
+
big : p.big || false,
|
|
375
|
+
values : p.values || null,
|
|
376
|
+
required : required,
|
|
377
|
+
name : field_name,
|
|
378
|
+
mapsTo : field_name
|
|
379
|
+
} as FxOrmProperty.NormalizedProperty;
|
|
380
|
+
} else {
|
|
381
|
+
field_opts = {
|
|
382
|
+
type : "integer",
|
|
383
|
+
unsigned : true,
|
|
384
|
+
size : 4,
|
|
385
|
+
required : required,
|
|
386
|
+
name : field_name,
|
|
387
|
+
mapsTo : field_name
|
|
388
|
+
} as FxOrmProperty.NormalizedProperty;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
fields[field_name] = field_opts;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return fields;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/** @internal */
|
|
398
|
+
export function extractHasManyExtraConditions (
|
|
399
|
+
association: FxOrmAssociation.InstanceAssociationItem_HasMany,
|
|
400
|
+
conditions: FxOrmModel.ModelFindByDescriptorItem['conditions'],
|
|
401
|
+
extra_where?: FxOrmModel.ModelFindByDescriptorItem['conditions'],
|
|
402
|
+
) {
|
|
403
|
+
// extract extra info on conditions
|
|
404
|
+
extra_where = { ...(extra_where || {}) } as typeof extra_where;
|
|
405
|
+
// populate out conditions in extra, not belonging to either one model of associations
|
|
406
|
+
for (let k in conditions) {
|
|
407
|
+
if (association.props[k]) {
|
|
408
|
+
(extra_where as any)[k] = conditions[k];
|
|
409
|
+
delete conditions[k];
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return extra_where
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @description top conditions means that conditions which matches query's top select.
|
|
418
|
+
*/
|
|
419
|
+
export function extractSelectTopConditions (
|
|
420
|
+
conditions: FxOrmModel.ModelOptions__Find['conditions'],
|
|
421
|
+
topFields: string[],
|
|
422
|
+
) {
|
|
423
|
+
const topConditions = util.pick(conditions, topFields);
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
topConditions,
|
|
427
|
+
tableConditions: util.omit(conditions, topFields)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// If the parent associations key is `serial`, the join tables
|
|
432
|
+
// key should be changed to `integer`.
|
|
433
|
+
export function convertPropToJoinKeyProp (
|
|
434
|
+
props: Record<string, FxOrmProperty.NormalizedProperty>,
|
|
435
|
+
opts: { required: boolean, makeKey: boolean }
|
|
436
|
+
): Record<string, FxOrmProperty.NormalizedProperty> {
|
|
437
|
+
var prop: FxOrmProperty.NormalizedProperty;
|
|
438
|
+
|
|
439
|
+
for (let k in props) {
|
|
440
|
+
prop = props[k];
|
|
441
|
+
|
|
442
|
+
prop.required = opts.required;
|
|
443
|
+
|
|
444
|
+
if (prop.type == 'serial') {
|
|
445
|
+
prop.type = 'integer';
|
|
446
|
+
}
|
|
447
|
+
if (opts.makeKey) {
|
|
448
|
+
prop.key = true;
|
|
449
|
+
} else {
|
|
450
|
+
delete prop.key;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return props;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export function getRealPath (path_str: string, stack_index?: number) {
|
|
458
|
+
const path = require("path"); // for now, load here (only when needed)
|
|
459
|
+
let cwd = process.cwd();
|
|
460
|
+
const err = new Error();
|
|
461
|
+
let tmp = (err.stack || '').split(/\r?\n/)[typeof stack_index !== "undefined" ? stack_index : 3], m;
|
|
462
|
+
|
|
463
|
+
if ((m = tmp.match(/^\s*at\s+(.+):\d+:\d+$/)) !== null) {
|
|
464
|
+
cwd = path.dirname(m[1]);
|
|
465
|
+
} else if ((m = tmp.match(/^\s*at\s+module\.exports\s+\((.+?)\)/)) !== null) {
|
|
466
|
+
cwd = path.dirname(m[1]);
|
|
467
|
+
} else if ((m = tmp.match(/^\s*at\s+.+\s+\((.+):\d+:\d+\)$/)) !== null) {
|
|
468
|
+
cwd = path.dirname(m[1]);
|
|
469
|
+
}
|
|
470
|
+
const pathIsAbsolute = path.isAbsolute || require('path-is-absolute');
|
|
471
|
+
if (!pathIsAbsolute(path_str)) {
|
|
472
|
+
path_str = path.join(cwd, path_str);
|
|
473
|
+
}
|
|
474
|
+
if (path_str.substr(-1) === path.sep) {
|
|
475
|
+
path_str += "index";
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return path_str;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* @description rename a field name in <dataIn> according to the name map in <properties>
|
|
483
|
+
* @example
|
|
484
|
+
* ```
|
|
485
|
+
* dataIn: { a: 1, b: 2 };
|
|
486
|
+
* properties: { a: { mapsTo: 'a1' }, b: { mapsTo: 'b' } };
|
|
487
|
+
* -->
|
|
488
|
+
* result: { a1: 1, b: 2 };
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* @param dataIn
|
|
492
|
+
* @param properties
|
|
493
|
+
*/
|
|
494
|
+
export function transformPropertyNames (
|
|
495
|
+
dataIn: FxOrmInstance.InstanceDataPayload,
|
|
496
|
+
properties: Record<string, FxOrmProperty.NormalizedProperty> | FxOrmModel.ModelPropertyDefinition
|
|
497
|
+
) {
|
|
498
|
+
var prop: FxOrmModel.ModelPropertyDefinition;
|
|
499
|
+
var dataOut: FxOrmInstance.InstanceDataPayload = {};
|
|
500
|
+
|
|
501
|
+
for (let k in dataIn) {
|
|
502
|
+
prop = properties[k];
|
|
503
|
+
if (prop) {
|
|
504
|
+
dataOut[prop.mapsTo!] = dataIn[k];
|
|
505
|
+
} else {
|
|
506
|
+
dataOut[k] = dataIn[k];
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return dataOut;
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
export function transformOrderPropertyNames (
|
|
513
|
+
order: FxOrmQuery.ChainFindOptions['order'],
|
|
514
|
+
properties: Record<string, FxOrmProperty.NormalizedProperty>
|
|
515
|
+
) {
|
|
516
|
+
if (!order) return order;
|
|
517
|
+
|
|
518
|
+
const newOrder: FxOrmQuery.ChainFindOptions['order'] = JSON.parse(JSON.stringify(order));
|
|
519
|
+
|
|
520
|
+
// Rename order properties according to mapsTo
|
|
521
|
+
for (let i = 0, item: typeof newOrder[any]; i < newOrder.length; i++) {
|
|
522
|
+
item = newOrder[i];
|
|
523
|
+
|
|
524
|
+
// [ 'SQL..??', [arg1, arg2]]
|
|
525
|
+
if (Array.isArray(item[1])) continue;
|
|
526
|
+
|
|
527
|
+
if (Array.isArray(item[0])) {
|
|
528
|
+
// [ ['table or alias Name', 'propName'], 'Z']
|
|
529
|
+
let maybePropertyName = item[0][1], orderFlag: IOrderFlag = 'A';
|
|
530
|
+
[maybePropertyName, orderFlag] = standardizeOrder(maybePropertyName)[0] as FxOrmQuery.OrderNormalizedTupleWithoutTable;
|
|
531
|
+
if (!item[1]) { item[1] = orderFlag as any; }
|
|
532
|
+
item[0][1] = properties[maybePropertyName as string].mapsTo!;
|
|
533
|
+
} else {
|
|
534
|
+
const [maybePropertyName] = standardizeOrder(item[0])[0] as FxOrmQuery.OrderNormalizedTupleWithoutTable;
|
|
535
|
+
// normal order
|
|
536
|
+
item[0] = properties[maybePropertyName as string].mapsTo!;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return newOrder;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export function renameDatastoreFieldsToPropertyNames (
|
|
543
|
+
data: FxOrmInstance.InstanceDataPayload, fieldToPropertyMap: FxOrmProperty.FieldToPropertyMapType
|
|
544
|
+
) {
|
|
545
|
+
for (let k in data) {
|
|
546
|
+
const prop = fieldToPropertyMap[k];
|
|
547
|
+
if (prop && prop.name != k) {
|
|
548
|
+
data[prop.name] = data[k];
|
|
549
|
+
delete data[k];
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return data;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export function camelCaseHasMany(text: string) {
|
|
556
|
+
return ucfirst(text[0]) + text.substr(1).replace(/_([a-z])/, function (m, l) {
|
|
557
|
+
return l.toUpperCase();
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
export function ucfirst(text: string) {
|
|
562
|
+
return text[0].toUpperCase() + text.substr(1);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
export function formatNameFor (
|
|
566
|
+
key: 'assoc:hasMany' | 'assoc:hasOne' | 'findBy:common' | 'findBy:hasOne' | 'assoc:extendsTo' | 'findBy:extendsTo' | 'field:lazyload' | 'syncify:assoc',
|
|
567
|
+
name: string
|
|
568
|
+
) {
|
|
569
|
+
switch (key) {
|
|
570
|
+
case 'assoc:hasMany':
|
|
571
|
+
return camelCaseHasMany(name)
|
|
572
|
+
case 'findBy:common':
|
|
573
|
+
case 'assoc:hasOne':
|
|
574
|
+
case 'findBy:hasOne':
|
|
575
|
+
return ucfirst(name)
|
|
576
|
+
case 'assoc:extendsTo':
|
|
577
|
+
case 'findBy:extendsTo':
|
|
578
|
+
return ucfirst(name)
|
|
579
|
+
case 'field:lazyload':
|
|
580
|
+
return ucfirst(name.toLocaleLowerCase())
|
|
581
|
+
case 'syncify:assoc':
|
|
582
|
+
return name + 'Sync'
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
export function combineMergeInfoToArray (
|
|
587
|
+
merges: FxOrmQuery.ChainFindOptions['merge']
|
|
588
|
+
): FxOrmQuery.ChainFindMergeInfo[] {
|
|
589
|
+
if (!Array.isArray(merges))
|
|
590
|
+
merges = [merges]
|
|
591
|
+
|
|
592
|
+
return merges.filter(Boolean)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export function parseTableInputForSelect (ta_str: string) {
|
|
596
|
+
const [pure_table, alias = typeof pure_table === 'string' ? pure_table : ''] = QueryHelpers.parseTableInputStr(ta_str)
|
|
597
|
+
|
|
598
|
+
return {
|
|
599
|
+
pure_table,
|
|
600
|
+
alias,
|
|
601
|
+
from_tuple: [pure_table, alias] as FxSqlQuerySql.SqlTableTuple
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
export function tableAlias (
|
|
606
|
+
table: string | FxSqlQuerySql.SqlFromTableInput,
|
|
607
|
+
alias: string = typeof table === 'string' ? table : '',
|
|
608
|
+
same_suffix: string = ''
|
|
609
|
+
) {
|
|
610
|
+
if (QueryHelpers.maybeKnexRawOrQueryBuilder(table)) {
|
|
611
|
+
if (!alias) {
|
|
612
|
+
throw new Error(`[tableAlias] when table is knex.raw or knex.queryBuilder, alias must be provided!`)
|
|
613
|
+
}
|
|
614
|
+
return alias;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return `${table} ${alias}${same_suffix ? ` ${same_suffix}` : ''}`
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
export function tableAliasCalculatorInOneQuery () {
|
|
621
|
+
const countHash = {} as {[k: string]: number}
|
|
622
|
+
|
|
623
|
+
return function increment (tableName: string, get_only: boolean = false) {
|
|
624
|
+
countHash[tableName] = countHash[tableName] || 0;
|
|
625
|
+
if (!get_only) {
|
|
626
|
+
countHash[tableName]++;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
return countHash[tableName]
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export function ORM_Error(err: Error, cb?: FxOrmCommon.VoidCallback): FxOrmNS.ORMLike {
|
|
634
|
+
var Emitter: any = new events.EventEmitter();
|
|
635
|
+
|
|
636
|
+
Emitter.use = Emitter.define = Emitter.sync = Emitter.load = function () {};
|
|
637
|
+
|
|
638
|
+
if (typeof cb === "function")
|
|
639
|
+
cb(err);
|
|
640
|
+
|
|
641
|
+
process.nextTick(function () {
|
|
642
|
+
Emitter.emit("connect", err);
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
return Emitter;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
export function queryParamCast (val: any): any {
|
|
649
|
+
if (typeof val == 'string') {
|
|
650
|
+
switch (val) {
|
|
651
|
+
case '1':
|
|
652
|
+
case 'true':
|
|
653
|
+
return true;
|
|
654
|
+
case '0':
|
|
655
|
+
case 'false':
|
|
656
|
+
return false;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return val;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
export function isDriverNotSupportedError (err: FxOrmError.ExtendedError) {
|
|
663
|
+
if (err.code === 'MODULE_NOT_FOUND') return true;
|
|
664
|
+
|
|
665
|
+
if(
|
|
666
|
+
[
|
|
667
|
+
// windows not found
|
|
668
|
+
'The system cannot find the file specified',
|
|
669
|
+
// unix like not found
|
|
670
|
+
'No such file or directory',
|
|
671
|
+
].some((msg: string) => {
|
|
672
|
+
return err.message.indexOf(msg) > -1 || (err.message.toLocaleLowerCase()).indexOf(msg.toLowerCase()) > -1
|
|
673
|
+
})
|
|
674
|
+
)
|
|
675
|
+
return true
|
|
676
|
+
|
|
677
|
+
if(
|
|
678
|
+
[
|
|
679
|
+
'find module',
|
|
680
|
+
].some((msg: string) => {
|
|
681
|
+
return err.message.indexOf(msg) > -1
|
|
682
|
+
})
|
|
683
|
+
)
|
|
684
|
+
return true
|
|
685
|
+
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
export const catchBlocking = FxORMCore.catchBlocking;
|
|
690
|
+
export const takeAwayResult = FxORMCore.takeAwayResult
|
|
691
|
+
|
|
692
|
+
export function doWhenErrIs (
|
|
693
|
+
compare: {
|
|
694
|
+
message?: string,
|
|
695
|
+
literalCode?: string
|
|
696
|
+
},
|
|
697
|
+
callback: (...args: any[]) => any,
|
|
698
|
+
err?: FxOrmError.ExtendedError,
|
|
699
|
+
) {
|
|
700
|
+
if (!err || util.isEmpty(compare)) return ;
|
|
701
|
+
|
|
702
|
+
if (!(err instanceof Error)) return ;
|
|
703
|
+
|
|
704
|
+
if (compare.hasOwnProperty('message') && err.message !== compare.message) return ;
|
|
705
|
+
|
|
706
|
+
if (compare.hasOwnProperty('literalCode') && err.literalCode !== compare.literalCode) return ;
|
|
707
|
+
|
|
708
|
+
callback(err)
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
export function getErrWaitor (shouldWait: boolean = false): FxOrmError.ErrorWaitor {
|
|
712
|
+
return {
|
|
713
|
+
evt: shouldWait ? new coroutine.Event : null,
|
|
714
|
+
err: null,
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
export function getValueWaitor <T = any>(shouldWait: boolean = false): FxOrmCommon.ValueWaitor<T> {
|
|
719
|
+
return {
|
|
720
|
+
evt: shouldWait ? new coroutine.Event : null,
|
|
721
|
+
value: null as T,
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
export function parallelQueryIfPossible<T = any, RESP = any> (
|
|
726
|
+
can_parallel: boolean,
|
|
727
|
+
iteratee: T[],
|
|
728
|
+
iterator: (value: T, index?: number, array?: T[]) => RESP
|
|
729
|
+
): RESP[] {
|
|
730
|
+
if (can_parallel && iteratee.length > 1)
|
|
731
|
+
return coroutine.parallel(iteratee, (item: T) => iterator(item));
|
|
732
|
+
|
|
733
|
+
return iteratee.map(iterator)
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* @description do some mutation for field-value in conditions
|
|
738
|
+
*
|
|
739
|
+
* @param conditions
|
|
740
|
+
* @param host
|
|
741
|
+
*/
|
|
742
|
+
export function filterWhereConditionsInput (
|
|
743
|
+
conditions: FxSqlQuerySubQuery.SubQueryConditions,
|
|
744
|
+
host: {
|
|
745
|
+
properties: Record<string, FxOrmProperty.NormalizedProperty>
|
|
746
|
+
} | FxOrmModel.Model
|
|
747
|
+
): FxSqlQuerySubQuery.SubQueryConditions {
|
|
748
|
+
if (host.properties) {
|
|
749
|
+
filterDate(conditions, { properties: host.properties });
|
|
750
|
+
} else {
|
|
751
|
+
filterDate(conditions, { properties: (host as FxOrmModel.Model).allProperties });
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
return conditions;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
export function addUnwritableProperty (
|
|
758
|
+
obj: any,
|
|
759
|
+
property: string,
|
|
760
|
+
value: any,
|
|
761
|
+
propertyConfiguration: PropertyDescriptor = {}
|
|
762
|
+
) {
|
|
763
|
+
Object.defineProperty(
|
|
764
|
+
obj,
|
|
765
|
+
property,
|
|
766
|
+
{
|
|
767
|
+
value,
|
|
768
|
+
...propertyConfiguration,
|
|
769
|
+
writable: false
|
|
770
|
+
}
|
|
771
|
+
)
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
export function addHiddenReadonlyProperty (
|
|
775
|
+
obj: any,
|
|
776
|
+
property: string,
|
|
777
|
+
getter: () => any,
|
|
778
|
+
propertyConfiguration: PropertyDescriptor = {}
|
|
779
|
+
) {
|
|
780
|
+
Object.defineProperty(
|
|
781
|
+
obj,
|
|
782
|
+
property,
|
|
783
|
+
{
|
|
784
|
+
get: getter,
|
|
785
|
+
...propertyConfiguration,
|
|
786
|
+
enumerable: false
|
|
787
|
+
}
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export function addHiddenUnwritableMethodToInstance (
|
|
792
|
+
instance: FxOrmInstance.Instance,
|
|
793
|
+
method_name: 'save' | 'saveSync' | string,
|
|
794
|
+
fn: (...args: any[]) => any,
|
|
795
|
+
propertyConfiguration: PropertyDescriptor = {}
|
|
796
|
+
) {
|
|
797
|
+
Object.defineProperty(
|
|
798
|
+
instance,
|
|
799
|
+
method_name,
|
|
800
|
+
{
|
|
801
|
+
value: fn.bind(instance),
|
|
802
|
+
...propertyConfiguration,
|
|
803
|
+
writable: true,
|
|
804
|
+
enumerable: false,
|
|
805
|
+
}
|
|
806
|
+
)
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
export function addHiddenPropertyToInstance (
|
|
810
|
+
instance: FxOrmInstance.Instance,
|
|
811
|
+
property_name: string,
|
|
812
|
+
value: any,
|
|
813
|
+
propertyConfiguration: PropertyDescriptor = {}
|
|
814
|
+
) {
|
|
815
|
+
Object.defineProperty(
|
|
816
|
+
instance,
|
|
817
|
+
property_name,
|
|
818
|
+
{
|
|
819
|
+
value: value,
|
|
820
|
+
...propertyConfiguration,
|
|
821
|
+
enumerable: false
|
|
822
|
+
}
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
export function addHiddenReadonlyPropertyToInstance (
|
|
827
|
+
instance: FxOrmInstance.Instance,
|
|
828
|
+
property_name: string,
|
|
829
|
+
getter: () => any,
|
|
830
|
+
propertyConfiguration: PropertyDescriptor = {}
|
|
831
|
+
) {
|
|
832
|
+
Object.defineProperty(
|
|
833
|
+
instance,
|
|
834
|
+
property_name,
|
|
835
|
+
{
|
|
836
|
+
get: getter,
|
|
837
|
+
...propertyConfiguration,
|
|
838
|
+
enumerable: false
|
|
839
|
+
}
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
export function fillSyncVersionAccessorForAssociation (
|
|
844
|
+
association: FxOrmAssociation.InstanceAssociationItem
|
|
845
|
+
) {
|
|
846
|
+
if (!association.getSyncAccessor)
|
|
847
|
+
association.getSyncAccessor = formatNameFor('syncify:assoc', association.getAccessor)
|
|
848
|
+
|
|
849
|
+
if (!association.setSyncAccessor)
|
|
850
|
+
association.setSyncAccessor = formatNameFor('syncify:assoc', association.setAccessor)
|
|
851
|
+
|
|
852
|
+
if (!association.delSyncAccessor)
|
|
853
|
+
association.delSyncAccessor = formatNameFor('syncify:assoc', association.delAccessor)
|
|
854
|
+
|
|
855
|
+
if (!association.hasSyncAccessor)
|
|
856
|
+
association.hasSyncAccessor = formatNameFor('syncify:assoc', association.hasAccessor)
|
|
857
|
+
|
|
858
|
+
if (!association.addSyncAccessor && association.addAccessor)
|
|
859
|
+
association.addSyncAccessor = formatNameFor('syncify:assoc', association.addAccessor)
|
|
860
|
+
|
|
861
|
+
if (!association.modelFindBySyncAccessor && association.modelFindByAccessor)
|
|
862
|
+
association.modelFindBySyncAccessor = formatNameFor('syncify:assoc', association.modelFindByAccessor)
|
|
863
|
+
|
|
864
|
+
return association;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
const AvailableHooks = [
|
|
869
|
+
'beforeSet', 'afterSet',
|
|
870
|
+
'beforeRemove', 'afterRemove',
|
|
871
|
+
'beforeAdd', 'afterAdd'
|
|
872
|
+
]
|
|
873
|
+
export function addHookPatchHelperForAssociation (
|
|
874
|
+
association: FxOrmAssociation.InstanceAssociationItem,
|
|
875
|
+
// { for = 'hasOne' }: Fibjs.AnyObject = {}
|
|
876
|
+
) {
|
|
877
|
+
// setup hooks
|
|
878
|
+
for (let k in AvailableHooks) {
|
|
879
|
+
association[AvailableHooks[k]] = createHookHelper(association.hooks, AvailableHooks[k], { initialHooks: Object.assign({}, association.hooks) });
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
export function generateUID4SoloGet (
|
|
884
|
+
m_opts: FxOrmModel.ModelConstructorOptions,
|
|
885
|
+
ids: (string | number)[]
|
|
886
|
+
) {
|
|
887
|
+
return m_opts.driver.uid + "/" + m_opts.table + "/" + ids.join("/");
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
export function generateUID4ChainFind (
|
|
891
|
+
m_opts: FxOrmModel.ModelConstructorOptions,
|
|
892
|
+
merges: FxOrmQuery.ChainFindMergeInfo[] = [],
|
|
893
|
+
data: FxOrmInstance.InstanceDataPayload
|
|
894
|
+
) {
|
|
895
|
+
const merge_id = merges.map(merge => (merge ? merge.from.table : "")).join(',');
|
|
896
|
+
let uid = m_opts.driver.uid + "/" + m_opts.table + (merge_id ? `+${merge_id}` : "");
|
|
897
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
898
|
+
uid += "/" + data[m_opts.keys[i]];
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
return uid;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
export function generateUID4Model (
|
|
905
|
+
m_opts: FxOrmModel.ModelConstructorOptions
|
|
906
|
+
) {
|
|
907
|
+
return m_opts.driver.uid + "/" + m_opts.table + "/" + m_opts.keys.join("/")
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
export function makeIdForDriverTable (driver_uid: string, table: string) {
|
|
911
|
+
return `${driver_uid}/${table}`
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
export function bindInstance (instance: FxOrmInstance.Instance, fn: (...args: any[]) => any) {
|
|
915
|
+
return fn.bind(instance)
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
export function buildAssocHooksContext(
|
|
919
|
+
hookName: keyof FxOrmAssociation.InstanceAssociationItem['hooks'],
|
|
920
|
+
payload: {
|
|
921
|
+
$ref: FxOrmAssociation.__AssocHooksCtx,
|
|
922
|
+
useChannel?: FxOrmAssociation.__AssocHooksCtx['useChannel']
|
|
923
|
+
}
|
|
924
|
+
): Record<string, any> {
|
|
925
|
+
const {
|
|
926
|
+
$ref = {} as FxOrmAssociation.__AssocHooksCtx,
|
|
927
|
+
useChannel = $ref.useChannel || reusableChannelGenerator()
|
|
928
|
+
} = payload;
|
|
929
|
+
|
|
930
|
+
const {
|
|
931
|
+
instance = null,
|
|
932
|
+
association = null,
|
|
933
|
+
associations = [],
|
|
934
|
+
association_ids = [],
|
|
935
|
+
removeConditions = {},
|
|
936
|
+
} = $ref;
|
|
937
|
+
|
|
938
|
+
if (!instance)
|
|
939
|
+
throw `[buildAssocHooksContext] instance is required`
|
|
940
|
+
|
|
941
|
+
if (!$ref || typeof $ref !== 'object')
|
|
942
|
+
throw `[buildAssocHooksContext]$ref must be valid Object`
|
|
943
|
+
|
|
944
|
+
const ctx: any = $ref;
|
|
945
|
+
if (!ctx.hasOwnProperty('$ref') || Object.getOwnPropertyDescriptor(ctx, '$ref')!.configurable)
|
|
946
|
+
Object.defineProperty(ctx, '$ref', { get () { return ctx }, configurable: false, enumerable: true})
|
|
947
|
+
|
|
948
|
+
ctx.association = association
|
|
949
|
+
ctx.associations = associations
|
|
950
|
+
ctx.association_ids = association_ids
|
|
951
|
+
|
|
952
|
+
ctx.useChannel = useChannel
|
|
953
|
+
|
|
954
|
+
switch (hookName) {
|
|
955
|
+
case 'beforeSet':
|
|
956
|
+
case 'afterSet':
|
|
957
|
+
break
|
|
958
|
+
case 'beforeAdd':
|
|
959
|
+
case 'afterAdd':
|
|
960
|
+
break
|
|
961
|
+
case 'beforeRemove':
|
|
962
|
+
ctx.removeConditions = removeConditions
|
|
963
|
+
break
|
|
964
|
+
case 'afterRemove':
|
|
965
|
+
ctx.removeConditions = removeConditions
|
|
966
|
+
break
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
return ctx
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
export function makeHandlerDecorator(
|
|
973
|
+
opts: {
|
|
974
|
+
thisArg?: any
|
|
975
|
+
onlyOnce?: boolean
|
|
976
|
+
} = {},
|
|
977
|
+
hdlr: () => any
|
|
978
|
+
) {
|
|
979
|
+
const {
|
|
980
|
+
thisArg = null,
|
|
981
|
+
onlyOnce = true
|
|
982
|
+
} = opts;
|
|
983
|
+
let finishOnce = false
|
|
984
|
+
|
|
985
|
+
return (err: FxOrmCommon.Arraible<FxOrmError.ExtendedError> | false) => {
|
|
986
|
+
if (onlyOnce && finishOnce) {
|
|
987
|
+
console.warn(`[makeHandlerDecorator] this function was once only`)
|
|
988
|
+
return ;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
finishOnce = true;
|
|
992
|
+
|
|
993
|
+
if (err)
|
|
994
|
+
throw err;
|
|
995
|
+
|
|
996
|
+
if (err === false) return ;
|
|
997
|
+
|
|
998
|
+
return hdlr.call(thisArg)
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
function getChannelInfo () {
|
|
1003
|
+
return {
|
|
1004
|
+
id: uuid.snowflake().hex(),
|
|
1005
|
+
executed: false,
|
|
1006
|
+
fn: null as ((...args: any[]) => any) | null
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
export function reusableChannelGenerator () {
|
|
1011
|
+
const DEFAULT_KEY = `$$default`
|
|
1012
|
+
const channelInfos: {[k: string]: Record<string, any> } = {
|
|
1013
|
+
[DEFAULT_KEY]: getChannelInfo()
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
return function useChannel () {
|
|
1017
|
+
let name = '', _channel: ((...args: any[]) => any) | undefined
|
|
1018
|
+
|
|
1019
|
+
selectArgs(Array.prototype.slice.apply(arguments), (arg_type, arg) => {
|
|
1020
|
+
switch (arg_type) {
|
|
1021
|
+
case 'string':
|
|
1022
|
+
name = arg
|
|
1023
|
+
break
|
|
1024
|
+
case 'function':
|
|
1025
|
+
_channel = arg
|
|
1026
|
+
break
|
|
1027
|
+
}
|
|
1028
|
+
})
|
|
1029
|
+
if (!name)
|
|
1030
|
+
name = DEFAULT_KEY
|
|
1031
|
+
|
|
1032
|
+
if (!channelInfos.hasOwnProperty(name))
|
|
1033
|
+
channelInfos[name] = getChannelInfo()
|
|
1034
|
+
|
|
1035
|
+
const channelInfo = channelInfos[name]
|
|
1036
|
+
|
|
1037
|
+
_channel = _channel || channelInfo.fn
|
|
1038
|
+
if (!_channel)
|
|
1039
|
+
throw `[useChannel] channel is required and must be function type`
|
|
1040
|
+
|
|
1041
|
+
if (!channelInfo.fn && _channel)
|
|
1042
|
+
channelInfo.fn = _channel
|
|
1043
|
+
|
|
1044
|
+
const results = [
|
|
1045
|
+
function (...args: any) {
|
|
1046
|
+
if (channelInfo.executed)
|
|
1047
|
+
return ;
|
|
1048
|
+
|
|
1049
|
+
channelInfo.executed = true;
|
|
1050
|
+
|
|
1051
|
+
if (typeof channelInfo.fn !== 'function')
|
|
1052
|
+
throw `[reusableChannelGenerator/useChannel] channel with name ${name} haven't been set 'fn'!`
|
|
1053
|
+
|
|
1054
|
+
channelInfo.fn!.apply(null, args)
|
|
1055
|
+
},
|
|
1056
|
+
function (fn: (...args: any[]) => any) {
|
|
1057
|
+
if (typeof fn !== 'function')
|
|
1058
|
+
throw `[reusableChannelGenerator/useChannel::setChannel] new channel must be function!`
|
|
1059
|
+
|
|
1060
|
+
channelInfo.fn = fn
|
|
1061
|
+
}
|
|
1062
|
+
] as any as FxOrmHook.HookChannelResults
|
|
1063
|
+
|
|
1064
|
+
results.run = (...args: any[]) => {
|
|
1065
|
+
return results[0].apply(null, args)
|
|
1066
|
+
}
|
|
1067
|
+
results.set = (fn, ...args: any[]) => {
|
|
1068
|
+
return (results as any)[1].apply(null, [fn].concat(args))
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
return results
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
export const createHookHelper = function (
|
|
1076
|
+
hooks: Record<string, any>,
|
|
1077
|
+
hook: keyof FxOrmModel.Hooks | keyof FxOrmAssociation.InstanceAssociationItem['hooks'],
|
|
1078
|
+
{ initialHooks = [] }: Record<string, any> = {}
|
|
1079
|
+
) {
|
|
1080
|
+
return function (this: any,
|
|
1081
|
+
cb: FxOrmHook.HookActionCallback | FxOrmHook.HookResultCallback,
|
|
1082
|
+
opts?: FxOrmModel.ModelHookPatchOptions
|
|
1083
|
+
) {
|
|
1084
|
+
if (typeof cb !== "function") {
|
|
1085
|
+
delete hooks[hook];
|
|
1086
|
+
return this;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
const { oldhook = undefined } = opts || {}
|
|
1090
|
+
let tmp = null as any
|
|
1091
|
+
switch (oldhook) {
|
|
1092
|
+
default:
|
|
1093
|
+
case 'initial':
|
|
1094
|
+
hooks[hook] = initialHooks[hook] as any;
|
|
1095
|
+
break
|
|
1096
|
+
case 'overwrite':
|
|
1097
|
+
case undefined:
|
|
1098
|
+
hooks[hook] = cb as any;
|
|
1099
|
+
break
|
|
1100
|
+
case 'prepend':
|
|
1101
|
+
tmp = arraify(hooks[hook]);
|
|
1102
|
+
tmp.push(cb)
|
|
1103
|
+
hooks[hook] = tmp;
|
|
1104
|
+
break
|
|
1105
|
+
case 'append':
|
|
1106
|
+
tmp = arraify(hooks[hook]);
|
|
1107
|
+
tmp.unshift(cb)
|
|
1108
|
+
hooks[hook] = tmp;
|
|
1109
|
+
break
|
|
1110
|
+
}
|
|
1111
|
+
return this;
|
|
1112
|
+
};
|
|
1113
|
+
};
|
|
1114
|
+
|
|
1115
|
+
export function attachOnceTypedHookRefToInstance (
|
|
1116
|
+
instance: FxOrmInstance.Instance,
|
|
1117
|
+
type:
|
|
1118
|
+
'save'
|
|
1119
|
+
| 'create'
|
|
1120
|
+
| 'remove'
|
|
1121
|
+
,
|
|
1122
|
+
typedHookRef: Record<string, any>,
|
|
1123
|
+
) {
|
|
1124
|
+
// ensure instance.$hookRef existed
|
|
1125
|
+
if (!instance.hasOwnProperty('$hookRef')) {
|
|
1126
|
+
const hookRef = {} as FxOrmInstance.Instance['$hookRef']
|
|
1127
|
+
addHiddenReadonlyPropertyToInstance(
|
|
1128
|
+
instance,
|
|
1129
|
+
'$hookRef',
|
|
1130
|
+
() => hookRef,
|
|
1131
|
+
{
|
|
1132
|
+
configurable: false
|
|
1133
|
+
}
|
|
1134
|
+
)
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
typedHookRef = {...typedHookRef}
|
|
1138
|
+
typedHookRef.instance = instance
|
|
1139
|
+
typedHookRef.useChannel = reusableChannelGenerator()
|
|
1140
|
+
|
|
1141
|
+
instance.$hookRef[type] = typedHookRef as FxOrmInstance.Instance['$hookRef'][typeof type]
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
export function arraify<T = any> (item: T | T[]): T[] {
|
|
1145
|
+
return Array.isArray(item) ? item : [item]
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
export function flattenArray<T = any> (input: ArrayLike<T | T[]>): T[] {
|
|
1149
|
+
return Array.prototype.concat.apply([], input as any) as T[]
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
export function firstEl<T = any> (item: T | T[]): T {
|
|
1153
|
+
return Array.isArray(item) ? item[0] : item
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
export function isKeyProperty(prop: FxOrmProperty.NormalizedProperty) {
|
|
1157
|
+
return prop.key;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
export function isKeyPrimaryProperty(prop: FxOrmProperty.NormalizedProperty) {
|
|
1161
|
+
return prop.key && prop.klass === 'primary';
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
export function coercePositiveInt(value: any): number | undefined;
|
|
1165
|
+
export function coercePositiveInt(value: any, fallbackValue: undefined): number | undefined;
|
|
1166
|
+
export function coercePositiveInt<T extends number>(value: any, fallbackValue: T): number | T;
|
|
1167
|
+
export function coercePositiveInt<T extends number>(value: any, fallbackValue?: T): number | T | undefined {
|
|
1168
|
+
const num = parseInt(value, 10);
|
|
1169
|
+
|
|
1170
|
+
if (Number.isNaN(num)) return fallbackValue;
|
|
1171
|
+
|
|
1172
|
+
return Math.abs(num);
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
export function getUUID () {
|
|
1176
|
+
return uuid.snowflake().hex()
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
export const DEFAULT_GENERATE_SQL_QUERY_SELECT: FxOrmDMLDriver.DMLDriver_FindOptions['generateSqlSelect']
|
|
1180
|
+
= function (ctx) {
|
|
1181
|
+
return this.query.select()
|
|
1182
|
+
.from(ctx.fromTuple)
|
|
1183
|
+
.select(ctx.selectFields)
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
export function isVirtualViewModel (model: FxOrmModel.Model) {
|
|
1187
|
+
return (
|
|
1188
|
+
Object.keys(model.allProperties).length === 0
|
|
1189
|
+
&& Object.keys(model.virtualProperties).length > 0
|
|
1190
|
+
)
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
export function disAllowOpForVModel (model: FxOrmModel.Model, opName: string) {
|
|
1194
|
+
if (isVirtualViewModel(model)) {
|
|
1195
|
+
throw new ORMError(`operation '${opName}' not supported for virtual model`, 'NO_SUPPORT', { model: model.name });
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
export function normalizeVirtualViewOption (
|
|
1200
|
+
virtualView: FxOrmModel.ModelDefineOptions['virtualView'],
|
|
1201
|
+
knex: import('../knex/types').Knex,
|
|
1202
|
+
): FxOrmModel.ModelConstructorOptions['virtualView'] {
|
|
1203
|
+
let result: FxOrmModel.ModelConstructorOptions['virtualView'] = {
|
|
1204
|
+
disabled: true,
|
|
1205
|
+
subQuery: '()'
|
|
1206
|
+
};
|
|
1207
|
+
|
|
1208
|
+
if (!virtualView) {
|
|
1209
|
+
return result;
|
|
1210
|
+
} else if (typeof virtualView === 'string') {
|
|
1211
|
+
result = {
|
|
1212
|
+
disabled: false,
|
|
1213
|
+
subQuery: virtualView as `(${string})`
|
|
1214
|
+
}
|
|
1215
|
+
} else if (QueryHelpers.maybeKnexRawOrQueryBuilder(virtualView)) {
|
|
1216
|
+
result = {
|
|
1217
|
+
disabled: false,
|
|
1218
|
+
subQuery: knex.raw(virtualView.toQuery()).wrap('(', ')').toQuery() as `(${string})`
|
|
1219
|
+
}
|
|
1220
|
+
} else if (typeof virtualView === 'object') {
|
|
1221
|
+
result = {
|
|
1222
|
+
disabled: !!virtualView.disabled,
|
|
1223
|
+
subQuery: virtualView.subQuery
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
if (!QueryHelpers.isWrapperdSubQuerySelect(result?.subQuery)) {
|
|
1228
|
+
throw new Error(`[normalizeVirtualViewOption] invalid virtualView.subQuery: ${result?.subQuery}, it must be knex.raw(...), knex's query build, or sub query string wrapped with ()`)
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
return result;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
/** @internal only for plugin developers */
|
|
1235
|
+
export function __wrapTableSourceAsGneratingSqlSelect(
|
|
1236
|
+
{
|
|
1237
|
+
virtualView,
|
|
1238
|
+
customSelect: _customSelect,
|
|
1239
|
+
generateSqlSelect,
|
|
1240
|
+
}: {
|
|
1241
|
+
virtualView: FxOrmModel.ModelConstructorOptions['virtualView'],
|
|
1242
|
+
customSelect?: FxOrmModel.ModelDefineOptions['customSelect']
|
|
1243
|
+
generateSqlSelect?: FxOrmModel.ModelDefineOptions['generateSqlSelect'],
|
|
1244
|
+
},
|
|
1245
|
+
opts: {
|
|
1246
|
+
dialect: FxSqlQueryDialect.Dialect,
|
|
1247
|
+
modelTable: string,
|
|
1248
|
+
}
|
|
1249
|
+
): FxOrmModel.ModelDefineOptions['generateSqlSelect'] {
|
|
1250
|
+
const {
|
|
1251
|
+
modelTable, dialect
|
|
1252
|
+
} = opts;
|
|
1253
|
+
|
|
1254
|
+
if (!virtualView.disabled) {
|
|
1255
|
+
return function (ctx, querySelect) {
|
|
1256
|
+
querySelect
|
|
1257
|
+
.from(`${virtualView.subQuery} as ${modelTable}`)
|
|
1258
|
+
.select(ctx.selectVirtualFields)
|
|
1259
|
+
|
|
1260
|
+
return querySelect;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
if (!_customSelect) return generateSqlSelect;
|
|
1265
|
+
|
|
1266
|
+
const customSelect = arraify(_customSelect).filter(x => !!x && typeof x === 'object').map((tfq) => {
|
|
1267
|
+
return {
|
|
1268
|
+
from: arraify(tfq.from).filter(Boolean),
|
|
1269
|
+
select: arraify(tfq.select)
|
|
1270
|
+
.map(select => {
|
|
1271
|
+
return typeof select === 'function' ? select(dialect) : select
|
|
1272
|
+
})
|
|
1273
|
+
.filter(Boolean),
|
|
1274
|
+
wheres: (typeof tfq.wheres === 'string' ? { __sql: [[tfq.wheres]] } : tfq.wheres) as Exclude<typeof tfq.wheres, string>,
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
|
|
1278
|
+
return function (ctx) {
|
|
1279
|
+
const qSelect = this.query.select()
|
|
1280
|
+
.from([ctx.table, ctx.table]) // specify the alias as table name itself
|
|
1281
|
+
.select(ctx.selectFields)
|
|
1282
|
+
|
|
1283
|
+
customSelect.forEach((tfq) => {
|
|
1284
|
+
if (tfq.from.length) {
|
|
1285
|
+
tfq.from.forEach(fromItem => {
|
|
1286
|
+
// pre convert string like `a` as `[a, a]`
|
|
1287
|
+
if (typeof fromItem === 'string' && !QueryHelpers.isWrapperdSubQuerySelect(fromItem)) {
|
|
1288
|
+
let [tableName = ctx.table, tableAlias] = QueryHelpers.parseTableInputStr(fromItem, this.query.Dialect.type);
|
|
1289
|
+
tableAlias = tableAlias || tableName as string;
|
|
1290
|
+
qSelect.from([tableName, tableAlias]);
|
|
1291
|
+
} else {
|
|
1292
|
+
qSelect.from(fromItem);
|
|
1293
|
+
}
|
|
1294
|
+
})
|
|
1295
|
+
} else {
|
|
1296
|
+
qSelect.from([ctx.table, ctx.table])
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
const select = [...new Set([
|
|
1300
|
+
...ctx.selectVirtualFields,
|
|
1301
|
+
...tfq.select
|
|
1302
|
+
])]
|
|
1303
|
+
|
|
1304
|
+
if (select.length) qSelect.select(select as any);
|
|
1305
|
+
|
|
1306
|
+
if (tfq.wheres) {
|
|
1307
|
+
qSelect.where(tfq.wheres)
|
|
1308
|
+
};
|
|
1309
|
+
});
|
|
1310
|
+
|
|
1311
|
+
return qSelect;
|
|
1312
|
+
}
|
|
1313
|
+
}
|