fib-orm 1.4.2 → 2.1.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 +75 -0
  5. package/lib/cli/commands/dumpModel.d.ts +2 -0
  6. package/lib/cli/commands/dumpModel.js +107 -0
  7. package/lib/cli/commands/escape.d.ts +2 -0
  8. package/lib/cli/commands/escape.js +69 -0
  9. package/lib/cli/commands/spinner.d.ts +2 -0
  10. package/lib/cli/commands/spinner.js +37 -0
  11. package/lib/cli/commands/upgrade.d.ts +2 -0
  12. package/lib/cli/commands/upgrade.js +66 -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 +25 -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 +19 -0
  19. package/lib/codepoints-map.json +762 -0
  20. package/lib/db-driver/Typo.d.ts +111 -0
  21. package/lib/db-driver/Typo.js +2 -0
  22. package/lib/db-driver/built-ins/base.class.d.ts +91 -0
  23. package/lib/db-driver/built-ins/base.class.js +187 -0
  24. package/lib/db-driver/built-ins/driver-dm.d.ts +17 -0
  25. package/lib/db-driver/built-ins/driver-dm.js +54 -0
  26. package/lib/db-driver/built-ins/driver-mongodb.d.ts +23 -0
  27. package/lib/db-driver/built-ins/driver-mongodb.js +47 -0
  28. package/lib/db-driver/built-ins/driver-mysql.d.ts +17 -0
  29. package/lib/db-driver/built-ins/driver-mysql.js +38 -0
  30. package/lib/db-driver/built-ins/driver-postgresql.d.ts +21 -0
  31. package/lib/db-driver/built-ins/driver-postgresql.js +77 -0
  32. package/lib/db-driver/built-ins/driver-redis.d.ts +11 -0
  33. package/lib/db-driver/built-ins/driver-redis.js +36 -0
  34. package/lib/db-driver/built-ins/driver-sqlite.d.ts +16 -0
  35. package/lib/db-driver/built-ins/driver-sqlite.js +35 -0
  36. package/lib/db-driver/index.d.ts +3 -0
  37. package/lib/db-driver/index.js +5 -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 +4 -0
  56. package/lib/knex/sandbox.d.ts +4 -0
  57. package/lib/knex/sandbox.js +23 -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 +177 -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 +16 -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 +14 -0
  69. package/lib/orm/Associations/_utils.js +53 -0
  70. package/lib/orm/ChainFind.d.ts +3 -0
  71. package/lib/orm/ChainFind.js +328 -0
  72. package/lib/orm/ChainInstance.d.ts +6 -0
  73. package/lib/orm/ChainInstance.js +74 -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 +2 -0
  77. package/lib/orm/Drivers/DB/base.js +4 -0
  78. package/lib/orm/Drivers/DB/dm.d.ts +9 -0
  79. package/lib/orm/Drivers/DB/dm.js +24 -0
  80. package/lib/orm/Drivers/DB/mysql.d.ts +9 -0
  81. package/lib/orm/Drivers/DB/mysql.js +24 -0
  82. package/lib/orm/Drivers/DB/postgres.d.ts +9 -0
  83. package/lib/orm/Drivers/DB/postgres.js +23 -0
  84. package/lib/orm/Drivers/DB/sqlite3.d.ts +14 -0
  85. package/lib/orm/Drivers/DB/sqlite3.js +40 -0
  86. package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
  87. package/lib/orm/Drivers/DML/_ddl-sql.js +52 -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 +39 -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 +3 -0
  96. package/lib/orm/Drivers/DML/dm.d.ts +2 -0
  97. package/lib/orm/Drivers/DML/dm.js +294 -0
  98. package/lib/orm/Drivers/DML/index.d.ts +4 -0
  99. package/lib/orm/Drivers/DML/index.js +7 -0
  100. package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
  101. package/lib/orm/Drivers/DML/mysql.js +252 -0
  102. package/lib/orm/Drivers/DML/postgres.d.ts +2 -0
  103. package/lib/orm/Drivers/DML/postgres.js +277 -0
  104. package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
  105. package/lib/orm/Drivers/DML/sqlite.js +267 -0
  106. package/lib/orm/Error.d.ts +11 -0
  107. package/lib/orm/Error.js +41 -0
  108. package/lib/orm/Helpers.d.ts +35 -0
  109. package/lib/orm/Helpers.js +234 -0
  110. package/lib/orm/Hook.d.ts +13 -0
  111. package/lib/orm/Hook.js +100 -0
  112. package/lib/orm/Instance.d.ts +2 -0
  113. package/lib/orm/Instance.js +698 -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 +6 -0
  117. package/lib/orm/Model.js +974 -0
  118. package/lib/orm/ORM.d.ts +37 -0
  119. package/lib/orm/ORM.js +242 -0
  120. package/lib/orm/Patch/fib-cache.d.ts +39 -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 +14 -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 +120 -0
  128. package/lib/orm/Singleton.d.ts +5 -0
  129. package/lib/orm/Singleton.js +73 -0
  130. package/lib/orm/Typo/Adapter.d.ts +5 -0
  131. package/lib/orm/Typo/Adapter.js +2 -0
  132. package/lib/orm/Typo/DMLDriver.d.ts +209 -0
  133. package/lib/orm/Typo/DMLDriver.js +2 -0
  134. package/lib/orm/Typo/Db.d.ts +35 -0
  135. package/lib/orm/Typo/Db.js +2 -0
  136. package/lib/orm/Typo/Error.d.ts +29 -0
  137. package/lib/orm/Typo/Error.js +2 -0
  138. package/lib/orm/Typo/ORM.d.ts +231 -0
  139. package/lib/orm/Typo/ORM.js +4 -0
  140. package/lib/orm/Typo/Validators.d.ts +31 -0
  141. package/lib/orm/Typo/Validators.js +3 -0
  142. package/lib/orm/Typo/_common.d.ts +37 -0
  143. package/lib/orm/Typo/_common.js +2 -0
  144. package/lib/orm/Typo/assoc.d.ts +221 -0
  145. package/lib/orm/Typo/assoc.js +2 -0
  146. package/lib/orm/Typo/hook.d.ts +42 -0
  147. package/lib/orm/Typo/hook.js +2 -0
  148. package/lib/orm/Typo/instance.d.ts +156 -0
  149. package/lib/orm/Typo/instance.js +2 -0
  150. package/lib/orm/Typo/model.d.ts +311 -0
  151. package/lib/orm/Typo/model.js +7 -0
  152. package/lib/orm/Typo/patch.d.ts +31 -0
  153. package/lib/orm/Typo/patch.js +2 -0
  154. package/lib/orm/Typo/property.d.ts +57 -0
  155. package/lib/orm/Typo/property.js +2 -0
  156. package/lib/orm/Typo/query.d.ts +216 -0
  157. package/lib/orm/Typo/query.js +2 -0
  158. package/lib/orm/Typo/settings.d.ts +35 -0
  159. package/lib/orm/Typo/settings.js +6 -0
  160. package/lib/orm/Typo/synchronous.d.ts +47 -0
  161. package/lib/orm/Typo/synchronous.js +2 -0
  162. package/lib/orm/Utilities.d.ts +178 -0
  163. package/lib/orm/Utilities.js +975 -0
  164. package/lib/orm/Validators.d.ts +33 -0
  165. package/lib/orm/Validators.js +101 -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 +67 -0
  171. package/lib/orm/index.js +158 -0
  172. package/lib/orm-core/callback.d.ts +12 -0
  173. package/lib/orm-core/callback.js +2 -0
  174. package/lib/orm-core/dialect.d.ts +80 -0
  175. package/lib/orm-core/dialect.js +2 -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 +7 -0
  180. package/lib/orm-core/sync.d.ts +12 -0
  181. package/lib/orm-core/sync.js +2 -0
  182. package/lib/orm-core/utils.d.ts +1 -0
  183. package/lib/orm-core/utils.js +6 -0
  184. package/lib/orm-property/Property.d.ts +78 -0
  185. package/lib/orm-property/Property.js +2 -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 +23 -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 +238 -0
  196. package/lib/orm-property/transformers/index.d.ts +4 -0
  197. package/lib/orm-property/transformers/index.js +6 -0
  198. package/lib/orm-property/transformers/mysql.d.ts +101 -0
  199. package/lib/orm-property/transformers/mysql.js +262 -0
  200. package/lib/orm-property/transformers/postgresql.d.ts +54 -0
  201. package/lib/orm-property/transformers/postgresql.js +190 -0
  202. package/lib/orm-property/transformers/sqlite.d.ts +19 -0
  203. package/lib/orm-property/transformers/sqlite.js +172 -0
  204. package/lib/sql-ddl-sync/Dialects/dm.d.ts +39 -0
  205. package/lib/sql-ddl-sync/Dialects/dm.js +297 -0
  206. package/lib/sql-ddl-sync/Dialects/index.d.ts +6 -0
  207. package/lib/sql-ddl-sync/Dialects/index.js +8 -0
  208. package/lib/sql-ddl-sync/Dialects/mysql.d.ts +38 -0
  209. package/lib/sql-ddl-sync/Dialects/mysql.js +241 -0
  210. package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +39 -0
  211. package/lib/sql-ddl-sync/Dialects/postgresql.js +287 -0
  212. package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +43 -0
  213. package/lib/sql-ddl-sync/Dialects/sqlite.js +257 -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 +2 -0
  218. package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
  219. package/lib/sql-ddl-sync/Typo/DbIndex.js +2 -0
  220. package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
  221. package/lib/sql-ddl-sync/Typo/Dialect.js +2 -0
  222. package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
  223. package/lib/sql-ddl-sync/Typo/Driver.js +2 -0
  224. package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
  225. package/lib/sql-ddl-sync/Typo/Query.js +2 -0
  226. package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
  227. package/lib/sql-ddl-sync/Typo/SQL.js +2 -0
  228. package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
  229. package/lib/sql-ddl-sync/Typo/_common.js +2 -0
  230. package/lib/sql-ddl-sync/Utils.d.ts +39 -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 +372 -0
  234. package/lib/sql-query/Comparators.d.ts +3 -0
  235. package/lib/sql-query/Comparators.js +95 -0
  236. package/lib/sql-query/Create.d.ts +35 -0
  237. package/lib/sql-query/Create.js +78 -0
  238. package/lib/sql-query/Dialects/dm.d.ts +3 -0
  239. package/lib/sql-query/Dialects/dm.js +75 -0
  240. package/lib/sql-query/Dialects/index.d.ts +10 -0
  241. package/lib/sql-query/Dialects/index.js +13 -0
  242. package/lib/sql-query/Dialects/mssql.d.ts +3 -0
  243. package/lib/sql-query/Dialects/mssql.js +74 -0
  244. package/lib/sql-query/Dialects/mysql.d.ts +3 -0
  245. package/lib/sql-query/Dialects/mysql.js +101 -0
  246. package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
  247. package/lib/sql-query/Dialects/postgresql.js +77 -0
  248. package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
  249. package/lib/sql-query/Dialects/sqlite.js +62 -0
  250. package/lib/sql-query/Helpers.d.ts +37 -0
  251. package/lib/sql-query/Helpers.js +260 -0
  252. package/lib/sql-query/Insert.d.ts +12 -0
  253. package/lib/sql-query/Insert.js +34 -0
  254. package/lib/sql-query/Query.d.ts +28 -0
  255. package/lib/sql-query/Query.js +120 -0
  256. package/lib/sql-query/Remove.d.ts +16 -0
  257. package/lib/sql-query/Remove.js +94 -0
  258. package/lib/sql-query/Select.d.ts +62 -0
  259. package/lib/sql-query/Select.js +534 -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 +2 -0
  264. package/lib/sql-query/Typo/Comparators.d.ts +140 -0
  265. package/lib/sql-query/Typo/Comparators.js +2 -0
  266. package/lib/sql-query/Typo/Dialect.d.ts +39 -0
  267. package/lib/sql-query/Typo/Dialect.js +2 -0
  268. package/lib/sql-query/Typo/Field.d.ts +8 -0
  269. package/lib/sql-query/Typo/Field.js +2 -0
  270. package/lib/sql-query/Typo/Helper.d.ts +4 -0
  271. package/lib/sql-query/Typo/Helper.js +2 -0
  272. package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
  273. package/lib/sql-query/Typo/Query-ChainBuilder.js +2 -0
  274. package/lib/sql-query/Typo/Query.d.ts +34 -0
  275. package/lib/sql-query/Typo/Query.js +17 -0
  276. package/lib/sql-query/Typo/Sql.d.ts +143 -0
  277. package/lib/sql-query/Typo/Sql.js +2 -0
  278. package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
  279. package/lib/sql-query/Typo/SubQuery.js +2 -0
  280. package/lib/sql-query/Typo/index.d.ts +9 -0
  281. package/lib/sql-query/Typo/index.js +2 -0
  282. package/lib/sql-query/Update.d.ts +15 -0
  283. package/lib/sql-query/Update.js +39 -0
  284. package/lib/sql-query/Where.d.ts +4 -0
  285. package/lib/sql-query/Where.js +280 -0
  286. package/package.json +71 -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,4 @@
1
+ "use strict";
2
+ const sandbox_1 = require("./sandbox");
3
+ const Sandbox = new sandbox_1.default();
4
+ 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,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vm = require("vm");
4
+ const path = require("path");
5
+ const uuid = require("uuid");
6
+ const __root = path.resolve(__dirname, '../');
7
+ function patchMods() {
8
+ this.add({
9
+ 'uuid': {
10
+ v4() {
11
+ return uuid.snowflake();
12
+ }
13
+ }
14
+ });
15
+ }
16
+ class Box extends vm.SandBox {
17
+ constructor() {
18
+ const [mods = {}, __require = function () { }, __global = undefined] = Array.prototype.slice.apply(arguments);
19
+ 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);
20
+ patchMods.call(this);
21
+ }
22
+ }
23
+ 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
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addAdapter = addAdapter;
4
+ exports.getAdapter = getAdapter;
5
+ const DMLs = require("./Drivers/DML");
6
+ const adapters = {};
7
+ function addAdapter(name, constructor) {
8
+ if (adapters.hasOwnProperty(name)) {
9
+ throw new Error(`[addAdapter] adapter '${name}' already exists`);
10
+ }
11
+ Object.defineProperty(adapters, name, {
12
+ get() {
13
+ return constructor;
14
+ },
15
+ configurable: false
16
+ });
17
+ }
18
+ addAdapter('mysql', DMLs.mysql.Driver);
19
+ addAdapter('sqlite', DMLs.sqlite.Driver);
20
+ addAdapter('postgres', DMLs.postgres.Driver);
21
+ addAdapter('dm', DMLs.dm.Driver);
22
+ function getAdapter(name) {
23
+ switch (name) {
24
+ case 'mysql':
25
+ case 'sqlite':
26
+ break;
27
+ case 'pg':
28
+ case 'psql':
29
+ case 'postgres':
30
+ case 'postgresql':
31
+ name = 'postgres';
32
+ break;
33
+ case 'dm':
34
+ break;
35
+ }
36
+ // TODO: support install dml driver from node_modules
37
+ if (!(name in adapters)) {
38
+ // trigger error which could be detected by `isDriverNotSupportedError`
39
+ const err = new Error(`No adapter named '${name}' found.`);
40
+ err.code = 'MODULE_NOT_FOUND';
41
+ throw err;
42
+ }
43
+ return adapters[name];
44
+ }
@@ -0,0 +1,3 @@
1
+ import { FxOrmQuery } from "./Typo/query";
2
+ declare const AggregateFunctions: FxOrmQuery.AggregateConstructor;
3
+ export = AggregateFunctions;
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ const Error_1 = require("./Error");
3
+ const Utilities = require("./Utilities");
4
+ const AggregateFunctions = function (opts) {
5
+ if (typeof opts.driver.getQuery !== "function") {
6
+ throw new Error_1.default("This driver does not support aggregate functions", 'NO_SUPPORT');
7
+ }
8
+ if (!Array.isArray(opts.driver.aggregate_functions)) {
9
+ throw new Error_1.default("This driver does not support aggregate functions", 'NO_SUPPORT');
10
+ }
11
+ const aggregates = [[]];
12
+ let group_by = null;
13
+ let used_distinct = false;
14
+ const aggregationFunc = function (fun) {
15
+ fun = fun.toLocaleLowerCase();
16
+ return function () {
17
+ // select 1st array as args
18
+ var args = (arguments.length && Array.isArray(arguments[0]) ? arguments[0] : Array.prototype.slice.apply(arguments));
19
+ if (args.length > 0) {
20
+ aggregates[aggregates.length - 1].push({ func_name: fun, args: args, as: aggregateAlias(fun, args) });
21
+ // aggregates.push([]);
22
+ }
23
+ else {
24
+ aggregates[aggregates.length - 1].push({ func_name: fun, as: aggregateAlias(fun, args) });
25
+ }
26
+ if (fun === "distinct") {
27
+ used_distinct = true;
28
+ }
29
+ return proto;
30
+ };
31
+ };
32
+ const distinct_to_one_row = function () {
33
+ return used_distinct && aggregates.length === 1;
34
+ };
35
+ const proto = {
36
+ groupBy: function (...columns) {
37
+ group_by = columns;
38
+ return this;
39
+ },
40
+ limit: function (offset, limit) {
41
+ if (typeof limit === "number") {
42
+ opts.limit = [offset, limit];
43
+ }
44
+ else {
45
+ opts.limit = [0, offset]; // offset = limit
46
+ }
47
+ return this;
48
+ },
49
+ order: function (...orders) {
50
+ if (!Array.isArray(opts.order)) {
51
+ opts.order = [];
52
+ }
53
+ opts.order = Utilities.standardizeOrder(orders);
54
+ return this;
55
+ },
56
+ select: function (...columns) {
57
+ if (columns.length === 0) {
58
+ throw new Error_1.default("When using append you must at least define one property", 'PARAM_MISMATCH');
59
+ }
60
+ if (Array.isArray(columns[0])) {
61
+ opts.propertyList = opts.propertyList.concat(columns[0]);
62
+ }
63
+ else {
64
+ opts.propertyList = opts.propertyList.concat(Array.prototype.slice.apply(arguments));
65
+ }
66
+ return this;
67
+ },
68
+ as: function (alias) {
69
+ if (!aggregates.length || (aggregates.length === 1 && aggregates[0].length === 0)) {
70
+ throw new Error_1.default("No aggregate functions defined yet", 'PARAM_MISMATCH');
71
+ }
72
+ var len = aggregates.length;
73
+ aggregates[len - 1][aggregates[len - 1].length - 1].as = alias;
74
+ return this;
75
+ },
76
+ call: function (fun, args) {
77
+ if (args && args.length > 0) {
78
+ aggregates[aggregates.length - 1].push({ func_name: fun, args: args, as: aggregateAlias(fun, args) });
79
+ }
80
+ else {
81
+ aggregates[aggregates.length - 1].push({ func_name: fun, as: aggregateAlias(fun, args) });
82
+ }
83
+ if (fun.toLowerCase() === "distinct") {
84
+ used_distinct = true;
85
+ }
86
+ return this;
87
+ },
88
+ getSync: function () {
89
+ if (aggregates[aggregates.length - 1].length === 0) {
90
+ aggregates.length -= 1;
91
+ }
92
+ if (!aggregates.length) {
93
+ throw new Error_1.default("Missing aggregate functions", 'PARAM_MISMATCH');
94
+ }
95
+ const query = opts.driver.getQuery()
96
+ .select()
97
+ .from(opts.table)
98
+ .select(opts.propertyList);
99
+ for (let i = 0; i < aggregates.length; i++) {
100
+ for (let j = 0; j < aggregates[i].length; j++) {
101
+ query.fun(aggregates[i][j].func_name, aggregates[i][j].args, aggregates[i][j].as);
102
+ }
103
+ }
104
+ query.where(opts.conditions);
105
+ if (group_by !== null) {
106
+ query.groupBy.apply(query, group_by);
107
+ }
108
+ if (opts.order) {
109
+ for (let i = 0; i < opts.order.length; i++) {
110
+ query.order(opts.order[i][0], opts.order[i][1]);
111
+ }
112
+ }
113
+ if (opts.limit) {
114
+ query.offset(opts.limit[0]).limit(opts.limit[1]);
115
+ }
116
+ const q_str = query.build();
117
+ const data = opts.driver.execQuery(q_str);
118
+ if (group_by !== null) {
119
+ return data;
120
+ }
121
+ var items = [];
122
+ if (distinct_to_one_row()) {
123
+ for (let i = 0; i < data.length; i++) {
124
+ items.push(data[i][Object.keys(data[i]).pop()]);
125
+ }
126
+ return items;
127
+ }
128
+ for (let i = 0; i < aggregates.length; i++) {
129
+ for (let j = 0; j < aggregates[i].length; j++) {
130
+ items.push(data[0][aggregates[i][j].as] || null);
131
+ }
132
+ }
133
+ return items;
134
+ },
135
+ get: function (cb) {
136
+ if (typeof cb !== "function") {
137
+ throw new Error_1.default("You must pass a callback to Model.aggregate().get()", 'MISSING_CALLBACK');
138
+ }
139
+ const syncReponse = Utilities.catchBlocking(proto.getSync, [], { thisArg: proto });
140
+ if (syncReponse.error instanceof Error_1.default && syncReponse.error.code === Error_1.default.codes['PARAM_MISMATCH']) {
141
+ throw syncReponse.error;
142
+ }
143
+ Utilities.takeAwayResult(syncReponse, {
144
+ callback: cb && function (err, results) {
145
+ if (err)
146
+ return cb(err);
147
+ if (distinct_to_one_row())
148
+ return cb(null, results);
149
+ /**
150
+ * when not used_distinct, support to spread results as callback's arguments
151
+ */
152
+ results.unshift(null);
153
+ return cb.apply(null, results);
154
+ }
155
+ });
156
+ }
157
+ };
158
+ opts.driver.aggregate_functions.forEach(aggregate_function => {
159
+ addAggregate(proto, aggregate_function, aggregationFunc);
160
+ });
161
+ return proto;
162
+ };
163
+ function addAggregate(proto, fun, builder) {
164
+ let k;
165
+ if (Array.isArray(fun)) {
166
+ k = fun[0].toLowerCase();
167
+ proto[k] = builder(fun[1] || fun[0]);
168
+ }
169
+ else {
170
+ k = fun.toLowerCase();
171
+ proto[k] = builder(fun);
172
+ }
173
+ }
174
+ function aggregateAlias(fun, fields) {
175
+ return fun + (fields && fields.length ? "_" + fields.join("_") : "");
176
+ }
177
+ 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
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepare = prepare;
4
+ exports.extend = extend;
5
+ exports.autoFetch = autoFetch;
6
+ const util = require("util");
7
+ const Hook = require("../Hook");
8
+ const _cloneDeep = require("lodash.clonedeep");
9
+ const Error_1 = require("../Error");
10
+ const Singleton = require("../Singleton");
11
+ const Utilities = require("../Utilities");
12
+ const Helpers = require("../Helpers");
13
+ const Model_1 = require("../Model");
14
+ const _utils_1 = require("./_utils");
15
+ function noOperation(...args) { }
16
+ ;
17
+ /**
18
+ *
19
+ * @param db orm instance
20
+ * @param Model model
21
+ * @param associations association definitions
22
+ */
23
+ function prepare(Model, assocs, opts) {
24
+ const { extend_associations } = assocs;
25
+ const { db } = opts;
26
+ Model.extendsTo = function (name, properties, assoc_options) {
27
+ Utilities.disAllowOpForVModel(Model, 'model.extendsTo');
28
+ assoc_options = assoc_options || {};
29
+ for (let i = 0; i < db.plugins.length; i++) {
30
+ if (typeof db.plugins[i].beforeExtendsTo === "function") {
31
+ db.plugins[i].beforeExtendsTo(Model, {
32
+ association_name: name,
33
+ properties: properties,
34
+ assoc_options
35
+ });
36
+ }
37
+ }
38
+ const associationSemanticNameCore = assoc_options.name || Utilities.formatNameFor("assoc:extendsTo", name);
39
+ const association = {
40
+ name: name,
41
+ model: null,
42
+ table: assoc_options.table || (0, _utils_1.defineDefaultExtendsToTableName)(Model.table, name),
43
+ reverse: assoc_options.reverse,
44
+ // reversed : assoc_options.reversed,
45
+ autoFetch: assoc_options.autoFetch || false,
46
+ autoFetchLimit: assoc_options.autoFetchLimit || 2,
47
+ field: Utilities.wrapFieldObject({
48
+ field: assoc_options.field,
49
+ model: Model,
50
+ altName: Model.table
51
+ }) || Utilities.formatAssociatedField(Model, Model.table, false, false),
52
+ getAccessor: assoc_options.getAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.get, associationSemanticNameCore),
53
+ setAccessor: assoc_options.setAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.set, associationSemanticNameCore),
54
+ hasAccessor: assoc_options.hasAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.has, associationSemanticNameCore),
55
+ delAccessor: assoc_options.delAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.del, associationSemanticNameCore),
56
+ modelFindByAccessor: assoc_options.modelFindByAccessor || (0, _utils_1.defineAssociationAccessorMethodName)(_utils_1.ACCESSOR_KEYS.modelFindBy, associationSemanticNameCore),
57
+ hooks: Object.assign({}, assoc_options.hooks),
58
+ };
59
+ Utilities.fillSyncVersionAccessorForAssociation(association);
60
+ Utilities.addHookPatchHelperForAssociation(association);
61
+ const newProperties = _cloneDeep(properties);
62
+ const assoc_field = association.field;
63
+ for (let k in assoc_field) {
64
+ assoc_field[k].klass = 'extendsTo';
65
+ if (assoc_field[k].type === 'serial') {
66
+ assoc_field[k].type = 'integer';
67
+ assoc_field[k].key = true;
68
+ }
69
+ newProperties[k] = assoc_field[k];
70
+ }
71
+ const modelOpts = util.extend(util.pick(assoc_options, 'identityCache', 'autoSave', 'cascadeRemove', 'hooks', 'methods', 'validations'), {
72
+ id: Object.keys(assoc_field),
73
+ __for_extension: true,
74
+ });
75
+ association.model = db.define(association.table, newProperties, modelOpts);
76
+ association.model.hasOne(association.reverse || Model.table, Model, {
77
+ __for_extension: true,
78
+ field: assoc_field,
79
+ reverse: null,
80
+ reversed: false,
81
+ hooks: assoc_options.reverseHooks
82
+ });
83
+ extend_associations.push(association);
84
+ const findByAccessorChainOrRunSync = function (is_sync = false) {
85
+ return function () {
86
+ var cb = null, conditions = null, options = {};
87
+ const args = Array.prototype.slice.apply(arguments);
88
+ Helpers.selectArgs(args, (arg_type, arg) => {
89
+ switch (arg_type) {
90
+ case "function":
91
+ cb = arg;
92
+ break;
93
+ case "object":
94
+ if (conditions === null) {
95
+ conditions = arg;
96
+ }
97
+ else {
98
+ options = arg;
99
+ }
100
+ break;
101
+ }
102
+ });
103
+ if (conditions === null) {
104
+ throw new Error_1.default(`.${association.modelFindByAccessor}() is missing a conditions object`, 'PARAM_MISMATCH');
105
+ }
106
+ return (0, Model_1.listFindByChainOrRunSync)(Model, {}, [
107
+ {
108
+ association_name: association.name,
109
+ conditions: conditions
110
+ },
111
+ ], options, { callback: cb, is_sync });
112
+ };
113
+ };
114
+ Model[association.modelFindByAccessor] = findByAccessorChainOrRunSync();
115
+ Model[association.modelFindBySyncAccessor] = findByAccessorChainOrRunSync(true);
116
+ (0, _utils_1.addAssociationInfoToModel)(Model, name, {
117
+ type: 'extendsTo',
118
+ association: association
119
+ });
120
+ return association.model;
121
+ };
122
+ }
123
+ ;
124
+ function extend(Model, Instance, Driver, associations, cfg) {
125
+ for (let i = 0; i < associations.length; i++) {
126
+ extendInstance(Model, Instance, Driver, associations[i], cfg);
127
+ }
128
+ }
129
+ ;
130
+ function autoFetch(Instance, associations, opts, parallel = false) {
131
+ if (associations.length === 0) {
132
+ return;
133
+ }
134
+ Utilities.parallelQueryIfPossible(parallel, associations, (item) => autoFetchInstance(Instance, item, opts));
135
+ }
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,16 @@
1
+ import type { FxOrmInstance } from '../Typo/instance';
2
+ import type { FxOrmAssociation } from '../Typo/assoc';
3
+ import type { FxOrmModel } from '../Typo/model';
4
+ import type { FxOrmDMLDriver } from '../Typo/DMLDriver';
5
+ import type { FxOrmNS } from '../Typo/ORM';
6
+ export declare function prepare(Model: FxOrmModel.Model, assocs: {
7
+ one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[];
8
+ many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[];
9
+ extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[];
10
+ }, opts: {
11
+ db: FxOrmNS.ORM;
12
+ }): void;
13
+ export declare function extend(Model: FxOrmModel.Model, Instance: FxOrmInstance.Instance, Driver: FxOrmDMLDriver.DMLDriver, associations: FxOrmAssociation.InstanceAssociationItem_HasMany[], cfg: {
14
+ assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany;
15
+ }): void;
16
+ export declare function autoFetch(Instance: FxOrmInstance.Instance, associations: FxOrmAssociation.InstanceAssociationItem_HasMany[], opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany, parallel?: boolean): void;