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,260 @@
1
+ "use strict";
2
+ // Transforms:
3
+ // "name LIKE ? AND age > ?", ["John", 23]
4
+ // into:
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ChainBuilderBase = exports.DialectTypes = void 0;
7
+ exports.escapeQuery = escapeQuery;
8
+ exports.dateToString = dateToString;
9
+ exports.zeroPad = zeroPad;
10
+ exports.get_table_alias = get_table_alias;
11
+ exports.parseTableInputStr = parseTableInputStr;
12
+ exports.pickAliasFromFromDescriptor = pickAliasFromFromDescriptor;
13
+ exports.pickColumnAsFromSelectFieldsDescriptor = pickColumnAsFromSelectFieldsDescriptor;
14
+ exports.autoIncreatementTableIndex = autoIncreatementTableIndex;
15
+ exports.defaultTableAliasNameRule = defaultTableAliasNameRule;
16
+ exports.ucfirst = ucfirst;
17
+ exports.ensureNumber = ensureNumber;
18
+ exports.bufferToString = bufferToString;
19
+ exports.escapeValForKnex = escapeValForKnex;
20
+ exports.cutOffOrderDirectionFromColumnFirstStr = cutOffOrderDirectionFromColumnFirstStr;
21
+ exports.maybeKnexSubQuery = maybeKnexSubQuery;
22
+ exports.maybeKnexRaw = maybeKnexRaw;
23
+ exports.maybeKnexRawOrQueryBuilder = maybeKnexRawOrQueryBuilder;
24
+ exports.isWrapperdSubQuerySelect = isWrapperdSubQuerySelect;
25
+ exports.convertSqlFromTable = convertSqlFromTable;
26
+ // "name LIKE 'John' AND age > 23"
27
+ function escapeQuery(Dialect, query, args) {
28
+ let pos = 0;
29
+ return query.replace(/\?{1,2}/g, function (match) {
30
+ if (match == '?') {
31
+ return Dialect.escapeVal(args[pos++]);
32
+ }
33
+ else if (match == '??') {
34
+ return Dialect.escapeId(args[pos++]);
35
+ }
36
+ });
37
+ }
38
+ function dateToString(date, timeZone, opts) {
39
+ const dt = new Date(date);
40
+ if (timeZone != 'local') {
41
+ const tz = convertTimezone(timeZone);
42
+ dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
43
+ if (tz !== false) {
44
+ dt.setTime(dt.getTime() + (tz * 60000));
45
+ }
46
+ }
47
+ const year = dt.getFullYear();
48
+ const month = zeroPad(dt.getMonth() + 1);
49
+ const day = zeroPad(dt.getDate());
50
+ const hour = zeroPad(dt.getHours());
51
+ const minute = zeroPad(dt.getMinutes());
52
+ const second = zeroPad(dt.getSeconds());
53
+ const milli = zeroPad(dt.getMilliseconds(), 3);
54
+ if (opts.dialect == 'mysql' || opts.dialect == 'dm' || timeZone == 'local') {
55
+ return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milli;
56
+ }
57
+ else {
58
+ return year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + '.' + milli + 'Z';
59
+ }
60
+ }
61
+ function zeroPad(number, n = 2) {
62
+ number = "" + number;
63
+ while (number.length < n) {
64
+ number = "0" + number;
65
+ }
66
+ return number;
67
+ }
68
+ function convertTimezone(tz) {
69
+ if (tz == "Z")
70
+ return 0;
71
+ const m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
72
+ if (m) {
73
+ return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
74
+ }
75
+ return false;
76
+ }
77
+ function get_table_alias(sql, table) {
78
+ for (let i = 0; i < sql.from.length; i++) {
79
+ if (sql.from[i].table == table) {
80
+ return pickAliasFromFromDescriptor(sql.from[i]);
81
+ }
82
+ }
83
+ return typeof table === 'string' ? table : '';
84
+ }
85
+ ;
86
+ // TODO: add test about it
87
+ function unwrapIdentifier(identifier = '', dialect) {
88
+ switch (dialect) {
89
+ case 'postgresql':
90
+ return identifier.replace(/^"|"$/g, '');
91
+ case 'dm':
92
+ return identifier.replace(/^"|"$/g, '');
93
+ case 'sqlite':
94
+ case 'mysql':
95
+ return identifier.replace(/^`|`$/g, '');
96
+ }
97
+ return identifier;
98
+ }
99
+ function parseTableInputStr(table_name, dialect) {
100
+ if (!table_name)
101
+ throw new Error(`[parseTableInputStr] invalid input table_name!`);
102
+ let ta_tuple = ['', ''];
103
+ if (typeof table_name === 'string') {
104
+ table_name = table_name.trim();
105
+ let idx = table_name.lastIndexOf(' as ');
106
+ if (idx > 0) {
107
+ ta_tuple = [
108
+ table_name.slice(0, idx).trim(),
109
+ table_name.slice(idx + 4).trim()
110
+ ];
111
+ }
112
+ else {
113
+ ta_tuple = table_name.split(' ').slice(0, 2);
114
+ }
115
+ }
116
+ else if (maybeKnexRawOrQueryBuilder(table_name)) {
117
+ ta_tuple = [table_name, ''];
118
+ }
119
+ else {
120
+ ta_tuple = table_name.slice(0, 2);
121
+ }
122
+ if (dialect) {
123
+ if (typeof ta_tuple[0] === 'string')
124
+ ta_tuple[0] = unwrapIdentifier(ta_tuple[0], dialect);
125
+ ta_tuple[1] = unwrapIdentifier(ta_tuple[1], dialect);
126
+ }
127
+ return ta_tuple;
128
+ }
129
+ function pickAliasFromFromDescriptor(fd) {
130
+ return fd.alias || fd.a;
131
+ }
132
+ function pickColumnAsFromSelectFieldsDescriptor(sitem) {
133
+ return sitem.as || sitem.a;
134
+ }
135
+ function autoIncreatementTableIndex(from) {
136
+ return from.length + 1;
137
+ }
138
+ function defaultTableAliasNameRule(idx) {
139
+ return `t${idx}`;
140
+ }
141
+ function ucfirst(str = '') {
142
+ if (str.length <= 1)
143
+ return str.toUpperCase();
144
+ return str[0].toUpperCase() + str.slice(1);
145
+ }
146
+ function ensureNumber(num) {
147
+ if (typeof num !== 'number') {
148
+ num = parseInt(num);
149
+ }
150
+ if (isNaN(num))
151
+ return 0;
152
+ return num;
153
+ }
154
+ function bufferToString(buffer, dialect) {
155
+ switch (dialect) {
156
+ case 'dm':
157
+ return "HEXTORAW('" + buffer.toString('hex') + "')";
158
+ case 'mssql':
159
+ return "X'" + buffer.toString('hex') + "'";
160
+ case 'mysql':
161
+ return "X'" + buffer.toString('hex') + "'";
162
+ case 'sqlite':
163
+ return "X'" + buffer.toString('hex') + "'";
164
+ case 'postgresql':
165
+ return "'\\x" + buffer.toString('hex') + "'";
166
+ }
167
+ }
168
+ exports.DialectTypes = ['mysql', 'sqlite', 'mssql', 'postgresql', 'dm'];
169
+ function escapeValForKnex(val, Dialect, opts) {
170
+ // no escape for `knex.raw(...)`, `knex.ref(...)` and subquery
171
+ if (maybeKnexRawOrQueryBuilder(val))
172
+ return val;
173
+ // never escapeVal those types with `Dialect.escapeVal`, knex would escape them automatically
174
+ const _type = typeof val;
175
+ if (_type === 'string')
176
+ return val;
177
+ else if (_type === 'number')
178
+ return val;
179
+ else if (_type === 'symbol')
180
+ return val = null;
181
+ else if (_type === 'boolean') {
182
+ if (Dialect.DataTypes.isSQLITE || Dialect.type === 'dm')
183
+ return val ? 1 : 0;
184
+ return val;
185
+ }
186
+ else if (_type === 'function')
187
+ return Dialect.knex.raw(val(Dialect));
188
+ else if (val instanceof Date)
189
+ // TODO: how to suppor timezone?
190
+ return val;
191
+ else if (Buffer.isBuffer(val)) {
192
+ return (Dialect.type === 'postgresql' || Dialect.type === 'dm')
193
+ ? Dialect.knex.raw(bufferToString(val, Dialect.type))
194
+ : val;
195
+ }
196
+ else if (val instanceof Array)
197
+ return val;
198
+ else if (val === null)
199
+ return val;
200
+ else if (val === undefined)
201
+ return val = null;
202
+ return Dialect.escapeVal(val, opts.timezone);
203
+ }
204
+ function cutOffOrderDirectionFromColumnFirstStr(col_name) {
205
+ const result = {
206
+ col_name,
207
+ direction: 'ASC'
208
+ };
209
+ if (typeof col_name !== 'string')
210
+ return result;
211
+ if (col_name[0] === '-') {
212
+ result.col_name = col_name.substr(1);
213
+ result.direction = 'DESC';
214
+ }
215
+ return result;
216
+ }
217
+ function maybeKnexSubQuery(input) {
218
+ return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function";
219
+ }
220
+ function maybeKnexRaw(input) {
221
+ return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function";
222
+ }
223
+ function maybeKnexRawOrQueryBuilder(input) {
224
+ return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function";
225
+ }
226
+ function isWrapperdSubQuerySelect(sql) {
227
+ return typeof sql === 'string' && /^\(\s*SELECT\s.*\)$/i.test(sql);
228
+ }
229
+ /** @internal */
230
+ function convertSqlFromTable(sql_from_item, knex, isSubQuery) {
231
+ var _a, _b;
232
+ if (isSubQuery === void 0) { isSubQuery = isWrapperdSubQuerySelect((_b = (_a = sql_from_item.table) === null || _a === void 0 ? void 0 : _a.trim) === null || _b === void 0 ? void 0 : _b.call(_a)); }
233
+ // from: select subquery as table
234
+ return maybeKnexRawOrQueryBuilder(sql_from_item.table) ? knex.raw(sql_from_item.table).wrap('(', ')')
235
+ : isSubQuery ? knex.raw(sql_from_item.table.trim()).wrap('', '')
236
+ : sql_from_item.table;
237
+ }
238
+ class ChainBuilderBase {
239
+ constructor(Dialect) {
240
+ this.Dialect = Dialect;
241
+ Object.defineProperty(this, 'Dialect', {
242
+ get() {
243
+ return Dialect;
244
+ },
245
+ configurable: false
246
+ });
247
+ Object.defineProperty(this, 'knex', {
248
+ get() {
249
+ return Dialect.knex;
250
+ },
251
+ configurable: false
252
+ });
253
+ }
254
+ /**
255
+ * @override
256
+ */
257
+ build() { return ''; }
258
+ ;
259
+ }
260
+ exports.ChainBuilderBase = ChainBuilderBase;
@@ -0,0 +1,12 @@
1
+ import { ChainBuilderBase } from "./Helpers";
2
+ import { FxSqlQueryDialect } from "./Typo/Dialect";
3
+ import { FxSqlQuerySql } from "./Typo/Sql";
4
+ import { FxSqlQueryChainBuilder } from "./Typo/Query-ChainBuilder";
5
+ export declare class InsertQuery extends ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Insert {
6
+ private opts;
7
+ private sql;
8
+ constructor(Dialect: FxSqlQueryDialect.Dialect, opts: FxSqlQueryChainBuilder.ChainBuilderOptions);
9
+ into(table: string): this;
10
+ set(values: FxSqlQuerySql.DataToSet): this;
11
+ build(): string;
12
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InsertQuery = void 0;
4
+ const Helpers_1 = require("./Helpers");
5
+ class InsertQuery extends Helpers_1.ChainBuilderBase {
6
+ constructor(Dialect, opts) {
7
+ super(Dialect);
8
+ this.opts = opts;
9
+ this.sql = {};
10
+ }
11
+ into(table) {
12
+ this.sql.table = table;
13
+ return this;
14
+ }
15
+ set(values) {
16
+ this.sql.set = values;
17
+ return this;
18
+ }
19
+ build() {
20
+ const sqlBuilder = this.Dialect.knex(this.sql.table);
21
+ const col_vals = {};
22
+ if (this.sql.hasOwnProperty("set")) {
23
+ let val = null;
24
+ for (let k in this.sql.set) {
25
+ val = (0, Helpers_1.escapeValForKnex)(this.sql.set[k], this.Dialect, this.opts);
26
+ ;
27
+ col_vals[k] = val;
28
+ }
29
+ sqlBuilder.insert(col_vals);
30
+ }
31
+ return sqlBuilder.toQuery();
32
+ }
33
+ }
34
+ exports.InsertQuery = InsertQuery;
@@ -0,0 +1,28 @@
1
+ import { CreateQuery } from "./Create";
2
+ import { SelectQuery } from "./Select";
3
+ import { InsertQuery } from "./Insert";
4
+ import { UpdateQuery } from "./Update";
5
+ import { RemoveQuery } from "./Remove";
6
+ export import Helpers = require('./Helpers');
7
+ export import Dialects = require('./Dialects');
8
+ import { FxSqlQuery } from './Typo/Query';
9
+ import { FxSqlQueryDialect } from './Typo/Dialect';
10
+ export declare const comparators: import("./Typo/Comparators").FxSqlQueryComparator.ComparatorHash;
11
+ export declare const Text: FxSqlQuery.TypedQueryObjectWrapper<"text">;
12
+ export declare class Query implements FxSqlQuery.Class_Query {
13
+ readonly Dialect: FxSqlQueryDialect.Dialect;
14
+ readonly knex: FxSqlQuery.Class_Query['knex'];
15
+ private opts;
16
+ private _fns;
17
+ private _proxyFn;
18
+ constructor(_opts?: string | FxSqlQuery.QueryOptions);
19
+ escape: FxSqlQueryDialect.Dialect['escape'];
20
+ escapeId: FxSqlQueryDialect.Dialect['escapeId'];
21
+ escapeVal: FxSqlQueryDialect.Dialect['escapeVal'];
22
+ create(): CreateQuery;
23
+ select(): SelectQuery;
24
+ insert(): InsertQuery;
25
+ update(): UpdateQuery;
26
+ remove(): RemoveQuery;
27
+ }
28
+ export * from './Typo/index';
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ /// <reference lib="es2017" />
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.Query = exports.Text = exports.comparators = void 0;
19
+ const util = require("util");
20
+ const Helpers_1 = require("./Helpers");
21
+ const Create_1 = require("./Create");
22
+ const Select_1 = require("./Select");
23
+ const Insert_1 = require("./Insert");
24
+ const Update_1 = require("./Update");
25
+ const Remove_1 = require("./Remove");
26
+ const ComparatorsHash = require("./Comparators");
27
+ exports.Helpers = require("./Helpers");
28
+ exports.Dialects = require("./Dialects");
29
+ exports.comparators = ComparatorsHash;
30
+ exports.Text = buildQueryType("text");
31
+ function mountDialect(Dialect) {
32
+ const FKnex = require('../knex/knex');
33
+ const knexClientMap = {
34
+ dm: 'pg'
35
+ };
36
+ Object.defineProperty(this, 'Dialect', {
37
+ value: util.extend({}, Dialect),
38
+ writable: false,
39
+ configurable: false
40
+ });
41
+ const knexClient = knexClientMap[this.Dialect.type] || this.Dialect.type;
42
+ const knexInst = FKnex({ client: knexClient, useNullAsDefault: true });
43
+ Object.defineProperty(this.Dialect, 'knex', {
44
+ value: knexInst,
45
+ writable: false,
46
+ configurable: false
47
+ });
48
+ Object.defineProperty(this, 'knex', {
49
+ value: knexInst,
50
+ writable: false,
51
+ configurable: false
52
+ });
53
+ }
54
+ class Query {
55
+ _proxyFn(fn_name) {
56
+ if (!this._fns[fn_name]) {
57
+ switch (fn_name) {
58
+ case 'escape':
59
+ this._fns[fn_name] = exports.Helpers.escapeQuery.bind(exports.Helpers, this.Dialect);
60
+ break;
61
+ case 'escapeId':
62
+ this._fns[fn_name] = this.Dialect.escapeId.bind(this.Dialect);
63
+ break;
64
+ case 'escapeVal':
65
+ this._fns[fn_name] = this.Dialect.escapeVal.bind(this.Dialect);
66
+ break;
67
+ }
68
+ }
69
+ return this._fns[fn_name];
70
+ }
71
+ constructor(_opts) {
72
+ this._fns = {};
73
+ let opts = null;
74
+ if (typeof _opts === "string") {
75
+ if (!Helpers_1.DialectTypes.includes(_opts))
76
+ throw `invalid dialect type ${_opts}`;
77
+ opts = { dialect: _opts };
78
+ }
79
+ else {
80
+ opts = Object.assign({}, _opts);
81
+ }
82
+ this.opts = opts;
83
+ opts.dialect = opts.dialect || 'mysql';
84
+ mountDialect.call(this, exports.Dialects[opts.dialect]);
85
+ this.escape = this._proxyFn('escape');
86
+ this.escapeId = this._proxyFn('escapeId');
87
+ this.escapeVal = this._proxyFn('escapeVal');
88
+ }
89
+ create() {
90
+ return new Create_1.CreateQuery(this.Dialect);
91
+ }
92
+ select() {
93
+ return new Select_1.SelectQuery(this.Dialect, this.opts);
94
+ }
95
+ insert() {
96
+ return new Insert_1.InsertQuery(this.Dialect, this.opts);
97
+ }
98
+ update() {
99
+ return new Update_1.UpdateQuery(this.Dialect, this.opts);
100
+ }
101
+ remove() {
102
+ return new Remove_1.RemoveQuery(this.Dialect, this.opts);
103
+ }
104
+ }
105
+ exports.Query = Query;
106
+ function buildQueryType(type) {
107
+ return function (data) {
108
+ var o = {
109
+ data: data
110
+ };
111
+ Object.defineProperty(o, "type", {
112
+ value: function () {
113
+ return type;
114
+ },
115
+ enumerable: false
116
+ });
117
+ return o;
118
+ };
119
+ }
120
+ __exportStar(require("./Typo/index"), exports);
@@ -0,0 +1,16 @@
1
+ import { ChainBuilderBase } from './Helpers';
2
+ import { FxSqlQuery } from './Typo/Query';
3
+ import { FxSqlQueryChainBuilder } from './Typo/Query-ChainBuilder';
4
+ import { FxSqlQueryDialect } from './Typo/Dialect';
5
+ import { FxSqlQuerySubQuery } from './Typo/SubQuery';
6
+ export declare class RemoveQuery extends ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Remove {
7
+ private opts;
8
+ private sql;
9
+ constructor(Dialect: FxSqlQueryDialect.Dialect, opts: FxSqlQueryChainBuilder.ChainBuilderOptions);
10
+ from(table: string): this;
11
+ where(...whereConditions: FxSqlQuerySubQuery.SubQueryBuildDescriptor['wheres'][]): this;
12
+ build(): string;
13
+ offset(offset: number): this;
14
+ limit(limit: number): this;
15
+ order(column: string | string[], dir: FxSqlQuery.QueryOrderDirection): this;
16
+ }
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RemoveQuery = void 0;
4
+ const util = require("util");
5
+ const Where = require("./Where");
6
+ const Helpers_1 = require("./Helpers");
7
+ class RemoveQuery extends Helpers_1.ChainBuilderBase {
8
+ constructor(Dialect, opts) {
9
+ super(Dialect);
10
+ this.opts = opts;
11
+ this.sql = {
12
+ where: [],
13
+ order: []
14
+ };
15
+ }
16
+ from(table) {
17
+ this.sql.table = table;
18
+ return this;
19
+ }
20
+ where(...whereConditions) {
21
+ for (let i = 0; i < whereConditions.length; i++) {
22
+ this.sql.where.push({
23
+ table: null,
24
+ wheres: whereConditions[i]
25
+ });
26
+ }
27
+ return this;
28
+ }
29
+ build() {
30
+ var extraQuery = [];
31
+ const sqlBuilder = this.Dialect.knex(this.sql.table);
32
+ Where.build(sqlBuilder, this.Dialect, this.sql.where, this.opts);
33
+ sqlBuilder.del();
34
+ const sqlList = [sqlBuilder.toQuery()];
35
+ // order
36
+ if (this.sql.order.length > 0) {
37
+ // this.sql.order has been normalized
38
+ const order = this.sql.order;
39
+ const tmp = [];
40
+ for (let i = 0; i < order.length; i++) {
41
+ const col_desc = order[i].c;
42
+ const zdir = order[i].d;
43
+ if (Array.isArray(col_desc)) {
44
+ tmp.push(this.Dialect.escapeId.apply(this.Dialect, col_desc) + " " + zdir);
45
+ }
46
+ else {
47
+ tmp.push(this.Dialect.escapeId(col_desc) + " " + zdir);
48
+ }
49
+ }
50
+ if (tmp.length > 0) {
51
+ extraQuery.push("ORDER BY " + tmp.join(", "));
52
+ sqlList.push(util.last(extraQuery));
53
+ }
54
+ }
55
+ // limit for all Dialects but MSSQL
56
+ if (!this.Dialect.limitAsTop) {
57
+ if (this.sql.hasOwnProperty("limit")) {
58
+ if (this.sql.hasOwnProperty("offset")) {
59
+ extraQuery.push("LIMIT " + this.sql.limit + " OFFSET " + this.sql.offset);
60
+ }
61
+ else {
62
+ extraQuery.push("LIMIT " + this.sql.limit);
63
+ }
64
+ sqlList.push(util.last(extraQuery));
65
+ }
66
+ else if (this.sql.hasOwnProperty("offset")) {
67
+ extraQuery.push("OFFSET " + this.sql.offset);
68
+ sqlBuilder.offset(this.sql.offset);
69
+ sqlList.push(util.last(extraQuery));
70
+ }
71
+ // limit as: SELECT TOP n (MSSQL only)
72
+ }
73
+ else if (this.sql.hasOwnProperty("limit")) {
74
+ sqlList[0].replace('delete from', `delete top ${this.sql.limit}`);
75
+ }
76
+ return sqlList.join(' ');
77
+ }
78
+ offset(offset) {
79
+ this.sql.offset = offset;
80
+ return this;
81
+ }
82
+ limit(limit) {
83
+ this.sql.limit = limit;
84
+ return this;
85
+ }
86
+ order(column, dir) {
87
+ this.sql.order.push({
88
+ c: Array.isArray(column) ? [(0, Helpers_1.get_table_alias)(this.sql, column[0]), column[1]] : column,
89
+ d: (dir == "Z" ? "DESC" : "ASC")
90
+ });
91
+ return this;
92
+ }
93
+ }
94
+ exports.RemoveQuery = RemoveQuery;
@@ -0,0 +1,62 @@
1
+ import Helpers = require('./Helpers');
2
+ import { FxSqlQueryDialect } from './Typo/Dialect';
3
+ import { FxSqlQueryChainBuilder } from './Typo/Query-ChainBuilder';
4
+ import { FxSqlQuerySql } from './Typo/Sql';
5
+ import { FxSqlQuery } from "./Typo/Query";
6
+ import { FxSqlQueryHelpler } from './Typo/Helper';
7
+ import { FxSqlQuerySubQuery } from "./Typo/SubQuery";
8
+ export declare class SelectQuery extends Helpers.ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Select {
9
+ private opts;
10
+ Dialect: FxSqlQueryDialect.Dialect;
11
+ private sql;
12
+ private _aggregation_functions;
13
+ private fun_stack;
14
+ private get_aggregate_fun;
15
+ constructor(Dialect: FxSqlQueryDialect.Dialect, opts: FxSqlQuery.QueryOptions);
16
+ select(fields?: any): this;
17
+ calculateFoundRows(): this;
18
+ as(_as: string): this;
19
+ fun(fun: string, column?: FxSqlQuerySql.SqlColumnType, _as?: string): this;
20
+ /**
21
+ *
22
+ * @param table from-table
23
+ * @param from_id from-table id(s), align with to_id
24
+ * @param to_table to table
25
+ * @param to_id to-table id(s), align with from_id
26
+ * @param from_opts join descriptor
27
+ */
28
+ from(table: FxSqlQuerySql.SqlTableInputType, from_id?: FxSqlQueryHelpler.Arraiable<string>, to_table?: string, to_id?: FxSqlQueryHelpler.Arraiable<string>, from_opts?: FxSqlQuerySql.QueryFromDescriptorOpts): this;
29
+ where(...whereConditions: (FxSqlQuerySubQuery.SubQueryBuildDescriptor['wheres'] | FxSqlQuerySubQuery.WhereExistsTuple_Flatten[0])[]): this;
30
+ whereExists(table: string, table_link: string, link: FxSqlQuerySql.WhereExistsLinkTuple, cond: FxSqlQuerySubQuery.SubQueryBuildDescriptor['wheres']): this;
31
+ groupBy(...args: FxSqlQuerySql.SqlGroupByType[]): this;
32
+ offset(offset: number): this;
33
+ limit(limit: number): this;
34
+ order(column: FxSqlQuery.OrderNormalizedResult[0], dir?: FxSqlQuery.OrderNormalizedResult[1]): this;
35
+ build(): string;
36
+ abs(...args: any[]): any;
37
+ ceil(...args: any[]): any;
38
+ floor(...args: any[]): any;
39
+ round(...args: any[]): any;
40
+ avg(...args: any[]): any;
41
+ min(...args: any[]): any;
42
+ max(...args: any[]): any;
43
+ log(...args: any[]): any;
44
+ log2(...args: any[]): any;
45
+ log10(...args: any[]): any;
46
+ exp(...args: any[]): any;
47
+ power(...args: any[]): any;
48
+ acos(...args: any[]): any;
49
+ asin(...args: any[]): any;
50
+ atan(...args: any[]): any;
51
+ cos(...args: any[]): any;
52
+ sin(...args: any[]): any;
53
+ tan(...args: any[]): any;
54
+ conv(...args: any[]): any;
55
+ random(...args: any[]): any;
56
+ rand(...args: any[]): any;
57
+ radians(...args: any[]): any;
58
+ degrees(...args: any[]): any;
59
+ sum(...args: any[]): any;
60
+ count(...args: any[]): any;
61
+ distinct(...args: any[]): any;
62
+ }