fib-orm 1.4.2 → 2.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.
Files changed (295) hide show
  1. package/License +1 -1
  2. package/README.md +53 -0
  3. package/lib/cli/commands/ddlFrom.d.ts +2 -0
  4. package/lib/cli/commands/ddlFrom.js +74 -0
  5. package/lib/cli/commands/dumpModel.d.ts +2 -0
  6. package/lib/cli/commands/dumpModel.js +106 -0
  7. package/lib/cli/commands/escape.d.ts +2 -0
  8. package/lib/cli/commands/escape.js +68 -0
  9. package/lib/cli/commands/spinner.d.ts +2 -0
  10. package/lib/cli/commands/spinner.js +36 -0
  11. package/lib/cli/commands/upgrade.d.ts +2 -0
  12. package/lib/cli/commands/upgrade.js +65 -0
  13. package/lib/cli/helpers/ddl.d.ts +17 -0
  14. package/lib/cli/helpers/ddl.js +55 -0
  15. package/lib/cli/helpers/package.d.ts +26 -0
  16. package/lib/cli/helpers/package.js +56 -0
  17. package/lib/cli/index.d.ts +2 -0
  18. package/lib/cli/index.js +18 -0
  19. package/lib/codepoints-map.json +762 -0
  20. package/lib/db-driver/Typo.d.ts +112 -0
  21. package/lib/db-driver/Typo.js +1 -0
  22. package/lib/db-driver/built-ins/base.class.d.ts +96 -0
  23. package/lib/db-driver/built-ins/base.class.js +186 -0
  24. package/lib/db-driver/built-ins/driver-dm.d.ts +18 -0
  25. package/lib/db-driver/built-ins/driver-dm.js +53 -0
  26. package/lib/db-driver/built-ins/driver-mongodb.d.ts +24 -0
  27. package/lib/db-driver/built-ins/driver-mongodb.js +46 -0
  28. package/lib/db-driver/built-ins/driver-mysql.d.ts +18 -0
  29. package/lib/db-driver/built-ins/driver-mysql.js +37 -0
  30. package/lib/db-driver/built-ins/driver-postgresql.d.ts +22 -0
  31. package/lib/db-driver/built-ins/driver-postgresql.js +76 -0
  32. package/lib/db-driver/built-ins/driver-redis.d.ts +12 -0
  33. package/lib/db-driver/built-ins/driver-redis.js +35 -0
  34. package/lib/db-driver/built-ins/driver-sqlite.d.ts +17 -0
  35. package/lib/db-driver/built-ins/driver-sqlite.js +34 -0
  36. package/lib/db-driver/index.d.ts +3 -0
  37. package/lib/db-driver/index.js +4 -0
  38. package/lib/db-driver/utils.d.ts +14 -0
  39. package/lib/db-driver/utils.js +187 -0
  40. package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +12 -0
  41. package/lib/knex/deps/tarn/dist/PendingOperation.js +48 -0
  42. package/lib/knex/deps/tarn/dist/Pool.d.ts +97 -0
  43. package/lib/knex/deps/tarn/dist/Pool.js +516 -0
  44. package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +16 -0
  45. package/lib/knex/deps/tarn/dist/PromiseInspection.js +21 -0
  46. package/lib/knex/deps/tarn/dist/Resource.d.ts +9 -0
  47. package/lib/knex/deps/tarn/dist/Resource.js +19 -0
  48. package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +2 -0
  49. package/lib/knex/deps/tarn/dist/TimeoutError.js +5 -0
  50. package/lib/knex/deps/tarn/dist/tarn.d.ts +3 -0
  51. package/lib/knex/deps/tarn/dist/tarn.js +10 -0
  52. package/lib/knex/deps/tarn/dist/utils.d.ts +14 -0
  53. package/lib/knex/deps/tarn/dist/utils.js +60 -0
  54. package/lib/knex/knex.d.ts +2 -0
  55. package/lib/knex/knex.js +3 -0
  56. package/lib/knex/sandbox.d.ts +4 -0
  57. package/lib/knex/sandbox.js +22 -0
  58. package/lib/orm/Adapters.d.ts +3 -0
  59. package/lib/orm/Adapters.js +44 -0
  60. package/lib/orm/AggregateFunctions.d.ts +3 -0
  61. package/lib/orm/AggregateFunctions.js +176 -0
  62. package/lib/orm/Associations/Extend.d.ts +22 -0
  63. package/lib/orm/Associations/Extend.js +274 -0
  64. package/lib/orm/Associations/Many.d.ts +17 -0
  65. package/lib/orm/Associations/Many.js +569 -0
  66. package/lib/orm/Associations/One.d.ts +16 -0
  67. package/lib/orm/Associations/One.js +310 -0
  68. package/lib/orm/Associations/_utils.d.ts +15 -0
  69. package/lib/orm/Associations/_utils.js +52 -0
  70. package/lib/orm/ChainFind.d.ts +3 -0
  71. package/lib/orm/ChainFind.js +327 -0
  72. package/lib/orm/ChainInstance.d.ts +6 -0
  73. package/lib/orm/ChainInstance.js +73 -0
  74. package/lib/orm/Debug.d.ts +2 -0
  75. package/lib/orm/Debug.js +17 -0
  76. package/lib/orm/Drivers/DB/base.d.ts +3 -0
  77. package/lib/orm/Drivers/DB/base.js +3 -0
  78. package/lib/orm/Drivers/DB/dm.d.ts +11 -0
  79. package/lib/orm/Drivers/DB/dm.js +23 -0
  80. package/lib/orm/Drivers/DB/mysql.d.ts +11 -0
  81. package/lib/orm/Drivers/DB/mysql.js +23 -0
  82. package/lib/orm/Drivers/DB/postgres.d.ts +11 -0
  83. package/lib/orm/Drivers/DB/postgres.js +22 -0
  84. package/lib/orm/Drivers/DB/sqlite3.d.ts +16 -0
  85. package/lib/orm/Drivers/DB/sqlite3.js +39 -0
  86. package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
  87. package/lib/orm/Drivers/DML/_ddl-sql.js +51 -0
  88. package/lib/orm/Drivers/DML/_dml-helpers.d.ts +19 -0
  89. package/lib/orm/Drivers/DML/_dml-helpers.js +51 -0
  90. package/lib/orm/Drivers/DML/_shared.d.ts +9 -0
  91. package/lib/orm/Drivers/DML/_shared.js +38 -0
  92. package/lib/orm/Drivers/DML/_utils.d.ts +8 -0
  93. package/lib/orm/Drivers/DML/_utils.js +67 -0
  94. package/lib/orm/Drivers/DML/constants.d.ts +3 -0
  95. package/lib/orm/Drivers/DML/constants.js +2 -0
  96. package/lib/orm/Drivers/DML/dm.d.ts +2 -0
  97. package/lib/orm/Drivers/DML/dm.js +293 -0
  98. package/lib/orm/Drivers/DML/index.d.ts +4 -0
  99. package/lib/orm/Drivers/DML/index.js +6 -0
  100. package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
  101. package/lib/orm/Drivers/DML/mysql.js +251 -0
  102. package/lib/orm/Drivers/DML/postgres.d.ts +3 -0
  103. package/lib/orm/Drivers/DML/postgres.js +276 -0
  104. package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
  105. package/lib/orm/Drivers/DML/sqlite.js +266 -0
  106. package/lib/orm/Error.d.ts +11 -0
  107. package/lib/orm/Error.js +40 -0
  108. package/lib/orm/Helpers.d.ts +35 -0
  109. package/lib/orm/Helpers.js +233 -0
  110. package/lib/orm/Hook.d.ts +13 -0
  111. package/lib/orm/Hook.js +99 -0
  112. package/lib/orm/Instance.d.ts +2 -0
  113. package/lib/orm/Instance.js +697 -0
  114. package/lib/orm/LazyLoad.d.ts +4 -0
  115. package/lib/orm/LazyLoad.js +76 -0
  116. package/lib/orm/Model.d.ts +7 -0
  117. package/lib/orm/Model.js +971 -0
  118. package/lib/orm/ORM.d.ts +37 -0
  119. package/lib/orm/ORM.js +241 -0
  120. package/lib/orm/Patch/fib-cache.d.ts +40 -0
  121. package/lib/orm/Patch/fib-cache.js +2 -0
  122. package/lib/orm/Patch/plugin.d.ts +4 -0
  123. package/lib/orm/Patch/plugin.js +13 -0
  124. package/lib/orm/Property.d.ts +9 -0
  125. package/lib/orm/Property.js +71 -0
  126. package/lib/orm/Settings.d.ts +42 -0
  127. package/lib/orm/Settings.js +119 -0
  128. package/lib/orm/Singleton.d.ts +5 -0
  129. package/lib/orm/Singleton.js +72 -0
  130. package/lib/orm/Typo/Adapter.d.ts +5 -0
  131. package/lib/orm/Typo/Adapter.js +1 -0
  132. package/lib/orm/Typo/DMLDriver.d.ts +210 -0
  133. package/lib/orm/Typo/DMLDriver.js +1 -0
  134. package/lib/orm/Typo/Db.d.ts +38 -0
  135. package/lib/orm/Typo/Db.js +1 -0
  136. package/lib/orm/Typo/Error.d.ts +31 -0
  137. package/lib/orm/Typo/Error.js +1 -0
  138. package/lib/orm/Typo/ORM.d.ts +233 -0
  139. package/lib/orm/Typo/ORM.js +3 -0
  140. package/lib/orm/Typo/Validators.d.ts +32 -0
  141. package/lib/orm/Typo/Validators.js +2 -0
  142. package/lib/orm/Typo/_common.d.ts +38 -0
  143. package/lib/orm/Typo/_common.js +1 -0
  144. package/lib/orm/Typo/assoc.d.ts +221 -0
  145. package/lib/orm/Typo/assoc.js +1 -0
  146. package/lib/orm/Typo/hook.d.ts +42 -0
  147. package/lib/orm/Typo/hook.js +1 -0
  148. package/lib/orm/Typo/instance.d.ts +157 -0
  149. package/lib/orm/Typo/instance.js +1 -0
  150. package/lib/orm/Typo/model.d.ts +313 -0
  151. package/lib/orm/Typo/model.js +6 -0
  152. package/lib/orm/Typo/patch.d.ts +31 -0
  153. package/lib/orm/Typo/patch.js +1 -0
  154. package/lib/orm/Typo/property.d.ts +57 -0
  155. package/lib/orm/Typo/property.js +1 -0
  156. package/lib/orm/Typo/query.d.ts +216 -0
  157. package/lib/orm/Typo/query.js +1 -0
  158. package/lib/orm/Typo/settings.d.ts +35 -0
  159. package/lib/orm/Typo/settings.js +5 -0
  160. package/lib/orm/Typo/synchronous.d.ts +47 -0
  161. package/lib/orm/Typo/synchronous.js +1 -0
  162. package/lib/orm/Utilities.d.ts +179 -0
  163. package/lib/orm/Utilities.js +975 -0
  164. package/lib/orm/Validators.d.ts +34 -0
  165. package/lib/orm/Validators.js +100 -0
  166. package/lib/orm/Where/common.d.ts +1 -0
  167. package/lib/orm/Where/common.js +7 -0
  168. package/lib/orm/Where/filterDate.d.ts +11 -0
  169. package/lib/orm/Where/filterDate.js +58 -0
  170. package/lib/orm/index.d.ts +68 -0
  171. package/lib/orm/index.js +157 -0
  172. package/lib/orm-core/callback.d.ts +12 -0
  173. package/lib/orm-core/callback.js +1 -0
  174. package/lib/orm-core/dialect.d.ts +80 -0
  175. package/lib/orm-core/dialect.js +1 -0
  176. package/lib/orm-core/error.d.ts +16 -0
  177. package/lib/orm-core/error.js +29 -0
  178. package/lib/orm-core/index.d.ts +6 -0
  179. package/lib/orm-core/index.js +6 -0
  180. package/lib/orm-core/sync.d.ts +12 -0
  181. package/lib/orm-core/sync.js +1 -0
  182. package/lib/orm-core/utils.d.ts +1 -0
  183. package/lib/orm-core/utils.js +5 -0
  184. package/lib/orm-property/Property.d.ts +78 -0
  185. package/lib/orm-property/Property.js +1 -0
  186. package/lib/orm-property/Utils.d.ts +9 -0
  187. package/lib/orm-property/Utils.js +13 -0
  188. package/lib/orm-property/customTypes.d.ts +2 -0
  189. package/lib/orm-property/customTypes.js +13 -0
  190. package/lib/orm-property/index.d.ts +10 -0
  191. package/lib/orm-property/index.js +22 -0
  192. package/lib/orm-property/transformer.d.ts +5 -0
  193. package/lib/orm-property/transformer.js +10 -0
  194. package/lib/orm-property/transformers/dm.d.ts +29 -0
  195. package/lib/orm-property/transformers/dm.js +237 -0
  196. package/lib/orm-property/transformers/index.d.ts +4 -0
  197. package/lib/orm-property/transformers/index.js +5 -0
  198. package/lib/orm-property/transformers/mysql.d.ts +102 -0
  199. package/lib/orm-property/transformers/mysql.js +261 -0
  200. package/lib/orm-property/transformers/postgresql.d.ts +54 -0
  201. package/lib/orm-property/transformers/postgresql.js +189 -0
  202. package/lib/orm-property/transformers/sqlite.d.ts +19 -0
  203. package/lib/orm-property/transformers/sqlite.js +171 -0
  204. package/lib/sql-ddl-sync/Dialects/dm.d.ts +40 -0
  205. package/lib/sql-ddl-sync/Dialects/dm.js +296 -0
  206. package/lib/sql-ddl-sync/Dialects/index.d.ts +7 -0
  207. package/lib/sql-ddl-sync/Dialects/index.js +7 -0
  208. package/lib/sql-ddl-sync/Dialects/mysql.d.ts +39 -0
  209. package/lib/sql-ddl-sync/Dialects/mysql.js +240 -0
  210. package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +40 -0
  211. package/lib/sql-ddl-sync/Dialects/postgresql.js +286 -0
  212. package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +44 -0
  213. package/lib/sql-ddl-sync/Dialects/sqlite.js +256 -0
  214. package/lib/sql-ddl-sync/SQL.d.ts +12 -0
  215. package/lib/sql-ddl-sync/SQL.js +108 -0
  216. package/lib/sql-ddl-sync/Typo/Collection.d.ts +13 -0
  217. package/lib/sql-ddl-sync/Typo/Collection.js +1 -0
  218. package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
  219. package/lib/sql-ddl-sync/Typo/DbIndex.js +1 -0
  220. package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
  221. package/lib/sql-ddl-sync/Typo/Dialect.js +1 -0
  222. package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
  223. package/lib/sql-ddl-sync/Typo/Driver.js +1 -0
  224. package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
  225. package/lib/sql-ddl-sync/Typo/Query.js +1 -0
  226. package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
  227. package/lib/sql-ddl-sync/Typo/SQL.js +1 -0
  228. package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
  229. package/lib/sql-ddl-sync/Typo/_common.js +1 -0
  230. package/lib/sql-ddl-sync/Utils.d.ts +40 -0
  231. package/lib/sql-ddl-sync/Utils.js +217 -0
  232. package/lib/sql-ddl-sync/index.d.ts +87 -0
  233. package/lib/sql-ddl-sync/index.js +371 -0
  234. package/lib/sql-query/Comparators.d.ts +3 -0
  235. package/lib/sql-query/Comparators.js +94 -0
  236. package/lib/sql-query/Create.d.ts +35 -0
  237. package/lib/sql-query/Create.js +77 -0
  238. package/lib/sql-query/Dialects/dm.d.ts +3 -0
  239. package/lib/sql-query/Dialects/dm.js +74 -0
  240. package/lib/sql-query/Dialects/index.d.ts +10 -0
  241. package/lib/sql-query/Dialects/index.js +12 -0
  242. package/lib/sql-query/Dialects/mssql.d.ts +3 -0
  243. package/lib/sql-query/Dialects/mssql.js +73 -0
  244. package/lib/sql-query/Dialects/mysql.d.ts +3 -0
  245. package/lib/sql-query/Dialects/mysql.js +100 -0
  246. package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
  247. package/lib/sql-query/Dialects/postgresql.js +76 -0
  248. package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
  249. package/lib/sql-query/Dialects/sqlite.js +61 -0
  250. package/lib/sql-query/Helpers.d.ts +38 -0
  251. package/lib/sql-query/Helpers.js +259 -0
  252. package/lib/sql-query/Insert.d.ts +12 -0
  253. package/lib/sql-query/Insert.js +33 -0
  254. package/lib/sql-query/Query.d.ts +29 -0
  255. package/lib/sql-query/Query.js +119 -0
  256. package/lib/sql-query/Remove.d.ts +16 -0
  257. package/lib/sql-query/Remove.js +93 -0
  258. package/lib/sql-query/Select.d.ts +62 -0
  259. package/lib/sql-query/Select.js +533 -0
  260. package/lib/sql-query/Set.d.ts +4 -0
  261. package/lib/sql-query/Set.js +16 -0
  262. package/lib/sql-query/Typo/Aggregation.d.ts +32 -0
  263. package/lib/sql-query/Typo/Aggregation.js +1 -0
  264. package/lib/sql-query/Typo/Comparators.d.ts +140 -0
  265. package/lib/sql-query/Typo/Comparators.js +1 -0
  266. package/lib/sql-query/Typo/Dialect.d.ts +39 -0
  267. package/lib/sql-query/Typo/Dialect.js +1 -0
  268. package/lib/sql-query/Typo/Field.d.ts +8 -0
  269. package/lib/sql-query/Typo/Field.js +1 -0
  270. package/lib/sql-query/Typo/Helper.d.ts +4 -0
  271. package/lib/sql-query/Typo/Helper.js +1 -0
  272. package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
  273. package/lib/sql-query/Typo/Query-ChainBuilder.js +1 -0
  274. package/lib/sql-query/Typo/Query.d.ts +34 -0
  275. package/lib/sql-query/Typo/Query.js +16 -0
  276. package/lib/sql-query/Typo/Sql.d.ts +143 -0
  277. package/lib/sql-query/Typo/Sql.js +1 -0
  278. package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
  279. package/lib/sql-query/Typo/SubQuery.js +1 -0
  280. package/lib/sql-query/Typo/index.d.ts +9 -0
  281. package/lib/sql-query/Typo/index.js +1 -0
  282. package/lib/sql-query/Update.d.ts +15 -0
  283. package/lib/sql-query/Update.js +38 -0
  284. package/lib/sql-query/Where.d.ts +4 -0
  285. package/lib/sql-query/Where.js +280 -0
  286. package/package.json +72 -36
  287. package/.travis.yml +0 -14
  288. package/@types/index.d.ts +0 -254
  289. package/@types/orm_mirror/orm.d.ts +0 -275
  290. package/@types/orm_mirror/sql-query.d.ts +0 -79
  291. package/Readme.md +0 -843
  292. package/lib/index.js +0 -13
  293. package/lib/modules/mysql.js +0 -35
  294. package/lib/modules/sqlite3.js +0 -27
  295. package/lib/patch.js +0 -271
@@ -0,0 +1,37 @@
1
+ import events = require("events");
2
+ import type { FxOrmNS } from "./Typo/ORM";
3
+ import type { FxOrmCommon } from "./Typo/_common";
4
+ import type { FxOrmDMLDriver } from "./Typo/DMLDriver";
5
+ import type { FxOrmCoreCallbackNS } from "../orm-core/index";
6
+ import type { FxOrmSettings } from "./Typo/settings";
7
+ export declare class ORM extends events.EventEmitter implements FxOrmNS.ORM {
8
+ validators: FxOrmNS.ORM['validators'];
9
+ enforce: FxOrmNS.ORM['enforce'];
10
+ settings: FxOrmNS.ORM['settings'];
11
+ driver_name: FxOrmNS.ORM['driver_name'];
12
+ driver: FxOrmNS.ORM['driver'];
13
+ comparators: FxOrmNS.ORM['comparators'];
14
+ models: FxOrmNS.ORM['models'];
15
+ plugins: FxOrmNS.ORM['plugins'];
16
+ customTypes: FxOrmNS.ORM['customTypes'];
17
+ constructor(driver_name: string, driver: FxOrmDMLDriver.DMLDriver, settings: FxOrmSettings.SettingInstance);
18
+ use(...[plugin_const, opts]: Parameters<FxOrmNS.ORM['use']>): this;
19
+ define(...[name, properties, opts]: Parameters<FxOrmNS.ORM['define']>): any;
20
+ defineType(...[name, opts]: Parameters<FxOrmNS.ORM['defineType']>): this;
21
+ pingSync(): void;
22
+ ping(...[cb]: Parameters<FxOrmNS.ORM['ping']>): this;
23
+ closeSync(): void;
24
+ close(...[cb]: Parameters<FxOrmNS.ORM['close']>): this;
25
+ load(): any;
26
+ syncSync(): void;
27
+ sync(...[cb]: Parameters<FxOrmNS.ORM['sync']>): this;
28
+ dropSync(): void;
29
+ drop(...[cb]: Parameters<FxOrmNS.ORM['drop']>): this;
30
+ queryParamCastserial(...chains: any[]): {
31
+ get: (cb: FxOrmCommon.GenericCallback<any[]>) => any;
32
+ };
33
+ begin(): void;
34
+ commit(): void;
35
+ rollback(): void;
36
+ trans<T>(func: FxOrmCoreCallbackNS.ExecutionCallback<T>): boolean;
37
+ }
package/lib/orm/ORM.js ADDED
@@ -0,0 +1,241 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.ORM = void 0;
3
+ const util = require("util");
4
+ const events = require("events");
5
+ const uuid = require("uuid");
6
+ const SqlQuery = require("../sql-query/Query");
7
+ const plugin_1 = require("./Patch/plugin");
8
+ const Model_1 = require("./Model");
9
+ const Error_1 = require("./Error");
10
+ const Utilities = require("./Utilities");
11
+ const Enforces = require("@fibjs/enforce");
12
+ const validators = require("./Validators");
13
+ const Settings = require("./Settings");
14
+ class ORM extends events.EventEmitter {
15
+ constructor(driver_name, driver, settings) {
16
+ super();
17
+ this.validators = validators;
18
+ this.enforce = Enforces;
19
+ this.settings = settings;
20
+ this.driver_name = driver_name;
21
+ this.driver = driver;
22
+ this.driver.uid = uuid.node().hex();
23
+ this.comparators = Object.assign({}, SqlQuery.comparators);
24
+ this.models = {};
25
+ this.plugins = [];
26
+ this.customTypes = {};
27
+ events.EventEmitter.call(this);
28
+ var onError = (err) => {
29
+ if (this.settings.get("connection.reconnect")) {
30
+ if (typeof this.driver.reconnect === "undefined") {
31
+ return this.emit("error", new Error_1.default("Connection lost - driver does not support reconnection", 'CONNECTION_LOST'));
32
+ }
33
+ this.driver.reconnect(() => {
34
+ this.driver.on("error", onError);
35
+ });
36
+ if (this.listeners("error").length === 0) {
37
+ // since user want auto reconnect,
38
+ // don't emit without listeners or it will throw
39
+ return;
40
+ }
41
+ }
42
+ this.emit("error", err);
43
+ };
44
+ driver.on("error", onError);
45
+ this.use(plugin_1.default);
46
+ }
47
+ use(...[plugin_const, opts]) {
48
+ if (typeof plugin_const === "string") {
49
+ try {
50
+ plugin_const = require(Utilities.getRealPath(plugin_const));
51
+ }
52
+ catch (e) {
53
+ throw e;
54
+ }
55
+ }
56
+ var plugin = plugin_const(this, opts || {});
57
+ if (typeof plugin.define === "function") {
58
+ for (let k in this.models) {
59
+ plugin.define(this.models[k], this);
60
+ }
61
+ }
62
+ this.plugins.push(plugin);
63
+ return this;
64
+ }
65
+ ;
66
+ define(...[name, properties, opts]) {
67
+ properties = properties || {};
68
+ opts = opts || {};
69
+ for (let i = 0; i < this.plugins.length; i++) {
70
+ // TODO: only pass normalized properties to beforeDefine
71
+ if (typeof this.plugins[i].beforeDefine === "function") {
72
+ this.plugins[i].beforeDefine(name, properties, opts);
73
+ }
74
+ }
75
+ const m_settings = opts.useSelfSettings ? Settings.Container(this.settings.get('*')) : this.settings;
76
+ const virtualView = Utilities.normalizeVirtualViewOption(opts.virtualView, this.driver.knex);
77
+ const modelTable = opts.table || opts.collection || ((m_settings.get("model.namePrefix") || "") + name);
78
+ const generateSqlSelect = Utilities.__wrapTableSourceAsGneratingSqlSelect({
79
+ virtualView,
80
+ customSelect: opts.customSelect,
81
+ generateSqlSelect: opts.generateSqlSelect,
82
+ }, {
83
+ dialect: this.driver.query.Dialect,
84
+ modelTable,
85
+ });
86
+ this.models[name] = new Model_1.Model({
87
+ name: name,
88
+ db: this,
89
+ settings: m_settings,
90
+ driver_name: this.driver_name,
91
+ driver: this.driver,
92
+ table: opts.table || opts.collection || ((m_settings.get("model.namePrefix") || "") + name),
93
+ tableComment: opts.tableComment || '',
94
+ virtualView: virtualView,
95
+ generateSqlSelect,
96
+ // not standard Record<string, FxOrmProperty.NormalizedProperty> here, but we should pass it firstly
97
+ properties: properties,
98
+ __for_extension: opts.__for_extension || false,
99
+ indexes: opts.indexes || [],
100
+ identityCache: opts.hasOwnProperty("identityCache") ? opts.identityCache : m_settings.get("instance.identityCache"),
101
+ keys: opts.id,
102
+ autoSave: opts.hasOwnProperty("autoSave") ? opts.autoSave : m_settings.get("instance.autoSave"),
103
+ autoFetch: opts.hasOwnProperty("autoFetch") ? opts.autoFetch : m_settings.get("instance.autoFetch"),
104
+ autoFetchLimit: opts.autoFetchLimit || m_settings.get("instance.autoFetchLimit"),
105
+ cascadeRemove: opts.hasOwnProperty("cascadeRemove") ? opts.cascadeRemove : m_settings.get("instance.cascadeRemove"),
106
+ hooks: opts.hooks || {},
107
+ methods: opts.methods || {},
108
+ validations: opts.validations || {},
109
+ ievents: opts.ievents || {},
110
+ instanceCacheSize: opts.hasOwnProperty("instanceCacheSize") ? opts.instanceCacheSize : m_settings.get("instance.cacheSize"),
111
+ });
112
+ for (let i = 0; i < this.plugins.length; i++) {
113
+ if (typeof this.plugins[i].define === "function") {
114
+ this.plugins[i].define(this.models[name], this);
115
+ }
116
+ }
117
+ return this.models[name];
118
+ }
119
+ ;
120
+ defineType(...[name, opts]) {
121
+ this.customTypes[name] = opts;
122
+ this.driver.customTypes[name] = opts;
123
+ return this;
124
+ }
125
+ ;
126
+ pingSync() {
127
+ this.driver.ping();
128
+ }
129
+ ;
130
+ ping(...[cb]) {
131
+ this.driver.ping(cb);
132
+ return this;
133
+ }
134
+ ;
135
+ closeSync() {
136
+ this.driver.close();
137
+ }
138
+ close(...[cb]) {
139
+ const syncResponse = Utilities.catchBlocking(this.closeSync, [], { thisArg: this });
140
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
141
+ return this;
142
+ }
143
+ ;
144
+ load() {
145
+ var files = util.flatten(Array.prototype.slice.apply(arguments));
146
+ var cb = function (err) { };
147
+ if (typeof files[files.length - 1] == "function") {
148
+ cb = files.pop();
149
+ }
150
+ var loadNext = function () {
151
+ if (files.length === 0) {
152
+ return cb(null);
153
+ }
154
+ var file = files.shift();
155
+ try {
156
+ return require(Utilities.getRealPath(file, 4))(this, function (err) {
157
+ if (err)
158
+ return cb(err);
159
+ return loadNext();
160
+ });
161
+ }
162
+ catch (ex) {
163
+ return cb(ex);
164
+ }
165
+ }.bind(this);
166
+ return loadNext();
167
+ }
168
+ ;
169
+ syncSync() {
170
+ var modelIds = Object.keys(this.models);
171
+ if (modelIds.length === 0)
172
+ return;
173
+ modelIds.forEach(modelId => {
174
+ this.models[modelId].syncSync();
175
+ });
176
+ }
177
+ ;
178
+ sync(...[cb]) {
179
+ const syncResponse = Utilities.catchBlocking(this.syncSync, [], { thisArg: this });
180
+ Utilities.takeAwayResult(syncResponse, { no_throw: !!cb, callback: cb });
181
+ return this;
182
+ }
183
+ ;
184
+ dropSync() {
185
+ var modelIds = Object.keys(this.models);
186
+ if (modelIds.length === 0)
187
+ return;
188
+ modelIds.forEach(modelId => {
189
+ this.models[modelId].dropSync();
190
+ });
191
+ }
192
+ ;
193
+ drop(...[cb]) {
194
+ const syncResponse = Utilities.catchBlocking(this.dropSync, [], { thisArg: this });
195
+ Utilities.takeAwayResult(syncResponse, { no_throw: !!cb, callback: cb });
196
+ return this;
197
+ }
198
+ ;
199
+ queryParamCastserial(...chains) {
200
+ return {
201
+ get: function (cb) {
202
+ var params = [];
203
+ var getNext = function () {
204
+ if (params.length === chains.length) {
205
+ params.unshift(null);
206
+ return cb.apply(null, params);
207
+ }
208
+ chains[params.length].run(function (err, instances) {
209
+ if (err) {
210
+ params.unshift(err);
211
+ return cb.apply(null, params);
212
+ }
213
+ params.push(instances);
214
+ return getNext();
215
+ });
216
+ };
217
+ getNext();
218
+ return this;
219
+ }
220
+ };
221
+ }
222
+ ;
223
+ begin() {
224
+ return this.driver.db.connection.begin();
225
+ }
226
+ ;
227
+ commit() {
228
+ return this.driver.db.connection.commit();
229
+ }
230
+ ;
231
+ rollback() {
232
+ return this.driver.db.connection.rollback();
233
+ }
234
+ ;
235
+ trans(func) {
236
+ const connection = this.driver.db.connection;
237
+ return connection.trans(func.bind(connection));
238
+ }
239
+ ;
240
+ }
241
+ exports.ORM = ORM;
@@ -0,0 +1,40 @@
1
+ /// <reference types="@fibjs/types" />
2
+ declare const fibCache: any;
3
+ declare module "fib-cache" {
4
+ interface LRUOptions {
5
+ max?: number;
6
+ ttl?: number;
7
+ resolver?: (key: string) => any;
8
+ }
9
+ interface LRUItem {
10
+ expiry: number;
11
+ key: string;
12
+ prev: LRUItem;
13
+ next: LRUItem;
14
+ owner: LRU;
15
+ value: any;
16
+ ready: Class_Event;
17
+ }
18
+ class LRU {
19
+ constructor(options?: LRUOptions);
20
+ items: Record<string, LRUItem>;
21
+ size: number;
22
+ first: LRUItem;
23
+ last: LRUItem;
24
+ max: number;
25
+ ttl: number;
26
+ resolver: (key: string) => any;
27
+ clear(): LRU;
28
+ delete_item(item: LRUItem): LRU;
29
+ delete(key: string): LRU;
30
+ entries(keys?: string[]): [string, any][];
31
+ evict(): LRU;
32
+ get(key: string): any;
33
+ has(key: string): boolean;
34
+ keys(): string[];
35
+ new_item(key: string, value?: any): LRUItem;
36
+ order_item(item: LRUItem): void;
37
+ set(key: string, value: any): LRU;
38
+ values(keys?: string[]): any[];
39
+ }
40
+ }
@@ -0,0 +1,2 @@
1
+ const fibCache = require('fib-cache');
2
+ module.exports = fibCache;
@@ -0,0 +1,4 @@
1
+ import { FxOrmNS } from "../Typo/ORM";
2
+ export default function (orm: FxOrmNS.ORM, plugin_opts: {
3
+ enable: boolean;
4
+ }): FxOrmNS.Plugin;
@@ -0,0 +1,13 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ function default_1(orm, plugin_opts) {
3
+ function beforeDefine(name, properties, opts) {
4
+ opts.hooks = opts.hooks || {};
5
+ }
6
+ function define(m) {
7
+ }
8
+ return {
9
+ beforeDefine,
10
+ define
11
+ };
12
+ }
13
+ exports.default = default_1;
@@ -0,0 +1,9 @@
1
+ import { FxOrmModel } from './Typo/model';
2
+ import { FxOrmNS } from './Typo/ORM';
3
+ import { FxOrmProperty } from './Typo/property';
4
+ export declare function normalize(opts: {
5
+ prop: FxOrmModel.ComplexModelPropertyDefinition;
6
+ name: string;
7
+ customTypes: FxOrmNS.ORM['customTypes'];
8
+ settings: FxOrmNS.ORM['settings'];
9
+ }): FxOrmProperty.NormalizedProperty;
@@ -0,0 +1,71 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.normalize = void 0;
3
+ const _cloneDeep = require("lodash.clonedeep");
4
+ const Error_1 = require("./Error");
5
+ const KNOWN_TYPES = [
6
+ "text", "number", "integer", "boolean", "date", "enum", "object",
7
+ "binary", "point", "serial"
8
+ ];
9
+ function normalize(opts) {
10
+ const orig_prop = opts.prop;
11
+ let result_prop = orig_prop;
12
+ if (typeof opts.prop === "function") {
13
+ const primitiveProp = opts.prop;
14
+ switch (primitiveProp.name) {
15
+ case "String":
16
+ result_prop = { type: "text" };
17
+ break;
18
+ case "Number":
19
+ result_prop = { type: "number" };
20
+ break;
21
+ case "Boolean":
22
+ result_prop = { type: "boolean" };
23
+ break;
24
+ case "Date":
25
+ result_prop = { type: "date" };
26
+ break;
27
+ case "Object":
28
+ result_prop = { type: "object" };
29
+ break;
30
+ case "Buffer":
31
+ result_prop = { type: "binary" };
32
+ break;
33
+ }
34
+ }
35
+ else if (typeof opts.prop === "string") {
36
+ result_prop = {
37
+ type: opts.prop
38
+ };
39
+ }
40
+ else if (Array.isArray(opts.prop)) {
41
+ result_prop = { type: "enum", values: opts.prop };
42
+ }
43
+ else {
44
+ result_prop = _cloneDeep(orig_prop);
45
+ }
46
+ if (!KNOWN_TYPES.includes(result_prop.type) && !(result_prop.type in opts.customTypes)) {
47
+ throw new Error_1.default("Unknown property type: " + result_prop.type, 'NO_SUPPORT');
48
+ }
49
+ if (!result_prop.hasOwnProperty("required") && opts.settings.get("properties.required")) {
50
+ result_prop.required = true;
51
+ }
52
+ // Defaults to true. Setting to false hides properties from JSON.stringify(modelInstance).
53
+ if (!result_prop.hasOwnProperty("enumerable") || result_prop.enumerable === true) {
54
+ result_prop.enumerable = true;
55
+ }
56
+ // Defaults to true. Rational means floating point here.
57
+ if (result_prop.type == "number" && result_prop.rational === undefined) {
58
+ result_prop.rational = true;
59
+ }
60
+ if (!('mapsTo' in result_prop)) {
61
+ result_prop.mapsTo = opts.name;
62
+ }
63
+ if (result_prop.type == "number" && result_prop.rational === false) {
64
+ result_prop.type = "integer";
65
+ delete result_prop.rational;
66
+ }
67
+ result_prop.name = opts.name;
68
+ return result_prop;
69
+ }
70
+ exports.normalize = normalize;
71
+ ;
@@ -0,0 +1,42 @@
1
+ import { FxOrmSettings } from './Typo/settings';
2
+ export declare const defaults: () => {
3
+ model: {
4
+ namePrefix: string;
5
+ repair_column: boolean;
6
+ /**
7
+ * @dangerous
8
+ */
9
+ allow_drop_column: boolean;
10
+ };
11
+ properties: {
12
+ primary_key: string;
13
+ association_key: string;
14
+ required: boolean;
15
+ };
16
+ instance: {
17
+ defaultFindLimit: number;
18
+ defaultCacheSize: number;
19
+ identityCache: boolean;
20
+ identityCacheSaveCheck: boolean;
21
+ autoSave: boolean;
22
+ autoFetch: boolean;
23
+ autoFetchLimit: number;
24
+ cascadeRemove: boolean;
25
+ returnAllErrors: boolean;
26
+ saveAssociationsByDefault: boolean;
27
+ };
28
+ hasOne: {};
29
+ hasMany: {
30
+ key: boolean;
31
+ };
32
+ extendsTo: {
33
+ throwWhenNotFound: boolean;
34
+ };
35
+ connection: {
36
+ reconnect: boolean;
37
+ pool: boolean;
38
+ debug: boolean;
39
+ };
40
+ };
41
+ export declare function Container(settings: object): FxOrmSettings.SettingInstance;
42
+ export declare const defaultSettingsInstance: FxOrmSettings.SettingInstance;
@@ -0,0 +1,119 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.defaultSettingsInstance = exports.Container = exports.defaults = void 0;
3
+ const _cloneDeep = require("lodash.clonedeep");
4
+ const default_settings = {
5
+ model: {
6
+ namePrefix: '',
7
+ // TODO: add test about it
8
+ repair_column: false,
9
+ /**
10
+ * @dangerous
11
+ */
12
+ allow_drop_column: false,
13
+ },
14
+ properties: {
15
+ primary_key: "id",
16
+ association_key: "{name}_{field}",
17
+ required: false
18
+ },
19
+ instance: {
20
+ defaultFindLimit: 1000,
21
+ defaultCacheSize: 1000,
22
+ identityCache: false,
23
+ identityCacheSaveCheck: true,
24
+ autoSave: false,
25
+ autoFetch: false,
26
+ autoFetchLimit: 1,
27
+ cascadeRemove: true,
28
+ returnAllErrors: false,
29
+ saveAssociationsByDefault: true
30
+ },
31
+ hasOne: {
32
+ // allowRemoveReverse : false,
33
+ },
34
+ hasMany: {
35
+ // Makes the foreign key fields a composite key
36
+ key: false
37
+ },
38
+ extendsTo: {
39
+ // would default as false in the future
40
+ throwWhenNotFound: true,
41
+ },
42
+ connection: {
43
+ reconnect: true,
44
+ pool: false,
45
+ debug: false
46
+ }
47
+ };
48
+ const defaults = function () {
49
+ return default_settings;
50
+ };
51
+ exports.defaults = defaults;
52
+ function Container(settings) {
53
+ return {
54
+ set: function (key, value) {
55
+ set(key, value, settings);
56
+ return this;
57
+ },
58
+ get: function (key, def) {
59
+ const v = get(key, def, settings);
60
+ if (v instanceof Function) {
61
+ return v;
62
+ }
63
+ else {
64
+ return _cloneDeep(v);
65
+ }
66
+ },
67
+ unset: function () {
68
+ for (let i = 0; i < arguments.length; i++) {
69
+ if (typeof arguments[i] === "string") {
70
+ unset(arguments[i], settings);
71
+ }
72
+ }
73
+ return this;
74
+ }
75
+ };
76
+ }
77
+ exports.Container = Container;
78
+ function set(key, value, obj) {
79
+ const p = key.indexOf(".");
80
+ if (p === -1) {
81
+ return obj[key] = value;
82
+ }
83
+ if (!obj.hasOwnProperty(key.substr(0, p))) {
84
+ obj[key.substr(0, p)] = {};
85
+ }
86
+ return set(key.substr(p + 1), value, obj[key.substr(0, p)]);
87
+ }
88
+ function get(key, def, obj) {
89
+ const p = key.indexOf(".");
90
+ if (p === -1) {
91
+ if (key === '*') {
92
+ return obj;
93
+ }
94
+ return obj.hasOwnProperty(key) ? obj[key] : def;
95
+ }
96
+ if (!obj.hasOwnProperty(key.substr(0, p))) {
97
+ return def;
98
+ }
99
+ return get(key.substr(p + 1), def, obj[key.substr(0, p)]);
100
+ }
101
+ function unset(key, obj) {
102
+ const p = key.indexOf(".");
103
+ if (p === -1) {
104
+ if (key === '*') {
105
+ return 'reset';
106
+ }
107
+ else {
108
+ delete obj[key];
109
+ }
110
+ return;
111
+ }
112
+ if (!obj.hasOwnProperty(key.substr(0, p))) {
113
+ return;
114
+ }
115
+ if (unset(key.substr(p + 1), obj[key.substr(0, p)]) === 'reset') {
116
+ obj[key.substr(0, p)] = {};
117
+ }
118
+ }
119
+ exports.defaultSettingsInstance = Container((0, exports.defaults)());
@@ -0,0 +1,5 @@
1
+ import type { FxOrmNS } from "./Typo/ORM";
2
+ export declare const clear: FxOrmNS.SingletonModule['clear'];
3
+ export declare const modelClear: FxOrmNS.SingletonModule['modelClear'];
4
+ export declare const get: FxOrmNS.SingletonModule['get'];
5
+ export declare const modelGet: FxOrmNS.SingletonModule['modelGet'];
@@ -0,0 +1,72 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.modelGet = exports.get = exports.modelClear = exports.clear = void 0;
3
+ const Helpers = require("./Helpers");
4
+ const coroutine = require("coroutine");
5
+ /**
6
+ * refactor this module with LRUCache
7
+ */
8
+ const models = {};
9
+ const clear = function (key) {
10
+ let driver_table_uid2 = '';
11
+ if (key && typeof key === 'string')
12
+ driver_table_uid2 = Helpers.parseDriverUidAndTableNameFromUID(key).driver_table_uid;
13
+ Object.values(models).forEach(model => {
14
+ const { driver_table_uid: driver_table_uid1 } = Helpers.parseDriverUidAndTableNameFromUID(model.uid);
15
+ if (driver_table_uid2 && driver_table_uid2 !== driver_table_uid1)
16
+ return;
17
+ (0, exports.modelClear)(model, key);
18
+ });
19
+ return this;
20
+ };
21
+ exports.clear = clear;
22
+ const modelClear = function (model, key) {
23
+ if (key && typeof key === "string") {
24
+ model.caches.delete(key);
25
+ }
26
+ else {
27
+ model.caches.clear();
28
+ }
29
+ return this;
30
+ };
31
+ exports.modelClear = modelClear;
32
+ // synchronous get
33
+ const get = function (key, opts, reFetchSync) {
34
+ const { driver_table_uid } = Helpers.parseDriverUidAndTableNameFromUID(key);
35
+ return (0, exports.modelGet)(models[driver_table_uid], key, opts, reFetchSync);
36
+ };
37
+ exports.get = get;
38
+ const modelGet = function (model, key, opts, reFetchSync) {
39
+ if (opts.identityCache === false)
40
+ return reFetchSync();
41
+ var value = model.caches.get(key);
42
+ if (value !== undefined) {
43
+ if (opts && opts.saveCheck && typeof value.saved === "function" && !value.saved())
44
+ // if not saved, don't return it, fetch original from db
45
+ return reFetchSync();
46
+ }
47
+ else {
48
+ value = reFetchSync();
49
+ model.caches.set(key, value);
50
+ }
51
+ const expected_expire = typeof opts.identityCache === "number" ? (opts.identityCache * 1000) : model.caches.ttl;
52
+ const expire_expection_delta = expected_expire - model.caches.ttl;
53
+ if (expire_expection_delta > 0) {
54
+ coroutine.start(() => {
55
+ while (true) {
56
+ // quit when cached key-value has not been here
57
+ if (!model.caches.has(key))
58
+ break;
59
+ model.caches.set(key, value);
60
+ if (expected_expire > Date.now()) {
61
+ model.caches.delete(key);
62
+ break;
63
+ }
64
+ }
65
+ });
66
+ }
67
+ else if (expire_expection_delta < 0) {
68
+ setTimeout(() => model.caches.delete(key), expected_expire);
69
+ }
70
+ return value;
71
+ };
72
+ exports.modelGet = modelGet;
@@ -0,0 +1,5 @@
1
+ export declare namespace FxOrmAdapter {
2
+ interface AddAdapatorFunction {
3
+ (name: string, constructor: Function): void;
4
+ }
5
+ }
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });