@snowtop/ent 0.1.0-alpha160-test7 → 0.1.0-alpha160

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 (317) hide show
  1. package/core/query/shared_assoc_test.d.ts +2 -0
  2. package/core/query/shared_assoc_test.js +804 -0
  3. package/core/query/shared_test.d.ts +21 -0
  4. package/core/query/shared_test.js +736 -0
  5. package/graphql/query/shared_assoc_test.d.ts +1 -0
  6. package/graphql/query/shared_assoc_test.js +203 -0
  7. package/package.json +8 -53
  8. package/dist/package.json +0 -64
  9. package/src/action/action.ts +0 -330
  10. package/src/action/executor.ts +0 -453
  11. package/src/action/experimental_action.ts +0 -277
  12. package/src/action/index.ts +0 -31
  13. package/src/action/operations.ts +0 -967
  14. package/src/action/orchestrator.ts +0 -1527
  15. package/src/action/privacy.ts +0 -37
  16. package/src/action/relative_value.ts +0 -242
  17. package/src/action/transaction.ts +0 -38
  18. package/src/auth/auth.ts +0 -77
  19. package/src/auth/index.ts +0 -8
  20. package/src/core/base.ts +0 -367
  21. package/src/core/clause.ts +0 -1065
  22. package/src/core/config.ts +0 -219
  23. package/src/core/const.ts +0 -5
  24. package/src/core/context.ts +0 -135
  25. package/src/core/convert.ts +0 -106
  26. package/src/core/date.ts +0 -23
  27. package/src/core/db.ts +0 -498
  28. package/src/core/ent.ts +0 -1740
  29. package/src/core/global_schema.ts +0 -49
  30. package/src/core/loaders/assoc_count_loader.ts +0 -99
  31. package/src/core/loaders/assoc_edge_loader.ts +0 -250
  32. package/src/core/loaders/index.ts +0 -12
  33. package/src/core/loaders/loader.ts +0 -66
  34. package/src/core/loaders/object_loader.ts +0 -489
  35. package/src/core/loaders/query_loader.ts +0 -314
  36. package/src/core/loaders/raw_count_loader.ts +0 -175
  37. package/src/core/logger.ts +0 -49
  38. package/src/core/privacy.ts +0 -660
  39. package/src/core/query/assoc_query.ts +0 -240
  40. package/src/core/query/custom_clause_query.ts +0 -174
  41. package/src/core/query/custom_query.ts +0 -302
  42. package/src/core/query/index.ts +0 -9
  43. package/src/core/query/query.ts +0 -674
  44. package/src/core/query_impl.ts +0 -32
  45. package/src/core/viewer.ts +0 -52
  46. package/src/ent.code-workspace +0 -73
  47. package/src/graphql/builtins/connection.ts +0 -25
  48. package/src/graphql/builtins/edge.ts +0 -16
  49. package/src/graphql/builtins/node.ts +0 -12
  50. package/src/graphql/graphql.ts +0 -891
  51. package/src/graphql/graphql_field_helpers.ts +0 -221
  52. package/src/graphql/index.ts +0 -42
  53. package/src/graphql/mutations/union.ts +0 -39
  54. package/src/graphql/node_resolver.ts +0 -122
  55. package/src/graphql/query/connection_type.ts +0 -113
  56. package/src/graphql/query/edge_connection.ts +0 -171
  57. package/src/graphql/query/page_info.ts +0 -34
  58. package/src/graphql/query/shared_edge_connection.ts +0 -287
  59. package/src/graphql/scalars/orderby_direction.ts +0 -13
  60. package/src/graphql/scalars/time.ts +0 -38
  61. package/src/imports/dataz/example1/_auth.ts +0 -51
  62. package/src/imports/dataz/example1/_viewer.ts +0 -35
  63. package/src/imports/index.ts +0 -213
  64. package/src/index.ts +0 -145
  65. package/src/parse_schema/parse.ts +0 -585
  66. package/src/schema/base_schema.ts +0 -224
  67. package/src/schema/field.ts +0 -1087
  68. package/src/schema/index.ts +0 -53
  69. package/src/schema/json_field.ts +0 -94
  70. package/src/schema/schema.ts +0 -1028
  71. package/src/schema/struct_field.ts +0 -234
  72. package/src/schema/union_field.ts +0 -105
  73. package/src/scripts/custom_compiler.ts +0 -331
  74. package/src/scripts/custom_graphql.ts +0 -550
  75. package/src/scripts/migrate_v0.1.ts +0 -41
  76. package/src/scripts/move_types.ts +0 -131
  77. package/src/scripts/read_schema.ts +0 -67
  78. package/src/setupPackage.js +0 -42
  79. package/src/testutils/action/complex_schemas.ts +0 -517
  80. package/src/testutils/builder.ts +0 -422
  81. package/src/testutils/context/test_context.ts +0 -25
  82. package/src/testutils/db/fixture.ts +0 -32
  83. package/src/testutils/db/temp_db.ts +0 -941
  84. package/src/testutils/db/value.ts +0 -294
  85. package/src/testutils/db_mock.ts +0 -351
  86. package/src/testutils/db_time_zone.ts +0 -40
  87. package/src/testutils/ent-graphql-tests/index.ts +0 -653
  88. package/src/testutils/fake_comms.ts +0 -50
  89. package/src/testutils/fake_data/const.ts +0 -64
  90. package/src/testutils/fake_data/events_query.ts +0 -145
  91. package/src/testutils/fake_data/fake_contact.ts +0 -150
  92. package/src/testutils/fake_data/fake_event.ts +0 -150
  93. package/src/testutils/fake_data/fake_tag.ts +0 -139
  94. package/src/testutils/fake_data/fake_user.ts +0 -232
  95. package/src/testutils/fake_data/index.ts +0 -1
  96. package/src/testutils/fake_data/internal.ts +0 -8
  97. package/src/testutils/fake_data/tag_query.ts +0 -56
  98. package/src/testutils/fake_data/test_helpers.ts +0 -388
  99. package/src/testutils/fake_data/user_query.ts +0 -524
  100. package/src/testutils/fake_log.ts +0 -52
  101. package/src/testutils/mock_date.ts +0 -10
  102. package/src/testutils/mock_log.ts +0 -39
  103. package/src/testutils/parse_sql.ts +0 -685
  104. package/src/testutils/test_edge_global_schema.ts +0 -49
  105. package/src/testutils/write.ts +0 -70
  106. package/src/tsc/ast.ts +0 -351
  107. package/src/tsc/compilerOptions.ts +0 -85
  108. package/src/tsc/move_generated.ts +0 -191
  109. package/src/tsc/transform.ts +0 -226
  110. package/src/tsc/transform_action.ts +0 -224
  111. package/src/tsc/transform_ent.ts +0 -66
  112. package/src/tsc/transform_schema.ts +0 -546
  113. package/tsconfig.json +0 -20
  114. /package/{dist/action → action}/action.d.ts +0 -0
  115. /package/{dist/action → action}/action.js +0 -0
  116. /package/{dist/action → action}/executor.d.ts +0 -0
  117. /package/{dist/action → action}/executor.js +0 -0
  118. /package/{dist/action → action}/experimental_action.d.ts +0 -0
  119. /package/{dist/action → action}/experimental_action.js +0 -0
  120. /package/{dist/action → action}/index.d.ts +0 -0
  121. /package/{dist/action → action}/index.js +0 -0
  122. /package/{dist/action → action}/operations.d.ts +0 -0
  123. /package/{dist/action → action}/operations.js +0 -0
  124. /package/{dist/action → action}/orchestrator.d.ts +0 -0
  125. /package/{dist/action → action}/orchestrator.js +0 -0
  126. /package/{dist/action → action}/privacy.d.ts +0 -0
  127. /package/{dist/action → action}/privacy.js +0 -0
  128. /package/{dist/action → action}/relative_value.d.ts +0 -0
  129. /package/{dist/action → action}/relative_value.js +0 -0
  130. /package/{dist/action → action}/transaction.d.ts +0 -0
  131. /package/{dist/action → action}/transaction.js +0 -0
  132. /package/{dist/auth → auth}/auth.d.ts +0 -0
  133. /package/{dist/auth → auth}/auth.js +0 -0
  134. /package/{dist/auth → auth}/index.d.ts +0 -0
  135. /package/{dist/auth → auth}/index.js +0 -0
  136. /package/{dist/core → core}/base.d.ts +0 -0
  137. /package/{dist/core → core}/base.js +0 -0
  138. /package/{dist/core → core}/clause.d.ts +0 -0
  139. /package/{dist/core → core}/clause.js +0 -0
  140. /package/{dist/core → core}/config.d.ts +0 -0
  141. /package/{dist/core → core}/config.js +0 -0
  142. /package/{dist/core → core}/const.d.ts +0 -0
  143. /package/{dist/core → core}/const.js +0 -0
  144. /package/{dist/core → core}/context.d.ts +0 -0
  145. /package/{dist/core → core}/context.js +0 -0
  146. /package/{dist/core → core}/convert.d.ts +0 -0
  147. /package/{dist/core → core}/convert.js +0 -0
  148. /package/{dist/core → core}/date.d.ts +0 -0
  149. /package/{dist/core → core}/date.js +0 -0
  150. /package/{dist/core → core}/db.d.ts +0 -0
  151. /package/{dist/core → core}/db.js +0 -0
  152. /package/{dist/core → core}/ent.d.ts +0 -0
  153. /package/{dist/core → core}/ent.js +0 -0
  154. /package/{dist/core → core}/global_schema.d.ts +0 -0
  155. /package/{dist/core → core}/global_schema.js +0 -0
  156. /package/{dist/core → core}/loaders/assoc_count_loader.d.ts +0 -0
  157. /package/{dist/core → core}/loaders/assoc_count_loader.js +0 -0
  158. /package/{dist/core → core}/loaders/assoc_edge_loader.d.ts +0 -0
  159. /package/{dist/core → core}/loaders/assoc_edge_loader.js +0 -0
  160. /package/{dist/core → core}/loaders/index.d.ts +0 -0
  161. /package/{dist/core → core}/loaders/index.js +0 -0
  162. /package/{dist/core → core}/loaders/loader.d.ts +0 -0
  163. /package/{dist/core → core}/loaders/loader.js +0 -0
  164. /package/{dist/core → core}/loaders/object_loader.d.ts +0 -0
  165. /package/{dist/core → core}/loaders/object_loader.js +0 -0
  166. /package/{dist/core → core}/loaders/query_loader.d.ts +0 -0
  167. /package/{dist/core → core}/loaders/query_loader.js +0 -0
  168. /package/{dist/core → core}/loaders/raw_count_loader.d.ts +0 -0
  169. /package/{dist/core → core}/loaders/raw_count_loader.js +0 -0
  170. /package/{dist/core → core}/logger.d.ts +0 -0
  171. /package/{dist/core → core}/logger.js +0 -0
  172. /package/{dist/core → core}/privacy.d.ts +0 -0
  173. /package/{dist/core → core}/privacy.js +0 -0
  174. /package/{dist/core → core}/query/assoc_query.d.ts +0 -0
  175. /package/{dist/core → core}/query/assoc_query.js +0 -0
  176. /package/{dist/core → core}/query/custom_clause_query.d.ts +0 -0
  177. /package/{dist/core → core}/query/custom_clause_query.js +0 -0
  178. /package/{dist/core → core}/query/custom_query.d.ts +0 -0
  179. /package/{dist/core → core}/query/custom_query.js +0 -0
  180. /package/{dist/core → core}/query/index.d.ts +0 -0
  181. /package/{dist/core → core}/query/index.js +0 -0
  182. /package/{dist/core → core}/query/query.d.ts +0 -0
  183. /package/{dist/core → core}/query/query.js +0 -0
  184. /package/{dist/core → core}/query_impl.d.ts +0 -0
  185. /package/{dist/core → core}/query_impl.js +0 -0
  186. /package/{dist/core → core}/viewer.d.ts +0 -0
  187. /package/{dist/core → core}/viewer.js +0 -0
  188. /package/{dist/graphql → graphql}/builtins/connection.d.ts +0 -0
  189. /package/{dist/graphql → graphql}/builtins/connection.js +0 -0
  190. /package/{dist/graphql → graphql}/builtins/edge.d.ts +0 -0
  191. /package/{dist/graphql → graphql}/builtins/edge.js +0 -0
  192. /package/{dist/graphql → graphql}/builtins/node.d.ts +0 -0
  193. /package/{dist/graphql → graphql}/builtins/node.js +0 -0
  194. /package/{dist/graphql → graphql}/graphql.d.ts +0 -0
  195. /package/{dist/graphql → graphql}/graphql.js +0 -0
  196. /package/{dist/graphql → graphql}/graphql_field_helpers.d.ts +0 -0
  197. /package/{dist/graphql → graphql}/graphql_field_helpers.js +0 -0
  198. /package/{dist/graphql → graphql}/index.d.ts +0 -0
  199. /package/{dist/graphql → graphql}/index.js +0 -0
  200. /package/{dist/graphql → graphql}/mutations/union.d.ts +0 -0
  201. /package/{dist/graphql → graphql}/mutations/union.js +0 -0
  202. /package/{dist/graphql → graphql}/node_resolver.d.ts +0 -0
  203. /package/{dist/graphql → graphql}/node_resolver.js +0 -0
  204. /package/{dist/graphql → graphql}/query/connection_type.d.ts +0 -0
  205. /package/{dist/graphql → graphql}/query/connection_type.js +0 -0
  206. /package/{dist/graphql → graphql}/query/edge_connection.d.ts +0 -0
  207. /package/{dist/graphql → graphql}/query/edge_connection.js +0 -0
  208. /package/{dist/graphql → graphql}/query/page_info.d.ts +0 -0
  209. /package/{dist/graphql → graphql}/query/page_info.js +0 -0
  210. /package/{dist/graphql → graphql}/query/shared_edge_connection.d.ts +0 -0
  211. /package/{dist/graphql → graphql}/query/shared_edge_connection.js +0 -0
  212. /package/{dist/graphql → graphql}/scalars/orderby_direction.d.ts +0 -0
  213. /package/{dist/graphql → graphql}/scalars/orderby_direction.js +0 -0
  214. /package/{dist/graphql → graphql}/scalars/time.d.ts +0 -0
  215. /package/{dist/graphql → graphql}/scalars/time.js +0 -0
  216. /package/{dist/imports → imports}/dataz/example1/_auth.d.ts +0 -0
  217. /package/{dist/imports → imports}/dataz/example1/_auth.js +0 -0
  218. /package/{dist/imports → imports}/dataz/example1/_viewer.d.ts +0 -0
  219. /package/{dist/imports → imports}/dataz/example1/_viewer.js +0 -0
  220. /package/{dist/imports → imports}/index.d.ts +0 -0
  221. /package/{dist/imports → imports}/index.js +0 -0
  222. /package/{dist/index.d.ts → index.d.ts} +0 -0
  223. /package/{dist/index.js → index.js} +0 -0
  224. /package/{dist/parse_schema → parse_schema}/parse.d.ts +0 -0
  225. /package/{dist/parse_schema → parse_schema}/parse.js +0 -0
  226. /package/{dist/schema → schema}/base_schema.d.ts +0 -0
  227. /package/{dist/schema → schema}/base_schema.js +0 -0
  228. /package/{dist/schema → schema}/field.d.ts +0 -0
  229. /package/{dist/schema → schema}/field.js +0 -0
  230. /package/{dist/schema → schema}/index.d.ts +0 -0
  231. /package/{dist/schema → schema}/index.js +0 -0
  232. /package/{dist/schema → schema}/json_field.d.ts +0 -0
  233. /package/{dist/schema → schema}/json_field.js +0 -0
  234. /package/{dist/schema → schema}/schema.d.ts +0 -0
  235. /package/{dist/schema → schema}/schema.js +0 -0
  236. /package/{dist/schema → schema}/struct_field.d.ts +0 -0
  237. /package/{dist/schema → schema}/struct_field.js +0 -0
  238. /package/{dist/schema → schema}/union_field.d.ts +0 -0
  239. /package/{dist/schema → schema}/union_field.js +0 -0
  240. /package/{dist/scripts → scripts}/custom_compiler.d.ts +0 -0
  241. /package/{dist/scripts → scripts}/custom_compiler.js +0 -0
  242. /package/{dist/scripts → scripts}/custom_graphql.d.ts +0 -0
  243. /package/{dist/scripts → scripts}/custom_graphql.js +0 -0
  244. /package/{dist/scripts → scripts}/migrate_v0.1.d.ts +0 -0
  245. /package/{dist/scripts → scripts}/migrate_v0.1.js +0 -0
  246. /package/{dist/scripts → scripts}/move_types.d.ts +0 -0
  247. /package/{dist/scripts → scripts}/move_types.js +0 -0
  248. /package/{dist/scripts → scripts}/read_schema.d.ts +0 -0
  249. /package/{dist/scripts → scripts}/read_schema.js +0 -0
  250. /package/{dist/testutils → testutils}/action/complex_schemas.d.ts +0 -0
  251. /package/{dist/testutils → testutils}/action/complex_schemas.js +0 -0
  252. /package/{dist/testutils → testutils}/builder.d.ts +0 -0
  253. /package/{dist/testutils → testutils}/builder.js +0 -0
  254. /package/{dist/testutils → testutils}/context/test_context.d.ts +0 -0
  255. /package/{dist/testutils → testutils}/context/test_context.js +0 -0
  256. /package/{dist/testutils → testutils}/db/fixture.d.ts +0 -0
  257. /package/{dist/testutils → testutils}/db/fixture.js +0 -0
  258. /package/{dist/testutils → testutils}/db/temp_db.d.ts +0 -0
  259. /package/{dist/testutils → testutils}/db/temp_db.js +0 -0
  260. /package/{dist/testutils → testutils}/db/value.d.ts +0 -0
  261. /package/{dist/testutils → testutils}/db/value.js +0 -0
  262. /package/{dist/testutils → testutils}/db_mock.d.ts +0 -0
  263. /package/{dist/testutils → testutils}/db_mock.js +0 -0
  264. /package/{dist/testutils → testutils}/db_time_zone.d.ts +0 -0
  265. /package/{dist/testutils → testutils}/db_time_zone.js +0 -0
  266. /package/{dist/testutils → testutils}/ent-graphql-tests/index.d.ts +0 -0
  267. /package/{dist/testutils → testutils}/ent-graphql-tests/index.js +0 -0
  268. /package/{dist/testutils → testutils}/fake_comms.d.ts +0 -0
  269. /package/{dist/testutils → testutils}/fake_comms.js +0 -0
  270. /package/{dist/testutils → testutils}/fake_data/const.d.ts +0 -0
  271. /package/{dist/testutils → testutils}/fake_data/const.js +0 -0
  272. /package/{dist/testutils → testutils}/fake_data/events_query.d.ts +0 -0
  273. /package/{dist/testutils → testutils}/fake_data/events_query.js +0 -0
  274. /package/{dist/testutils → testutils}/fake_data/fake_contact.d.ts +0 -0
  275. /package/{dist/testutils → testutils}/fake_data/fake_contact.js +0 -0
  276. /package/{dist/testutils → testutils}/fake_data/fake_event.d.ts +0 -0
  277. /package/{dist/testutils → testutils}/fake_data/fake_event.js +0 -0
  278. /package/{dist/testutils → testutils}/fake_data/fake_tag.d.ts +0 -0
  279. /package/{dist/testutils → testutils}/fake_data/fake_tag.js +0 -0
  280. /package/{dist/testutils → testutils}/fake_data/fake_user.d.ts +0 -0
  281. /package/{dist/testutils → testutils}/fake_data/fake_user.js +0 -0
  282. /package/{dist/testutils → testutils}/fake_data/index.d.ts +0 -0
  283. /package/{dist/testutils → testutils}/fake_data/index.js +0 -0
  284. /package/{dist/testutils → testutils}/fake_data/internal.d.ts +0 -0
  285. /package/{dist/testutils → testutils}/fake_data/internal.js +0 -0
  286. /package/{dist/testutils → testutils}/fake_data/tag_query.d.ts +0 -0
  287. /package/{dist/testutils → testutils}/fake_data/tag_query.js +0 -0
  288. /package/{dist/testutils → testutils}/fake_data/test_helpers.d.ts +0 -0
  289. /package/{dist/testutils → testutils}/fake_data/test_helpers.js +0 -0
  290. /package/{dist/testutils → testutils}/fake_data/user_query.d.ts +0 -0
  291. /package/{dist/testutils → testutils}/fake_data/user_query.js +0 -0
  292. /package/{dist/testutils → testutils}/fake_log.d.ts +0 -0
  293. /package/{dist/testutils → testutils}/fake_log.js +0 -0
  294. /package/{dist/testutils → testutils}/mock_date.d.ts +0 -0
  295. /package/{dist/testutils → testutils}/mock_date.js +0 -0
  296. /package/{dist/testutils → testutils}/mock_log.d.ts +0 -0
  297. /package/{dist/testutils → testutils}/mock_log.js +0 -0
  298. /package/{dist/testutils → testutils}/parse_sql.d.ts +0 -0
  299. /package/{dist/testutils → testutils}/parse_sql.js +0 -0
  300. /package/{dist/testutils → testutils}/test_edge_global_schema.d.ts +0 -0
  301. /package/{dist/testutils → testutils}/test_edge_global_schema.js +0 -0
  302. /package/{dist/testutils → testutils}/write.d.ts +0 -0
  303. /package/{dist/testutils → testutils}/write.js +0 -0
  304. /package/{dist/tsc → tsc}/ast.d.ts +0 -0
  305. /package/{dist/tsc → tsc}/ast.js +0 -0
  306. /package/{dist/tsc → tsc}/compilerOptions.d.ts +0 -0
  307. /package/{dist/tsc → tsc}/compilerOptions.js +0 -0
  308. /package/{dist/tsc → tsc}/move_generated.d.ts +0 -0
  309. /package/{dist/tsc → tsc}/move_generated.js +0 -0
  310. /package/{dist/tsc → tsc}/transform.d.ts +0 -0
  311. /package/{dist/tsc → tsc}/transform.js +0 -0
  312. /package/{dist/tsc → tsc}/transform_action.d.ts +0 -0
  313. /package/{dist/tsc → tsc}/transform_action.js +0 -0
  314. /package/{dist/tsc → tsc}/transform_ent.d.ts +0 -0
  315. /package/{dist/tsc → tsc}/transform_ent.js +0 -0
  316. /package/{dist/tsc → tsc}/transform_schema.d.ts +0 -0
  317. /package/{dist/tsc → tsc}/transform_schema.js +0 -0
@@ -1,685 +0,0 @@
1
- import { Data } from "../core/base";
2
-
3
- import {
4
- AST,
5
- Column,
6
- Delete,
7
- Insert_Replace,
8
- Parser,
9
- Select,
10
- Update,
11
- } from "node-sql-parser";
12
- //import { assert } from "console";
13
- interface InsertReplace extends Insert_Replace {
14
- returning: any;
15
- }
16
-
17
- interface CustomUpdate extends Update {
18
- returning: any;
19
- }
20
-
21
- function getTableName(table: any): string {
22
- if (Array.isArray(table)) {
23
- table = table[0];
24
- }
25
- if (typeof table.table === "string") {
26
- if (table.db !== null || table.as !== null) {
27
- throw new Error(
28
- "don't support complex table properties. table db/as not as expected",
29
- );
30
- }
31
- return table.table;
32
- }
33
- throw new Error(`unsupported table format ${table}`);
34
- }
35
-
36
- // returning & partition missing from Insert_Replace types...
37
- function isInsertOrReplace(ast: AST[] | AST): ast is InsertReplace {
38
- const t = ast as Insert_Replace;
39
- return t.type === "replace" || t.type == "insert";
40
- }
41
-
42
- function isSelect(ast: AST[] | AST): ast is Select {
43
- return (ast as Select).type === "select";
44
- }
45
-
46
- function isUpdate(ast: AST[] | AST): ast is CustomUpdate {
47
- return (ast as Update).type === "update";
48
- }
49
-
50
- function isDelete(ast: AST[] | AST): ast is Delete {
51
- return (ast as Delete).type === "delete";
52
- }
53
-
54
- interface ColsInfo {
55
- allCols?: boolean;
56
- count?: boolean;
57
- columns?: string[];
58
- }
59
-
60
- function getColumns(cols: string[] | null | any[] | Column[] | "*"): ColsInfo {
61
- if (!cols) {
62
- return {};
63
- }
64
- if (cols === "*") {
65
- return { allCols: true };
66
- }
67
- let result: string[] = [];
68
- let count: boolean | undefined;
69
- for (let col of cols) {
70
- if (typeof col === "string") {
71
- result.push(col);
72
- } else {
73
- assert(col.type === "expr", "invalid col type");
74
- if (col.as !== "count") {
75
- // count supported as function below...
76
- assert(col.as === null, "column as not-null"); // TODO support this
77
- }
78
- assert(col.expr !== null, "null col expr");
79
- if (col.expr.column) {
80
- // regular column
81
- result.push(col.expr.column);
82
- } else if (col.expr.type === "function") {
83
- assert(
84
- col.expr.name === "count",
85
- "count is the only supported function for now",
86
- );
87
- // TODO count(col) is different. returns non-null or in our case undefined values
88
- assert(col.expr.args.type === "expr_list");
89
- if (col.expr.args.value?.length !== 1) {
90
- throw new Error("only one supported arg");
91
- }
92
- if (col.expr.args.value[0].value === 1) {
93
- count = true;
94
- } else {
95
- throw new Error(
96
- `only count(1) or count(*) supported. count(${col.expr.args.value[0].value}) not supported`,
97
- );
98
- }
99
- } else if (col.expr.type == "aggr_func") {
100
- assert(
101
- col.expr.name === "COUNT",
102
- "count is the only supported function for now",
103
- );
104
- if (col.expr.args?.expr?.type !== "star") {
105
- throw new Error("unsupported count expr");
106
- }
107
- count = true;
108
- } else {
109
- throw new Error("unsupported expr type");
110
- }
111
- }
112
- }
113
- return { columns: result, count };
114
- }
115
-
116
- function getColumnFromRef(col: any): string {
117
- assert(col !== null, "null column ref");
118
- assert(col.type === "column_ref", "column type column_ref");
119
- assert(col.table === null, "column table not null");
120
- return col.column;
121
- }
122
-
123
- function isPreparedStatementValue(val: any) {
124
- if (val.type !== "origin") {
125
- return false;
126
- }
127
- let str = val.value as string;
128
-
129
- // TODO this should work for not-postgres
130
- return str.startsWith("$");
131
- }
132
-
133
- // regex from https://www.regextester.com/97766
134
- const isoStringRegex =
135
- /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z)?$/;
136
-
137
- function formatForReturn(val: any): any {
138
- if (typeof val === "string" && isoStringRegex.test(val)) {
139
- return new Date(val);
140
- }
141
- return val;
142
- }
143
-
144
- // go through the data and return it as needed
145
- export function getDataToReturn(
146
- data: Data,
147
- colNames?: Set<string>,
148
- returningAll?: boolean,
149
- ): Data {
150
- let ret: Data = {};
151
- if (returningAll) {
152
- for (const key in data) {
153
- ret[key] = formatForReturn(data[key]);
154
- }
155
- } else if (colNames) {
156
- for (const key of colNames) {
157
- ret[key] = formatForReturn(data[key]);
158
- }
159
- } else {
160
- throw new Error(`must pass returningAll or colNames`);
161
- }
162
- return ret;
163
- }
164
-
165
- function processBeforeStoring(val: any) {
166
- if (typeof val !== "string") {
167
- return val;
168
- }
169
- // convert postgres lists into lists before storing
170
- if (val[0] === "{" && val[val.length - 1] === "}") {
171
- try {
172
- // valid json, don't convert
173
- JSON.parse(val);
174
- } catch (e) {
175
- return val.substring(1, val.length - 1).split(",");
176
- }
177
- }
178
- return val;
179
- }
180
-
181
- function parseInsertStatement(
182
- ast: InsertReplace,
183
- values: any[], // values passed to query
184
- returningAll: boolean,
185
- ): [string, Data, Data | null] {
186
- const tableName = getTableName(ast.table);
187
- const colInfo = getColumns(ast.columns);
188
-
189
- let data: Data = {};
190
- if (ast.values.length !== 1) {
191
- throw new Error(`unexpected number of values ${ast.values}`);
192
- }
193
- const val = ast.values[0];
194
- for (const val2 of val.value) {
195
- assert(isPreparedStatementValue(val2), "prepared statement");
196
- }
197
- assert(val.value.length == colInfo?.columns?.length, "cols values mismatch");
198
- const columns = colInfo?.columns!;
199
-
200
- // INSERT INTO tableName (cols) VALUES (pos args)
201
- for (let i = 0; i < columns.length; i++) {
202
- let col = columns[i];
203
- data[col] = processBeforeStoring(values[i]);
204
- }
205
-
206
- let returningData: Data | null = null;
207
- if (returningAll) {
208
- returningData = getDataToReturn(data, undefined, true);
209
- } else if (ast.returning) {
210
- assert(ast.returning.type === "returning");
211
- let returningCols = new Set<string>();
212
- for (const col of ast.returning.columns) {
213
- const colName = getColumnFromRef(col);
214
- if (data[colName] === undefined) {
215
- throw new Error(`invalid column ${colName}`);
216
- }
217
- returningCols.add(colName);
218
- }
219
- returningData = getDataToReturn(data, returningCols);
220
- }
221
- return [tableName, data, returningData];
222
- }
223
-
224
- export interface Where {
225
- // if returns true, row should be returned
226
- apply(data: Data): boolean;
227
- }
228
-
229
- export class EqOp {
230
- constructor(private col: string, private value: any) {}
231
-
232
- apply(data: Data): boolean {
233
- return data[this.col] === this.value;
234
- }
235
- }
236
-
237
- export class NotEqOp {
238
- constructor(private col: string, private value: any) {}
239
-
240
- apply(data: Data): boolean {
241
- return data[this.col] !== this.value;
242
- }
243
- }
244
-
245
- export class GreaterOp {
246
- constructor(private col: string, private value: any) {}
247
-
248
- apply(data: Data): boolean {
249
- return data[this.col] > this.value;
250
- }
251
- }
252
-
253
- export class LessOp {
254
- constructor(private col: string, private value: any) {}
255
-
256
- apply(data: Data): boolean {
257
- return data[this.col] < this.value;
258
- }
259
- }
260
-
261
- export class GreaterEqOp {
262
- constructor(private col: string, private value: any) {}
263
-
264
- apply(data: Data): boolean {
265
- return data[this.col] >= this.value;
266
- }
267
- }
268
-
269
- export class LessEqOp {
270
- constructor(private col: string, private value: any) {}
271
-
272
- apply(data: Data): boolean {
273
- return data[this.col] <= this.value;
274
- }
275
- }
276
-
277
- export class InOp {
278
- constructor(private col: string, private values: any[]) {}
279
-
280
- apply(data: Data): boolean {
281
- for (const val of this.values) {
282
- if (data[this.col] == val) {
283
- return true;
284
- }
285
- }
286
- return false;
287
- }
288
- }
289
-
290
- export class AndOp {
291
- constructor(private ops: Where[]) {}
292
-
293
- apply(data: Data): boolean {
294
- return this.ops.every((op) => op.apply(data));
295
- }
296
- }
297
-
298
- export class OrOp {
299
- constructor(private ops: Where[]) {}
300
-
301
- apply(data: Data): boolean {
302
- return this.ops.some((op) => op.apply(data));
303
- }
304
- }
305
-
306
- function getValues(root: any, values: any[]): any[] {
307
- for (const val2 of root) {
308
- assert(isPreparedStatementValue(val2), "prepared statement");
309
- }
310
- // TODO support non-prepared statement vlaues
311
- return values;
312
- }
313
-
314
- const preparedRegex = new RegExp(/\$(\d+)/);
315
- function getValueFromRegex(val: any, values: any[]): any {
316
- // TODO support non-prepared statements
317
- assert(isPreparedStatementValue(val), "prepared statement");
318
-
319
- const result = preparedRegex.exec(val.value);
320
- assert(result !== null);
321
- let pos: number = parseInt(result![1], 10);
322
- return values[pos - 1];
323
- }
324
-
325
- function getOp(where: any, values: any[]): Where {
326
- let col: string;
327
- let value: any;
328
-
329
- switch (where.operator) {
330
- case "=":
331
- col = getColumnFromRef(where.left);
332
- value = getValueFromRegex(where.right, values);
333
- return new EqOp(col, value);
334
-
335
- case ">":
336
- col = getColumnFromRef(where.left);
337
- value = getValueFromRegex(where.right, values);
338
- return new GreaterOp(col, value);
339
-
340
- case "<":
341
- col = getColumnFromRef(where.left);
342
- value = getValueFromRegex(where.right, values);
343
- return new LessOp(col, value);
344
-
345
- case ">=":
346
- col = getColumnFromRef(where.left);
347
- value = getValueFromRegex(where.right, values);
348
- return new GreaterEqOp(col, value);
349
-
350
- case "<=":
351
- col = getColumnFromRef(where.left);
352
- value = getValueFromRegex(where.right, values);
353
- return new LessEqOp(col, value);
354
-
355
- case "IN":
356
- col = getColumnFromRef(where.left);
357
- assert(where.right.type === "expr_list");
358
- // TODo support non prepared statement
359
- let vals = getValues(where.right.value, values);
360
- return new InOp(col, vals);
361
-
362
- case "AND":
363
- return new AndOp([getOp(where.left, values), getOp(where.right, values)]);
364
- case "OR":
365
- return new OrOp([getOp(where.left, values), getOp(where.right, values)]);
366
-
367
- case "IS":
368
- return new EqOp(getColumnFromRef(where.left), where.right?.value);
369
-
370
- case "IS NOT":
371
- return new NotEqOp(getColumnFromRef(where.left), where.right?.value);
372
-
373
- default:
374
- console.debug(where);
375
- throw new Error(`unsupported op ${where.operator}`);
376
- }
377
- }
378
-
379
- function buildWhereStatement(ast: Select, values: any[]): Where | null {
380
- if (ast.where === null) {
381
- return null;
382
- }
383
-
384
- assert(ast.where.type === "binary_expr", "valid where");
385
-
386
- return getOp(ast.where, values);
387
- }
388
-
389
- // existing assert not throwing :(
390
- function assert(val: boolean, msg?: string) {
391
- // console.log(val);
392
- if (!val) {
393
- throw new Error(msg || "assertion error");
394
- }
395
- }
396
-
397
- function parseSelectStatement(
398
- ast: Select,
399
- values: any[], // values passed to query
400
- map: Map<string, Data[]>,
401
- ): Data[] {
402
- // TODO support these as needed
403
- // console.log(ast);
404
- // console.log(ast.groupby === null);
405
- assert(ast.groupby === null, "non-null groupby");
406
- assert(ast.having === null, "non-null having");
407
- assert(ast.with === null, "non-null with");
408
- assert(ast.distinct == null, "non-null distinct");
409
- assert(ast.options === null, "non-null options");
410
-
411
- const tableName = getTableName(ast.from);
412
- const colsInfo = getColumns(ast.columns);
413
- // console.log(tableName, columns, allColumns);
414
-
415
- let limit: number | null = null;
416
- if (ast.limit !== null) {
417
- assert(ast.limit.seperator == "", "ast limit separator not as expected");
418
- assert(ast.limit.value.length == 1, "ast limit not as expected");
419
- assert(ast.limit.value[0].type == "number", "limit type was not 0");
420
- limit = ast.limit.value[0].value;
421
- }
422
- let orderings: [string, string][] = [];
423
- if (ast.orderby !== null) {
424
- for (const order of ast.orderby) {
425
- const col = getColumnFromRef(order.expr);
426
- orderings.push([col, order.type]);
427
- }
428
- // TODO fix this
429
- if (colsInfo.count) {
430
- throw new Error("cannot do count and order by");
431
- }
432
- }
433
- let where = buildWhereStatement(ast, values);
434
-
435
- const list = map.get(tableName) || [];
436
-
437
- let results: Data[] = [];
438
- let limitApplied = false;
439
-
440
- for (const data of list) {
441
- if (where && !where.apply(data)) {
442
- continue;
443
- }
444
- if (colsInfo.allCols) {
445
- results.push(getDataToReturn(data, undefined, true));
446
- } else {
447
- let cols = new Set<string>();
448
- for (const col of colsInfo?.columns || []) {
449
- cols.add(col);
450
- }
451
- results.push(getDataToReturn(data, cols));
452
- }
453
-
454
- // don't apply limit early if there's an ordering or count
455
- if (
456
- orderings.length === 0 &&
457
- !colsInfo.count &&
458
- limit !== null &&
459
- limit == results.length
460
- ) {
461
- limitApplied = true;
462
- break;
463
- }
464
- }
465
-
466
- if (colsInfo.count) {
467
- // if doing count, we just count and return
468
- results = [{ count: results.length }];
469
- }
470
-
471
- for (const order of orderings) {
472
- const [col, type] = order;
473
-
474
- results.sort((a, b) => {
475
- if (type === "ASC") {
476
- if (a[col] > b[col]) {
477
- return 1;
478
- }
479
- if (a[col] < b[col]) {
480
- return -1;
481
- }
482
- return 0;
483
- }
484
- // DESC
485
- if (a[col] > b[col]) {
486
- return -1;
487
- }
488
- if (a[col] < b[col]) {
489
- return 1;
490
- }
491
- return 0;
492
- });
493
- }
494
-
495
- // apply limit after if not applied e.g. there was an ordering or no where clause
496
- if (limit !== null && !limitApplied) {
497
- results = results.slice(0, limit);
498
- }
499
-
500
- return results;
501
- }
502
-
503
- function parseUpdateStatement(
504
- ast: CustomUpdate,
505
- values: any[],
506
- map: Map<string, Data[]>,
507
- returningAll: boolean,
508
- ) {
509
- const tableName = getTableName(ast.table);
510
- let op: Where | null = null;
511
- if (ast.where) {
512
- op = getOp(ast.where, values);
513
- }
514
- assert(Array.isArray(ast.set));
515
- let overwrite: Data = {};
516
- for (const set of ast.set) {
517
- let col = set.column;
518
- let value = getValueFromRegex(set.value, values);
519
- overwrite[col] = processBeforeStoring(value);
520
- }
521
-
522
- let columns = new Set<string>();
523
- if (ast.returning) {
524
- assert(ast.returning.type === "returning");
525
- for (const col of ast.returning.columns) {
526
- const colName = getColumnFromRef(col);
527
- columns.add(colName);
528
- }
529
- }
530
-
531
- // overwrite the row
532
- let list = map.get(tableName) || [];
533
- let returnedRows: Data[] = [];
534
- for (let i = 0; i < list.length; i++) {
535
- let data = list[i];
536
-
537
- if (op && !op.apply(data)) {
538
- continue;
539
- }
540
-
541
- for (const key in overwrite) {
542
- data[key] = overwrite[key];
543
- }
544
- list[i] = data;
545
-
546
- if (returningAll) {
547
- returnedRows.push(getDataToReturn(data, undefined, true));
548
- } else if (columns.size) {
549
- for (const col of columns) {
550
- if (data[col] === undefined) {
551
- throw new Error(`invalid column ${col}`);
552
- }
553
- }
554
- returnedRows.push(getDataToReturn(data, columns));
555
- }
556
- }
557
- map.set(tableName, list);
558
- return returnedRows;
559
- }
560
-
561
- function parseDeleteStatement(
562
- ast: Delete,
563
- values: any[],
564
- map: Map<string, Data[]>,
565
- ) {
566
- const tableName = getTableName(ast.table);
567
- let list = map.get(tableName) || [];
568
- if (!ast.where) {
569
- map.set(tableName, []);
570
- return;
571
- }
572
-
573
- const op = getOp(ast.where, values);
574
-
575
- for (let i = 0; i < list.length; i++) {
576
- let data = list[i];
577
- if (op.apply(data)) {
578
- list.splice(i, 1);
579
- // to fix indices
580
- i--;
581
- map.set(tableName, list);
582
- }
583
- }
584
- }
585
-
586
- export interface queryResult {
587
- rows: Data[];
588
- rowCount: number;
589
- oid: number;
590
- fields: any[];
591
- command: string;
592
- }
593
-
594
- function newQueryResult(partial: Partial<queryResult>): queryResult {
595
- return {
596
- rows: [],
597
- rowCount: 0,
598
- oid: 0,
599
- fields: [],
600
- command: "",
601
- ...partial,
602
- };
603
- }
604
-
605
- function getAst(query: string): [AST | AST[] | undefined, boolean] {
606
- // console.log(query, values);
607
- let idx = query.indexOf("RETURNING *");
608
- let returningAll: boolean = false;
609
- if (idx !== -1) {
610
- query = query.substr(0, idx);
611
- returningAll = true;
612
- }
613
-
614
- let idx2 = query.indexOf("ON CONFLICT");
615
- if (idx2 !== -1) {
616
- // console.log(query);
617
- query = query.substr(0, idx2);
618
- // we don't care about on conflict since we just write regardless of pkey
619
- }
620
-
621
- // nothing to do here
622
- switch (query) {
623
- case "BEGIN":
624
- case "ROLLBACK":
625
- case "COMMIT":
626
- return [undefined, false];
627
- }
628
-
629
- let ast: AST | AST[];
630
- try {
631
- const p = new Parser();
632
- const parsed = p.parse(query, {
633
- database: "postgresql",
634
- });
635
- ast = parsed.ast;
636
- } catch (err) {
637
- console.trace();
638
- console.log(query, err);
639
- throw err;
640
- }
641
- return [ast!, returningAll];
642
- }
643
-
644
- export function performQuery(
645
- query: string,
646
- values: any[],
647
- map: Map<string, Data[]>,
648
- ): queryResult | undefined {
649
- const [ast, returningAll] = getAst(query);
650
- if (!ast) {
651
- return;
652
- }
653
-
654
- if (isInsertOrReplace(ast)) {
655
- const [tableName, data, returningData] = parseInsertStatement(
656
- ast,
657
- values,
658
- returningAll,
659
- );
660
- let list = map.get(tableName) || [];
661
- list.push(data);
662
- map.set(tableName, list);
663
- if (returningData !== null) {
664
- return newQueryResult({
665
- rows: [returningData],
666
- rowCount: 1,
667
- });
668
- }
669
- } else if (isSelect(ast)) {
670
- const results = parseSelectStatement(ast, values, map);
671
-
672
- return newQueryResult({
673
- rows: results,
674
- rowCount: results.length,
675
- });
676
- } else if (isUpdate(ast)) {
677
- const returning = parseUpdateStatement(ast, values, map, returningAll);
678
- return newQueryResult({
679
- rows: returning,
680
- rowCount: returning.length,
681
- });
682
- } else if (isDelete(ast)) {
683
- parseDeleteStatement(ast, values, map);
684
- }
685
- }