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
package/src/orm/Model.ts
ADDED
|
@@ -0,0 +1,1235 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
/// <reference lib="es2017" />
|
|
4
|
+
|
|
5
|
+
const util = require('util');
|
|
6
|
+
import type { FxOrmModel } from './Typo/model';
|
|
7
|
+
|
|
8
|
+
import ChainFind from './ChainFind';
|
|
9
|
+
import { Instance } from "./Instance";
|
|
10
|
+
import * as LazyLoad from './LazyLoad';
|
|
11
|
+
import * as ManyAssociation from './Associations/Many';
|
|
12
|
+
import * as OneAssociation from './Associations/One';
|
|
13
|
+
import * as ExtendAssociation from './Associations/Extend';
|
|
14
|
+
import * as Property from './Property';
|
|
15
|
+
import * as Singleton from './Singleton';
|
|
16
|
+
import * as Utilities from './Utilities';
|
|
17
|
+
import * as Validators from './Validators';
|
|
18
|
+
import ORMError from "./Error";
|
|
19
|
+
import * as Hook from './Hook';
|
|
20
|
+
import AggregateFunctions from './AggregateFunctions';
|
|
21
|
+
import * as Helpers from './Helpers';
|
|
22
|
+
import { getMapsToFromPropertyHash } from './Associations/_utils';
|
|
23
|
+
import type { FxOrmAssociation } from './Typo/assoc';
|
|
24
|
+
import type { FxOrmProperty } from './Typo/property';
|
|
25
|
+
import type { FxOrmInstance } from './Typo/instance';
|
|
26
|
+
import type { FxOrmCommon } from './Typo/_common';
|
|
27
|
+
import type { FxOrmNS } from './Typo/ORM';
|
|
28
|
+
import type { FxOrmSqlDDLSync } from '../sql-ddl-sync/index';
|
|
29
|
+
import type { FxOrmDMLDriver } from './Typo/DMLDriver';
|
|
30
|
+
import type { FxOrmError } from './Typo/Error';
|
|
31
|
+
import type { FxOrmQuery } from './Typo/query';
|
|
32
|
+
const fc = require('fib-cache');
|
|
33
|
+
|
|
34
|
+
import type {
|
|
35
|
+
FxSqlQuerySubQuery,
|
|
36
|
+
FxSqlQueryColumns,
|
|
37
|
+
} from '../sql-query/Query';
|
|
38
|
+
import { pickPointTypeFields } from './Drivers/DML/_dml-helpers';
|
|
39
|
+
|
|
40
|
+
const AvailableHooks: (keyof FxOrmModel.Hooks)[] = [
|
|
41
|
+
"beforeCreate", "afterCreate",
|
|
42
|
+
"beforeSave", "afterSave",
|
|
43
|
+
"beforeValidation", "afterValidation",
|
|
44
|
+
"beforeRemove", "afterRemove",
|
|
45
|
+
"afterLoad",
|
|
46
|
+
"afterAutoFetch"
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
export const Model = function (
|
|
50
|
+
m_opts: FxOrmModel.ModelConstructorOptions
|
|
51
|
+
) {
|
|
52
|
+
m_opts = util.extend(m_opts || {}, { keys: m_opts.keys || [] });
|
|
53
|
+
m_opts.keys = (Array.isArray(m_opts.keys) ? m_opts.keys : [m_opts.keys]).filter(Boolean);
|
|
54
|
+
|
|
55
|
+
const one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[] = [];
|
|
56
|
+
const many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[] = [];
|
|
57
|
+
const extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[] = [];
|
|
58
|
+
const association_properties: string[] = [];
|
|
59
|
+
const model_selectable_fields: FxSqlQueryColumns.SelectInputArgType[] = [];
|
|
60
|
+
const fieldToPropertyMap: FxOrmProperty.FieldToPropertyMapType = {};
|
|
61
|
+
/**
|
|
62
|
+
* @description compared to m_opts.properties, allProperties means all properties including:
|
|
63
|
+
* 1. association key properties, such as `{assoc_name}_{id}` property in default `hasOne` case,
|
|
64
|
+
* which was not included by m_opts.properties;
|
|
65
|
+
* 2. dynamically added properties by `model.addProperty(...)`, in fact, all association-about properties
|
|
66
|
+
* were added by `addProperty` method
|
|
67
|
+
*/
|
|
68
|
+
const allProperties: Record<string, FxOrmProperty.NormalizedProperty> = {};
|
|
69
|
+
const virtualProperties: Record<string, FxOrmProperty.NormalizedProperty> = {};
|
|
70
|
+
// const __propertiesByName = new Proxy<typeof allProperties>(allProperties, {
|
|
71
|
+
// get: (target, prop) => {
|
|
72
|
+
// return target[prop as string] || virtualProperties[prop as string];
|
|
73
|
+
// },
|
|
74
|
+
// has: (target, prop) => {
|
|
75
|
+
// return Reflect.has(target, prop) || (prop in virtualProperties);
|
|
76
|
+
// },
|
|
77
|
+
// ownKeys: (target) => {
|
|
78
|
+
// return Reflect.ownKeys(target).concat(Object.keys(virtualProperties));
|
|
79
|
+
// }
|
|
80
|
+
// });
|
|
81
|
+
|
|
82
|
+
const keyProperties: FxOrmProperty.NormalizedProperty[] = [];
|
|
83
|
+
|
|
84
|
+
const initialHooks = Object.assign({}, m_opts.hooks)
|
|
85
|
+
|
|
86
|
+
const createInstanceSync = function (
|
|
87
|
+
_data: FxOrmInstance.InstanceDataPayload,
|
|
88
|
+
inst_opts: FxOrmInstance.CreateOptions,
|
|
89
|
+
cb?: FxOrmCommon.GenericCallback<FxOrmInstance.Instance>
|
|
90
|
+
): FxOrmInstance.Instance {
|
|
91
|
+
if (!inst_opts) {
|
|
92
|
+
inst_opts = {};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// shallow copy to ensure properties are configurable (deletable)
|
|
96
|
+
const data = { ..._data } as FxOrmInstance.InstanceDataPayload;
|
|
97
|
+
|
|
98
|
+
let found_assoc = false;
|
|
99
|
+
|
|
100
|
+
for (let k in data) {
|
|
101
|
+
if (k === "extra_field") continue;
|
|
102
|
+
if (m_opts.properties.hasOwnProperty(k)) continue;
|
|
103
|
+
if (inst_opts.extra && inst_opts.extra.hasOwnProperty(k)) continue;
|
|
104
|
+
if (m_opts.keys.indexOf(k) >= 0) continue;
|
|
105
|
+
if (association_properties.indexOf(k) >= 0) continue;
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < one_associations.length; i++) {
|
|
108
|
+
if (one_associations[i].name === k) {
|
|
109
|
+
found_assoc = true;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!found_assoc) {
|
|
114
|
+
for (let i = 0; i < many_associations.length; i++) {
|
|
115
|
+
if (many_associations[i].name === k) {
|
|
116
|
+
found_assoc = true;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (!found_assoc) {
|
|
122
|
+
const desc = Object.getOwnPropertyDescriptor(data, k);
|
|
123
|
+
if (!desc || desc.configurable) {
|
|
124
|
+
delete data[k];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const assoc_opts = {
|
|
130
|
+
autoFetch : inst_opts.autoFetch || false,
|
|
131
|
+
autoFetchLimit : inst_opts.autoFetchLimit,
|
|
132
|
+
cascadeRemove : inst_opts.cascadeRemove
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const __setupAssociations = function (instance: FxOrmInstance.Instance) {
|
|
136
|
+
OneAssociation.extend(model, instance, m_opts.driver, one_associations, { assoc_opts });
|
|
137
|
+
ManyAssociation.extend(model, instance, m_opts.driver, many_associations, { assoc_opts });
|
|
138
|
+
ExtendAssociation.extend(model, instance, m_opts.driver, extend_associations, { assoc_opts });
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const instance = new Instance(model, {
|
|
142
|
+
uid : inst_opts.uid || m_opts.uid, // singleton unique id
|
|
143
|
+
keys : m_opts.keys,
|
|
144
|
+
isNew : inst_opts.isNew || false,
|
|
145
|
+
isShell : inst_opts.isShell || false,
|
|
146
|
+
data : data,
|
|
147
|
+
autoSave : inst_opts.autoSave || false,
|
|
148
|
+
extra : inst_opts.extra,
|
|
149
|
+
extra_info : inst_opts.extra_info,
|
|
150
|
+
driver : m_opts.driver,
|
|
151
|
+
table : m_opts.table,
|
|
152
|
+
hooks : m_opts.hooks,
|
|
153
|
+
methods : m_opts.methods,
|
|
154
|
+
validations : m_opts.validations,
|
|
155
|
+
one_associations : one_associations,
|
|
156
|
+
many_associations : many_associations,
|
|
157
|
+
extend_associations : extend_associations,
|
|
158
|
+
association_properties : association_properties,
|
|
159
|
+
__setupAssociations : __setupAssociations,
|
|
160
|
+
fieldToPropertyMap : fieldToPropertyMap,
|
|
161
|
+
keyProperties : keyProperties,
|
|
162
|
+
events : m_opts.ievents
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
if (model_selectable_fields !== null) {
|
|
166
|
+
LazyLoad.extend(instance, model, m_opts.properties);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
OneAssociation.autoFetch(instance, one_associations, assoc_opts, false);
|
|
170
|
+
ManyAssociation.autoFetch(instance, many_associations, assoc_opts, false);
|
|
171
|
+
ExtendAssociation.autoFetch(instance, extend_associations, assoc_opts, false);
|
|
172
|
+
|
|
173
|
+
Hook.wait(instance, m_opts.hooks.afterAutoFetch, function (err: Error) {
|
|
174
|
+
if (err)
|
|
175
|
+
throw err
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
return instance;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const model = function (
|
|
182
|
+
..._data: FxOrmModel.ModelInstanceConstructorOptions
|
|
183
|
+
) {
|
|
184
|
+
let data = _data.length > 1 ? _data : _data[0];
|
|
185
|
+
|
|
186
|
+
if (Array.isArray(m_opts.keys) && Array.isArray(data)) {
|
|
187
|
+
if (data.length == m_opts.keys.length) {
|
|
188
|
+
const data2: FxOrmModel.ModelInstanceConstructorOptions[0] = {};
|
|
189
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
190
|
+
data2[m_opts.keys[i]] = data[i];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return createInstanceSync(data2, { isShell: true });
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
const err: FxOrmNS.ExtensibleError = new Error('Model requires ' + m_opts.keys.length + ' keys, only ' + data.length + ' were provided');
|
|
197
|
+
err.model = m_opts.table;
|
|
198
|
+
|
|
199
|
+
throw err;
|
|
200
|
+
}
|
|
201
|
+
} else if (typeof data === "number" || typeof data === "string") {
|
|
202
|
+
const data2: FxOrmModel.ModelInstanceConstructorOptions[0] = {};
|
|
203
|
+
data2[m_opts.keys[0]] = data;
|
|
204
|
+
|
|
205
|
+
return createInstanceSync(data2, { isShell: true });
|
|
206
|
+
} else if (typeof data === "undefined") {
|
|
207
|
+
data = {};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let isNew = false;
|
|
211
|
+
|
|
212
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
213
|
+
if (!data.hasOwnProperty(m_opts.keys[i])) {
|
|
214
|
+
isNew = true;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (keyProperties.length != 1 || (keyProperties.length == 1 && keyProperties[0].type != 'serial')) {
|
|
220
|
+
isNew = true;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return createInstanceSync(data, {
|
|
224
|
+
isNew,
|
|
225
|
+
autoSave: m_opts.autoSave,
|
|
226
|
+
cascadeRemove: m_opts.cascadeRemove
|
|
227
|
+
});
|
|
228
|
+
} as FxOrmModel.Model;
|
|
229
|
+
|
|
230
|
+
Utilities.addUnwritableProperty(model, 'name', m_opts.name || m_opts.table, { configurable: false })
|
|
231
|
+
Utilities.addUnwritableProperty(model, 'allProperties', allProperties, { configurable: false })
|
|
232
|
+
Utilities.addUnwritableProperty(model, 'virtualProperties', virtualProperties, { configurable: false })
|
|
233
|
+
Utilities.addHiddenReadonlyProperty(model, '__propertiesByName', function () {
|
|
234
|
+
return Object.assign({}, allProperties, virtualProperties);
|
|
235
|
+
}, { configurable: false })
|
|
236
|
+
|
|
237
|
+
Utilities.addUnwritableProperty(model, 'properties', m_opts.properties, { configurable: false })
|
|
238
|
+
Utilities.addUnwritableProperty(model, 'settings', m_opts.settings, { configurable: false })
|
|
239
|
+
Utilities.addUnwritableProperty(model, 'keys', m_opts.keys, { configurable: false })
|
|
240
|
+
Utilities.addUnwritableProperty(model, 'caches', new fc.LRU({
|
|
241
|
+
max: m_opts.instanceCacheSize > 0 && Number.isInteger(m_opts.instanceCacheSize) ? m_opts.instanceCacheSize : m_opts.settings.get('instance.defaultCacheSize'),
|
|
242
|
+
ttl: (typeof m_opts.identityCache === 'number' ? m_opts.identityCache : 1) * 1000
|
|
243
|
+
}), { configurable: false })
|
|
244
|
+
|
|
245
|
+
model.dropSync = function (
|
|
246
|
+
this: FxOrmModel.Model,
|
|
247
|
+
) {
|
|
248
|
+
if (typeof m_opts.driver.doDrop !== "function") {
|
|
249
|
+
throw new ORMError("Driver does not support Model.drop()", 'NO_SUPPORT', { model: m_opts.table });
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (Utilities.isVirtualViewModel(model)) {
|
|
253
|
+
return ;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return m_opts.driver.doDrop({
|
|
257
|
+
table : m_opts.table,
|
|
258
|
+
properties : m_opts.properties,
|
|
259
|
+
one_associations : one_associations,
|
|
260
|
+
many_associations : many_associations
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
model.drop = function (
|
|
265
|
+
this:FxOrmModel.Model,
|
|
266
|
+
cb?: FxOrmCommon.GenericCallback<void>
|
|
267
|
+
) {
|
|
268
|
+
const syncResponse = Utilities.catchBlocking(model.dropSync, [], { thisArg: model });
|
|
269
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
270
|
+
|
|
271
|
+
return this;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
model.syncSync = function () {
|
|
275
|
+
if (typeof m_opts.driver.doSync !== "function") {
|
|
276
|
+
throw new ORMError("Driver does not support Model.sync()", 'NO_SUPPORT', { model: m_opts.table })
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (Utilities.isVirtualViewModel(model)) {
|
|
280
|
+
if (m_opts.driver.ddlSync.hasCollectionSync(m_opts.driver.sqlDriver, m_opts.table)) {
|
|
281
|
+
console.warn(`model '${model.name}' defined as virtual view model, but collection with same already exists.`)
|
|
282
|
+
}
|
|
283
|
+
return ;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
m_opts.driver.doSync({
|
|
287
|
+
repair_column : !!model.settings.get(`model.dbsync.repair_column.${model.name}`),
|
|
288
|
+
allow_drop_column : !!model.settings.get(`model.dbsync.allow_drop_column.${model.name}`),
|
|
289
|
+
extension : m_opts.__for_extension,
|
|
290
|
+
id : m_opts.keys,
|
|
291
|
+
table : m_opts.table,
|
|
292
|
+
tableComment : m_opts.tableComment,
|
|
293
|
+
allProperties : allProperties,
|
|
294
|
+
indexes : m_opts.indexes || [],
|
|
295
|
+
customTypes : m_opts.db.customTypes,
|
|
296
|
+
one_associations : one_associations,
|
|
297
|
+
many_associations : many_associations,
|
|
298
|
+
extend_associations : extend_associations
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
model.sync = function <T>(
|
|
303
|
+
this:FxOrmModel.Model,
|
|
304
|
+
cb?: FxOrmCommon.GenericCallback<FxOrmSqlDDLSync.SyncResult>
|
|
305
|
+
) {
|
|
306
|
+
const syncResponse = Utilities.catchBlocking(model.syncSync, [], { thisArg: model });
|
|
307
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
308
|
+
|
|
309
|
+
return this;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const collectParamsForGet = function (ids: any[]) {
|
|
313
|
+
let options = {} as FxOrmModel.ModelOptions__Get;
|
|
314
|
+
|
|
315
|
+
ids = ids.filter(x => x !== undefined && x !== null)
|
|
316
|
+
if (typeof ids[ids.length - 1] === "object" && !Array.isArray(ids[ids.length - 1])) {
|
|
317
|
+
options = ids.pop();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (ids.length === 1 && Array.isArray(ids[0])) {
|
|
321
|
+
ids = ids[0];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (ids.length !== m_opts.keys.length) {
|
|
325
|
+
throw new ORMError("Model.get() IDs number mismatch (" + m_opts.keys.length + " needed, " + ids.length + " passed)", 'PARAM_MISMATCH', { model: m_opts.table });
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return { options, ids }
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
model.getSync = function (
|
|
332
|
+
this: FxOrmModel.Model,
|
|
333
|
+
...args
|
|
334
|
+
) {
|
|
335
|
+
if (!keyProperties.length) {
|
|
336
|
+
Utilities.disAllowOpForVModel(model, 'model.get');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
|
|
340
|
+
let prop: FxOrmProperty.NormalizedProperty;
|
|
341
|
+
|
|
342
|
+
const { options, ids } = collectParamsForGet(args);
|
|
343
|
+
|
|
344
|
+
for (let i = 0; i < keyProperties.length; i++) {
|
|
345
|
+
prop = keyProperties[i];
|
|
346
|
+
conditions[prop.mapsTo || prop.name] = ids[i];
|
|
347
|
+
}
|
|
348
|
+
Utilities.filterWhereConditionsInput(conditions!, model);
|
|
349
|
+
|
|
350
|
+
if (!options.hasOwnProperty("autoFetch")) {
|
|
351
|
+
options.autoFetch = m_opts.autoFetch;
|
|
352
|
+
}
|
|
353
|
+
if (!options.hasOwnProperty("autoFetchLimit")) {
|
|
354
|
+
options.autoFetchLimit = m_opts.autoFetchLimit;
|
|
355
|
+
}
|
|
356
|
+
if (!options.hasOwnProperty("cascadeRemove")) {
|
|
357
|
+
options.cascadeRemove = m_opts.cascadeRemove;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
let founditems: FxOrmDMLDriver.QueryDataPayload[],
|
|
361
|
+
err: FxOrmError.ExtendedError
|
|
362
|
+
|
|
363
|
+
function deferGet () {
|
|
364
|
+
const vFields = Object.entries(model.virtualProperties).map(([k, p]) => p.mapsTo || k);;
|
|
365
|
+
const { tableConditions, topConditions } = Utilities.extractSelectTopConditions(conditions, vFields);
|
|
366
|
+
|
|
367
|
+
const __pointTypeMapsTo = pickPointTypeFields(m_opts.driver, allProperties);
|
|
368
|
+
|
|
369
|
+
try {
|
|
370
|
+
founditems = m_opts.driver.find(
|
|
371
|
+
model_selectable_fields,
|
|
372
|
+
m_opts.table,
|
|
373
|
+
tableConditions,
|
|
374
|
+
{
|
|
375
|
+
limit: 1,
|
|
376
|
+
topConditions,
|
|
377
|
+
selectVirtualFields: vFields,
|
|
378
|
+
generateSqlSelect: m_opts.generateSqlSelect,
|
|
379
|
+
__pointTypeMapsTo,
|
|
380
|
+
}
|
|
381
|
+
);
|
|
382
|
+
} catch (ex) {
|
|
383
|
+
const code = typeof ex === 'object' && ex !== null && 'code' in ex ? String(ex.code) : undefined;
|
|
384
|
+
throw new ORMError(ex instanceof Error ? ex.message : String(ex), 'QUERY_ERROR', code ? { originalCode: code } : undefined);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (founditems.length === 0) {
|
|
388
|
+
throw new ORMError("Not found", 'NOT_FOUND', { model: m_opts.table });
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
Utilities.renameDatastoreFieldsToPropertyNames(founditems[0], fieldToPropertyMap);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const uid = Utilities.generateUID4SoloGet(m_opts, ids);
|
|
395
|
+
return Singleton.modelGet(
|
|
396
|
+
model,
|
|
397
|
+
uid,
|
|
398
|
+
{
|
|
399
|
+
identityCache : (options.hasOwnProperty("identityCache") ? options.identityCache : m_opts.identityCache),
|
|
400
|
+
saveCheck : m_opts.settings.get("instance.identityCacheSaveCheck")
|
|
401
|
+
},
|
|
402
|
+
function () {
|
|
403
|
+
deferGet();
|
|
404
|
+
|
|
405
|
+
return createInstanceSync(founditems[0], {
|
|
406
|
+
uid : uid,
|
|
407
|
+
autoSave : options.autoSave,
|
|
408
|
+
autoFetch : (options.autoFetchLimit === 0 ? false : options.autoFetch),
|
|
409
|
+
autoFetchLimit : options.autoFetchLimit,
|
|
410
|
+
cascadeRemove : options.cascadeRemove
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
model.get = function (
|
|
417
|
+
this: FxOrmModel.Model,
|
|
418
|
+
...args: any[]
|
|
419
|
+
) {
|
|
420
|
+
const cb = Helpers.pickCallback<FxOrmModel.ModelMethodCallback__Get>(args);
|
|
421
|
+
|
|
422
|
+
args = args.filter(x => x !== cb);
|
|
423
|
+
|
|
424
|
+
if (!cb)
|
|
425
|
+
throw new ORMError("Missing Model.get() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
426
|
+
|
|
427
|
+
collectParamsForGet(args);
|
|
428
|
+
|
|
429
|
+
process.nextTick(() => {
|
|
430
|
+
const syncReponse = Utilities.catchBlocking<FxOrmInstance.Instance>(model.getSync, args, { thisArg: model });
|
|
431
|
+
Utilities.takeAwayResult(syncReponse, { no_throw: true, callback: cb });
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
return this;
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
const chainOrRunSync = function (
|
|
438
|
+
this: FxOrmModel.Model,
|
|
439
|
+
..._args: any[]
|
|
440
|
+
) {
|
|
441
|
+
let conditions: FxSqlQuerySubQuery.SubQueryConditions | null = null;
|
|
442
|
+
let options = {} as FxOrmModel.ModelOptions__Find;
|
|
443
|
+
let order: FxOrmModel.ModelOptions__Find['order'] = [];
|
|
444
|
+
let merges: FxOrmQuery.ChainFindMergeInfo[] = [];
|
|
445
|
+
|
|
446
|
+
Helpers.selectArgs(arguments, (arg_type, arg) => {
|
|
447
|
+
switch (arg_type) {
|
|
448
|
+
case "number":
|
|
449
|
+
options.limit = arg;
|
|
450
|
+
break;
|
|
451
|
+
case "object":
|
|
452
|
+
if (Array.isArray(arg)) {
|
|
453
|
+
if (arg.length > 0) {
|
|
454
|
+
order = arg;
|
|
455
|
+
}
|
|
456
|
+
} else {
|
|
457
|
+
if (conditions === null) {
|
|
458
|
+
conditions = { ...arg };
|
|
459
|
+
Utilities.filterWhereConditionsInput(conditions!, model);
|
|
460
|
+
} else {
|
|
461
|
+
if (options.hasOwnProperty("limit")) {
|
|
462
|
+
arg.limit = options.limit;
|
|
463
|
+
}
|
|
464
|
+
options = arg;
|
|
465
|
+
|
|
466
|
+
if (options.hasOwnProperty("__merge")) {
|
|
467
|
+
merges = Utilities.combineMergeInfoToArray(options.__merge || []);
|
|
468
|
+
merges.forEach(merge => {
|
|
469
|
+
if (!Array.isArray(merge.select) || !merge.select.length) {
|
|
470
|
+
merge.select = [];
|
|
471
|
+
|
|
472
|
+
if (options.extra && Object.keys(options.extra).length)
|
|
473
|
+
merge.select = merge.select.concat(Object.keys(options.extra))
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
merge.select = merge.select.map(field => {
|
|
477
|
+
if (typeof field === 'object') return field;
|
|
478
|
+
|
|
479
|
+
const propname = field;
|
|
480
|
+
const mapsTo = options.extra?.[field]?.mapsTo || field;
|
|
481
|
+
|
|
482
|
+
return {
|
|
483
|
+
as: propname,
|
|
484
|
+
column_name: mapsTo
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
merge.select = Array.from( new Set(merge.select) )
|
|
489
|
+
});
|
|
490
|
+
delete options.__merge;
|
|
491
|
+
}
|
|
492
|
+
if (options.hasOwnProperty("order")) {
|
|
493
|
+
order = options.order;
|
|
494
|
+
delete options.order;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
break;
|
|
499
|
+
case "string":
|
|
500
|
+
order = arg
|
|
501
|
+
// if (arg[0] === "-") {
|
|
502
|
+
// order = [ arg.substr(1), "Z" ];
|
|
503
|
+
// } else {
|
|
504
|
+
// order = [ arg ];
|
|
505
|
+
// }
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
if (!options.hasOwnProperty("limit")) {
|
|
511
|
+
options.limit = m_opts.settings.get('instance.defaultFindLimit');
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (!options.hasOwnProperty("identityCache")) {
|
|
515
|
+
options.identityCache = m_opts.identityCache;
|
|
516
|
+
}
|
|
517
|
+
if (!options.hasOwnProperty("autoFetchLimit")) {
|
|
518
|
+
options.autoFetchLimit = m_opts.autoFetchLimit;
|
|
519
|
+
}
|
|
520
|
+
if (!options.hasOwnProperty("cascadeRemove")) {
|
|
521
|
+
options.cascadeRemove = m_opts.cascadeRemove;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
let normalized_order_without_table: FxOrmQuery.OrderNormalizedTupleMixin = []
|
|
525
|
+
if (order) {
|
|
526
|
+
normalized_order_without_table = Utilities.standardizeOrder(order);
|
|
527
|
+
} else {
|
|
528
|
+
normalized_order_without_table = []
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
let base_table = m_opts.table;
|
|
532
|
+
|
|
533
|
+
let normalized_order = normalized_order_without_table
|
|
534
|
+
if (merges && merges.length) {
|
|
535
|
+
base_table = options.chainfind_linktable || base_table;
|
|
536
|
+
normalized_order = Utilities.addTableToStandardedOrder(
|
|
537
|
+
normalized_order_without_table,
|
|
538
|
+
Utilities.parseTableInputForSelect(base_table).alias
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (conditions) {
|
|
543
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return new ChainFind(model, {
|
|
547
|
+
only: options.only || model_selectable_fields,
|
|
548
|
+
keys: m_opts.keys,
|
|
549
|
+
table: base_table,
|
|
550
|
+
driver: m_opts.driver,
|
|
551
|
+
conditions: conditions!,
|
|
552
|
+
associations: many_associations,
|
|
553
|
+
limit: options.limit,
|
|
554
|
+
order: normalized_order || [],
|
|
555
|
+
merge: merges,
|
|
556
|
+
exists: options.exists || [],
|
|
557
|
+
offset: options.offset,
|
|
558
|
+
properties: model.__propertiesByName,
|
|
559
|
+
keyProperties: keyProperties,
|
|
560
|
+
generateSqlSelect: m_opts.generateSqlSelect,
|
|
561
|
+
newInstanceSync: function (data: any) {
|
|
562
|
+
// We need to do the rename before we construct the UID & do the cache lookup
|
|
563
|
+
// because the cache is loaded using propertyName rather than fieldName
|
|
564
|
+
Utilities.renameDatastoreFieldsToPropertyNames(data, fieldToPropertyMap);
|
|
565
|
+
|
|
566
|
+
// Construct UID
|
|
567
|
+
const uid = Utilities.generateUID4ChainFind(m_opts, merges, data);
|
|
568
|
+
|
|
569
|
+
// Now we can do the cache lookup
|
|
570
|
+
return Singleton.modelGet(
|
|
571
|
+
model,
|
|
572
|
+
uid,
|
|
573
|
+
{
|
|
574
|
+
identityCache : options.identityCache,
|
|
575
|
+
saveCheck : m_opts.settings.get("instance.identityCacheSaveCheck")
|
|
576
|
+
},
|
|
577
|
+
function () {
|
|
578
|
+
return createInstanceSync(data, {
|
|
579
|
+
uid : uid,
|
|
580
|
+
autoSave : m_opts.autoSave,
|
|
581
|
+
autoFetch : (options.autoFetchLimit === 0 ? false : (options.autoFetch || m_opts.autoFetch)),
|
|
582
|
+
autoFetchLimit : options.autoFetchLimit,
|
|
583
|
+
cascadeRemove : options.cascadeRemove,
|
|
584
|
+
extra : options.extra,
|
|
585
|
+
extra_info : options.extra_info
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
model.findSync = function (
|
|
594
|
+
this:FxOrmModel.Model,
|
|
595
|
+
...args: any[]
|
|
596
|
+
) {
|
|
597
|
+
const chain: FxOrmQuery.IChainFind = chainOrRunSync.apply(model, args);
|
|
598
|
+
|
|
599
|
+
return chain.runSync()
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
model.find = function (
|
|
603
|
+
this:FxOrmModel.Model,
|
|
604
|
+
...args: any[]
|
|
605
|
+
) {
|
|
606
|
+
var cb: FxOrmModel.ModelMethodCallback__Find | null = null;
|
|
607
|
+
if (typeof util.last(args) === 'function')
|
|
608
|
+
cb = args.pop();
|
|
609
|
+
|
|
610
|
+
const chain = chainOrRunSync.apply(model, args);
|
|
611
|
+
|
|
612
|
+
if (cb)
|
|
613
|
+
chain.run(cb);
|
|
614
|
+
|
|
615
|
+
return chain;
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
model.where = model.all = model.find;
|
|
619
|
+
model.whereSync = model.allSync = model.findSync;
|
|
620
|
+
|
|
621
|
+
model.oneSync = function (
|
|
622
|
+
this:FxOrmModel.Model,
|
|
623
|
+
...args: any[]
|
|
624
|
+
) {
|
|
625
|
+
const results = this.find(...args).limit(1).runSync();
|
|
626
|
+
|
|
627
|
+
return results.length ? results[0] : null;
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
model.one = function (...args: any[]) {
|
|
631
|
+
const cb = Helpers.pickCallback<FxOrmModel.ModelMethodCallback__Get>(args);
|
|
632
|
+
|
|
633
|
+
args = args.filter(x => x !== cb);
|
|
634
|
+
|
|
635
|
+
if (!cb) {
|
|
636
|
+
throw new ORMError("Missing Model.one() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
const done = cb;
|
|
640
|
+
const newCb = function (err: FxOrmError.ExtendedError | null, results: FxOrmInstance.Instance[]) {
|
|
641
|
+
if (err)
|
|
642
|
+
return done(err);
|
|
643
|
+
|
|
644
|
+
return (done as any)(null, results.length ? results[0] : null);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
args.push(1);
|
|
648
|
+
|
|
649
|
+
const chain = this.find(...args);
|
|
650
|
+
|
|
651
|
+
chain.run(newCb);
|
|
652
|
+
|
|
653
|
+
return chain;
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
model.countSync = function (conditions) {
|
|
657
|
+
Utilities.disAllowOpForVModel(model, 'model.count');
|
|
658
|
+
|
|
659
|
+
if (conditions) {
|
|
660
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
661
|
+
Utilities.filterWhereConditionsInput(conditions!, model);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const data = m_opts.driver.count(
|
|
665
|
+
m_opts.table,
|
|
666
|
+
conditions || {},
|
|
667
|
+
{}
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
if (data.length === 0)
|
|
671
|
+
return 0;
|
|
672
|
+
|
|
673
|
+
return data[0].c;
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
model.count = function () {
|
|
677
|
+
var conditions: FxSqlQuerySubQuery.SubQueryConditions = {};
|
|
678
|
+
const cb = Helpers.pickCallback<FxOrmModel.ModelMethodCallback__Count>(arguments);
|
|
679
|
+
|
|
680
|
+
Helpers.selectArgs(arguments, (arg_type, arg) => {
|
|
681
|
+
switch (arg_type) {
|
|
682
|
+
case "object":
|
|
683
|
+
conditions = arg;
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
if (!cb) {
|
|
689
|
+
throw new ORMError("Missing Model.count() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
process.nextTick(() => {
|
|
693
|
+
const syncResponse = Utilities.catchBlocking(model.countSync, [conditions, cb], {thisArg: model});
|
|
694
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
return this;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
model.aggregate = function () {
|
|
701
|
+
Utilities.disAllowOpForVModel(model, 'model.aggregate');
|
|
702
|
+
|
|
703
|
+
var conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
|
|
704
|
+
var propertyList: string[] = [];
|
|
705
|
+
|
|
706
|
+
Helpers.selectArgs(arguments, (arg_type, arg) => {
|
|
707
|
+
if (arg_type === 'object') {
|
|
708
|
+
if (Array.isArray(arg)) {
|
|
709
|
+
propertyList = arg;
|
|
710
|
+
} else {
|
|
711
|
+
conditions = arg;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
if (conditions) {
|
|
717
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
718
|
+
Utilities.filterWhereConditionsInput(conditions!, model);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
return new AggregateFunctions({
|
|
722
|
+
table : m_opts.table,
|
|
723
|
+
driver_name : m_opts.driver_name,
|
|
724
|
+
driver : m_opts.driver,
|
|
725
|
+
conditions : conditions,
|
|
726
|
+
propertyList : propertyList,
|
|
727
|
+
properties : allProperties
|
|
728
|
+
});
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
model.existsSync = function (...ids) {
|
|
732
|
+
Utilities.disAllowOpForVModel(model, 'model.exists');
|
|
733
|
+
|
|
734
|
+
let conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* assign keys' id columns comparator-eq value
|
|
738
|
+
* as its order in `m_opts.keys`
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* define('test', {
|
|
742
|
+
* key_1: {
|
|
743
|
+
* type: 'integer', key: true
|
|
744
|
+
* },
|
|
745
|
+
* key_2: {
|
|
746
|
+
* type: 'integer', key: true
|
|
747
|
+
* }
|
|
748
|
+
* })
|
|
749
|
+
*/
|
|
750
|
+
const firstItem = ids[0];
|
|
751
|
+
if (ids.length === 1 && typeof firstItem === "object") {
|
|
752
|
+
if (Array.isArray(firstItem)) {
|
|
753
|
+
/**
|
|
754
|
+
* @example
|
|
755
|
+
* Person.exists([1, 7])
|
|
756
|
+
*/
|
|
757
|
+
const col_values = firstItem
|
|
758
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
759
|
+
conditions[m_opts.keys[i]] = col_values[i];
|
|
760
|
+
}
|
|
761
|
+
} else {
|
|
762
|
+
/**
|
|
763
|
+
* @access general usage
|
|
764
|
+
* @example
|
|
765
|
+
* Person.exists({key_1: 1, key_2: 7})
|
|
766
|
+
*/
|
|
767
|
+
conditions = firstItem;
|
|
768
|
+
}
|
|
769
|
+
} else {
|
|
770
|
+
/**
|
|
771
|
+
* @example
|
|
772
|
+
* Person.exists(1, 7)
|
|
773
|
+
*/
|
|
774
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
775
|
+
conditions[m_opts.keys[i]] = ids[i] as any;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
if (conditions) {
|
|
780
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
781
|
+
Utilities.filterWhereConditionsInput(conditions!, model);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const data = m_opts.driver.count(m_opts.table, conditions, {});
|
|
785
|
+
|
|
786
|
+
if (data.length === 0)
|
|
787
|
+
return false;
|
|
788
|
+
|
|
789
|
+
return data[0].c > 0;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
model.exists = function (...ids) {
|
|
793
|
+
var cb: FxOrmModel.ModelMethodCallback__Boolean | null = null;
|
|
794
|
+
const candidate = ids.pop();
|
|
795
|
+
if (typeof candidate === 'function') {
|
|
796
|
+
cb = candidate;
|
|
797
|
+
} else if (candidate !== undefined) {
|
|
798
|
+
ids.push(candidate);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
if (typeof cb !== "function") {
|
|
802
|
+
throw new ORMError("Missing Model.exists() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const syncResponse = Utilities.catchBlocking(model.existsSync, ids, { thisArg: model });
|
|
806
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
807
|
+
|
|
808
|
+
return this;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
model.create = function (...args: any[]) {
|
|
812
|
+
const doneHolder: { value: FxOrmCommon.ExecutionCallback<FxOrmInstance.Instance | FxOrmInstance.Instance[]> | null } = { value: null };
|
|
813
|
+
Helpers.selectArgs(arguments, (arg_type, arg, idx) => {
|
|
814
|
+
switch (arg_type) {
|
|
815
|
+
case "function":
|
|
816
|
+
doneHolder.value = arg;
|
|
817
|
+
args = args.filter(x => x !== doneHolder.value)
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
const syncResponse = Utilities.catchBlocking(
|
|
823
|
+
model.createSync,
|
|
824
|
+
args,
|
|
825
|
+
{ thisArg: model }
|
|
826
|
+
)
|
|
827
|
+
|
|
828
|
+
Utilities.takeAwayResult(syncResponse, { callback: doneHolder.value || undefined });
|
|
829
|
+
|
|
830
|
+
return syncResponse.result;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
model.createSync = function (): any {
|
|
834
|
+
Utilities.disAllowOpForVModel(model, 'model.create');
|
|
835
|
+
|
|
836
|
+
let create_single: boolean = false;
|
|
837
|
+
let opts: FxOrmModel.ModelOptions__Create = {};
|
|
838
|
+
let itemsParams: FxOrmInstance.InstanceDataPayload[] = []
|
|
839
|
+
|
|
840
|
+
Helpers.selectArgs(arguments, (arg_type, arg, idx) => {
|
|
841
|
+
switch (arg_type) {
|
|
842
|
+
case "object":
|
|
843
|
+
if ( !create_single && Array.isArray(arg) ) {
|
|
844
|
+
itemsParams = itemsParams.concat(arg);
|
|
845
|
+
} else if (idx === 0) {
|
|
846
|
+
create_single = true;
|
|
847
|
+
itemsParams.push(arg);
|
|
848
|
+
} else {
|
|
849
|
+
opts = { parallel: false };
|
|
850
|
+
}
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
const items: FxOrmInstance.Instance[] = Utilities.parallelQueryIfPossible(
|
|
856
|
+
false,
|
|
857
|
+
itemsParams,
|
|
858
|
+
(data) => {
|
|
859
|
+
const item = createInstanceSync(data, {
|
|
860
|
+
isNew : true,
|
|
861
|
+
autoSave : m_opts.autoSave,
|
|
862
|
+
// not fetch associated instance on its creation.
|
|
863
|
+
autoFetch : false
|
|
864
|
+
});
|
|
865
|
+
|
|
866
|
+
item.saveSync();
|
|
867
|
+
|
|
868
|
+
return item;
|
|
869
|
+
}
|
|
870
|
+
)
|
|
871
|
+
|
|
872
|
+
const results = create_single ? items[0] : items;
|
|
873
|
+
|
|
874
|
+
return results;
|
|
875
|
+
};
|
|
876
|
+
|
|
877
|
+
model.clearSync = function () {
|
|
878
|
+
Utilities.disAllowOpForVModel(model, 'model.clear');
|
|
879
|
+
|
|
880
|
+
return m_opts.driver.clear(m_opts.table);
|
|
881
|
+
};
|
|
882
|
+
|
|
883
|
+
model.clear = function (cb?) {
|
|
884
|
+
Utilities.disAllowOpForVModel(model, 'model.clear');
|
|
885
|
+
|
|
886
|
+
m_opts.driver.clear(m_opts.table, function (err: Error) {
|
|
887
|
+
if (typeof cb === "function") cb(err);
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
return this;
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
model.prependValidation = function (key: string, validation: FibjsEnforce.IValidator) {
|
|
894
|
+
if(m_opts.validations.hasOwnProperty(key)) {
|
|
895
|
+
(m_opts.validations[key] as FibjsEnforce.IValidator[]).splice(0, 0, validation);
|
|
896
|
+
} else {
|
|
897
|
+
m_opts.validations[key] = [validation];
|
|
898
|
+
}
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
// control current owned fields
|
|
902
|
+
const currFields: {[k: string]: true} = {};
|
|
903
|
+
|
|
904
|
+
model.findBy = function (...args: any[]): FxOrmQuery.IChainFind {
|
|
905
|
+
if (Array.isArray(args[0])) {
|
|
906
|
+
const [by_list, self_conditions = {}, self_options, cb] = args as FxOrmModel.FindByListStyleFunctionArgs
|
|
907
|
+
return listFindByChainOrRunSync(model, self_conditions, by_list, self_options, { callback: cb }) as FxOrmQuery.IChainFind;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
const [association_name, self_conditions, findby_options, cb] = args as FxOrmModel.FindByItemStyleFunctionArgs
|
|
911
|
+
return soloFindByChainOrRunSync(model, association_name, self_conditions, findby_options, { callback: cb, is_sync: false }) as FxOrmQuery.IChainFind;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
model.findBySync = function (...args: any[]) {
|
|
915
|
+
if (Array.isArray(args[0])) {
|
|
916
|
+
const [by_list, self_conditions = {}, self_options] = args as FxOrmModel.FindByListStyleFunctionArgs
|
|
917
|
+
return listFindByChainOrRunSync(model, self_conditions, by_list, self_options, { is_sync: true }) as any;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
const [association_name, self_conditions, findby_options] = args as FxOrmModel.FindByItemStyleFunctionArgs
|
|
921
|
+
return soloFindByChainOrRunSync(model, association_name, self_conditions, findby_options, { is_sync: true }) as any;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
model.addProperty = function (propIn, options) {
|
|
925
|
+
var cType: FxOrmProperty.CustomPropertyType;
|
|
926
|
+
var prop = Property.normalize({
|
|
927
|
+
prop: propIn as FxOrmModel.ModelPropertyDefinition, name: (options && options.name || propIn.name),
|
|
928
|
+
customTypes: m_opts.db.customTypes, settings: m_opts.settings
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
if (!m_opts.virtualView.disabled) { prop.virtual = true; }
|
|
932
|
+
|
|
933
|
+
if (prop.type === 'serial') {
|
|
934
|
+
prop.key = true;
|
|
935
|
+
prop.klass = prop.klass || 'primary';
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// Maintains backwards compatibility
|
|
939
|
+
if (m_opts.keys.indexOf(prop.name) !== -1) {
|
|
940
|
+
prop.key = true;
|
|
941
|
+
} else if (prop.key) {
|
|
942
|
+
m_opts.keys.push(prop.name);
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
if (options && options.klass) {
|
|
946
|
+
prop.klass = options.klass;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
switch (prop.klass) {
|
|
950
|
+
default:
|
|
951
|
+
m_opts.properties[prop.name] = prop;
|
|
952
|
+
break;
|
|
953
|
+
case 'extendsTo':
|
|
954
|
+
association_properties.push(prop.name)
|
|
955
|
+
break;
|
|
956
|
+
case 'hasOne':
|
|
957
|
+
association_properties.push(prop.name)
|
|
958
|
+
break;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
if (!prop.virtual) { allProperties[prop.name] = prop; }
|
|
962
|
+
else {
|
|
963
|
+
virtualProperties[prop.name] = prop;
|
|
964
|
+
// TODO: should we force virtual view without keys?
|
|
965
|
+
m_opts.keys = m_opts.keys.filter(key => key !== prop.name);
|
|
966
|
+
prop.required = false;
|
|
967
|
+
prop.index = false;
|
|
968
|
+
prop.primary = false;
|
|
969
|
+
delete prop.klass;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
fieldToPropertyMap[prop.mapsTo!] = prop;
|
|
973
|
+
|
|
974
|
+
if (prop.required) {
|
|
975
|
+
model.prependValidation(prop.name, Validators.required());
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
if (Utilities.isKeyProperty(prop) && !m_opts.__for_extension) {
|
|
979
|
+
keyProperties.push(prop);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
if (prop.lazyload !== true && !currFields[prop.name]) {
|
|
983
|
+
currFields[prop.name] = true;
|
|
984
|
+
|
|
985
|
+
if (prop.virtual) {
|
|
986
|
+
return prop;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
if ((cType = m_opts.db.customTypes[prop.type]) && cType.datastoreGet) {
|
|
990
|
+
model_selectable_fields.push({
|
|
991
|
+
a: prop.mapsTo, sql: cType.datastoreGet(prop, m_opts.db.driver.query)
|
|
992
|
+
});
|
|
993
|
+
} else {
|
|
994
|
+
model_selectable_fields.push(prop.mapsTo!);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
return prop;
|
|
999
|
+
} as FxOrmModel.Model['addProperty'];
|
|
1000
|
+
|
|
1001
|
+
Object.defineProperty(model, "table", {
|
|
1002
|
+
value: m_opts.table,
|
|
1003
|
+
enumerable: false
|
|
1004
|
+
});
|
|
1005
|
+
Object.defineProperty(model, "id", {
|
|
1006
|
+
value: m_opts.keys,
|
|
1007
|
+
enumerable: false
|
|
1008
|
+
});
|
|
1009
|
+
Object.defineProperty(model, "uid", {
|
|
1010
|
+
value: Utilities.generateUID4Model(m_opts),
|
|
1011
|
+
enumerable: false
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
// Standardize validations
|
|
1015
|
+
for (let k in m_opts.validations) {
|
|
1016
|
+
const validationHash = m_opts.validations[k]
|
|
1017
|
+
if (!Array.isArray(validationHash)) {
|
|
1018
|
+
m_opts.validations[k] = [ validationHash ];
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
if (Object.keys(m_opts.properties).every((k) => m_opts.properties[k].virtual === true)) {
|
|
1023
|
+
m_opts.virtualView.disabled = false;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
// If no keys are defined add the default one
|
|
1027
|
+
if (m_opts.virtualView.disabled && m_opts.keys.length == 0 && !Object.values(m_opts.properties).some((p: FxOrmProperty.NormalizedProperty) => p.key === true)) {
|
|
1028
|
+
m_opts.properties[m_opts.settings.get("properties.primary_key")] = {
|
|
1029
|
+
type: 'serial', key: true, required: false, klass: 'primary'
|
|
1030
|
+
} as FxOrmProperty.NormalizedProperty;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
let p: FxOrmProperty.NormalizedProperty,
|
|
1034
|
+
keyPrimaryP: FxOrmProperty.NormalizedProperty = null as any,
|
|
1035
|
+
keyP: FxOrmProperty.NormalizedProperty = null as any;
|
|
1036
|
+
// standardize properties
|
|
1037
|
+
for (let k in m_opts.properties) {
|
|
1038
|
+
p = model.addProperty(m_opts.properties[k], { name: k });
|
|
1039
|
+
if (Utilities.isKeyPrimaryProperty(p)) keyPrimaryP = keyPrimaryP || p;
|
|
1040
|
+
else if (Utilities.isKeyProperty(p)) keyP = keyP || p;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
if (keyP && keyPrimaryP) {
|
|
1044
|
+
keyProperties.splice(0, keyProperties.length);
|
|
1045
|
+
keyProperties.push(keyPrimaryP);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// if no any serial-type, keyPrimiary property
|
|
1049
|
+
if (m_opts.virtualView.disabled && keyProperties.length === 0) {
|
|
1050
|
+
if (keyP) // use keyPrimary as the only keyProperties
|
|
1051
|
+
keyProperties.push(keyP);
|
|
1052
|
+
else
|
|
1053
|
+
throw new ORMError("Model defined without any keys", 'BAD_MODEL', { model: m_opts.table });
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// setup hooks
|
|
1057
|
+
for (let k in AvailableHooks) {
|
|
1058
|
+
model[AvailableHooks[k]] = Utilities.createHookHelper(m_opts.hooks, AvailableHooks[k], { initialHooks }) as any;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
Utilities.addUnwritableProperty(model, 'associations', {}, { configurable: false })
|
|
1062
|
+
Utilities.addUnwritableProperty(model, '__keyProperties', keyProperties, { enumerable: false, configurable: false })
|
|
1063
|
+
|
|
1064
|
+
OneAssociation.prepare(model, { one_associations, many_associations, extend_associations }, { db: m_opts.db });
|
|
1065
|
+
ManyAssociation.prepare(model, { one_associations, many_associations, extend_associations }, { db: m_opts.db });
|
|
1066
|
+
ExtendAssociation.prepare(model, { one_associations, many_associations, extend_associations }, { db: m_opts.db });
|
|
1067
|
+
|
|
1068
|
+
return model;
|
|
1069
|
+
} as any as (new (opts: FxOrmModel.ModelDefineOptions) => FxOrmModel.Model);
|
|
1070
|
+
|
|
1071
|
+
function soloFindByChainOrRunSync <T = any>(
|
|
1072
|
+
model: FxOrmModel.Model,
|
|
1073
|
+
association_name: FxOrmModel.ModelFindByDescriptorItem['association_name'],
|
|
1074
|
+
conditions: FxOrmModel.ModelFindByDescriptorItem['conditions'],
|
|
1075
|
+
findby_options: FxOrmModel.ModelFindByDescriptorItem['options'],
|
|
1076
|
+
opts: FxOrmCommon.SyncCallbackInputArags
|
|
1077
|
+
): FxOrmQuery.IChainFind | FxOrmInstance.Instance[] {
|
|
1078
|
+
const { callback: cb, is_sync = false } = opts
|
|
1079
|
+
|
|
1080
|
+
if (is_sync) {
|
|
1081
|
+
const modelFindBySyncAccessor = model.associations[association_name].association.modelFindBySyncAccessor
|
|
1082
|
+
if (!modelFindBySyncAccessor || typeof model[modelFindBySyncAccessor] !== 'function')
|
|
1083
|
+
throw new Error(`[soloFindByChainOrRunSync] invalid association name ${association_name} provided!`)
|
|
1084
|
+
|
|
1085
|
+
return model[modelFindBySyncAccessor](conditions, findby_options)
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
const findByAccessor = model.associations[association_name].association.modelFindByAccessor
|
|
1089
|
+
if (!findByAccessor || typeof model[findByAccessor] !== 'function')
|
|
1090
|
+
throw new Error(`[soloFindByChainOrRunSync] invalid association name ${association_name} provided!`)
|
|
1091
|
+
|
|
1092
|
+
if (typeof cb === 'function')
|
|
1093
|
+
return model[findByAccessor](conditions, findby_options, cb)
|
|
1094
|
+
|
|
1095
|
+
return model[findByAccessor](conditions, findby_options)
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
export function listFindByChainOrRunSync (
|
|
1099
|
+
model: FxOrmModel.Model,
|
|
1100
|
+
self_conditions: FxOrmQuery.QueryConditions__Find,
|
|
1101
|
+
by_list: FxOrmModel.ModelFindByDescriptorItem[],
|
|
1102
|
+
self_options: FxOrmModel.ModelOptions__Find,
|
|
1103
|
+
opts: FxOrmCommon.SyncCallbackInputArags
|
|
1104
|
+
): FxOrmQuery.IChainFind | (FxOrmInstance.Instance[]) {
|
|
1105
|
+
const { callback: cb, is_sync = false } = opts
|
|
1106
|
+
|
|
1107
|
+
self_options = self_options || {};
|
|
1108
|
+
const merges = Utilities.combineMergeInfoToArray(self_options.__merge || []);
|
|
1109
|
+
|
|
1110
|
+
const tableCountHash = {} as {[t: string]: number}
|
|
1111
|
+
function countTable (t: string, is_add: boolean = false) {
|
|
1112
|
+
if (!tableCountHash[t])
|
|
1113
|
+
tableCountHash[t] = 0
|
|
1114
|
+
|
|
1115
|
+
if (is_add)
|
|
1116
|
+
tableCountHash[t]++
|
|
1117
|
+
|
|
1118
|
+
return tableCountHash[t];
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
function getTableAlias(alias_from_t: string) {
|
|
1122
|
+
return `${alias_from_t}${countTable(alias_from_t, true)}`
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
by_list.forEach(by_item => {
|
|
1126
|
+
const association = Helpers.tryGetAssociationItemFromModel(by_item.association_name, model);
|
|
1127
|
+
if (!association)
|
|
1128
|
+
return ;
|
|
1129
|
+
|
|
1130
|
+
const isHasMany = Helpers.getManyAssociationItemFromModel(by_item.association_name, model) === association;
|
|
1131
|
+
const isHasOne = Helpers.getOneAssociationItemFromModel(by_item.association_name, model) === association;
|
|
1132
|
+
const isExtendsTo = Helpers.getExtendsToAssociationItemFromModel(by_item.association_name, model) === association;
|
|
1133
|
+
|
|
1134
|
+
const by_item_conditions = { ...by_item.conditions };
|
|
1135
|
+
Utilities.filterWhereConditionsInput(by_item_conditions, model);
|
|
1136
|
+
|
|
1137
|
+
if (isHasMany) { // support hasmany
|
|
1138
|
+
const left_info = {
|
|
1139
|
+
table: `${model.table}`,
|
|
1140
|
+
alias: getTableAlias(`lt_${model.table}`),
|
|
1141
|
+
ids: model.id
|
|
1142
|
+
}
|
|
1143
|
+
const ljoin_info = {
|
|
1144
|
+
table: `${association.mergeTable}`,
|
|
1145
|
+
alias: getTableAlias(`lj_${association.mergeTable}`),
|
|
1146
|
+
ids: getMapsToFromPropertyHash(association.mergeId)
|
|
1147
|
+
}
|
|
1148
|
+
const rjoin_info = {
|
|
1149
|
+
table: `${association.mergeTable}`,
|
|
1150
|
+
alias: getTableAlias(`rj_${association.mergeTable}`),
|
|
1151
|
+
ids: getMapsToFromPropertyHash(association.mergeAssocId)
|
|
1152
|
+
}
|
|
1153
|
+
const right_info = {
|
|
1154
|
+
table: `${association.model.table}`,
|
|
1155
|
+
alias: getTableAlias(`rt_${association.model.table}`),
|
|
1156
|
+
ids: association.model.id
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
const extraProps = (association as FxOrmAssociation.InstanceAssociationItem_HasMany).props;
|
|
1160
|
+
let extra_where = Utilities.extractHasManyExtraConditions(
|
|
1161
|
+
association as FxOrmAssociation.InstanceAssociationItem_HasMany,
|
|
1162
|
+
by_item_conditions
|
|
1163
|
+
);
|
|
1164
|
+
|
|
1165
|
+
extra_where = { ...by_item.join_where, ...extra_where };
|
|
1166
|
+
Utilities.filterWhereConditionsInput(extra_where, model);
|
|
1167
|
+
|
|
1168
|
+
merges.push({
|
|
1169
|
+
from: { table: Utilities.tableAlias(ljoin_info.table, ljoin_info.alias), field: ljoin_info.ids },
|
|
1170
|
+
to: { table: left_info.table, field: left_info.ids },
|
|
1171
|
+
where : [ ljoin_info.alias, extra_where ],
|
|
1172
|
+
table : left_info.alias,
|
|
1173
|
+
select: (by_item.extra_select || []).filter(x => extraProps.hasOwnProperty(x))
|
|
1174
|
+
});
|
|
1175
|
+
|
|
1176
|
+
merges.push({
|
|
1177
|
+
from: { table: Utilities.tableAlias(right_info.table, right_info.alias), field: right_info.ids },
|
|
1178
|
+
to: { table: rjoin_info.table, field: rjoin_info.ids },
|
|
1179
|
+
where : [ right_info.alias, by_item_conditions ],
|
|
1180
|
+
table : ljoin_info.alias,
|
|
1181
|
+
select: [],
|
|
1182
|
+
});
|
|
1183
|
+
|
|
1184
|
+
self_options.chainfind_linktable = Utilities.tableAlias(model.table);
|
|
1185
|
+
} else if (isHasOne) { // support hasone
|
|
1186
|
+
const reled_ids = typeof association.field === 'string' ? [association.field] : (
|
|
1187
|
+
Array.isArray(association.field) ? association.field : getMapsToFromPropertyHash(association.field)
|
|
1188
|
+
)
|
|
1189
|
+
|
|
1190
|
+
const base_info = {
|
|
1191
|
+
table: `${model.table}`,
|
|
1192
|
+
alias: `_base$${model.table}`,
|
|
1193
|
+
ids: !association.reversed ? reled_ids : association.model.id
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
const rel_info = {
|
|
1197
|
+
table: `${association.model.table}`,
|
|
1198
|
+
alias: `_rel$${association.model.table}`,
|
|
1199
|
+
ids: !association.reversed ? association.model.id : reled_ids
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
merges.push({
|
|
1203
|
+
from: { table: Utilities.tableAlias(rel_info.table, rel_info.alias), field: rel_info.ids },
|
|
1204
|
+
to: { table: Utilities.tableAlias(base_info.table, base_info.alias), field: base_info.ids },
|
|
1205
|
+
where : [ rel_info.alias, by_item_conditions ],
|
|
1206
|
+
table : base_info.alias,
|
|
1207
|
+
select: []
|
|
1208
|
+
});
|
|
1209
|
+
|
|
1210
|
+
self_options.chainfind_linktable = Utilities.tableAlias(base_info.table, base_info.alias);
|
|
1211
|
+
} else if (isExtendsTo) { // support extendsTo
|
|
1212
|
+
merges.push({
|
|
1213
|
+
from : { table: association.model.table, field: Object.keys(association.field) },
|
|
1214
|
+
to : { table: model.table, field: model.id },
|
|
1215
|
+
where : [ association.model.table, by_item_conditions ],
|
|
1216
|
+
table : model.table,
|
|
1217
|
+
select: []
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
|
|
1222
|
+
self_options.__merge = merges;
|
|
1223
|
+
self_options.extra = {};
|
|
1224
|
+
|
|
1225
|
+
if (typeof cb === "function") {
|
|
1226
|
+
return (model.find as any).call(model, self_conditions, self_options, cb);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
const chain = model.find(self_conditions, self_options);
|
|
1230
|
+
|
|
1231
|
+
if (is_sync)
|
|
1232
|
+
return chain.runSync();
|
|
1233
|
+
|
|
1234
|
+
return chain;
|
|
1235
|
+
}
|