fib-orm 1.4.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/License +1 -1
  2. package/README.md +53 -0
  3. package/lib/cli/commands/ddlFrom.d.ts +2 -0
  4. package/lib/cli/commands/ddlFrom.js +74 -0
  5. package/lib/cli/commands/dumpModel.d.ts +2 -0
  6. package/lib/cli/commands/dumpModel.js +106 -0
  7. package/lib/cli/commands/escape.d.ts +2 -0
  8. package/lib/cli/commands/escape.js +68 -0
  9. package/lib/cli/commands/spinner.d.ts +2 -0
  10. package/lib/cli/commands/spinner.js +36 -0
  11. package/lib/cli/commands/upgrade.d.ts +2 -0
  12. package/lib/cli/commands/upgrade.js +65 -0
  13. package/lib/cli/helpers/ddl.d.ts +17 -0
  14. package/lib/cli/helpers/ddl.js +55 -0
  15. package/lib/cli/helpers/package.d.ts +26 -0
  16. package/lib/cli/helpers/package.js +56 -0
  17. package/lib/cli/index.d.ts +2 -0
  18. package/lib/cli/index.js +18 -0
  19. package/lib/codepoints-map.json +762 -0
  20. package/lib/db-driver/Typo.d.ts +112 -0
  21. package/lib/db-driver/Typo.js +1 -0
  22. package/lib/db-driver/built-ins/base.class.d.ts +96 -0
  23. package/lib/db-driver/built-ins/base.class.js +186 -0
  24. package/lib/db-driver/built-ins/driver-dm.d.ts +18 -0
  25. package/lib/db-driver/built-ins/driver-dm.js +53 -0
  26. package/lib/db-driver/built-ins/driver-mongodb.d.ts +24 -0
  27. package/lib/db-driver/built-ins/driver-mongodb.js +46 -0
  28. package/lib/db-driver/built-ins/driver-mysql.d.ts +18 -0
  29. package/lib/db-driver/built-ins/driver-mysql.js +37 -0
  30. package/lib/db-driver/built-ins/driver-postgresql.d.ts +22 -0
  31. package/lib/db-driver/built-ins/driver-postgresql.js +76 -0
  32. package/lib/db-driver/built-ins/driver-redis.d.ts +12 -0
  33. package/lib/db-driver/built-ins/driver-redis.js +35 -0
  34. package/lib/db-driver/built-ins/driver-sqlite.d.ts +17 -0
  35. package/lib/db-driver/built-ins/driver-sqlite.js +34 -0
  36. package/lib/db-driver/index.d.ts +3 -0
  37. package/lib/db-driver/index.js +4 -0
  38. package/lib/db-driver/utils.d.ts +14 -0
  39. package/lib/db-driver/utils.js +187 -0
  40. package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +12 -0
  41. package/lib/knex/deps/tarn/dist/PendingOperation.js +48 -0
  42. package/lib/knex/deps/tarn/dist/Pool.d.ts +97 -0
  43. package/lib/knex/deps/tarn/dist/Pool.js +516 -0
  44. package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +16 -0
  45. package/lib/knex/deps/tarn/dist/PromiseInspection.js +21 -0
  46. package/lib/knex/deps/tarn/dist/Resource.d.ts +9 -0
  47. package/lib/knex/deps/tarn/dist/Resource.js +19 -0
  48. package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +2 -0
  49. package/lib/knex/deps/tarn/dist/TimeoutError.js +5 -0
  50. package/lib/knex/deps/tarn/dist/tarn.d.ts +3 -0
  51. package/lib/knex/deps/tarn/dist/tarn.js +10 -0
  52. package/lib/knex/deps/tarn/dist/utils.d.ts +14 -0
  53. package/lib/knex/deps/tarn/dist/utils.js +60 -0
  54. package/lib/knex/knex.d.ts +2 -0
  55. package/lib/knex/knex.js +3 -0
  56. package/lib/knex/sandbox.d.ts +4 -0
  57. package/lib/knex/sandbox.js +22 -0
  58. package/lib/orm/Adapters.d.ts +3 -0
  59. package/lib/orm/Adapters.js +44 -0
  60. package/lib/orm/AggregateFunctions.d.ts +3 -0
  61. package/lib/orm/AggregateFunctions.js +176 -0
  62. package/lib/orm/Associations/Extend.d.ts +22 -0
  63. package/lib/orm/Associations/Extend.js +274 -0
  64. package/lib/orm/Associations/Many.d.ts +17 -0
  65. package/lib/orm/Associations/Many.js +569 -0
  66. package/lib/orm/Associations/One.d.ts +16 -0
  67. package/lib/orm/Associations/One.js +310 -0
  68. package/lib/orm/Associations/_utils.d.ts +15 -0
  69. package/lib/orm/Associations/_utils.js +52 -0
  70. package/lib/orm/ChainFind.d.ts +3 -0
  71. package/lib/orm/ChainFind.js +327 -0
  72. package/lib/orm/ChainInstance.d.ts +6 -0
  73. package/lib/orm/ChainInstance.js +73 -0
  74. package/lib/orm/Debug.d.ts +2 -0
  75. package/lib/orm/Debug.js +17 -0
  76. package/lib/orm/Drivers/DB/base.d.ts +3 -0
  77. package/lib/orm/Drivers/DB/base.js +3 -0
  78. package/lib/orm/Drivers/DB/dm.d.ts +11 -0
  79. package/lib/orm/Drivers/DB/dm.js +23 -0
  80. package/lib/orm/Drivers/DB/mysql.d.ts +11 -0
  81. package/lib/orm/Drivers/DB/mysql.js +23 -0
  82. package/lib/orm/Drivers/DB/postgres.d.ts +11 -0
  83. package/lib/orm/Drivers/DB/postgres.js +22 -0
  84. package/lib/orm/Drivers/DB/sqlite3.d.ts +16 -0
  85. package/lib/orm/Drivers/DB/sqlite3.js +39 -0
  86. package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
  87. package/lib/orm/Drivers/DML/_ddl-sql.js +51 -0
  88. package/lib/orm/Drivers/DML/_dml-helpers.d.ts +19 -0
  89. package/lib/orm/Drivers/DML/_dml-helpers.js +51 -0
  90. package/lib/orm/Drivers/DML/_shared.d.ts +9 -0
  91. package/lib/orm/Drivers/DML/_shared.js +38 -0
  92. package/lib/orm/Drivers/DML/_utils.d.ts +8 -0
  93. package/lib/orm/Drivers/DML/_utils.js +67 -0
  94. package/lib/orm/Drivers/DML/constants.d.ts +3 -0
  95. package/lib/orm/Drivers/DML/constants.js +2 -0
  96. package/lib/orm/Drivers/DML/dm.d.ts +2 -0
  97. package/lib/orm/Drivers/DML/dm.js +293 -0
  98. package/lib/orm/Drivers/DML/index.d.ts +4 -0
  99. package/lib/orm/Drivers/DML/index.js +6 -0
  100. package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
  101. package/lib/orm/Drivers/DML/mysql.js +251 -0
  102. package/lib/orm/Drivers/DML/postgres.d.ts +3 -0
  103. package/lib/orm/Drivers/DML/postgres.js +276 -0
  104. package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
  105. package/lib/orm/Drivers/DML/sqlite.js +266 -0
  106. package/lib/orm/Error.d.ts +11 -0
  107. package/lib/orm/Error.js +40 -0
  108. package/lib/orm/Helpers.d.ts +35 -0
  109. package/lib/orm/Helpers.js +233 -0
  110. package/lib/orm/Hook.d.ts +13 -0
  111. package/lib/orm/Hook.js +99 -0
  112. package/lib/orm/Instance.d.ts +2 -0
  113. package/lib/orm/Instance.js +697 -0
  114. package/lib/orm/LazyLoad.d.ts +4 -0
  115. package/lib/orm/LazyLoad.js +76 -0
  116. package/lib/orm/Model.d.ts +7 -0
  117. package/lib/orm/Model.js +971 -0
  118. package/lib/orm/ORM.d.ts +37 -0
  119. package/lib/orm/ORM.js +241 -0
  120. package/lib/orm/Patch/fib-cache.d.ts +40 -0
  121. package/lib/orm/Patch/fib-cache.js +2 -0
  122. package/lib/orm/Patch/plugin.d.ts +4 -0
  123. package/lib/orm/Patch/plugin.js +13 -0
  124. package/lib/orm/Property.d.ts +9 -0
  125. package/lib/orm/Property.js +71 -0
  126. package/lib/orm/Settings.d.ts +42 -0
  127. package/lib/orm/Settings.js +119 -0
  128. package/lib/orm/Singleton.d.ts +5 -0
  129. package/lib/orm/Singleton.js +72 -0
  130. package/lib/orm/Typo/Adapter.d.ts +5 -0
  131. package/lib/orm/Typo/Adapter.js +1 -0
  132. package/lib/orm/Typo/DMLDriver.d.ts +210 -0
  133. package/lib/orm/Typo/DMLDriver.js +1 -0
  134. package/lib/orm/Typo/Db.d.ts +38 -0
  135. package/lib/orm/Typo/Db.js +1 -0
  136. package/lib/orm/Typo/Error.d.ts +31 -0
  137. package/lib/orm/Typo/Error.js +1 -0
  138. package/lib/orm/Typo/ORM.d.ts +233 -0
  139. package/lib/orm/Typo/ORM.js +3 -0
  140. package/lib/orm/Typo/Validators.d.ts +32 -0
  141. package/lib/orm/Typo/Validators.js +2 -0
  142. package/lib/orm/Typo/_common.d.ts +38 -0
  143. package/lib/orm/Typo/_common.js +1 -0
  144. package/lib/orm/Typo/assoc.d.ts +221 -0
  145. package/lib/orm/Typo/assoc.js +1 -0
  146. package/lib/orm/Typo/hook.d.ts +42 -0
  147. package/lib/orm/Typo/hook.js +1 -0
  148. package/lib/orm/Typo/instance.d.ts +157 -0
  149. package/lib/orm/Typo/instance.js +1 -0
  150. package/lib/orm/Typo/model.d.ts +313 -0
  151. package/lib/orm/Typo/model.js +6 -0
  152. package/lib/orm/Typo/patch.d.ts +31 -0
  153. package/lib/orm/Typo/patch.js +1 -0
  154. package/lib/orm/Typo/property.d.ts +57 -0
  155. package/lib/orm/Typo/property.js +1 -0
  156. package/lib/orm/Typo/query.d.ts +216 -0
  157. package/lib/orm/Typo/query.js +1 -0
  158. package/lib/orm/Typo/settings.d.ts +35 -0
  159. package/lib/orm/Typo/settings.js +5 -0
  160. package/lib/orm/Typo/synchronous.d.ts +47 -0
  161. package/lib/orm/Typo/synchronous.js +1 -0
  162. package/lib/orm/Utilities.d.ts +179 -0
  163. package/lib/orm/Utilities.js +975 -0
  164. package/lib/orm/Validators.d.ts +34 -0
  165. package/lib/orm/Validators.js +100 -0
  166. package/lib/orm/Where/common.d.ts +1 -0
  167. package/lib/orm/Where/common.js +7 -0
  168. package/lib/orm/Where/filterDate.d.ts +11 -0
  169. package/lib/orm/Where/filterDate.js +58 -0
  170. package/lib/orm/index.d.ts +68 -0
  171. package/lib/orm/index.js +157 -0
  172. package/lib/orm-core/callback.d.ts +12 -0
  173. package/lib/orm-core/callback.js +1 -0
  174. package/lib/orm-core/dialect.d.ts +80 -0
  175. package/lib/orm-core/dialect.js +1 -0
  176. package/lib/orm-core/error.d.ts +16 -0
  177. package/lib/orm-core/error.js +29 -0
  178. package/lib/orm-core/index.d.ts +6 -0
  179. package/lib/orm-core/index.js +6 -0
  180. package/lib/orm-core/sync.d.ts +12 -0
  181. package/lib/orm-core/sync.js +1 -0
  182. package/lib/orm-core/utils.d.ts +1 -0
  183. package/lib/orm-core/utils.js +5 -0
  184. package/lib/orm-property/Property.d.ts +78 -0
  185. package/lib/orm-property/Property.js +1 -0
  186. package/lib/orm-property/Utils.d.ts +9 -0
  187. package/lib/orm-property/Utils.js +13 -0
  188. package/lib/orm-property/customTypes.d.ts +2 -0
  189. package/lib/orm-property/customTypes.js +13 -0
  190. package/lib/orm-property/index.d.ts +10 -0
  191. package/lib/orm-property/index.js +22 -0
  192. package/lib/orm-property/transformer.d.ts +5 -0
  193. package/lib/orm-property/transformer.js +10 -0
  194. package/lib/orm-property/transformers/dm.d.ts +29 -0
  195. package/lib/orm-property/transformers/dm.js +237 -0
  196. package/lib/orm-property/transformers/index.d.ts +4 -0
  197. package/lib/orm-property/transformers/index.js +5 -0
  198. package/lib/orm-property/transformers/mysql.d.ts +102 -0
  199. package/lib/orm-property/transformers/mysql.js +261 -0
  200. package/lib/orm-property/transformers/postgresql.d.ts +54 -0
  201. package/lib/orm-property/transformers/postgresql.js +189 -0
  202. package/lib/orm-property/transformers/sqlite.d.ts +19 -0
  203. package/lib/orm-property/transformers/sqlite.js +171 -0
  204. package/lib/sql-ddl-sync/Dialects/dm.d.ts +40 -0
  205. package/lib/sql-ddl-sync/Dialects/dm.js +296 -0
  206. package/lib/sql-ddl-sync/Dialects/index.d.ts +7 -0
  207. package/lib/sql-ddl-sync/Dialects/index.js +7 -0
  208. package/lib/sql-ddl-sync/Dialects/mysql.d.ts +39 -0
  209. package/lib/sql-ddl-sync/Dialects/mysql.js +240 -0
  210. package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +40 -0
  211. package/lib/sql-ddl-sync/Dialects/postgresql.js +286 -0
  212. package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +44 -0
  213. package/lib/sql-ddl-sync/Dialects/sqlite.js +256 -0
  214. package/lib/sql-ddl-sync/SQL.d.ts +12 -0
  215. package/lib/sql-ddl-sync/SQL.js +108 -0
  216. package/lib/sql-ddl-sync/Typo/Collection.d.ts +13 -0
  217. package/lib/sql-ddl-sync/Typo/Collection.js +1 -0
  218. package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
  219. package/lib/sql-ddl-sync/Typo/DbIndex.js +1 -0
  220. package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
  221. package/lib/sql-ddl-sync/Typo/Dialect.js +1 -0
  222. package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
  223. package/lib/sql-ddl-sync/Typo/Driver.js +1 -0
  224. package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
  225. package/lib/sql-ddl-sync/Typo/Query.js +1 -0
  226. package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
  227. package/lib/sql-ddl-sync/Typo/SQL.js +1 -0
  228. package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
  229. package/lib/sql-ddl-sync/Typo/_common.js +1 -0
  230. package/lib/sql-ddl-sync/Utils.d.ts +40 -0
  231. package/lib/sql-ddl-sync/Utils.js +217 -0
  232. package/lib/sql-ddl-sync/index.d.ts +87 -0
  233. package/lib/sql-ddl-sync/index.js +371 -0
  234. package/lib/sql-query/Comparators.d.ts +3 -0
  235. package/lib/sql-query/Comparators.js +94 -0
  236. package/lib/sql-query/Create.d.ts +35 -0
  237. package/lib/sql-query/Create.js +77 -0
  238. package/lib/sql-query/Dialects/dm.d.ts +3 -0
  239. package/lib/sql-query/Dialects/dm.js +74 -0
  240. package/lib/sql-query/Dialects/index.d.ts +10 -0
  241. package/lib/sql-query/Dialects/index.js +12 -0
  242. package/lib/sql-query/Dialects/mssql.d.ts +3 -0
  243. package/lib/sql-query/Dialects/mssql.js +73 -0
  244. package/lib/sql-query/Dialects/mysql.d.ts +3 -0
  245. package/lib/sql-query/Dialects/mysql.js +100 -0
  246. package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
  247. package/lib/sql-query/Dialects/postgresql.js +76 -0
  248. package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
  249. package/lib/sql-query/Dialects/sqlite.js +61 -0
  250. package/lib/sql-query/Helpers.d.ts +38 -0
  251. package/lib/sql-query/Helpers.js +259 -0
  252. package/lib/sql-query/Insert.d.ts +12 -0
  253. package/lib/sql-query/Insert.js +33 -0
  254. package/lib/sql-query/Query.d.ts +29 -0
  255. package/lib/sql-query/Query.js +119 -0
  256. package/lib/sql-query/Remove.d.ts +16 -0
  257. package/lib/sql-query/Remove.js +93 -0
  258. package/lib/sql-query/Select.d.ts +62 -0
  259. package/lib/sql-query/Select.js +533 -0
  260. package/lib/sql-query/Set.d.ts +4 -0
  261. package/lib/sql-query/Set.js +16 -0
  262. package/lib/sql-query/Typo/Aggregation.d.ts +32 -0
  263. package/lib/sql-query/Typo/Aggregation.js +1 -0
  264. package/lib/sql-query/Typo/Comparators.d.ts +140 -0
  265. package/lib/sql-query/Typo/Comparators.js +1 -0
  266. package/lib/sql-query/Typo/Dialect.d.ts +39 -0
  267. package/lib/sql-query/Typo/Dialect.js +1 -0
  268. package/lib/sql-query/Typo/Field.d.ts +8 -0
  269. package/lib/sql-query/Typo/Field.js +1 -0
  270. package/lib/sql-query/Typo/Helper.d.ts +4 -0
  271. package/lib/sql-query/Typo/Helper.js +1 -0
  272. package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
  273. package/lib/sql-query/Typo/Query-ChainBuilder.js +1 -0
  274. package/lib/sql-query/Typo/Query.d.ts +34 -0
  275. package/lib/sql-query/Typo/Query.js +16 -0
  276. package/lib/sql-query/Typo/Sql.d.ts +143 -0
  277. package/lib/sql-query/Typo/Sql.js +1 -0
  278. package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
  279. package/lib/sql-query/Typo/SubQuery.js +1 -0
  280. package/lib/sql-query/Typo/index.d.ts +9 -0
  281. package/lib/sql-query/Typo/index.js +1 -0
  282. package/lib/sql-query/Update.d.ts +15 -0
  283. package/lib/sql-query/Update.js +38 -0
  284. package/lib/sql-query/Where.d.ts +4 -0
  285. package/lib/sql-query/Where.js +280 -0
  286. package/package.json +72 -36
  287. package/.travis.yml +0 -14
  288. package/@types/index.d.ts +0 -254
  289. package/@types/orm_mirror/orm.d.ts +0 -275
  290. package/@types/orm_mirror/sql-query.d.ts +0 -79
  291. package/Readme.md +0 -843
  292. package/lib/index.js +0 -13
  293. package/lib/modules/mysql.js +0 -35
  294. package/lib/modules/sqlite3.js +0 -27
  295. package/lib/patch.js +0 -271
@@ -0,0 +1,259 @@
1
+ // Transforms:
2
+ // "name LIKE ? AND age > ?", ["John", 23]
3
+ // into:
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.ChainBuilderBase = exports.convertSqlFromTable = exports.isWrapperdSubQuerySelect = exports.maybeKnexRawOrQueryBuilder = exports.maybeKnexRaw = exports.maybeKnexSubQuery = exports.cutOffOrderDirectionFromColumnFirstStr = exports.escapeValForKnex = exports.DialectTypes = exports.bufferToString = exports.ensureNumber = exports.ucfirst = exports.defaultTableAliasNameRule = exports.autoIncreatementTableIndex = exports.pickColumnAsFromSelectFieldsDescriptor = exports.pickAliasFromFromDescriptor = exports.parseTableInputStr = exports.get_table_alias = exports.zeroPad = exports.dateToString = exports.escapeQuery = void 0;
6
+ // "name LIKE 'John' AND age > 23"
7
+ function escapeQuery(Dialect, query, args) {
8
+ let pos = 0;
9
+ return query.replace(/\?{1,2}/g, function (match) {
10
+ if (match == '?') {
11
+ return Dialect.escapeVal(args[pos++]);
12
+ }
13
+ else if (match == '??') {
14
+ return Dialect.escapeId(args[pos++]);
15
+ }
16
+ });
17
+ }
18
+ exports.escapeQuery = escapeQuery;
19
+ function dateToString(date, timeZone, opts) {
20
+ const dt = new Date(date);
21
+ if (timeZone != 'local') {
22
+ const tz = convertTimezone(timeZone);
23
+ dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
24
+ if (tz !== false) {
25
+ dt.setTime(dt.getTime() + (tz * 60000));
26
+ }
27
+ }
28
+ const year = dt.getFullYear();
29
+ const month = zeroPad(dt.getMonth() + 1);
30
+ const day = zeroPad(dt.getDate());
31
+ const hour = zeroPad(dt.getHours());
32
+ const minute = zeroPad(dt.getMinutes());
33
+ const second = zeroPad(dt.getSeconds());
34
+ const milli = zeroPad(dt.getMilliseconds(), 3);
35
+ if (opts.dialect == 'mysql' || opts.dialect == 'dm' || timeZone == 'local') {
36
+ return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milli;
37
+ }
38
+ else {
39
+ return year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + '.' + milli + 'Z';
40
+ }
41
+ }
42
+ exports.dateToString = dateToString;
43
+ function zeroPad(number, n = 2) {
44
+ number = "" + number;
45
+ while (number.length < n) {
46
+ number = "0" + number;
47
+ }
48
+ return number;
49
+ }
50
+ exports.zeroPad = zeroPad;
51
+ function convertTimezone(tz) {
52
+ if (tz == "Z")
53
+ return 0;
54
+ const m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
55
+ if (m) {
56
+ return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
57
+ }
58
+ return false;
59
+ }
60
+ function get_table_alias(sql, table) {
61
+ for (let i = 0; i < sql.from.length; i++) {
62
+ if (sql.from[i].table == table) {
63
+ return pickAliasFromFromDescriptor(sql.from[i]);
64
+ }
65
+ }
66
+ return typeof table === 'string' ? table : '';
67
+ }
68
+ exports.get_table_alias = get_table_alias;
69
+ ;
70
+ // TODO: add test about it
71
+ function unwrapIdentifier(identifier = '', dialect) {
72
+ switch (dialect) {
73
+ case 'postgresql':
74
+ return identifier.replace(/^"|"$/g, '');
75
+ case 'dm':
76
+ return identifier.replace(/^"|"$/g, '');
77
+ case 'sqlite':
78
+ case 'mysql':
79
+ return identifier.replace(/^`|`$/g, '');
80
+ }
81
+ return identifier;
82
+ }
83
+ function parseTableInputStr(table_name, dialect) {
84
+ if (!table_name)
85
+ throw new Error(`[parseTableInputStr] invalid input table_name!`);
86
+ let ta_tuple = ['', ''];
87
+ if (typeof table_name === 'string') {
88
+ table_name = table_name.trim();
89
+ let idx = table_name.lastIndexOf(' as ');
90
+ if (idx > 0) {
91
+ ta_tuple = [
92
+ table_name.slice(0, idx).trim(),
93
+ table_name.slice(idx + 4).trim()
94
+ ];
95
+ }
96
+ else {
97
+ ta_tuple = table_name.split(' ').slice(0, 2);
98
+ }
99
+ }
100
+ else if (maybeKnexRawOrQueryBuilder(table_name)) {
101
+ ta_tuple = [table_name, ''];
102
+ }
103
+ else {
104
+ ta_tuple = table_name.slice(0, 2);
105
+ }
106
+ if (dialect) {
107
+ if (typeof ta_tuple[0] === 'string')
108
+ ta_tuple[0] = unwrapIdentifier(ta_tuple[0], dialect);
109
+ ta_tuple[1] = unwrapIdentifier(ta_tuple[1], dialect);
110
+ }
111
+ return ta_tuple;
112
+ }
113
+ exports.parseTableInputStr = parseTableInputStr;
114
+ function pickAliasFromFromDescriptor(fd) {
115
+ return fd.alias || fd.a;
116
+ }
117
+ exports.pickAliasFromFromDescriptor = pickAliasFromFromDescriptor;
118
+ function pickColumnAsFromSelectFieldsDescriptor(sitem) {
119
+ return sitem.as || sitem.a;
120
+ }
121
+ exports.pickColumnAsFromSelectFieldsDescriptor = pickColumnAsFromSelectFieldsDescriptor;
122
+ function autoIncreatementTableIndex(from) {
123
+ return from.length + 1;
124
+ }
125
+ exports.autoIncreatementTableIndex = autoIncreatementTableIndex;
126
+ function defaultTableAliasNameRule(idx) {
127
+ return `t${idx}`;
128
+ }
129
+ exports.defaultTableAliasNameRule = defaultTableAliasNameRule;
130
+ function ucfirst(str = '') {
131
+ if (str.length <= 1)
132
+ return str.toUpperCase();
133
+ return str[0].toUpperCase() + str.slice(1);
134
+ }
135
+ exports.ucfirst = ucfirst;
136
+ function ensureNumber(num) {
137
+ if (typeof num !== 'number') {
138
+ num = parseInt(num);
139
+ }
140
+ if (isNaN(num))
141
+ return 0;
142
+ return num;
143
+ }
144
+ exports.ensureNumber = ensureNumber;
145
+ function bufferToString(buffer, dialect) {
146
+ switch (dialect) {
147
+ case 'dm':
148
+ return "HEXTORAW('" + buffer.toString('hex') + "')";
149
+ case 'mssql':
150
+ return "X'" + buffer.toString('hex') + "'";
151
+ case 'mysql':
152
+ return "X'" + buffer.toString('hex') + "'";
153
+ case 'sqlite':
154
+ return "X'" + buffer.toString('hex') + "'";
155
+ case 'postgresql':
156
+ return "'\\x" + buffer.toString('hex') + "'";
157
+ }
158
+ }
159
+ exports.bufferToString = bufferToString;
160
+ exports.DialectTypes = ['mysql', 'sqlite', 'mssql', 'postgresql', 'dm'];
161
+ function escapeValForKnex(val, Dialect, opts) {
162
+ // no escape for `knex.raw(...)`, `knex.ref(...)` and subquery
163
+ if (maybeKnexRawOrQueryBuilder(val))
164
+ return val;
165
+ // never escapeVal those types with `Dialect.escapeVal`, knex would escape them automatically
166
+ const _type = typeof val;
167
+ if (_type === 'string')
168
+ return val;
169
+ else if (_type === 'number')
170
+ return val;
171
+ else if (_type === 'symbol')
172
+ return val = null;
173
+ else if (_type === 'boolean') {
174
+ if (Dialect.DataTypes.isSQLITE || Dialect.type === 'dm')
175
+ return val ? 1 : 0;
176
+ return val;
177
+ }
178
+ else if (_type === 'function')
179
+ return Dialect.knex.raw(val(Dialect));
180
+ else if (val instanceof Date)
181
+ // TODO: how to suppor timezone?
182
+ return val;
183
+ else if (Buffer.isBuffer(val)) {
184
+ return (Dialect.type === 'postgresql' || Dialect.type === 'dm')
185
+ ? Dialect.knex.raw(bufferToString(val, Dialect.type))
186
+ : val;
187
+ }
188
+ else if (val instanceof Array)
189
+ return val;
190
+ else if (val === null)
191
+ return val;
192
+ else if (val === undefined)
193
+ return val = null;
194
+ return Dialect.escapeVal(val, opts.timezone);
195
+ }
196
+ exports.escapeValForKnex = escapeValForKnex;
197
+ function cutOffOrderDirectionFromColumnFirstStr(col_name) {
198
+ const result = {
199
+ col_name,
200
+ direction: 'ASC'
201
+ };
202
+ if (typeof col_name !== 'string')
203
+ return result;
204
+ if (col_name[0] === '-') {
205
+ result.col_name = col_name.substr(1);
206
+ result.direction = 'DESC';
207
+ }
208
+ return result;
209
+ }
210
+ exports.cutOffOrderDirectionFromColumnFirstStr = cutOffOrderDirectionFromColumnFirstStr;
211
+ function maybeKnexSubQuery(input) {
212
+ return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function";
213
+ }
214
+ exports.maybeKnexSubQuery = maybeKnexSubQuery;
215
+ function maybeKnexRaw(input) {
216
+ return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function";
217
+ }
218
+ exports.maybeKnexRaw = maybeKnexRaw;
219
+ function maybeKnexRawOrQueryBuilder(input) {
220
+ return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function";
221
+ }
222
+ exports.maybeKnexRawOrQueryBuilder = maybeKnexRawOrQueryBuilder;
223
+ function isWrapperdSubQuerySelect(sql) {
224
+ return typeof sql === 'string' && /^\(\s*SELECT\s.*\)$/i.test(sql);
225
+ }
226
+ exports.isWrapperdSubQuerySelect = isWrapperdSubQuerySelect;
227
+ /** @internal */
228
+ function convertSqlFromTable(sql_from_item, knex, isSubQuery) {
229
+ var _a, _b;
230
+ 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)); }
231
+ // from: select subquery as table
232
+ return maybeKnexRawOrQueryBuilder(sql_from_item.table) ? knex.raw(sql_from_item.table).wrap('(', ')')
233
+ : isSubQuery ? knex.raw(sql_from_item.table.trim()).wrap('', '')
234
+ : sql_from_item.table;
235
+ }
236
+ exports.convertSqlFromTable = convertSqlFromTable;
237
+ class ChainBuilderBase {
238
+ constructor(Dialect) {
239
+ this.Dialect = Dialect;
240
+ Object.defineProperty(this, 'Dialect', {
241
+ get() {
242
+ return Dialect;
243
+ },
244
+ configurable: false
245
+ });
246
+ Object.defineProperty(this, 'knex', {
247
+ get() {
248
+ return Dialect.knex;
249
+ },
250
+ configurable: false
251
+ });
252
+ }
253
+ /**
254
+ * @override
255
+ */
256
+ build() { return ''; }
257
+ ;
258
+ }
259
+ 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,33 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.InsertQuery = void 0;
3
+ const Helpers_1 = require("./Helpers");
4
+ class InsertQuery extends Helpers_1.ChainBuilderBase {
5
+ constructor(Dialect, opts) {
6
+ super(Dialect);
7
+ this.opts = opts;
8
+ this.sql = {};
9
+ }
10
+ into(table) {
11
+ this.sql.table = table;
12
+ return this;
13
+ }
14
+ set(values) {
15
+ this.sql.set = values;
16
+ return this;
17
+ }
18
+ build() {
19
+ const sqlBuilder = this.Dialect.knex(this.sql.table);
20
+ const col_vals = {};
21
+ if (this.sql.hasOwnProperty("set")) {
22
+ let val = null;
23
+ for (let k in this.sql.set) {
24
+ val = (0, Helpers_1.escapeValForKnex)(this.sql.set[k], this.Dialect, this.opts);
25
+ ;
26
+ col_vals[k] = val;
27
+ }
28
+ sqlBuilder.insert(col_vals);
29
+ }
30
+ return sqlBuilder.toQuery();
31
+ }
32
+ }
33
+ exports.InsertQuery = InsertQuery;
@@ -0,0 +1,29 @@
1
+ /// <reference lib="es2017" />
2
+ import { CreateQuery } from "./Create";
3
+ import { SelectQuery } from "./Select";
4
+ import { InsertQuery } from "./Insert";
5
+ import { UpdateQuery } from "./Update";
6
+ import { RemoveQuery } from "./Remove";
7
+ export import Helpers = require('./Helpers');
8
+ export import Dialects = require('./Dialects');
9
+ import { FxSqlQuery } from './Typo/Query';
10
+ import { FxSqlQueryDialect } from './Typo/Dialect';
11
+ export declare const comparators: import("./Typo/Comparators").FxSqlQueryComparator.ComparatorHash;
12
+ export declare const Text: FxSqlQuery.TypedQueryObjectWrapper<"text">;
13
+ export declare class Query implements FxSqlQuery.Class_Query {
14
+ readonly Dialect: FxSqlQueryDialect.Dialect;
15
+ readonly knex: FxSqlQuery.Class_Query['knex'];
16
+ private opts;
17
+ private _fns;
18
+ private _proxyFn;
19
+ constructor(_opts?: string | FxSqlQuery.QueryOptions);
20
+ escape: FxSqlQueryDialect.Dialect['escape'];
21
+ escapeId: FxSqlQueryDialect.Dialect['escapeId'];
22
+ escapeVal: FxSqlQueryDialect.Dialect['escapeVal'];
23
+ create(): CreateQuery;
24
+ select(): SelectQuery;
25
+ insert(): InsertQuery;
26
+ update(): UpdateQuery;
27
+ remove(): RemoveQuery;
28
+ }
29
+ export * from './Typo/index';
@@ -0,0 +1,119 @@
1
+ /// <reference lib="es2017" />
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Query = exports.Text = exports.comparators = void 0;
18
+ const util = require("util");
19
+ const Helpers_1 = require("./Helpers");
20
+ const Create_1 = require("./Create");
21
+ const Select_1 = require("./Select");
22
+ const Insert_1 = require("./Insert");
23
+ const Update_1 = require("./Update");
24
+ const Remove_1 = require("./Remove");
25
+ const ComparatorsHash = require("./Comparators");
26
+ exports.Helpers = require("./Helpers");
27
+ exports.Dialects = require("./Dialects");
28
+ exports.comparators = ComparatorsHash;
29
+ exports.Text = buildQueryType("text");
30
+ function mountDialect(Dialect) {
31
+ const FKnex = require('../knex/knex');
32
+ const knexClientMap = {
33
+ dm: 'pg'
34
+ };
35
+ Object.defineProperty(this, 'Dialect', {
36
+ value: util.extend({}, Dialect),
37
+ writable: false,
38
+ configurable: false
39
+ });
40
+ const knexClient = knexClientMap[this.Dialect.type] || this.Dialect.type;
41
+ const knexInst = FKnex({ client: knexClient, useNullAsDefault: true });
42
+ Object.defineProperty(this.Dialect, 'knex', {
43
+ value: knexInst,
44
+ writable: false,
45
+ configurable: false
46
+ });
47
+ Object.defineProperty(this, 'knex', {
48
+ value: knexInst,
49
+ writable: false,
50
+ configurable: false
51
+ });
52
+ }
53
+ class Query {
54
+ _proxyFn(fn_name) {
55
+ if (!this._fns[fn_name]) {
56
+ switch (fn_name) {
57
+ case 'escape':
58
+ this._fns[fn_name] = exports.Helpers.escapeQuery.bind(exports.Helpers, this.Dialect);
59
+ break;
60
+ case 'escapeId':
61
+ this._fns[fn_name] = this.Dialect.escapeId.bind(this.Dialect);
62
+ break;
63
+ case 'escapeVal':
64
+ this._fns[fn_name] = this.Dialect.escapeVal.bind(this.Dialect);
65
+ break;
66
+ }
67
+ }
68
+ return this._fns[fn_name];
69
+ }
70
+ constructor(_opts) {
71
+ this._fns = {};
72
+ let opts = null;
73
+ if (typeof _opts === "string") {
74
+ if (!Helpers_1.DialectTypes.includes(_opts))
75
+ throw `invalid dialect type ${_opts}`;
76
+ opts = { dialect: _opts };
77
+ }
78
+ else {
79
+ opts = Object.assign({}, _opts);
80
+ }
81
+ this.opts = opts;
82
+ opts.dialect = opts.dialect || 'mysql';
83
+ mountDialect.call(this, exports.Dialects[opts.dialect]);
84
+ this.escape = this._proxyFn('escape');
85
+ this.escapeId = this._proxyFn('escapeId');
86
+ this.escapeVal = this._proxyFn('escapeVal');
87
+ }
88
+ create() {
89
+ return new Create_1.CreateQuery(this.Dialect);
90
+ }
91
+ select() {
92
+ return new Select_1.SelectQuery(this.Dialect, this.opts);
93
+ }
94
+ insert() {
95
+ return new Insert_1.InsertQuery(this.Dialect, this.opts);
96
+ }
97
+ update() {
98
+ return new Update_1.UpdateQuery(this.Dialect, this.opts);
99
+ }
100
+ remove() {
101
+ return new Remove_1.RemoveQuery(this.Dialect, this.opts);
102
+ }
103
+ }
104
+ exports.Query = Query;
105
+ function buildQueryType(type) {
106
+ return function (data) {
107
+ var o = {
108
+ data: data
109
+ };
110
+ Object.defineProperty(o, "type", {
111
+ value: function () {
112
+ return type;
113
+ },
114
+ enumerable: false
115
+ });
116
+ return o;
117
+ };
118
+ }
119
+ __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,93 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.RemoveQuery = void 0;
3
+ const util = require("util");
4
+ const Where = require("./Where");
5
+ const Helpers_1 = require("./Helpers");
6
+ class RemoveQuery extends Helpers_1.ChainBuilderBase {
7
+ constructor(Dialect, opts) {
8
+ super(Dialect);
9
+ this.opts = opts;
10
+ this.sql = {
11
+ where: [],
12
+ order: []
13
+ };
14
+ }
15
+ from(table) {
16
+ this.sql.table = table;
17
+ return this;
18
+ }
19
+ where(...whereConditions) {
20
+ for (let i = 0; i < whereConditions.length; i++) {
21
+ this.sql.where.push({
22
+ table: null,
23
+ wheres: whereConditions[i]
24
+ });
25
+ }
26
+ return this;
27
+ }
28
+ build() {
29
+ var extraQuery = [];
30
+ const sqlBuilder = this.Dialect.knex(this.sql.table);
31
+ Where.build(sqlBuilder, this.Dialect, this.sql.where, this.opts);
32
+ sqlBuilder.del();
33
+ const sqlList = [sqlBuilder.toQuery()];
34
+ // order
35
+ if (this.sql.order.length > 0) {
36
+ // this.sql.order has been normalized
37
+ const order = this.sql.order;
38
+ const tmp = [];
39
+ for (let i = 0; i < order.length; i++) {
40
+ const col_desc = order[i].c;
41
+ const zdir = order[i].d;
42
+ if (Array.isArray(col_desc)) {
43
+ tmp.push(this.Dialect.escapeId.apply(this.Dialect, col_desc) + " " + zdir);
44
+ }
45
+ else {
46
+ tmp.push(this.Dialect.escapeId(col_desc) + " " + zdir);
47
+ }
48
+ }
49
+ if (tmp.length > 0) {
50
+ extraQuery.push("ORDER BY " + tmp.join(", "));
51
+ sqlList.push(util.last(extraQuery));
52
+ }
53
+ }
54
+ // limit for all Dialects but MSSQL
55
+ if (!this.Dialect.limitAsTop) {
56
+ if (this.sql.hasOwnProperty("limit")) {
57
+ if (this.sql.hasOwnProperty("offset")) {
58
+ extraQuery.push("LIMIT " + this.sql.limit + " OFFSET " + this.sql.offset);
59
+ }
60
+ else {
61
+ extraQuery.push("LIMIT " + this.sql.limit);
62
+ }
63
+ sqlList.push(util.last(extraQuery));
64
+ }
65
+ else if (this.sql.hasOwnProperty("offset")) {
66
+ extraQuery.push("OFFSET " + this.sql.offset);
67
+ sqlBuilder.offset(this.sql.offset);
68
+ sqlList.push(util.last(extraQuery));
69
+ }
70
+ // limit as: SELECT TOP n (MSSQL only)
71
+ }
72
+ else if (this.sql.hasOwnProperty("limit")) {
73
+ sqlList[0].replace('delete from', `delete top ${this.sql.limit}`);
74
+ }
75
+ return sqlList.join(' ');
76
+ }
77
+ offset(offset) {
78
+ this.sql.offset = offset;
79
+ return this;
80
+ }
81
+ limit(limit) {
82
+ this.sql.limit = limit;
83
+ return this;
84
+ }
85
+ order(column, dir) {
86
+ this.sql.order.push({
87
+ c: Array.isArray(column) ? [(0, Helpers_1.get_table_alias)(this.sql, column[0]), column[1]] : column,
88
+ d: (dir == "Z" ? "DESC" : "ASC")
89
+ });
90
+ return this;
91
+ }
92
+ }
93
+ 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
+ }