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
package/License CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2013 Diogo Resende and other contributors
3
+ Copyright (c) 2018 Richard and other contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ ## fib-orm
2
+
3
+ ORM for [fibjs](https://fibjs.org) — a unified ORM package supporting SQLite, MySQL, and PostgreSQL.
4
+
5
+ ### Install
6
+
7
+ ```bash
8
+ npm install fib-orm
9
+ ```
10
+
11
+ ### Quick Start
12
+
13
+ ```js
14
+ const ORM = require('fib-orm');
15
+
16
+ const db = ORM.connectSync('sqlite:test.db');
17
+
18
+ const User = db.define('user', {
19
+ name: String,
20
+ age: Number
21
+ });
22
+
23
+ db.syncSync();
24
+
25
+ User.createSync({ name: 'Alice', age: 30 });
26
+ const users = User.findSync();
27
+ console.log(users);
28
+
29
+ db.closeSync();
30
+ ```
31
+
32
+ ### Build
33
+
34
+ ```bash
35
+ npm run build
36
+ ```
37
+
38
+ ### Test
39
+
40
+ ```bash
41
+ # SQLite
42
+ ORM_PROTOCOL=sqlite fibjs test
43
+
44
+ # MySQL
45
+ ORM_PROTOCOL=mysql fibjs test
46
+
47
+ # PostgreSQL
48
+ ORM_PROTOCOL=postgres fibjs test
49
+ ```
50
+
51
+ ### License
52
+
53
+ MIT
@@ -0,0 +1,2 @@
1
+ declare const _default: (cli: import("@fxjs/cli/typings/Cli")) => import("@fibcli/make-cli").CliCommand;
2
+ export default _default;
@@ -0,0 +1,74 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+ const make_cli_1 = require("@fibcli/make-cli");
5
+ const index_1 = require("../../db-driver/index");
6
+ const index_2 = require("../../sql-ddl-sync/index");
7
+ const ddl_1 = require("../helpers/ddl");
8
+ exports.default = (0, make_cli_1.wrapSubcommand)({
9
+ name: 'ddlFrom',
10
+ aliases: [
11
+ 'ddlfrom'
12
+ ],
13
+ inputs: {
14
+ required: '<connection>',
15
+ optional: [
16
+ '[tableName]'
17
+ ]
18
+ },
19
+ examples: [
20
+ 'orm ddlFrom mysql://root:@localhost:3306/test',
21
+ ],
22
+ options: {
23
+ '-P, --pool [pool]': 'whether use connection pool or not',
24
+ '-O, --out [outPath]': 'path to output json file',
25
+ },
26
+ description: `read columns information from database, and generate ddl file`,
27
+ onAction: ([connection, [tableName]], options) => {
28
+ const driver = index_1.Driver.create(connection);
29
+ const sync = new index_2.Sync({ dbdriver: driver });
30
+ let outJson = '';
31
+ if (options.out) {
32
+ if (options.out === true) {
33
+ outJson = `./${driver.type}-${driver.config.database.replace(/[\.|\/]/g, '_')}.json`;
34
+ }
35
+ else if (typeof options.out === 'string') {
36
+ outJson = options.out;
37
+ }
38
+ if (outJson) {
39
+ outJson = path.resolve(process.cwd(), outJson);
40
+ }
41
+ }
42
+ let tableNames = [];
43
+ if (tableName) {
44
+ if (!sync.Dialect.hasCollectionSync(driver, tableName))
45
+ throw new Error(`[ddlFrom] table ${tableName} doen't exist!`);
46
+ tableNames = [tableName];
47
+ }
48
+ else {
49
+ switch (driver.type) {
50
+ case 'psql':
51
+ case 'mysql':
52
+ case 'sqlite':
53
+ tableNames = (0, ddl_1.getAllTableNames)(driver);
54
+ break;
55
+ default:
56
+ if (!tableName) {
57
+ throw new Error('[ddlFrom] tableName is required!');
58
+ }
59
+ tableNames = [tableName];
60
+ }
61
+ }
62
+ const tableDDLs = (0, ddl_1.getTableDDLs)(sync, { tableNames });
63
+ if (outJson) {
64
+ console.notice(`[ddlFrom] try to write to ${outJson}...`);
65
+ fs.mkdir(path.dirname(outJson), { recursive: true });
66
+ fs.writeFile(outJson, JSON.stringify(tableDDLs, null, 2));
67
+ console.notice(`[ddlFrom] write json to ${outJson} success!`);
68
+ }
69
+ else {
70
+ console.log('[ddlFrom] tableDDLs:');
71
+ console.log(JSON.stringify(tableDDLs, null, 2));
72
+ }
73
+ }
74
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: (cli: import("@fxjs/cli/typings/Cli")) => import("@fibcli/make-cli").CliCommand;
2
+ export default _default;
@@ -0,0 +1,106 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+ const make_cli_1 = require("@fibcli/make-cli");
5
+ const ORM = require("../../orm/index");
6
+ const index_1 = require("../../sql-ddl-sync/index");
7
+ const Diff = require("diff");
8
+ const ddl_1 = require("../helpers/ddl");
9
+ function alphaBetKeySort(obj) {
10
+ if (!obj)
11
+ return obj;
12
+ return Object.keys(obj).sort().reduce((acc, key) => {
13
+ // @ts-ignore
14
+ acc[key] = obj[key];
15
+ return acc;
16
+ }, {});
17
+ }
18
+ exports.default = (0, make_cli_1.wrapSubcommand)({
19
+ name: 'dumpModel',
20
+ inputs: {
21
+ required: '<modelFile>',
22
+ optional: [
23
+ '[modelFile]'
24
+ ]
25
+ },
26
+ examples: [],
27
+ options: {
28
+ '-O, --out [outPath]': 'path to output json file',
29
+ '--sync': 'sync user definition to database',
30
+ '--no-alphabet': 'sort properties alphabetically on patch',
31
+ },
32
+ description: `read columns information from database, and generate ddl file`,
33
+ onAction: ([modelFile], options) => {
34
+ modelFile = path.resolve(process.cwd(), modelFile);
35
+ const jsonDiffName = path.basename(modelFile, '.js') + '-dump.json';
36
+ const diffOutDir = path.dirname(modelFile);
37
+ const diffOutput = path.resolve(diffOutDir, jsonDiffName);
38
+ if (!fs.exists(modelFile)) {
39
+ throw new Error(`[dumpModel] model file not found: ${modelFile}`);
40
+ }
41
+ let modelFunc = require(modelFile);
42
+ if (typeof modelFunc !== 'function') {
43
+ if (typeof modelFunc.default === 'function')
44
+ modelFunc = modelFunc.default;
45
+ }
46
+ if (typeof modelFunc !== 'function') {
47
+ throw new Error(`[dumpModel] model function not found, it's typeof ${typeof modelFunc}`);
48
+ }
49
+ const { orm } = modelFunc(ORM);
50
+ if (options.sync) {
51
+ orm.syncSync();
52
+ }
53
+ const sync = new index_1.Sync({ dbdriver: orm.driver.sqlDriver });
54
+ const modelReports = {};
55
+ const modelByTable = {};
56
+ for (let k in orm.models) {
57
+ const model = orm.models[k];
58
+ modelByTable[model.table] = model;
59
+ }
60
+ (0, ddl_1.getTableDDLs)(sync, {
61
+ afterGetTableDDL: (ddl) => {
62
+ var _a;
63
+ const userDefinedProperties = ((_a = modelByTable[ddl.collection]) === null || _a === void 0 ? void 0 : _a.allProperties) || {};
64
+ const dataStoreProperties = ddl.properties;
65
+ const tPatchBaseName = `properties-for-t-[${ddl.collection}]`;
66
+ const tablePatchFile = path.resolve(diffOutDir, `./${tPatchBaseName}.patch`);
67
+ const propritiesDiff = Object.entries(dataStoreProperties).reduce((accu, [mapsTo, prop]) => {
68
+ let udP = userDefinedProperties[mapsTo] || null;
69
+ if (options.alphabet) {
70
+ udP = alphaBetKeySort(udP);
71
+ userDefinedProperties[mapsTo] = udP;
72
+ prop = alphaBetKeySort(prop);
73
+ dataStoreProperties[mapsTo] = prop;
74
+ }
75
+ const changes = Diff.diffJson(udP, prop);
76
+ const patchBaseName = `${tPatchBaseName}-p-${mapsTo}`;
77
+ const patch = Diff.createTwoFilesPatch(`${patchBaseName}-from-database.json`, `${patchBaseName}-user-defined.json`, JSON.stringify(prop, null, 2), JSON.stringify(udP, null, 2));
78
+ accu.push({
79
+ name: mapsTo,
80
+ mapsTo,
81
+ changes,
82
+ patch
83
+ });
84
+ return accu;
85
+ }, []);
86
+ const patch = Diff.createTwoFilesPatch(`${tPatchBaseName}-from-database.json`, `${tPatchBaseName}-user-defined.json`, JSON.stringify(dataStoreProperties, null, 2), JSON.stringify(userDefinedProperties, null, 2));
87
+ fs.writeFile(tablePatchFile, patch);
88
+ modelReports[ddl.collection] = {
89
+ propritiesDiff,
90
+ userDefinedProperties,
91
+ dataStoreProperties,
92
+ dataStoreRaw: ddl.rawColumns,
93
+ };
94
+ }
95
+ });
96
+ const dumpObj = {
97
+ connection: sync.dbdriver.uri,
98
+ driverType: sync.dbdriver.type,
99
+ modelReports,
100
+ };
101
+ console.notice(`[dumpModel] try to write to ${diffOutput}...`);
102
+ fs.mkdir(path.dirname(diffOutput), { recursive: true });
103
+ fs.writeFile(diffOutput, JSON.stringify(dumpObj, null, 2));
104
+ console.notice(`[dumpModel] write json to ${diffOutput} success!`);
105
+ }
106
+ });
@@ -0,0 +1,2 @@
1
+ declare const cmd: (cli: import("@fxjs/cli/typings/Cli")) => import("@fibcli/make-cli").CliCommand;
2
+ export default cmd;
@@ -0,0 +1,68 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ // import fs = require('fs');
3
+ // import path = require('path');
4
+ // import coroutine = require('coroutine');
5
+ const chalk = require("@fibjs/chalk");
6
+ const make_cli_1 = require("@fibcli/make-cli");
7
+ const Query_1 = require("../../sql-query/Query");
8
+ const VALID_DIALECTS = ['mysql', 'sqlite', 'postgresql', 'mssql', 'dm'];
9
+ const VALID_TYPES = ['sql', 'val', 'id'];
10
+ const cmd = (0, make_cli_1.wrapSubcommand)({
11
+ name: 'escapeSQL',
12
+ aliases: [
13
+ 'escape'
14
+ ],
15
+ inputs: {
16
+ required: '<sqlstring>',
17
+ rest: '[...args]'
18
+ },
19
+ examples: [],
20
+ options: {
21
+ '-d, --dialect-type [dialectType]': `dialect type, support ${VALID_DIALECTS.map(d => `'${d}'`).join(', ')}`,
22
+ '-t, --type [escapeType]': `escape type, support ${VALID_TYPES.map(t => `'${t}'`).join(', ')}`,
23
+ },
24
+ description: `escape SQL string with given arguments`,
25
+ onAction: ([sqlstring, , rest], options) => {
26
+ const dialectType = options.dialectType || 'mysql';
27
+ if (!VALID_DIALECTS.includes(dialectType)) {
28
+ throw new Error(`[escape] invalid dialect type: ${dialectType}`);
29
+ }
30
+ const escapeType = options.type || 'sql';
31
+ if (!VALID_TYPES.includes(escapeType)) {
32
+ throw new Error(`[escape] invalid escape type: ${escapeType}`);
33
+ }
34
+ VALID_TYPES;
35
+ const query = new Query_1.Query({ dialect: dialectType });
36
+ switch (escapeType) {
37
+ default:
38
+ case 'sql': {
39
+ const result = query.escape(sqlstring, rest);
40
+ console.log(`dialect-type: ${chalk.blue(dialectType)}`);
41
+ console.log(`args(${rest.length}): ${chalk.blue(rest.join(' '))}`);
42
+ console.log();
43
+ console.log(`input-sql: \t ${chalk.yellow(sqlstring)}`);
44
+ console.log(`escaped-sql:\t ${chalk.green(result)}`);
45
+ break;
46
+ }
47
+ case 'val': {
48
+ const result = query.escapeVal(sqlstring);
49
+ console.log(`dialect-type: ${chalk.blue(dialectType)}`);
50
+ // console.log(`args(${rest.length}): ${chalk.blue(rest.join(' '))}`)
51
+ console.log();
52
+ console.log(`input-value: \t ${chalk.yellow(sqlstring)}`);
53
+ console.log(`escaped-value:\t ${chalk.green(result)}`);
54
+ break;
55
+ }
56
+ case 'id': {
57
+ const result = query.escapeId(sqlstring);
58
+ console.log(`dialect-type: ${chalk.blue(dialectType)}`);
59
+ // console.log(`args(${rest.length}): ${chalk.blue(rest.join(' '))}`)
60
+ console.log();
61
+ console.log(`input-id: \t ${chalk.yellow(sqlstring)}`);
62
+ console.log(`escaped-id:\t ${chalk.green(result)}`);
63
+ break;
64
+ }
65
+ }
66
+ }
67
+ });
68
+ exports.default = cmd;
@@ -0,0 +1,2 @@
1
+ declare const _default: (cli: import("@fxjs/cli/typings/Cli")) => import("@fibcli/make-cli").CliCommand;
2
+ export default _default;
@@ -0,0 +1,36 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const logUpdate = require("@fibcli/log-update");
3
+ const cliSpinners = require("@fibcli/cli-spinners");
4
+ const make_cli_1 = require("@fibcli/make-cli");
5
+ const allSpinnerType = Object.keys(cliSpinners);
6
+ const DFLT_SPINNER_TYPE = 'dots';
7
+ exports.default = (0, make_cli_1.wrapSubcommand)({
8
+ name: 'spinner',
9
+ inputs: {
10
+ optional: [
11
+ '[text]'
12
+ ],
13
+ },
14
+ examples: [],
15
+ options: {
16
+ '-s, --spinner-type [spinnerType]': {
17
+ desc: `type of spinner, valid keys: ${allSpinnerType.join(', ')}`,
18
+ default: DFLT_SPINNER_TYPE
19
+ },
20
+ },
21
+ description: `just run spinner`,
22
+ onAction: ([, [text]], options) => {
23
+ text = text || ' Unicorns';
24
+ let spinnerType = options.spinnerType;
25
+ if (!spinnerType || allSpinnerType.indexOf(spinnerType) === -1) {
26
+ console.warn(`[spinner] type '${spinnerType}' invalid, correct to ${DFLT_SPINNER_TYPE}`);
27
+ spinnerType = DFLT_SPINNER_TYPE;
28
+ }
29
+ const spinner = cliSpinners[spinnerType];
30
+ let i = 0;
31
+ setInterval(() => {
32
+ const { frames } = spinner;
33
+ logUpdate(`${frames[i = ++i % frames.length]}${text}`);
34
+ }, spinner.interval);
35
+ }
36
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: (cli: import("@fxjs/cli/typings/Cli")) => import("@fibcli/make-cli").CliCommand;
2
+ export default _default;
@@ -0,0 +1,65 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const child_process = require("child_process");
3
+ const chalk = require("@fibjs/chalk");
4
+ const ora_1 = require("@fibcli/ora");
5
+ const make_cli_1 = require("@fibcli/make-cli");
6
+ const package_1 = require("../helpers/package");
7
+ const LOGPREFIX = `[upgrade]`;
8
+ exports.default = (0, make_cli_1.wrapSubcommand)({
9
+ name: 'upgrade',
10
+ aliases: [],
11
+ inputs: {
12
+ optional: [],
13
+ },
14
+ examples: [],
15
+ options: {},
16
+ description: `check & upgrade orm cli to latest version.`,
17
+ onAction: ([]) => {
18
+ const pkgInfo = (0, package_1.parsePkgInfo)();
19
+ const checkResult = (0, package_1.checkUpgrade)({
20
+ pkgCurrentVersion: '1.3.0', // leave here to debug
21
+ // pkgCurrentVersion: pkgInfo.version,
22
+ });
23
+ if (checkResult.errCode) {
24
+ console.log(`${chalk.blue(LOGPREFIX)} check upgrade failed!`);
25
+ console.error(checkResult.message);
26
+ return;
27
+ }
28
+ const result = checkResult.result;
29
+ console.log();
30
+ console.log(`${chalk.blue(LOGPREFIX)} local version : ${chalk.green(result.currentVersion)}`);
31
+ console.log(`${chalk.blue(LOGPREFIX)} latest version : ${chalk.green(result.latestVersion)}`);
32
+ if (result.needUpgrade) {
33
+ console.log();
34
+ let answer = console.readLine('will you upgrade to latest version for you, confirm? (Y/N)');
35
+ answer = answer || 'N';
36
+ const upgradeSpinner = (0, ora_1.default)({});
37
+ switch (answer) {
38
+ case 'Y':
39
+ case 'y':
40
+ case 'Yes':
41
+ console.log(`${chalk.blue(LOGPREFIX)} will upgrade for you...`);
42
+ console.log();
43
+ upgradeSpinner.color = 'green';
44
+ upgradeSpinner.text = `${chalk.blue(LOGPREFIX)} upgrading...`;
45
+ upgradeSpinner.start();
46
+ require('coroutine').sleep(1000);
47
+ child_process.run('npm i -g fib-orm');
48
+ upgradeSpinner.stop();
49
+ // need to upgrade to latest version
50
+ console.log();
51
+ console.log(`${chalk.blue(LOGPREFIX)} upgrade finished`);
52
+ break;
53
+ case 'N':
54
+ case 'n':
55
+ case 'No':
56
+ default:
57
+ console.log(`${chalk.blue(LOGPREFIX)} nothing to do.`);
58
+ break;
59
+ }
60
+ }
61
+ else {
62
+ console.log(chalk.green `${chalk.blue(LOGPREFIX)} orm-cli installation is up-to date!`);
63
+ }
64
+ }
65
+ });
@@ -0,0 +1,17 @@
1
+ import { IDbDriver } from "../../db-driver/index";
2
+ import { Sync } from '../../sql-ddl-sync/index';
3
+ export declare function getAllTableNames(driver: IDbDriver.ISQLDriver): string[];
4
+ export type IModelProperties = ReturnType<Sync['Dialect']['getCollectionPropertiesSync']>;
5
+ export type IRawColumns = ReturnType<Sync['Dialect']['getCollectionColumnsSync']>;
6
+ export declare function getTableDDLs(sync: Sync, { tableNames, afterGetTableDDL }?: {
7
+ tableNames?: string[];
8
+ afterGetTableDDL?: (ddl: {
9
+ collection: string;
10
+ properties: IModelProperties;
11
+ rawColumns: IRawColumns;
12
+ }) => void;
13
+ }): {
14
+ collection: string;
15
+ rawColumns: unknown[];
16
+ properties: IModelProperties;
17
+ }[];
@@ -0,0 +1,55 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.getTableDDLs = exports.getAllTableNames = void 0;
3
+ const coroutine = require("coroutine");
4
+ function getAllTableNames(driver) {
5
+ let tableNames = [];
6
+ switch (driver.type) {
7
+ case 'psql': {
8
+ const result = driver.execute('SELECT * FROM information_schema.tables');
9
+ result.forEach((x) => {
10
+ // skip system tables
11
+ if (x.table_name.startsWith('pg_'))
12
+ return;
13
+ // skip non public tables
14
+ if (x.table_schema !== 'public')
15
+ return;
16
+ tableNames.push(x.table_name);
17
+ });
18
+ break;
19
+ }
20
+ case 'mysql': {
21
+ const result = driver.execute('show tables;');
22
+ tableNames = result.reduce((accu, x) => (accu.push(Object.entries(x)[0][1]),
23
+ accu), []);
24
+ break;
25
+ }
26
+ case 'sqlite': {
27
+ const result = driver.execute(`SELECT name FROM sqlite_schema WHERE type='table' ORDER BY name`);
28
+ result.forEach((x) => {
29
+ // skip system tables
30
+ if (x.name.startsWith('sqlite_'))
31
+ return;
32
+ tableNames.push(x.name);
33
+ });
34
+ break;
35
+ }
36
+ default:
37
+ throw new Error(`[getAllTableNames] unsupported driver type: ${driver.type}`);
38
+ }
39
+ return tableNames;
40
+ }
41
+ exports.getAllTableNames = getAllTableNames;
42
+ function getTableDDLs(sync, { tableNames, afterGetTableDDL } = {}) {
43
+ tableNames = (tableNames === null || tableNames === void 0 ? void 0 : tableNames.length) ? tableNames : getAllTableNames(sync.dbdriver);
44
+ const tableDDLs = coroutine.parallel(tableNames, (tableName) => {
45
+ const ddl = {
46
+ collection: tableName,
47
+ properties: sync.Dialect.getCollectionPropertiesSync(sync.dbdriver, tableName),
48
+ rawColumns: sync.Dialect.getCollectionColumnsSync(sync.dbdriver, tableName),
49
+ };
50
+ afterGetTableDDL === null || afterGetTableDDL === void 0 ? void 0 : afterGetTableDDL(ddl);
51
+ return ddl;
52
+ });
53
+ return tableDDLs;
54
+ }
55
+ exports.getTableDDLs = getTableDDLs;
@@ -0,0 +1,26 @@
1
+ /// <reference types="@fibjs/types" />
2
+ export declare function parsePkgInfo(filepath?: string): Record<string, string | boolean | number | object> & {
3
+ name: string;
4
+ version: string;
5
+ };
6
+ export declare function getPackgageNpmInfo({ registry, pkgName }?: {
7
+ registry?: string;
8
+ pkgName?: string;
9
+ }): Class_HttpResponse;
10
+ export declare function checkUpgrade({ pkgCurrentVersion }: {
11
+ pkgCurrentVersion: string;
12
+ }): {
13
+ errCode: number;
14
+ message: string;
15
+ response: Class_HttpResponse;
16
+ result?: undefined;
17
+ } | {
18
+ errCode: number;
19
+ message: string;
20
+ result: {
21
+ latestVersion: string;
22
+ currentVersion: string;
23
+ needUpgrade: boolean;
24
+ };
25
+ response?: undefined;
26
+ };
@@ -0,0 +1,56 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.checkUpgrade = exports.getPackgageNpmInfo = exports.parsePkgInfo = void 0;
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const http = require("http");
6
+ const ssl = require("ssl");
7
+ ssl.loadRootCerts();
8
+ const semver = require("semver");
9
+ const PKGJSON_FILE = path.resolve(__dirname, '../../package.json');
10
+ function parsePkgInfo(filepath = PKGJSON_FILE) {
11
+ return JSON.parse(fs.readFile(filepath).toString());
12
+ }
13
+ exports.parsePkgInfo = parsePkgInfo;
14
+ const PKGJSON = parsePkgInfo();
15
+ function getPackgageNpmInfo({ registry = 'https://registry.npmjs.org', pkgName = PKGJSON.name } = {}) {
16
+ const pkgInfo = parsePkgInfo();
17
+ return http.get(`${registry}/${pkgName}`, {});
18
+ }
19
+ exports.getPackgageNpmInfo = getPackgageNpmInfo;
20
+ function checkUpgrade({ pkgCurrentVersion }) {
21
+ var _a, _b, _c;
22
+ const response = getPackgageNpmInfo();
23
+ if (response.statusCode !== 200) {
24
+ return {
25
+ errCode: 1,
26
+ message: `statusCode(${response.statusCode}) error: ${response.statusMessage}`,
27
+ response
28
+ };
29
+ }
30
+ const npmRepoInfo = response.json();
31
+ let latestVersion;
32
+ latestVersion = npmRepoInfo['dist-tags'].latest;
33
+ if (!latestVersion && npmRepoInfo['time']) {
34
+ latestVersion = (_b = (_a = Object.entries(npmRepoInfo['time'])
35
+ .filter(([k]) => ['created', 'modified'].includes(k))
36
+ .sort(([, d1], [, d2]) => {
37
+ return (new Date(d1)).getTime() - (new Date(d2)).getTime();
38
+ })) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b[0];
39
+ }
40
+ if (!latestVersion && npmRepoInfo['versions']) {
41
+ latestVersion = (_c = Object.keys(npmRepoInfo['versions'])
42
+ .sort((v1, v2) => {
43
+ return semver.gt(v1, v2) ? 1 : -1;
44
+ })) === null || _c === void 0 ? void 0 : _c[0];
45
+ }
46
+ return {
47
+ errCode: 0,
48
+ message: 'ok',
49
+ result: {
50
+ latestVersion,
51
+ currentVersion: pkgCurrentVersion,
52
+ needUpgrade: !semver.gte(pkgCurrentVersion, latestVersion)
53
+ }
54
+ };
55
+ }
56
+ exports.checkUpgrade = checkUpgrade;
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env fibjs
2
+ export {};
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env fibjs
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const pkg = require('../package.json');
4
+ const getCli = require("@fxjs/cli");
5
+ const upgrade_1 = require("./commands/upgrade");
6
+ const spinner_1 = require("./commands/spinner");
7
+ const ddlFrom_1 = require("./commands/ddlFrom");
8
+ const dumpModel_1 = require("./commands/dumpModel");
9
+ const escape_1 = require("./commands/escape");
10
+ const cli = getCli('orm-cli');
11
+ (0, upgrade_1.default)(cli);
12
+ (0, spinner_1.default)(cli);
13
+ (0, ddlFrom_1.default)(cli);
14
+ (0, dumpModel_1.default)(cli);
15
+ (0, escape_1.default)(cli);
16
+ cli.help();
17
+ cli.version(pkg.version);
18
+ cli.parse();