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,2 @@
1
+ declare const _default: any;
2
+ export = _default;
@@ -0,0 +1,3 @@
1
+ const sandbox_1 = require("./sandbox");
2
+ const Sandbox = new sandbox_1.default();
3
+ module.exports = Sandbox.require('knex', __dirname);
@@ -0,0 +1,4 @@
1
+ import vm = require('vm');
2
+ export default class Box extends vm.SandBox {
3
+ constructor();
4
+ }
@@ -0,0 +1,22 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const vm = require("vm");
3
+ const path = require("path");
4
+ const uuid = require("uuid");
5
+ const __root = path.resolve(__dirname, '../');
6
+ function patchMods() {
7
+ this.add({
8
+ 'uuid': {
9
+ v4() {
10
+ return uuid.snowflake();
11
+ }
12
+ }
13
+ });
14
+ }
15
+ class Box extends vm.SandBox {
16
+ constructor() {
17
+ const [mods = {}, __require = function () { }, __global = undefined] = Array.prototype.slice.apply(arguments);
18
+ super(Object.assign(Object.assign({}, mods), { 'fs': require('fs'), 'path': require('path'), 'os': require('os'), 'url': require('url'), 'tty': require('tty'), 'util': require('util'), 'events': require('events'), 'assert': require('assert'), 'crypto': require('crypto'), 'timers': require('timers'), 'tarn': require('./deps/tarn/dist/tarn') }), __require, __global);
19
+ patchMods.call(this);
20
+ }
21
+ }
22
+ exports.default = Box;
@@ -0,0 +1,3 @@
1
+ import type { FxOrmDMLDriver } from './Typo/DMLDriver';
2
+ export declare function addAdapter(name: string, constructor: FxOrmDMLDriver.DMLDriverConstructor): void;
3
+ export declare function getAdapter(name: string): FxOrmDMLDriver.DMLDriverConstructor;
@@ -0,0 +1,44 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.getAdapter = exports.addAdapter = void 0;
3
+ const DMLs = require("./Drivers/DML");
4
+ const adapters = {};
5
+ function addAdapter(name, constructor) {
6
+ if (adapters.hasOwnProperty(name)) {
7
+ throw new Error(`[addAdapter] adapter '${name}' already exists`);
8
+ }
9
+ Object.defineProperty(adapters, name, {
10
+ get() {
11
+ return constructor;
12
+ },
13
+ configurable: false
14
+ });
15
+ }
16
+ exports.addAdapter = addAdapter;
17
+ addAdapter('mysql', DMLs.mysql.Driver);
18
+ addAdapter('sqlite', DMLs.sqlite.Driver);
19
+ addAdapter('postgres', DMLs.postgres.Driver);
20
+ addAdapter('dm', DMLs.dm.Driver);
21
+ function getAdapter(name) {
22
+ switch (name) {
23
+ case 'mysql':
24
+ case 'sqlite':
25
+ break;
26
+ case 'pg':
27
+ case 'psql':
28
+ case 'postgres':
29
+ case 'postgresql':
30
+ name = 'postgres';
31
+ break;
32
+ case 'dm':
33
+ break;
34
+ }
35
+ // TODO: support install dml driver from node_modules
36
+ if (!(name in adapters)) {
37
+ // trigger error which could be detected by `isDriverNotSupportedError`
38
+ const err = new Error(`No adapter named '${name}' found.`);
39
+ err.code = 'MODULE_NOT_FOUND';
40
+ throw err;
41
+ }
42
+ return adapters[name];
43
+ }
44
+ exports.getAdapter = getAdapter;
@@ -0,0 +1,3 @@
1
+ import { FxOrmQuery } from "./Typo/query";
2
+ declare const AggregateFunctions: FxOrmQuery.AggregateConstructor;
3
+ export = AggregateFunctions;
@@ -0,0 +1,176 @@
1
+ const Error_1 = require("./Error");
2
+ const Utilities = require("./Utilities");
3
+ const AggregateFunctions = function (opts) {
4
+ if (typeof opts.driver.getQuery !== "function") {
5
+ throw new Error_1.default("This driver does not support aggregate functions", 'NO_SUPPORT');
6
+ }
7
+ if (!Array.isArray(opts.driver.aggregate_functions)) {
8
+ throw new Error_1.default("This driver does not support aggregate functions", 'NO_SUPPORT');
9
+ }
10
+ const aggregates = [[]];
11
+ let group_by = null;
12
+ let used_distinct = false;
13
+ const aggregationFunc = function (fun) {
14
+ fun = fun.toLocaleLowerCase();
15
+ return function () {
16
+ // select 1st array as args
17
+ var args = (arguments.length && Array.isArray(arguments[0]) ? arguments[0] : Array.prototype.slice.apply(arguments));
18
+ if (args.length > 0) {
19
+ aggregates[aggregates.length - 1].push({ func_name: fun, args: args, as: aggregateAlias(fun, args) });
20
+ // aggregates.push([]);
21
+ }
22
+ else {
23
+ aggregates[aggregates.length - 1].push({ func_name: fun, as: aggregateAlias(fun, args) });
24
+ }
25
+ if (fun === "distinct") {
26
+ used_distinct = true;
27
+ }
28
+ return proto;
29
+ };
30
+ };
31
+ const distinct_to_one_row = function () {
32
+ return used_distinct && aggregates.length === 1;
33
+ };
34
+ const proto = {
35
+ groupBy: function (...columns) {
36
+ group_by = columns;
37
+ return this;
38
+ },
39
+ limit: function (offset, limit) {
40
+ if (typeof limit === "number") {
41
+ opts.limit = [offset, limit];
42
+ }
43
+ else {
44
+ opts.limit = [0, offset]; // offset = limit
45
+ }
46
+ return this;
47
+ },
48
+ order: function (...orders) {
49
+ if (!Array.isArray(opts.order)) {
50
+ opts.order = [];
51
+ }
52
+ opts.order = Utilities.standardizeOrder(orders);
53
+ return this;
54
+ },
55
+ select: function (...columns) {
56
+ if (columns.length === 0) {
57
+ throw new Error_1.default("When using append you must at least define one property", 'PARAM_MISMATCH');
58
+ }
59
+ if (Array.isArray(columns[0])) {
60
+ opts.propertyList = opts.propertyList.concat(columns[0]);
61
+ }
62
+ else {
63
+ opts.propertyList = opts.propertyList.concat(Array.prototype.slice.apply(arguments));
64
+ }
65
+ return this;
66
+ },
67
+ as: function (alias) {
68
+ if (!aggregates.length || (aggregates.length === 1 && aggregates[0].length === 0)) {
69
+ throw new Error_1.default("No aggregate functions defined yet", 'PARAM_MISMATCH');
70
+ }
71
+ var len = aggregates.length;
72
+ aggregates[len - 1][aggregates[len - 1].length - 1].as = alias;
73
+ return this;
74
+ },
75
+ call: function (fun, args) {
76
+ if (args && args.length > 0) {
77
+ aggregates[aggregates.length - 1].push({ func_name: fun, args: args, as: aggregateAlias(fun, args) });
78
+ }
79
+ else {
80
+ aggregates[aggregates.length - 1].push({ func_name: fun, as: aggregateAlias(fun, args) });
81
+ }
82
+ if (fun.toLowerCase() === "distinct") {
83
+ used_distinct = true;
84
+ }
85
+ return this;
86
+ },
87
+ getSync: function () {
88
+ if (aggregates[aggregates.length - 1].length === 0) {
89
+ aggregates.length -= 1;
90
+ }
91
+ if (!aggregates.length) {
92
+ throw new Error_1.default("Missing aggregate functions", 'PARAM_MISMATCH');
93
+ }
94
+ const query = opts.driver.getQuery()
95
+ .select()
96
+ .from(opts.table)
97
+ .select(opts.propertyList);
98
+ for (let i = 0; i < aggregates.length; i++) {
99
+ for (let j = 0; j < aggregates[i].length; j++) {
100
+ query.fun(aggregates[i][j].func_name, aggregates[i][j].args, aggregates[i][j].as);
101
+ }
102
+ }
103
+ query.where(opts.conditions);
104
+ if (group_by !== null) {
105
+ query.groupBy.apply(query, group_by);
106
+ }
107
+ if (opts.order) {
108
+ for (let i = 0; i < opts.order.length; i++) {
109
+ query.order(opts.order[i][0], opts.order[i][1]);
110
+ }
111
+ }
112
+ if (opts.limit) {
113
+ query.offset(opts.limit[0]).limit(opts.limit[1]);
114
+ }
115
+ const q_str = query.build();
116
+ const data = opts.driver.execQuery(q_str);
117
+ if (group_by !== null) {
118
+ return data;
119
+ }
120
+ var items = [];
121
+ if (distinct_to_one_row()) {
122
+ for (let i = 0; i < data.length; i++) {
123
+ items.push(data[i][Object.keys(data[i]).pop()]);
124
+ }
125
+ return items;
126
+ }
127
+ for (let i = 0; i < aggregates.length; i++) {
128
+ for (let j = 0; j < aggregates[i].length; j++) {
129
+ items.push(data[0][aggregates[i][j].as] || null);
130
+ }
131
+ }
132
+ return items;
133
+ },
134
+ get: function (cb) {
135
+ if (typeof cb !== "function") {
136
+ throw new Error_1.default("You must pass a callback to Model.aggregate().get()", 'MISSING_CALLBACK');
137
+ }
138
+ const syncReponse = Utilities.catchBlocking(proto.getSync, [], { thisArg: proto });
139
+ if (syncReponse.error instanceof Error_1.default && syncReponse.error.code === Error_1.default.codes['PARAM_MISMATCH']) {
140
+ throw syncReponse.error;
141
+ }
142
+ Utilities.takeAwayResult(syncReponse, {
143
+ callback: cb && function (err, results) {
144
+ if (err)
145
+ return cb(err);
146
+ if (distinct_to_one_row())
147
+ return cb(null, results);
148
+ /**
149
+ * when not used_distinct, support to spread results as callback's arguments
150
+ */
151
+ results.unshift(null);
152
+ return cb.apply(null, results);
153
+ }
154
+ });
155
+ }
156
+ };
157
+ opts.driver.aggregate_functions.forEach(aggregate_function => {
158
+ addAggregate(proto, aggregate_function, aggregationFunc);
159
+ });
160
+ return proto;
161
+ };
162
+ function addAggregate(proto, fun, builder) {
163
+ let k;
164
+ if (Array.isArray(fun)) {
165
+ k = fun[0].toLowerCase();
166
+ proto[k] = builder(fun[1] || fun[0]);
167
+ }
168
+ else {
169
+ k = fun.toLowerCase();
170
+ proto[k] = builder(fun);
171
+ }
172
+ }
173
+ function aggregateAlias(fun, fields) {
174
+ return fun + (fields && fields.length ? "_" + fields.join("_") : "");
175
+ }
176
+ module.exports = AggregateFunctions;
@@ -0,0 +1,22 @@
1
+ import type { FxOrmInstance } from '../Typo/instance';
2
+ import type { FxOrmAssociation } from '../Typo/assoc';
3
+ import type { FxOrmNS } from '../Typo/ORM';
4
+ import type { FxOrmModel } from '../Typo/model';
5
+ import type { FxOrmDMLDriver } from '../Typo/DMLDriver';
6
+ /**
7
+ *
8
+ * @param db orm instance
9
+ * @param Model model
10
+ * @param associations association definitions
11
+ */
12
+ export declare function prepare(Model: FxOrmModel.Model, assocs: {
13
+ one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[];
14
+ many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[];
15
+ extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[];
16
+ }, opts: {
17
+ db: FxOrmNS.ORM;
18
+ }): void;
19
+ export declare function extend(Model: FxOrmModel.Model, Instance: FxOrmInstance.Instance, Driver: FxOrmDMLDriver.DMLDriver, associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[], cfg: {
20
+ assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasOne;
21
+ }): void;
22
+ export declare function autoFetch(Instance: FxOrmInstance.Instance, associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[], opts: FxOrmNS.ModelAutoFetchOptions, parallel?: boolean): void;
@@ -0,0 +1,274 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.autoFetch = exports.extend = exports.prepare = void 0;
3
+ const util = require("util");
4
+ const Hook = require("../Hook");
5
+ const _cloneDeep = require("lodash.clonedeep");
6
+ const Error_1 = require("../Error");
7
+ const Singleton = require("../Singleton");
8
+ const Utilities = require("../Utilities");
9
+ const Helpers = require("../Helpers");
10
+ const Model_1 = require("../Model");
11
+ const _utils_1 = require("./_utils");
12
+ function noOperation(...args) { }
13
+ ;
14
+ /**
15
+ *
16
+ * @param db orm instance
17
+ * @param Model model
18
+ * @param associations association definitions
19
+ */
20
+ function prepare(Model, assocs, opts) {
21
+ const { extend_associations } = assocs;
22
+ const { db } = opts;
23
+ Model.extendsTo = function (name, properties, assoc_options) {
24
+ Utilities.disAllowOpForVModel(Model, 'model.extendsTo');
25
+ assoc_options = assoc_options || {};
26
+ for (let i = 0; i < db.plugins.length; i++) {
27
+ if (typeof db.plugins[i].beforeExtendsTo === "function") {
28
+ db.plugins[i].beforeExtendsTo(Model, {
29
+ association_name: name,
30
+ properties: properties,
31
+ assoc_options
32
+ });
33
+ }
34
+ }
35
+ const associationSemanticNameCore = assoc_options.name || Utilities.formatNameFor("assoc:extendsTo", name);
36
+ const association = {
37
+ name: name,
38
+ model: null,
39
+ table: assoc_options.table || (0, _utils_1.defineDefaultExtendsToTableName)(Model.table, name),
40
+ reverse: assoc_options.reverse,
41
+ // reversed : assoc_options.reversed,
42
+ autoFetch: assoc_options.autoFetch || false,
43
+ autoFetchLimit: assoc_options.autoFetchLimit || 2,
44
+ field: Utilities.wrapFieldObject({
45
+ field: assoc_options.field,
46
+ model: Model,
47
+ altName: Model.table
48
+ }) || Utilities.formatAssociatedField(Model, Model.table, false, false),
49
+ getAccessor: assoc_options.getAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.get, associationSemanticNameCore),
50
+ setAccessor: assoc_options.setAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.set, associationSemanticNameCore),
51
+ hasAccessor: assoc_options.hasAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.has, associationSemanticNameCore),
52
+ delAccessor: assoc_options.delAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.del, associationSemanticNameCore),
53
+ modelFindByAccessor: assoc_options.modelFindByAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.modelFindBy, associationSemanticNameCore),
54
+ hooks: Object.assign({}, assoc_options.hooks),
55
+ };
56
+ Utilities.fillSyncVersionAccessorForAssociation(association);
57
+ Utilities.addHookPatchHelperForAssociation(association);
58
+ const newProperties = _cloneDeep(properties);
59
+ const assoc_field = association.field;
60
+ for (let k in assoc_field) {
61
+ assoc_field[k].klass = 'extendsTo';
62
+ if (assoc_field[k].type === 'serial') {
63
+ assoc_field[k].type = 'integer';
64
+ assoc_field[k].key = true;
65
+ }
66
+ newProperties[k] = assoc_field[k];
67
+ }
68
+ const modelOpts = util.extend(util.pick(assoc_options, 'identityCache', 'autoSave', 'cascadeRemove', 'hooks', 'methods', 'validations'), {
69
+ id: Object.keys(assoc_field),
70
+ __for_extension: true,
71
+ });
72
+ association.model = db.define(association.table, newProperties, modelOpts);
73
+ association.model.hasOne(association.reverse || Model.table, Model, {
74
+ __for_extension: true,
75
+ field: assoc_field,
76
+ reverse: null,
77
+ reversed: false,
78
+ hooks: assoc_options.reverseHooks
79
+ });
80
+ extend_associations.push(association);
81
+ const findByAccessorChainOrRunSync = function (is_sync = false) {
82
+ return function () {
83
+ var cb = null, conditions = null, options = {};
84
+ const args = Array.prototype.slice.apply(arguments);
85
+ Helpers.selectArgs(args, (arg_type, arg) => {
86
+ switch (arg_type) {
87
+ case "function":
88
+ cb = arg;
89
+ break;
90
+ case "object":
91
+ if (conditions === null) {
92
+ conditions = arg;
93
+ }
94
+ else {
95
+ options = arg;
96
+ }
97
+ break;
98
+ }
99
+ });
100
+ if (conditions === null) {
101
+ throw new Error_1.default(`.${association.modelFindByAccessor}() is missing a conditions object`, 'PARAM_MISMATCH');
102
+ }
103
+ return (0, Model_1.listFindByChainOrRunSync)(Model, {}, [
104
+ {
105
+ association_name: association.name,
106
+ conditions: conditions
107
+ },
108
+ ], options, { callback: cb, is_sync });
109
+ };
110
+ };
111
+ Model[association.modelFindByAccessor] = findByAccessorChainOrRunSync();
112
+ Model[association.modelFindBySyncAccessor] = findByAccessorChainOrRunSync(true);
113
+ (0, _utils_1.addAssociationInfoToModel)(Model, name, {
114
+ type: 'extendsTo',
115
+ association: association
116
+ });
117
+ return association.model;
118
+ };
119
+ }
120
+ exports.prepare = prepare;
121
+ ;
122
+ function extend(Model, Instance, Driver, associations, cfg) {
123
+ for (let i = 0; i < associations.length; i++) {
124
+ extendInstance(Model, Instance, Driver, associations[i], cfg);
125
+ }
126
+ }
127
+ exports.extend = extend;
128
+ ;
129
+ function autoFetch(Instance, associations, opts, parallel = false) {
130
+ if (associations.length === 0) {
131
+ return;
132
+ }
133
+ Utilities.parallelQueryIfPossible(parallel, associations, (item) => autoFetchInstance(Instance, item, opts));
134
+ }
135
+ exports.autoFetch = autoFetch;
136
+ ;
137
+ function extendInstance(Model, Instance, Driver, association, cfg) {
138
+ Utilities.addHiddenPropertyToInstance(Instance, association.hasSyncAccessor, function () {
139
+ if (!Instance[Model.id + ''])
140
+ throw new Error_1.default("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table });
141
+ try {
142
+ return !!association.model.getSync(Utilities.values(Instance, Model.id));
143
+ }
144
+ catch (error) {
145
+ if (!Model.settings.get('extendsTo.throwWhenNotFound') && error.literalCode === 'NOT_FOUND')
146
+ return false;
147
+ throw error;
148
+ }
149
+ });
150
+ Utilities.addHiddenPropertyToInstance(Instance, association.hasAccessor, function (cb) {
151
+ process.nextTick(() => {
152
+ const syncResponse = Utilities.catchBlocking(Instance[association.hasSyncAccessor]);
153
+ syncResponse.result = !!syncResponse.result;
154
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
155
+ });
156
+ return this;
157
+ });
158
+ Utilities.addHiddenPropertyToInstance(Instance, association.getSyncAccessor, function (opts = {}) {
159
+ if (!Instance[Model.id + ''])
160
+ throw new Error_1.default("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table });
161
+ try {
162
+ return association.model.getSync(Utilities.values(Instance, Model.id), opts);
163
+ }
164
+ catch (error) {
165
+ if (!Model.settings.get('extendsTo.throwWhenNotFound') && error.literalCode === 'NOT_FOUND')
166
+ return null;
167
+ throw error;
168
+ }
169
+ });
170
+ Utilities.addHiddenPropertyToInstance(Instance, association.getAccessor, function () {
171
+ let opts = {};
172
+ let cb;
173
+ const args = Array.prototype.slice.apply(arguments);
174
+ Helpers.selectArgs(args, (arg_type, arg) => {
175
+ switch (arg_type) {
176
+ case "function":
177
+ cb = arg;
178
+ break;
179
+ case "object":
180
+ opts = arg;
181
+ break;
182
+ }
183
+ });
184
+ process.nextTick(() => {
185
+ const syncResponse = Utilities.catchBlocking(Instance[association.getSyncAccessor], [opts]);
186
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
187
+ });
188
+ return this;
189
+ });
190
+ Utilities.addHiddenPropertyToInstance(Instance, association.setSyncAccessor, function (Extension) {
191
+ const $ref = {
192
+ instance: Instance,
193
+ association: Extension,
194
+ useChannel: Utilities.reusableChannelGenerator()
195
+ };
196
+ Hook.wait(Instance, association.hooks[`beforeSet`], Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
197
+ let Extension = $ref.association;
198
+ Instance.$emit(`before:set:${association.name}`, Extension);
199
+ Instance.saveSync();
200
+ Instance.$emit(`before-del-extension:${association.setAccessor}`);
201
+ Instance[association.delSyncAccessor]();
202
+ Instance.$emit(`after-del-extension:${association.setAccessor}`);
203
+ const fields = Object.keys(association.field);
204
+ if (!Extension.isInstance) {
205
+ $ref.association = Extension = new association.model(Extension);
206
+ }
207
+ for (let i = 0; i < Model.id.length; i++) {
208
+ Extension[fields[i]] = Instance[Model.id[i]];
209
+ }
210
+ Instance.$emit(`before-save-extension:${association.setAccessor}`, Extension);
211
+ Extension.saveSync();
212
+ Instance.$emit(`after-save-extension:${association.setAccessor}`, Extension);
213
+ Instance.$emit(`after:set:${association.name}`, Extension);
214
+ }), Utilities.buildAssocHooksContext('beforeSet', { $ref }));
215
+ Hook.trigger(Instance, association.hooks['afterSet'], Utilities.buildAssocHooksContext('afterSet', { $ref }));
216
+ return $ref.association;
217
+ });
218
+ Utilities.addHiddenPropertyToInstance(Instance, association.setAccessor, function (Extension, cb) {
219
+ process.nextTick(() => {
220
+ const syncResponse = Utilities.catchBlocking(Instance[association.setSyncAccessor], [Extension]);
221
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
222
+ });
223
+ return this;
224
+ });
225
+ Utilities.addHiddenPropertyToInstance(Instance, association.delSyncAccessor, function () {
226
+ if (!Instance[Model.id + ''])
227
+ throw new Error_1.default("Instance not saved, cannot get extension", 'NOT_DEFINED', { model: Model.table });
228
+ const conditions = {};
229
+ const fields = Object.keys(association.field);
230
+ for (let i = 0; i < Model.id.length; i++) {
231
+ conditions[fields[i]] = Instance[Model.id[i]];
232
+ }
233
+ const $ref = {
234
+ instance: Instance,
235
+ removeConditions: conditions,
236
+ useChannel: Utilities.reusableChannelGenerator()
237
+ };
238
+ Hook.wait(Instance, association.hooks[`beforeRemove`], Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
239
+ const extensions = association.model.findSync($ref.removeConditions);
240
+ Instance.$emit(`before:del:${association.name}`, extensions);
241
+ for (let i = 0; i < extensions.length; i++) {
242
+ Singleton.clear(extensions[i].__singleton_uid() + '');
243
+ extensions[i].removeSync();
244
+ }
245
+ Instance.$emit(`after:del:${association.name}`, extensions);
246
+ }), Utilities.buildAssocHooksContext('beforeRemove', { $ref }));
247
+ Hook.trigger(Instance, association.hooks['afterRemove'], Utilities.buildAssocHooksContext('afterRemove', { $ref }));
248
+ return;
249
+ });
250
+ Utilities.addHiddenPropertyToInstance(Instance, association.delAccessor, function (cb) {
251
+ process.nextTick(() => {
252
+ const syncResponse = Utilities.catchBlocking(Instance[association.delSyncAccessor]);
253
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
254
+ });
255
+ return this;
256
+ });
257
+ }
258
+ function autoFetchInstance(Instance, association, opts) {
259
+ if (!Instance.saved())
260
+ return;
261
+ if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
262
+ opts.autoFetchLimit = association.autoFetchLimit;
263
+ }
264
+ if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch))
265
+ return;
266
+ if (Instance.isPersisted()) {
267
+ try {
268
+ const Assoc = Instance[association.getSyncAccessor]({ autoFetchLimit: opts.autoFetchLimit - 1 });
269
+ Instance[association.name] = Assoc;
270
+ }
271
+ catch (err) { }
272
+ }
273
+ return;
274
+ }
@@ -0,0 +1,17 @@
1
+ /// <reference lib="es5" />
2
+ import type { FxOrmInstance } from '../Typo/instance';
3
+ import type { FxOrmAssociation } from '../Typo/assoc';
4
+ import type { FxOrmModel } from '../Typo/model';
5
+ import type { FxOrmDMLDriver } from '../Typo/DMLDriver';
6
+ import type { FxOrmNS } from '../Typo/ORM';
7
+ export declare function prepare(Model: FxOrmModel.Model, assocs: {
8
+ one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[];
9
+ many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[];
10
+ extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[];
11
+ }, opts: {
12
+ db: FxOrmNS.ORM;
13
+ }): void;
14
+ export declare function extend(Model: FxOrmModel.Model, Instance: FxOrmInstance.Instance, Driver: FxOrmDMLDriver.DMLDriver, associations: FxOrmAssociation.InstanceAssociationItem_HasMany[], cfg: {
15
+ assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany;
16
+ }): void;
17
+ export declare function autoFetch(Instance: FxOrmInstance.Instance, associations: FxOrmAssociation.InstanceAssociationItem_HasMany[], opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany, parallel?: boolean): void;