fib-orm 1.4.1 → 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 -193
  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,251 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.Driver = void 0;
3
+ const util = require("util");
4
+ const mysql_1 = require("../DB/mysql");
5
+ const shared = require("./_shared");
6
+ const DDL = require("./_ddl-sql");
7
+ const Query_1 = require("../../../sql-query/Query");
8
+ const Sync = require("../../../sql-ddl-sync/index");
9
+ const utils = require("./_utils");
10
+ const Utilities = require("../../Utilities");
11
+ const _dml_helpers_1 = require("./_dml-helpers");
12
+ exports.Driver = function (config, connection, opts) {
13
+ this.dialect = 'mysql';
14
+ this.opts = opts || {};
15
+ this.customTypes = {};
16
+ Object.defineProperty(this, 'db', {
17
+ value: connection || new mysql_1.Database(config),
18
+ writable: false
19
+ });
20
+ Object.defineProperty(this, 'sqlDriver', {
21
+ value: this.db,
22
+ writable: false
23
+ });
24
+ Object.defineProperty(this, 'config', {
25
+ value: this.db.config,
26
+ writable: false
27
+ });
28
+ if (!this.config.timezone)
29
+ this.config.timezone = "local";
30
+ Object.defineProperty(this, 'ddlSync', {
31
+ value: Sync.dialect(this.dialect),
32
+ writable: false
33
+ });
34
+ Object.defineProperty(this, 'query', {
35
+ value: new Query_1.Query({ dialect: this.dialect, timezone: this.config.timezone }),
36
+ writable: false
37
+ });
38
+ utils.getKnexInstance(this);
39
+ this.aggregate_functions = ["ABS", "CEIL", "FLOOR", "ROUND",
40
+ "AVG", "MIN", "MAX",
41
+ "LOG", "LOG2", "LOG10", "EXP", "POWER",
42
+ "ACOS", "ASIN", "ATAN", "COS", "SIN", "TAN",
43
+ "CONV", ["RANDOM", "RAND"], "RADIANS", "DEGREES",
44
+ "SUM", "COUNT",
45
+ "DISTINCT"];
46
+ };
47
+ util.extend(exports.Driver.prototype, shared, DDL);
48
+ exports.Driver.prototype.ping = function (cb) {
49
+ this.db.ping();
50
+ if (cb)
51
+ setImmediate(cb);
52
+ return this;
53
+ };
54
+ exports.Driver.prototype.on = function (ev, cb) {
55
+ if (ev == "error") {
56
+ this.db.eventor.on("error", cb);
57
+ this.db.eventor.on("unhandledError", cb);
58
+ }
59
+ return this;
60
+ };
61
+ exports.Driver.prototype.connect = function (cb) {
62
+ const syncResponse = Utilities.catchBlocking(() => {
63
+ return this.db.connect();
64
+ });
65
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
66
+ return syncResponse.result;
67
+ };
68
+ exports.Driver.prototype.reconnect = function (cb) {
69
+ const connOpts = this.config.href || this.config;
70
+ this.db = new mysql_1.Database(connOpts);
71
+ const syncResponse = Utilities.catchBlocking(() => {
72
+ return this.connect();
73
+ });
74
+ if (typeof cb === 'function')
75
+ cb(null, syncResponse.result);
76
+ return syncResponse.result;
77
+ };
78
+ exports.Driver.prototype.close = function (cb) {
79
+ const errResults = Utilities.catchBlocking(() => this.db.close());
80
+ Utilities.takeAwayResult(errResults, { no_throw: !!cb, callback: cb });
81
+ return errResults.result;
82
+ };
83
+ exports.Driver.prototype.getQuery = function () {
84
+ return this.query;
85
+ };
86
+ exports.Driver.prototype.execSimpleQuery = function (query, cb) {
87
+ if (this.opts.debug) {
88
+ require("../../Debug").sql('mysql', query);
89
+ }
90
+ return this.db.query(query, cb);
91
+ };
92
+ exports.Driver.prototype.find = function (selectFields, table, conditions, opts) {
93
+ const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
94
+ const __pointTypeMapsTo = opts.__pointTypeMapsTo || [];
95
+ (0, _dml_helpers_1.filterFieldsOnFind)({
96
+ dmlDriver: this,
97
+ pointPropertiesMapsTo: __pointTypeMapsTo,
98
+ }, {
99
+ selectFields,
100
+ });
101
+ const ctx = {
102
+ table,
103
+ fromTuple: from_tuple,
104
+ selectFields,
105
+ selectVirtualFields: opts.selectVirtualFields || []
106
+ };
107
+ let q = this.query.select();
108
+ q = typeof (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect) === 'function' ? (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect.call(this, ctx, q)) || q : Utilities.DEFAULT_GENERATE_SQL_QUERY_SELECT.call(this, ctx, q);
109
+ if (opts.offset) {
110
+ q.offset(opts.offset);
111
+ }
112
+ if (typeof opts.limit == "number") {
113
+ q.limit(opts.limit);
114
+ }
115
+ else if (opts.offset) {
116
+ // OFFSET cannot be used without LIMIT so we use the biggest BIGINT number possible
117
+ q.limit('18446744073709551615');
118
+ }
119
+ utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions, opts.topConditions]);
120
+ utils.buildOrderToQuery.apply(this, [q, opts.order]);
121
+ utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
122
+ utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
123
+ const results = this.execSimpleQuery(q.build());
124
+ if (__pointTypeMapsTo.length > 0 && Array.isArray(results)) {
125
+ results.forEach(item => {
126
+ __pointTypeMapsTo.forEach(field => {
127
+ item[field] = (0, _dml_helpers_1.safeParseJson)(item[field]);
128
+ });
129
+ });
130
+ }
131
+ return results;
132
+ };
133
+ exports.Driver.prototype.count = function (table, conditions, opts, cb) {
134
+ const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
135
+ const q = this.query.select()
136
+ .from(from_tuple)
137
+ .count(null, 'c');
138
+ utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions]);
139
+ utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
140
+ utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
141
+ return this.execSimpleQuery(q.build(), cb);
142
+ };
143
+ exports.Driver.prototype.insert = function (table, data, keyProperties, cb) {
144
+ const q = this.query.insert()
145
+ .into(table)
146
+ .set(data)
147
+ .build();
148
+ const syncResponse = Utilities.catchBlocking(() => {
149
+ const info = this.execSimpleQuery(q);
150
+ const ids = {};
151
+ if (keyProperties) {
152
+ if (keyProperties.length == 1 && info.hasOwnProperty("insertId") && info.insertId !== 0) {
153
+ ids[keyProperties[0].name] = info.insertId;
154
+ }
155
+ else {
156
+ for (let i = 0, prop; i < keyProperties.length; i++) {
157
+ prop = keyProperties[i];
158
+ ids[prop.name] = data[prop.mapsTo];
159
+ }
160
+ }
161
+ }
162
+ return ids;
163
+ });
164
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
165
+ return syncResponse.result;
166
+ };
167
+ exports.Driver.prototype.update = function (table, changes, conditions, cb) {
168
+ var q = this.query.update()
169
+ .into(table)
170
+ .set(changes)
171
+ .where(conditions)
172
+ .build();
173
+ return this.execSimpleQuery(q, cb);
174
+ };
175
+ exports.Driver.prototype.remove = function (table, conditions, cb) {
176
+ var q = this.query.remove()
177
+ .from(table)
178
+ .where(conditions)
179
+ .build();
180
+ return this.execSimpleQuery(q, cb);
181
+ };
182
+ exports.Driver.prototype.clear = function (table, cb) {
183
+ var q = "TRUNCATE TABLE " + this.query.escapeId(table);
184
+ return this.execSimpleQuery(q, cb);
185
+ };
186
+ exports.Driver.prototype.valueToProperty = function (value, property) {
187
+ var customType;
188
+ switch (property.type) {
189
+ case "date":
190
+ if (util.isNumber(value) || util.isString(value))
191
+ value = new Date(value);
192
+ break;
193
+ case "boolean":
194
+ value = !!value;
195
+ break;
196
+ case "object":
197
+ if (typeof value == "object" && !Buffer.isBuffer(value)) {
198
+ break;
199
+ }
200
+ try {
201
+ value = JSON.parse(value);
202
+ }
203
+ catch (e) {
204
+ value = null;
205
+ }
206
+ break;
207
+ case "binary": {
208
+ // compat with standard behavior of `JSON.stringify`/`JSON.parse`
209
+ if (value && value.type === 'Buffer' && Array.isArray(value.data)) {
210
+ value = Buffer.from(value.data);
211
+ }
212
+ break;
213
+ }
214
+ default:
215
+ customType = this.customTypes[property.type];
216
+ if (customType && 'valueToProperty' in customType) {
217
+ value = customType.valueToProperty(value, property);
218
+ }
219
+ }
220
+ return value;
221
+ };
222
+ exports.Driver.prototype.propertyToValue = function (value, property) {
223
+ switch (property.type) {
224
+ case "date":
225
+ if (util.isNumber(value) || util.isString(value))
226
+ value = new Date(value);
227
+ break;
228
+ case "boolean":
229
+ value = (value) ? 1 : 0;
230
+ break;
231
+ case "object":
232
+ if (value !== null) {
233
+ value = JSON.stringify(value);
234
+ }
235
+ break;
236
+ case "point":
237
+ return function () { return 'POINT(' + value.x + ', ' + value.y + ')'; };
238
+ case "binary": {
239
+ break;
240
+ }
241
+ default:
242
+ const customType = this.customTypes[property.type];
243
+ if (customType && 'propertyToValue' in customType) {
244
+ value = customType.propertyToValue(value);
245
+ }
246
+ }
247
+ return value;
248
+ };
249
+ Object.defineProperty(exports.Driver.prototype, "isSql", {
250
+ value: true
251
+ });
@@ -0,0 +1,3 @@
1
+ /// <reference lib="es2015" />
2
+ import type { FxOrmDMLDriver } from "../../Typo/DMLDriver";
3
+ export declare const Driver: FxOrmDMLDriver.DMLDriverConstructor;
@@ -0,0 +1,276 @@
1
+ /// <reference lib="es2015" />
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Driver = void 0;
4
+ const util = require("util");
5
+ const shared = require("./_shared");
6
+ const DDL = require("./_ddl-sql");
7
+ const Sync = require("../../../sql-ddl-sync/index");
8
+ const Query_1 = require("../../../sql-query/Query");
9
+ const utils = require("./_utils");
10
+ const Utilities = require("../../Utilities");
11
+ const postgres_1 = require("../DB/postgres");
12
+ exports.Driver = function (config, connection, opts) {
13
+ this.dialect = 'postgresql';
14
+ this.opts = opts || {};
15
+ this.customTypes = {};
16
+ Object.defineProperty(this, 'db', {
17
+ value: connection || new postgres_1.default(config),
18
+ writable: false
19
+ });
20
+ Object.defineProperty(this, 'sqlDriver', {
21
+ value: this.db,
22
+ writable: false
23
+ });
24
+ Object.defineProperty(this, 'config', {
25
+ value: this.db.config,
26
+ writable: false
27
+ });
28
+ if (!this.config.timezone)
29
+ this.config.timezone = "local";
30
+ Object.defineProperty(this, 'ddlSync', {
31
+ value: Sync.dialect(this.dialect),
32
+ writable: false
33
+ });
34
+ Object.defineProperty(this, 'query', {
35
+ value: new Query_1.Query({ dialect: this.dialect, timezone: this.config.timezone }),
36
+ writable: false
37
+ });
38
+ utils.getKnexInstance(this);
39
+ this.aggregate_functions = [
40
+ "ABS", "CEIL", "FLOOR", "ROUND",
41
+ "AVG", "MIN", "MAX",
42
+ "LOG", "EXP", "POWER",
43
+ "ACOS", "ASIN", "ATAN", "COS", "SIN", "TAN",
44
+ "RANDOM", "RADIANS", "DEGREES",
45
+ "SUM", "COUNT",
46
+ "DISTINCT"
47
+ ];
48
+ };
49
+ util.extend(exports.Driver.prototype, shared, DDL);
50
+ exports.Driver.prototype.ping = function (cb) {
51
+ this.execSimpleQuery("SELECT * FROM pg_stat_activity LIMIT 1", function () {
52
+ return cb === null || cb === void 0 ? void 0 : cb();
53
+ });
54
+ return this;
55
+ };
56
+ exports.Driver.prototype.on = function (ev, cb) {
57
+ if (ev == "error") {
58
+ this.db.eventor.on("error", cb);
59
+ this.db.eventor.on("unhandledError", cb);
60
+ }
61
+ return this;
62
+ };
63
+ exports.Driver.prototype.connect = function (cb) {
64
+ const syncResponse = Utilities.catchBlocking(() => {
65
+ return this.db.connect();
66
+ });
67
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
68
+ return syncResponse.result;
69
+ };
70
+ exports.Driver.prototype.reconnect = function (cb) {
71
+ const connOpts = this.config.href || this.config;
72
+ this.db = new postgres_1.default(connOpts);
73
+ const syncResponse = Utilities.catchBlocking(() => {
74
+ return this.connect();
75
+ });
76
+ if (typeof cb === 'function')
77
+ cb(null, syncResponse.result);
78
+ return syncResponse.result;
79
+ };
80
+ exports.Driver.prototype.close = function (cb) {
81
+ const errResults = Utilities.catchBlocking(() => this.db.close());
82
+ Utilities.takeAwayResult(errResults, { no_throw: !!cb, callback: cb });
83
+ return errResults.result;
84
+ };
85
+ exports.Driver.prototype.getQuery = function () {
86
+ return this.query;
87
+ };
88
+ exports.Driver.prototype.execSimpleQuery = function (query, cb) {
89
+ if (this.opts.debug) {
90
+ require("../../Debug").sql('postgresql', query);
91
+ }
92
+ return this.db.query(query, cb);
93
+ };
94
+ exports.Driver.prototype.find = function (selectFields, table, conditions, opts) {
95
+ const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
96
+ const ctx = {
97
+ table,
98
+ fromTuple: from_tuple,
99
+ selectFields,
100
+ selectVirtualFields: opts.selectVirtualFields || []
101
+ };
102
+ let q = this.query.select();
103
+ q = typeof (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect) === 'function' ? (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect.call(this, ctx, q)) || q : Utilities.DEFAULT_GENERATE_SQL_QUERY_SELECT.call(this, ctx, q);
104
+ if (opts.offset) {
105
+ q.offset(opts.offset);
106
+ }
107
+ if (typeof opts.limit == "number") {
108
+ q.limit(opts.limit);
109
+ }
110
+ if (opts.order) {
111
+ for (let i = 0; i < opts.order.length; i++) {
112
+ q.order(opts.order[i][0], opts.order[i][1]);
113
+ }
114
+ }
115
+ utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions, opts.topConditions]);
116
+ utils.buildOrderToQuery.apply(this, [q, opts.order]);
117
+ utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
118
+ utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
119
+ return this.execSimpleQuery(q.build());
120
+ };
121
+ exports.Driver.prototype.count = function (table, conditions, opts, cb) {
122
+ const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
123
+ const q = this.query.select().from(from_tuple).count(null, 'c');
124
+ utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions]);
125
+ utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
126
+ utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
127
+ return this.execSimpleQuery(q.build(), cb);
128
+ };
129
+ exports.Driver.prototype.insert = function (table, data, keyProperties, cb) {
130
+ const q = this.query.insert().into(table).set(data).build();
131
+ const syncResponse = Utilities.catchBlocking(() => {
132
+ const results = this.execSimpleQuery(q + " RETURNING *");
133
+ const ids = {};
134
+ if (keyProperties) {
135
+ for (let i = 0; i < keyProperties.length; i++) {
136
+ const prop = keyProperties[i];
137
+ // Zero is a valid value for an ID column
138
+ ids[prop.name] = results[0][prop.mapsTo] !== undefined ? results[0][prop.mapsTo] : null;
139
+ }
140
+ }
141
+ return ids;
142
+ });
143
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
144
+ return syncResponse.result;
145
+ };
146
+ exports.Driver.prototype.update = function (table, changes, conditions, cb) {
147
+ const q = this.query.update().into(table).set(changes).where(conditions).build();
148
+ return this.execSimpleQuery(q, cb);
149
+ };
150
+ exports.Driver.prototype.remove = function (table, conditions, cb) {
151
+ const q = this.query.remove().from(table).where(conditions).build();
152
+ return this.execSimpleQuery(q, cb);
153
+ };
154
+ exports.Driver.prototype.clear = function (table, cb) {
155
+ const q = "TRUNCATE TABLE " + this.query.escapeId(table);
156
+ return this.execSimpleQuery(q, cb);
157
+ };
158
+ exports.Driver.prototype.valueToProperty = function (value, property) {
159
+ let customType, v;
160
+ switch (property.type) {
161
+ case "object":
162
+ if (typeof value == "object" && !Buffer.isBuffer(value)) {
163
+ break;
164
+ }
165
+ try {
166
+ value = JSON.parse(value);
167
+ }
168
+ catch (e) {
169
+ value = null;
170
+ }
171
+ break;
172
+ case "binary": {
173
+ // compat with standard behavior of `JSON.stringify`/`JSON.parse`
174
+ if (value && value.type === 'Buffer' && Array.isArray(value.data)) {
175
+ value = Buffer.from(value.data);
176
+ }
177
+ break;
178
+ }
179
+ case "point":
180
+ if (typeof value == "string") {
181
+ const m = value.match(/\((\-?[\d\.]+)[\s,]+(\-?[\d\.]+)\)/);
182
+ if (m) {
183
+ value = { x: parseFloat(m[1]), y: parseFloat(m[2]) };
184
+ }
185
+ }
186
+ break;
187
+ case "date":
188
+ if ((util.isNumber(value) || util.isString(value)))
189
+ value = new Date(value);
190
+ if (util.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
191
+ const tz = convertTimezone(this.config.timezone);
192
+ // shift local to UTC
193
+ value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000));
194
+ if (tz !== false) {
195
+ // shift UTC to timezone
196
+ value.setTime(value.getTime() - (tz * 60000));
197
+ }
198
+ }
199
+ break;
200
+ case "number":
201
+ if (typeof value == 'string') {
202
+ switch (value.trim()) {
203
+ case 'Infinity':
204
+ case '-Infinity':
205
+ case 'NaN':
206
+ value = Number(value);
207
+ break;
208
+ default:
209
+ v = parseFloat(value);
210
+ if (Number.isFinite(v)) {
211
+ value = v;
212
+ }
213
+ }
214
+ }
215
+ break;
216
+ case "integer":
217
+ if (typeof value == 'string') {
218
+ v = parseInt(value);
219
+ if (Number.isFinite(v)) {
220
+ value = v;
221
+ }
222
+ }
223
+ break;
224
+ default:
225
+ customType = this.customTypes[property.type];
226
+ if (customType && 'valueToProperty' in customType) {
227
+ value = customType.valueToProperty(value, property);
228
+ }
229
+ }
230
+ return value;
231
+ };
232
+ exports.Driver.prototype.propertyToValue = function (value, property) {
233
+ let customType;
234
+ switch (property.type) {
235
+ case "object":
236
+ if (value !== null) {
237
+ if (!Buffer.isBuffer(value))
238
+ value = JSON.stringify(value).replace(/\\/g, '\\\\');
239
+ }
240
+ break;
241
+ case "date":
242
+ if ((util.isNumber(value) || util.isString(value)))
243
+ value = new Date(value);
244
+ if (util.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
245
+ var tz = convertTimezone(this.config.timezone);
246
+ // shift local to UTC
247
+ value.setTime(value.getTime() + (value.getTimezoneOffset() * 60000));
248
+ if (tz !== false) {
249
+ // shift UTC to timezone
250
+ value.setTime(value.getTime() + (tz * 60000));
251
+ }
252
+ }
253
+ break;
254
+ case "point":
255
+ return function () { return 'POINT(' + value.x + ', ' + value.y + ')'; };
256
+ default:
257
+ customType = this.customTypes[property.type];
258
+ if (customType && 'propertyToValue' in customType) {
259
+ value = customType.propertyToValue(value);
260
+ }
261
+ }
262
+ return value;
263
+ };
264
+ Object.defineProperty(exports.Driver.prototype, "isSql", {
265
+ value: true
266
+ });
267
+ function convertTimezone(tz) {
268
+ if (tz == "Z") {
269
+ return 0;
270
+ }
271
+ var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
272
+ if (m) {
273
+ return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
274
+ }
275
+ return false;
276
+ }
@@ -0,0 +1,2 @@
1
+ import type { FxOrmDMLDriver } from '../../Typo/DMLDriver';
2
+ export declare const Driver: FxOrmDMLDriver.DMLDriverConstructor_SQLite;