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