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/ORM.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
const util = require("util");
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
const uuid = require('uuid');
|
|
6
|
+
import * as SqlQuery from '../sql-query/Query';
|
|
7
|
+
import ormPluginSyncPatch from './Patch/plugin'
|
|
8
|
+
|
|
9
|
+
import { Model } from "./Model";
|
|
10
|
+
import ORMError from "./Error";
|
|
11
|
+
import * as Utilities from './Utilities';
|
|
12
|
+
const Enforces = require("@fibjs/enforce");
|
|
13
|
+
|
|
14
|
+
import type { FxOrmNS } from "./Typo/ORM";
|
|
15
|
+
import type { FxOrmError } from "./Typo/Error";
|
|
16
|
+
import type { FxOrmCommon } from "./Typo/_common";
|
|
17
|
+
import type { FxOrmDMLDriver } from "./Typo/DMLDriver";
|
|
18
|
+
import type { FxOrmCoreCallbackNS } from "../orm-core/index";
|
|
19
|
+
import type { FxOrmModel } from "./Typo/model";
|
|
20
|
+
import type { FxOrmProperty } from "./Typo/property";
|
|
21
|
+
import type { FxOrmSettings } from "./Typo/settings";
|
|
22
|
+
|
|
23
|
+
import * as validators from "./Validators";
|
|
24
|
+
|
|
25
|
+
import * as Settings from "./Settings";
|
|
26
|
+
|
|
27
|
+
export class ORM extends EventEmitter implements FxOrmNS.ORM {
|
|
28
|
+
validators: FxOrmNS.ORM['validators'];
|
|
29
|
+
enforce: FxOrmNS.ORM['enforce'];
|
|
30
|
+
settings: FxOrmNS.ORM['settings'];
|
|
31
|
+
driver_name: FxOrmNS.ORM['driver_name'];
|
|
32
|
+
driver: FxOrmNS.ORM['driver'];
|
|
33
|
+
comparators: FxOrmNS.ORM['comparators'];
|
|
34
|
+
models: FxOrmNS.ORM['models'];
|
|
35
|
+
plugins: FxOrmNS.ORM['plugins'];
|
|
36
|
+
customTypes: FxOrmNS.ORM['customTypes'];
|
|
37
|
+
|
|
38
|
+
constructor (
|
|
39
|
+
driver_name: string,
|
|
40
|
+
driver: FxOrmDMLDriver.DMLDriver,
|
|
41
|
+
settings: FxOrmSettings.SettingInstance
|
|
42
|
+
) {
|
|
43
|
+
super();
|
|
44
|
+
|
|
45
|
+
this.validators = validators;
|
|
46
|
+
this.enforce = Enforces;
|
|
47
|
+
this.settings = settings;
|
|
48
|
+
this.driver_name = driver_name;
|
|
49
|
+
this.driver = driver;
|
|
50
|
+
this.driver.uid = uuid.node().hex();
|
|
51
|
+
this.comparators = {...SqlQuery.comparators};
|
|
52
|
+
this.models = {};
|
|
53
|
+
this.plugins = [];
|
|
54
|
+
this.customTypes = {};
|
|
55
|
+
|
|
56
|
+
EventEmitter.call(this);
|
|
57
|
+
|
|
58
|
+
var onError = (err: Error) => {
|
|
59
|
+
if (this.settings.get("connection.reconnect")) {
|
|
60
|
+
if (typeof this.driver.reconnect === "undefined") {
|
|
61
|
+
return this.emit("error", new ORMError("Connection lost - driver does not support reconnection", 'CONNECTION_LOST'));
|
|
62
|
+
}
|
|
63
|
+
this.driver.reconnect(() => {
|
|
64
|
+
this.driver.on("error", onError);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (this.listeners("error").length === 0) {
|
|
68
|
+
// since user want auto reconnect,
|
|
69
|
+
// don't emit without listeners or it will throw
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
this.emit("error", err);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
driver.on("error", onError);
|
|
77
|
+
|
|
78
|
+
this.use(ormPluginSyncPatch as any);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
use (...[
|
|
82
|
+
plugin_const,
|
|
83
|
+
opts
|
|
84
|
+
]: Parameters<FxOrmNS.ORM['use']>) {
|
|
85
|
+
if (typeof plugin_const === "string") {
|
|
86
|
+
try {
|
|
87
|
+
plugin_const = require(Utilities.getRealPath(plugin_const));
|
|
88
|
+
} catch (e) {
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
var plugin: FxOrmNS.Plugin = (plugin_const as any)(this, opts || {});
|
|
94
|
+
|
|
95
|
+
if (typeof plugin.define === "function") {
|
|
96
|
+
for (let k in this.models) {
|
|
97
|
+
plugin.define!(this.models[k], this as any);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.plugins.push(plugin);
|
|
102
|
+
|
|
103
|
+
return this;
|
|
104
|
+
};
|
|
105
|
+
define (name: string, properties: any, opts?: any): any {
|
|
106
|
+
properties = properties || {};
|
|
107
|
+
opts = opts || <FxOrmModel.ModelDefineOptions>{};
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < this.plugins.length; i++) {
|
|
110
|
+
// TODO: only pass normalized properties to beforeDefine
|
|
111
|
+
if (typeof this.plugins[i].beforeDefine === "function") {
|
|
112
|
+
(this.plugins[i].beforeDefine as (...args: any[]) => any)(name, properties, opts);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const m_settings = opts.useSelfSettings ? Settings.Container(this.settings.get('*')) : this.settings;
|
|
117
|
+
|
|
118
|
+
const virtualView = Utilities.normalizeVirtualViewOption(opts.virtualView, this.driver.knex);
|
|
119
|
+
|
|
120
|
+
const modelTable = opts.table || opts.collection || ((m_settings.get("model.namePrefix") || "") + name);
|
|
121
|
+
|
|
122
|
+
const generateSqlSelect = Utilities.__wrapTableSourceAsGneratingSqlSelect(
|
|
123
|
+
{
|
|
124
|
+
virtualView,
|
|
125
|
+
customSelect: opts.customSelect,
|
|
126
|
+
generateSqlSelect: opts.generateSqlSelect,
|
|
127
|
+
}, {
|
|
128
|
+
dialect: this.driver.query.Dialect,
|
|
129
|
+
modelTable,
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
this.models[name] = new Model({
|
|
134
|
+
name : name,
|
|
135
|
+
db : this,
|
|
136
|
+
settings : m_settings,
|
|
137
|
+
driver_name : this.driver_name,
|
|
138
|
+
driver : this.driver,
|
|
139
|
+
table : opts.table || opts.collection || ((m_settings.get("model.namePrefix") || "") + name),
|
|
140
|
+
tableComment : opts.tableComment || '',
|
|
141
|
+
virtualView : virtualView,
|
|
142
|
+
generateSqlSelect,
|
|
143
|
+
// not standard Record<string, FxOrmProperty.NormalizedProperty> here, but we should pass it firstly
|
|
144
|
+
properties : properties as Record<string, FxOrmProperty.NormalizedProperty>,
|
|
145
|
+
__for_extension : opts.__for_extension || false,
|
|
146
|
+
indexes : opts.indexes || [],
|
|
147
|
+
identityCache : opts.hasOwnProperty("identityCache") ? opts.identityCache : m_settings.get("instance.identityCache"),
|
|
148
|
+
keys : opts.id,
|
|
149
|
+
autoSave : opts.hasOwnProperty("autoSave") ? opts.autoSave : m_settings.get("instance.autoSave"),
|
|
150
|
+
autoFetch : opts.hasOwnProperty("autoFetch") ? opts.autoFetch : m_settings.get("instance.autoFetch"),
|
|
151
|
+
autoFetchLimit : opts.autoFetchLimit || m_settings.get("instance.autoFetchLimit"),
|
|
152
|
+
cascadeRemove : opts.hasOwnProperty("cascadeRemove") ? opts.cascadeRemove : m_settings.get("instance.cascadeRemove"),
|
|
153
|
+
hooks : opts.hooks || {},
|
|
154
|
+
methods : opts.methods || {},
|
|
155
|
+
validations : opts.validations || {},
|
|
156
|
+
ievents : opts.ievents || {},
|
|
157
|
+
|
|
158
|
+
instanceCacheSize : opts.hasOwnProperty("instanceCacheSize") ? opts.instanceCacheSize : m_settings.get("instance.cacheSize"),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
for (let i = 0; i < this.plugins.length; i++) {
|
|
162
|
+
if (typeof this.plugins[i].define === "function") {
|
|
163
|
+
this.plugins[i].define!(this.models[name], this as any);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return this.models[name] as any;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
defineType (...[name, opts]: Parameters<FxOrmNS.ORM['defineType']>) {
|
|
171
|
+
this.customTypes[name] = opts;
|
|
172
|
+
this.driver.customTypes[name] = opts;
|
|
173
|
+
return this;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
pingSync () {
|
|
177
|
+
this.driver.ping();
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
ping (...[cb]: Parameters<FxOrmNS.ORM['ping']>) {
|
|
181
|
+
this.driver.ping(cb);
|
|
182
|
+
|
|
183
|
+
return this;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
closeSync () {
|
|
187
|
+
this.driver.close()
|
|
188
|
+
}
|
|
189
|
+
close (...[cb]: Parameters<FxOrmNS.ORM['close']>) {
|
|
190
|
+
const syncResponse = Utilities.catchBlocking(this.closeSync, [], { thisArg: this});
|
|
191
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
192
|
+
return this;
|
|
193
|
+
};
|
|
194
|
+
load () {
|
|
195
|
+
var args = Array.prototype.slice.apply(arguments);
|
|
196
|
+
var cb = function (err?: Error | null) {};
|
|
197
|
+
|
|
198
|
+
if (typeof args[args.length - 1] == "function") {
|
|
199
|
+
cb = args.pop();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// util.flatten drops functions, so the callback must be removed first.
|
|
203
|
+
var files = util.flatten(args);
|
|
204
|
+
|
|
205
|
+
var loadNext = function (this: any) {
|
|
206
|
+
if (files.length === 0) {
|
|
207
|
+
return cb(null);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
var file = files.shift();
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
return require(Utilities.getRealPath(file, 4))(this, function (err: FxOrmError.ExtendedError) {
|
|
214
|
+
if (err) return cb(err);
|
|
215
|
+
|
|
216
|
+
return loadNext();
|
|
217
|
+
});
|
|
218
|
+
} catch (ex) {
|
|
219
|
+
return cb(ex as Error);
|
|
220
|
+
}
|
|
221
|
+
}.bind(this);
|
|
222
|
+
|
|
223
|
+
return loadNext();
|
|
224
|
+
};
|
|
225
|
+
syncSync (): void {
|
|
226
|
+
var modelIds = Object.keys(this.models);
|
|
227
|
+
|
|
228
|
+
if (modelIds.length === 0)
|
|
229
|
+
return ;
|
|
230
|
+
|
|
231
|
+
modelIds.forEach(modelId => {
|
|
232
|
+
this.models[modelId].syncSync()
|
|
233
|
+
})
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
sync (...[cb]: Parameters<FxOrmNS.ORM['sync']>) {
|
|
237
|
+
const syncResponse = Utilities.catchBlocking(this.syncSync, [], { thisArg: this })
|
|
238
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: !!cb, callback: cb })
|
|
239
|
+
|
|
240
|
+
return this;
|
|
241
|
+
};
|
|
242
|
+
dropSync (): void {
|
|
243
|
+
var modelIds = Object.keys(this.models);
|
|
244
|
+
|
|
245
|
+
if (modelIds.length === 0)
|
|
246
|
+
return ;
|
|
247
|
+
|
|
248
|
+
modelIds.forEach(modelId => {
|
|
249
|
+
this.models[modelId].dropSync()
|
|
250
|
+
})
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
drop (...[cb]: Parameters<FxOrmNS.ORM['drop']>) {
|
|
254
|
+
const syncResponse = Utilities.catchBlocking(this.dropSync, [], { thisArg: this })
|
|
255
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: !!cb, callback: cb })
|
|
256
|
+
|
|
257
|
+
return this;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
queryParamCastserial (...chains: any[]) {
|
|
261
|
+
return {
|
|
262
|
+
get: function (cb: FxOrmCommon.GenericCallback<any[]>) {
|
|
263
|
+
var params: any[] = [];
|
|
264
|
+
var getNext = function () {
|
|
265
|
+
if (params.length === chains.length) {
|
|
266
|
+
params.unshift(null);
|
|
267
|
+
return cb.apply(null, params as any);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
chains[params.length].run(function (err: Error, instances: any[]) {
|
|
271
|
+
if (err) {
|
|
272
|
+
params.unshift(err);
|
|
273
|
+
return cb.apply(null, params as any);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
params.push(instances);
|
|
277
|
+
return getNext();
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
getNext();
|
|
282
|
+
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
begin () {
|
|
289
|
+
return this.driver.db.connection.begin();
|
|
290
|
+
};
|
|
291
|
+
commit () {
|
|
292
|
+
return this.driver.db.connection.commit();
|
|
293
|
+
};
|
|
294
|
+
rollback () {
|
|
295
|
+
return this.driver.db.connection.rollback();
|
|
296
|
+
};
|
|
297
|
+
trans<T> (func: FxOrmCoreCallbackNS.ExecutionCallback<T>) {
|
|
298
|
+
const connection = this.driver.db.connection;
|
|
299
|
+
return connection.trans(func.bind(connection));
|
|
300
|
+
};
|
|
301
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FxOrmModel } from "../Typo/model";
|
|
2
|
+
import type { FxOrmNS } from "../Typo/ORM";
|
|
3
|
+
|
|
4
|
+
export default function (
|
|
5
|
+
orm: FxOrmNS.ORM,
|
|
6
|
+
plugin_opts: {
|
|
7
|
+
enable: boolean
|
|
8
|
+
}
|
|
9
|
+
): FxOrmNS.Plugin {
|
|
10
|
+
function beforeDefine (
|
|
11
|
+
name: string,
|
|
12
|
+
properties: Record<string, FxOrmModel.ModelPropertyDefinition>,
|
|
13
|
+
opts: FxOrmModel.ModelDefineOptions
|
|
14
|
+
) {
|
|
15
|
+
opts.hooks = opts.hooks || {};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function define (m: FxOrmModel.Model) {
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
beforeDefine,
|
|
23
|
+
define
|
|
24
|
+
} as any
|
|
25
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
import cloneDeep from './cloneDeep';
|
|
4
|
+
import ORMError from "./Error";
|
|
5
|
+
import type { FxOrmModel } from './Typo/model';
|
|
6
|
+
import type { FxOrmNS } from './Typo/ORM';
|
|
7
|
+
import type { FxOrmProperty } from './Typo/property';
|
|
8
|
+
|
|
9
|
+
const KNOWN_TYPES = [
|
|
10
|
+
"text", "number", "integer", "boolean", "date", "enum", "object",
|
|
11
|
+
"binary", "point", "serial"
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
export function normalize
|
|
15
|
+
(opts: {
|
|
16
|
+
prop: FxOrmModel.ComplexModelPropertyDefinition
|
|
17
|
+
name: string
|
|
18
|
+
customTypes: FxOrmNS.ORM['customTypes']
|
|
19
|
+
settings: FxOrmNS.ORM['settings']
|
|
20
|
+
}): FxOrmProperty.NormalizedProperty {
|
|
21
|
+
const orig_prop = opts.prop as FxOrmProperty.NormalizedProperty
|
|
22
|
+
let result_prop: FxOrmProperty.NormalizedProperty = orig_prop
|
|
23
|
+
|
|
24
|
+
if (typeof opts.prop === "function") {
|
|
25
|
+
const primitiveProp: FxOrmModel.ComplexModelPropertyDefinition = opts.prop
|
|
26
|
+
switch (primitiveProp.name) {
|
|
27
|
+
case "String":
|
|
28
|
+
result_prop = { type: "text" } as FxOrmProperty.NormalizedProperty;
|
|
29
|
+
break;
|
|
30
|
+
case "Number":
|
|
31
|
+
result_prop = { type: "number" } as FxOrmProperty.NormalizedProperty;
|
|
32
|
+
break;
|
|
33
|
+
case "Boolean":
|
|
34
|
+
result_prop = { type: "boolean" } as FxOrmProperty.NormalizedProperty;
|
|
35
|
+
break;
|
|
36
|
+
case "Date":
|
|
37
|
+
result_prop = { type: "date" } as FxOrmProperty.NormalizedProperty;
|
|
38
|
+
break;
|
|
39
|
+
case "Object":
|
|
40
|
+
result_prop = { type: "object" } as FxOrmProperty.NormalizedProperty;
|
|
41
|
+
break;
|
|
42
|
+
case "Buffer":
|
|
43
|
+
result_prop = { type: "binary" } as FxOrmProperty.NormalizedProperty;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
} else if (typeof opts.prop === "string") {
|
|
47
|
+
result_prop = {
|
|
48
|
+
type: opts.prop
|
|
49
|
+
} as FxOrmProperty.NormalizedProperty;
|
|
50
|
+
} else if (Array.isArray(opts.prop)) {
|
|
51
|
+
result_prop = { type: "enum", values: opts.prop } as FxOrmProperty.NormalizedProperty;
|
|
52
|
+
} else {
|
|
53
|
+
result_prop = cloneDeep<FxOrmProperty.NormalizedProperty>(orig_prop);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!KNOWN_TYPES.includes(result_prop.type as any) && !(result_prop.type in opts.customTypes)) {
|
|
57
|
+
throw new ORMError("Unknown property type: " + result_prop.type, 'NO_SUPPORT');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!result_prop.hasOwnProperty("required") && opts.settings.get("properties.required")) {
|
|
61
|
+
result_prop.required = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Defaults to true. Setting to false hides properties from JSON.stringify(modelInstance).
|
|
65
|
+
if (!result_prop.hasOwnProperty("enumerable") || result_prop.enumerable === true) {
|
|
66
|
+
result_prop.enumerable = true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Defaults to true. Rational means floating point here.
|
|
70
|
+
if (result_prop.type == "number" && result_prop.rational === undefined) {
|
|
71
|
+
result_prop.rational = true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!('mapsTo' in result_prop)) {
|
|
75
|
+
result_prop.mapsTo = opts.name
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (result_prop.type == "number" && result_prop.rational === false) {
|
|
79
|
+
result_prop.type = "integer";
|
|
80
|
+
delete result_prop.rational;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
result_prop.name = opts.name;
|
|
84
|
+
|
|
85
|
+
return result_prop;
|
|
86
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
import cloneDeep from './cloneDeep';
|
|
4
|
+
import type { FxOrmSettings } from './Typo/settings';
|
|
5
|
+
|
|
6
|
+
const default_settings = {
|
|
7
|
+
model : {
|
|
8
|
+
namePrefix : '',
|
|
9
|
+
// TODO: add test about it
|
|
10
|
+
repair_column : false,
|
|
11
|
+
/**
|
|
12
|
+
* @dangerous
|
|
13
|
+
*/
|
|
14
|
+
allow_drop_column : false,
|
|
15
|
+
},
|
|
16
|
+
properties : {
|
|
17
|
+
primary_key : "id",
|
|
18
|
+
association_key : "{name}_{field}",
|
|
19
|
+
required : false
|
|
20
|
+
},
|
|
21
|
+
instance : {
|
|
22
|
+
defaultFindLimit : 1000,
|
|
23
|
+
defaultCacheSize : 1000,
|
|
24
|
+
identityCache : false,
|
|
25
|
+
identityCacheSaveCheck : true,
|
|
26
|
+
autoSave : false,
|
|
27
|
+
autoFetch : false,
|
|
28
|
+
autoFetchLimit : 1,
|
|
29
|
+
cascadeRemove : true,
|
|
30
|
+
returnAllErrors : false,
|
|
31
|
+
saveAssociationsByDefault : true
|
|
32
|
+
},
|
|
33
|
+
hasOne : {
|
|
34
|
+
// allowRemoveReverse : false,
|
|
35
|
+
},
|
|
36
|
+
hasMany : {
|
|
37
|
+
// Makes the foreign key fields a composite key
|
|
38
|
+
key : false
|
|
39
|
+
},
|
|
40
|
+
extendsTo : {
|
|
41
|
+
// would default as false in the future
|
|
42
|
+
throwWhenNotFound : true,
|
|
43
|
+
},
|
|
44
|
+
connection : {
|
|
45
|
+
reconnect : true,
|
|
46
|
+
debug : false
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const defaults = function () {
|
|
51
|
+
return default_settings;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export function Container (settings: object): FxOrmSettings.SettingInstance {
|
|
55
|
+
return {
|
|
56
|
+
set: function (key: string, value: any) {
|
|
57
|
+
set(key, value, settings);
|
|
58
|
+
|
|
59
|
+
return this;
|
|
60
|
+
},
|
|
61
|
+
get: function (key, def) {
|
|
62
|
+
const v = get(key, def, settings)
|
|
63
|
+
|
|
64
|
+
if (v instanceof Function) {
|
|
65
|
+
return v;
|
|
66
|
+
} else {
|
|
67
|
+
return cloneDeep(v);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
unset: function () {
|
|
71
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
72
|
+
if (typeof arguments[i] === "string") {
|
|
73
|
+
unset(arguments[i], settings);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function set (key: string, value: any, obj: {[k: string]: any}): any {
|
|
83
|
+
const p = key.indexOf(".");
|
|
84
|
+
|
|
85
|
+
if (p === -1) {
|
|
86
|
+
return obj[key] = value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!obj.hasOwnProperty(key.substr(0, p))) {
|
|
90
|
+
obj[key.substr(0, p)] = {};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return set(key.substr(p + 1), value, obj[key.substr(0, p)]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function get(key: string, def: any, obj: {[k: string]: any}): any {
|
|
97
|
+
const p = key.indexOf(".");
|
|
98
|
+
|
|
99
|
+
if (p === -1) {
|
|
100
|
+
if (key === '*') {
|
|
101
|
+
return obj;
|
|
102
|
+
}
|
|
103
|
+
return obj.hasOwnProperty(key) ? obj[key] : def;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!obj.hasOwnProperty(key.substr(0, p))) {
|
|
107
|
+
return def;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return get(key.substr(p + 1), def, obj[key.substr(0, p)]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function unset(key: string, obj: any) {
|
|
114
|
+
const p = key.indexOf(".");
|
|
115
|
+
|
|
116
|
+
if (p === -1) {
|
|
117
|
+
if (key === '*') {
|
|
118
|
+
return 'reset';
|
|
119
|
+
} else {
|
|
120
|
+
delete obj[key];
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!obj.hasOwnProperty(key.substr(0, p))) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (unset(key.substr(p + 1), obj[key.substr(0, p)]) === 'reset') {
|
|
130
|
+
obj[key.substr(0, p)] = {};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export const defaultSettingsInstance = Container(defaults());
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
import type { FxOrmNS } from "./Typo/ORM";
|
|
4
|
+
import type { FxOrmModel } from "./Typo/model";
|
|
5
|
+
|
|
6
|
+
import * as Helpers from "./Helpers";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* refactor this module with LRUCache
|
|
10
|
+
*/
|
|
11
|
+
const models: {[driver_table_uid: string]: FxOrmModel.Model} = {};
|
|
12
|
+
|
|
13
|
+
export const clear: FxOrmNS.SingletonModule['clear'] = function (this: any, key?: string) {
|
|
14
|
+
let driver_table_uid2 = '';
|
|
15
|
+
if (key && typeof key === 'string')
|
|
16
|
+
driver_table_uid2 = Helpers.parseDriverUidAndTableNameFromUID(key).driver_table_uid;
|
|
17
|
+
|
|
18
|
+
Object.values(models).forEach(model => {
|
|
19
|
+
const { driver_table_uid: driver_table_uid1 } = Helpers.parseDriverUidAndTableNameFromUID(model.uid);
|
|
20
|
+
|
|
21
|
+
if (driver_table_uid2 && driver_table_uid2 !== driver_table_uid1)
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
modelClear(model, key)
|
|
25
|
+
});
|
|
26
|
+
return this;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const modelClear: FxOrmNS.SingletonModule['modelClear'] = function (this: any, model: FxOrmModel.Model, key?: string) {
|
|
30
|
+
if (key && typeof key === "string") {
|
|
31
|
+
model.caches.delete(key);
|
|
32
|
+
} else {
|
|
33
|
+
model.caches.clear();
|
|
34
|
+
}
|
|
35
|
+
return this;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// synchronous get
|
|
39
|
+
export const get: FxOrmNS.SingletonModule['get'] = function (this: any, key, opts, reFetchSync) {
|
|
40
|
+
const { driver_table_uid } = Helpers.parseDriverUidAndTableNameFromUID(key);
|
|
41
|
+
|
|
42
|
+
return modelGet(models[driver_table_uid], key, opts, reFetchSync);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const modelGet: FxOrmNS.SingletonModule['modelGet'] = function (model, key, opts, reFetchSync) {
|
|
46
|
+
const { driver_table_uid } = Helpers.parseDriverUidAndTableNameFromUID(model.uid);
|
|
47
|
+
if (!models[driver_table_uid])
|
|
48
|
+
models[driver_table_uid] = model;
|
|
49
|
+
|
|
50
|
+
if (opts.identityCache === false)
|
|
51
|
+
return reFetchSync();
|
|
52
|
+
|
|
53
|
+
var value = model.caches.get(key);
|
|
54
|
+
|
|
55
|
+
if (value !== undefined)
|
|
56
|
+
{
|
|
57
|
+
if (opts && opts.saveCheck && typeof value.saved === "function" && !value.saved())
|
|
58
|
+
// if not saved, don't return it, fetch original from db
|
|
59
|
+
return reFetchSync();
|
|
60
|
+
}else
|
|
61
|
+
{
|
|
62
|
+
value = reFetchSync();
|
|
63
|
+
model.caches.set(key, value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const expected_expire = typeof opts.identityCache === "number" ? (opts.identityCache * 1000) : model.caches.ttl;
|
|
67
|
+
const expire_expection_delta = expected_expire - model.caches.ttl;
|
|
68
|
+
|
|
69
|
+
if (expire_expection_delta > 0) {
|
|
70
|
+
/**
|
|
71
|
+
* The model-level cache TTL is shorter than the TTL requested
|
|
72
|
+
* for this lookup. Refresh the cache entry until the requested
|
|
73
|
+
* deadline, otherwise the model-level TTL would evict it early.
|
|
74
|
+
*/
|
|
75
|
+
const expireAt = Date.now() + expected_expire;
|
|
76
|
+
|
|
77
|
+
const refresh = () => {
|
|
78
|
+
if (!model.caches.has(key))
|
|
79
|
+
return;
|
|
80
|
+
|
|
81
|
+
const remain = expireAt - Date.now();
|
|
82
|
+
if (remain <= 0) {
|
|
83
|
+
model.caches.delete(key);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
model.caches.set(key, value);
|
|
88
|
+
setTimeout(refresh, Math.min(remain, Math.max(1, Math.floor(model.caches.ttl / 2))));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
refresh();
|
|
92
|
+
} else if (expire_expection_delta < 0) {
|
|
93
|
+
setTimeout(() => model.caches.delete(key), expected_expire);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return value;
|
|
97
|
+
}
|