@snowtop/ent 0.1.0-alpha160-test5 → 0.1.0-alpha160-test6

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/dist/package.json +64 -0
  2. package/{scripts → dist/scripts}/custom_compiler.js +0 -0
  3. package/{scripts → dist/scripts}/custom_graphql.js +0 -0
  4. package/package.json +47 -5
  5. package/src/action/action.ts +330 -0
  6. package/src/action/executor.ts +453 -0
  7. package/src/action/experimental_action.ts +277 -0
  8. package/src/action/index.ts +31 -0
  9. package/src/action/operations.ts +967 -0
  10. package/src/action/orchestrator.ts +1527 -0
  11. package/src/action/privacy.ts +37 -0
  12. package/src/action/relative_value.ts +242 -0
  13. package/src/action/transaction.ts +38 -0
  14. package/src/auth/auth.ts +77 -0
  15. package/src/auth/index.ts +8 -0
  16. package/src/core/base.ts +367 -0
  17. package/src/core/clause.ts +1065 -0
  18. package/src/core/config.ts +219 -0
  19. package/src/core/const.ts +5 -0
  20. package/src/core/context.ts +135 -0
  21. package/src/core/convert.ts +106 -0
  22. package/src/core/date.ts +23 -0
  23. package/src/core/db.ts +498 -0
  24. package/src/core/ent.ts +1740 -0
  25. package/src/core/global_schema.ts +49 -0
  26. package/src/core/loaders/assoc_count_loader.ts +99 -0
  27. package/src/core/loaders/assoc_edge_loader.ts +250 -0
  28. package/src/core/loaders/index.ts +12 -0
  29. package/src/core/loaders/loader.ts +66 -0
  30. package/src/core/loaders/object_loader.ts +489 -0
  31. package/src/core/loaders/query_loader.ts +314 -0
  32. package/src/core/loaders/raw_count_loader.ts +175 -0
  33. package/src/core/logger.ts +49 -0
  34. package/src/core/privacy.ts +660 -0
  35. package/src/core/query/assoc_query.ts +240 -0
  36. package/src/core/query/custom_clause_query.ts +174 -0
  37. package/src/core/query/custom_query.ts +302 -0
  38. package/src/core/query/index.ts +9 -0
  39. package/src/core/query/query.ts +674 -0
  40. package/src/core/query_impl.ts +32 -0
  41. package/src/core/viewer.ts +52 -0
  42. package/src/ent.code-workspace +73 -0
  43. package/src/graphql/builtins/connection.ts +25 -0
  44. package/src/graphql/builtins/edge.ts +16 -0
  45. package/src/graphql/builtins/node.ts +12 -0
  46. package/src/graphql/graphql.ts +891 -0
  47. package/src/graphql/graphql_field_helpers.ts +221 -0
  48. package/src/graphql/index.ts +42 -0
  49. package/src/graphql/mutations/union.ts +39 -0
  50. package/src/graphql/node_resolver.ts +122 -0
  51. package/src/graphql/query/connection_type.ts +113 -0
  52. package/src/graphql/query/edge_connection.ts +171 -0
  53. package/src/graphql/query/page_info.ts +34 -0
  54. package/src/graphql/query/shared_edge_connection.ts +287 -0
  55. package/src/graphql/scalars/orderby_direction.ts +13 -0
  56. package/src/graphql/scalars/time.ts +38 -0
  57. package/src/imports/dataz/example1/_auth.ts +51 -0
  58. package/src/imports/dataz/example1/_viewer.ts +35 -0
  59. package/src/imports/index.ts +213 -0
  60. package/src/index.ts +145 -0
  61. package/src/parse_schema/parse.ts +585 -0
  62. package/src/schema/base_schema.ts +224 -0
  63. package/src/schema/field.ts +1087 -0
  64. package/src/schema/index.ts +53 -0
  65. package/src/schema/json_field.ts +94 -0
  66. package/src/schema/schema.ts +1028 -0
  67. package/src/schema/struct_field.ts +234 -0
  68. package/src/schema/union_field.ts +105 -0
  69. package/src/scripts/custom_compiler.ts +331 -0
  70. package/src/scripts/custom_graphql.ts +550 -0
  71. package/src/scripts/migrate_v0.1.ts +41 -0
  72. package/src/scripts/move_types.ts +131 -0
  73. package/src/scripts/read_schema.ts +67 -0
  74. package/src/setupPackage.js +42 -0
  75. package/src/testutils/action/complex_schemas.ts +517 -0
  76. package/src/testutils/builder.ts +422 -0
  77. package/src/testutils/context/test_context.ts +25 -0
  78. package/src/testutils/db/fixture.ts +32 -0
  79. package/src/testutils/db/temp_db.ts +941 -0
  80. package/src/testutils/db/value.ts +294 -0
  81. package/src/testutils/db_mock.ts +351 -0
  82. package/src/testutils/db_time_zone.ts +40 -0
  83. package/src/testutils/ent-graphql-tests/index.ts +653 -0
  84. package/src/testutils/fake_comms.ts +50 -0
  85. package/src/testutils/fake_data/const.ts +64 -0
  86. package/src/testutils/fake_data/events_query.ts +145 -0
  87. package/src/testutils/fake_data/fake_contact.ts +150 -0
  88. package/src/testutils/fake_data/fake_event.ts +150 -0
  89. package/src/testutils/fake_data/fake_tag.ts +139 -0
  90. package/src/testutils/fake_data/fake_user.ts +232 -0
  91. package/src/testutils/fake_data/index.ts +1 -0
  92. package/src/testutils/fake_data/internal.ts +8 -0
  93. package/src/testutils/fake_data/tag_query.ts +56 -0
  94. package/src/testutils/fake_data/test_helpers.ts +388 -0
  95. package/src/testutils/fake_data/user_query.ts +524 -0
  96. package/src/testutils/fake_log.ts +52 -0
  97. package/src/testutils/mock_date.ts +10 -0
  98. package/src/testutils/mock_log.ts +39 -0
  99. package/src/testutils/parse_sql.ts +685 -0
  100. package/src/testutils/test_edge_global_schema.ts +49 -0
  101. package/src/testutils/write.ts +70 -0
  102. package/src/tsc/ast.ts +351 -0
  103. package/src/tsc/compilerOptions.ts +85 -0
  104. package/src/tsc/move_generated.ts +191 -0
  105. package/src/tsc/transform.ts +226 -0
  106. package/src/tsc/transform_action.ts +224 -0
  107. package/src/tsc/transform_ent.ts +66 -0
  108. package/src/tsc/transform_schema.ts +546 -0
  109. package/tsconfig.json +20 -0
  110. package/core/query/shared_assoc_test.d.ts +0 -2
  111. package/core/query/shared_assoc_test.js +0 -804
  112. package/core/query/shared_test.d.ts +0 -21
  113. package/core/query/shared_test.js +0 -736
  114. package/graphql/query/shared_assoc_test.d.ts +0 -1
  115. package/graphql/query/shared_assoc_test.js +0 -203
  116. /package/{action → dist/action}/action.d.ts +0 -0
  117. /package/{action → dist/action}/action.js +0 -0
  118. /package/{action → dist/action}/executor.d.ts +0 -0
  119. /package/{action → dist/action}/executor.js +0 -0
  120. /package/{action → dist/action}/experimental_action.d.ts +0 -0
  121. /package/{action → dist/action}/experimental_action.js +0 -0
  122. /package/{action → dist/action}/index.d.ts +0 -0
  123. /package/{action → dist/action}/index.js +0 -0
  124. /package/{action → dist/action}/operations.d.ts +0 -0
  125. /package/{action → dist/action}/operations.js +0 -0
  126. /package/{action → dist/action}/orchestrator.d.ts +0 -0
  127. /package/{action → dist/action}/orchestrator.js +0 -0
  128. /package/{action → dist/action}/privacy.d.ts +0 -0
  129. /package/{action → dist/action}/privacy.js +0 -0
  130. /package/{action → dist/action}/relative_value.d.ts +0 -0
  131. /package/{action → dist/action}/relative_value.js +0 -0
  132. /package/{action → dist/action}/transaction.d.ts +0 -0
  133. /package/{action → dist/action}/transaction.js +0 -0
  134. /package/{auth → dist/auth}/auth.d.ts +0 -0
  135. /package/{auth → dist/auth}/auth.js +0 -0
  136. /package/{auth → dist/auth}/index.d.ts +0 -0
  137. /package/{auth → dist/auth}/index.js +0 -0
  138. /package/{core → dist/core}/base.d.ts +0 -0
  139. /package/{core → dist/core}/base.js +0 -0
  140. /package/{core → dist/core}/clause.d.ts +0 -0
  141. /package/{core → dist/core}/clause.js +0 -0
  142. /package/{core → dist/core}/config.d.ts +0 -0
  143. /package/{core → dist/core}/config.js +0 -0
  144. /package/{core → dist/core}/const.d.ts +0 -0
  145. /package/{core → dist/core}/const.js +0 -0
  146. /package/{core → dist/core}/context.d.ts +0 -0
  147. /package/{core → dist/core}/context.js +0 -0
  148. /package/{core → dist/core}/convert.d.ts +0 -0
  149. /package/{core → dist/core}/convert.js +0 -0
  150. /package/{core → dist/core}/date.d.ts +0 -0
  151. /package/{core → dist/core}/date.js +0 -0
  152. /package/{core → dist/core}/db.d.ts +0 -0
  153. /package/{core → dist/core}/db.js +0 -0
  154. /package/{core → dist/core}/ent.d.ts +0 -0
  155. /package/{core → dist/core}/ent.js +0 -0
  156. /package/{core → dist/core}/global_schema.d.ts +0 -0
  157. /package/{core → dist/core}/global_schema.js +0 -0
  158. /package/{core → dist/core}/loaders/assoc_count_loader.d.ts +0 -0
  159. /package/{core → dist/core}/loaders/assoc_count_loader.js +0 -0
  160. /package/{core → dist/core}/loaders/assoc_edge_loader.d.ts +0 -0
  161. /package/{core → dist/core}/loaders/assoc_edge_loader.js +0 -0
  162. /package/{core → dist/core}/loaders/index.d.ts +0 -0
  163. /package/{core → dist/core}/loaders/index.js +0 -0
  164. /package/{core → dist/core}/loaders/loader.d.ts +0 -0
  165. /package/{core → dist/core}/loaders/loader.js +0 -0
  166. /package/{core → dist/core}/loaders/object_loader.d.ts +0 -0
  167. /package/{core → dist/core}/loaders/object_loader.js +0 -0
  168. /package/{core → dist/core}/loaders/query_loader.d.ts +0 -0
  169. /package/{core → dist/core}/loaders/query_loader.js +0 -0
  170. /package/{core → dist/core}/loaders/raw_count_loader.d.ts +0 -0
  171. /package/{core → dist/core}/loaders/raw_count_loader.js +0 -0
  172. /package/{core → dist/core}/logger.d.ts +0 -0
  173. /package/{core → dist/core}/logger.js +0 -0
  174. /package/{core → dist/core}/privacy.d.ts +0 -0
  175. /package/{core → dist/core}/privacy.js +0 -0
  176. /package/{core → dist/core}/query/assoc_query.d.ts +0 -0
  177. /package/{core → dist/core}/query/assoc_query.js +0 -0
  178. /package/{core → dist/core}/query/custom_clause_query.d.ts +0 -0
  179. /package/{core → dist/core}/query/custom_clause_query.js +0 -0
  180. /package/{core → dist/core}/query/custom_query.d.ts +0 -0
  181. /package/{core → dist/core}/query/custom_query.js +0 -0
  182. /package/{core → dist/core}/query/index.d.ts +0 -0
  183. /package/{core → dist/core}/query/index.js +0 -0
  184. /package/{core → dist/core}/query/query.d.ts +0 -0
  185. /package/{core → dist/core}/query/query.js +0 -0
  186. /package/{core → dist/core}/query_impl.d.ts +0 -0
  187. /package/{core → dist/core}/query_impl.js +0 -0
  188. /package/{core → dist/core}/viewer.d.ts +0 -0
  189. /package/{core → dist/core}/viewer.js +0 -0
  190. /package/{graphql → dist/graphql}/builtins/connection.d.ts +0 -0
  191. /package/{graphql → dist/graphql}/builtins/connection.js +0 -0
  192. /package/{graphql → dist/graphql}/builtins/edge.d.ts +0 -0
  193. /package/{graphql → dist/graphql}/builtins/edge.js +0 -0
  194. /package/{graphql → dist/graphql}/builtins/node.d.ts +0 -0
  195. /package/{graphql → dist/graphql}/builtins/node.js +0 -0
  196. /package/{graphql → dist/graphql}/graphql.d.ts +0 -0
  197. /package/{graphql → dist/graphql}/graphql.js +0 -0
  198. /package/{graphql → dist/graphql}/graphql_field_helpers.d.ts +0 -0
  199. /package/{graphql → dist/graphql}/graphql_field_helpers.js +0 -0
  200. /package/{graphql → dist/graphql}/index.d.ts +0 -0
  201. /package/{graphql → dist/graphql}/index.js +0 -0
  202. /package/{graphql → dist/graphql}/mutations/union.d.ts +0 -0
  203. /package/{graphql → dist/graphql}/mutations/union.js +0 -0
  204. /package/{graphql → dist/graphql}/node_resolver.d.ts +0 -0
  205. /package/{graphql → dist/graphql}/node_resolver.js +0 -0
  206. /package/{graphql → dist/graphql}/query/connection_type.d.ts +0 -0
  207. /package/{graphql → dist/graphql}/query/connection_type.js +0 -0
  208. /package/{graphql → dist/graphql}/query/edge_connection.d.ts +0 -0
  209. /package/{graphql → dist/graphql}/query/edge_connection.js +0 -0
  210. /package/{graphql → dist/graphql}/query/page_info.d.ts +0 -0
  211. /package/{graphql → dist/graphql}/query/page_info.js +0 -0
  212. /package/{graphql → dist/graphql}/query/shared_edge_connection.d.ts +0 -0
  213. /package/{graphql → dist/graphql}/query/shared_edge_connection.js +0 -0
  214. /package/{graphql → dist/graphql}/scalars/orderby_direction.d.ts +0 -0
  215. /package/{graphql → dist/graphql}/scalars/orderby_direction.js +0 -0
  216. /package/{graphql → dist/graphql}/scalars/time.d.ts +0 -0
  217. /package/{graphql → dist/graphql}/scalars/time.js +0 -0
  218. /package/{imports → dist/imports}/dataz/example1/_auth.d.ts +0 -0
  219. /package/{imports → dist/imports}/dataz/example1/_auth.js +0 -0
  220. /package/{imports → dist/imports}/dataz/example1/_viewer.d.ts +0 -0
  221. /package/{imports → dist/imports}/dataz/example1/_viewer.js +0 -0
  222. /package/{imports → dist/imports}/index.d.ts +0 -0
  223. /package/{imports → dist/imports}/index.js +0 -0
  224. /package/{index.d.ts → dist/index.d.ts} +0 -0
  225. /package/{index.js → dist/index.js} +0 -0
  226. /package/{parse_schema → dist/parse_schema}/parse.d.ts +0 -0
  227. /package/{parse_schema → dist/parse_schema}/parse.js +0 -0
  228. /package/{schema → dist/schema}/base_schema.d.ts +0 -0
  229. /package/{schema → dist/schema}/base_schema.js +0 -0
  230. /package/{schema → dist/schema}/field.d.ts +0 -0
  231. /package/{schema → dist/schema}/field.js +0 -0
  232. /package/{schema → dist/schema}/index.d.ts +0 -0
  233. /package/{schema → dist/schema}/index.js +0 -0
  234. /package/{schema → dist/schema}/json_field.d.ts +0 -0
  235. /package/{schema → dist/schema}/json_field.js +0 -0
  236. /package/{schema → dist/schema}/schema.d.ts +0 -0
  237. /package/{schema → dist/schema}/schema.js +0 -0
  238. /package/{schema → dist/schema}/struct_field.d.ts +0 -0
  239. /package/{schema → dist/schema}/struct_field.js +0 -0
  240. /package/{schema → dist/schema}/union_field.d.ts +0 -0
  241. /package/{schema → dist/schema}/union_field.js +0 -0
  242. /package/{scripts → dist/scripts}/custom_compiler.d.ts +0 -0
  243. /package/{scripts → dist/scripts}/custom_graphql.d.ts +0 -0
  244. /package/{scripts → dist/scripts}/migrate_v0.1.d.ts +0 -0
  245. /package/{scripts → dist/scripts}/migrate_v0.1.js +0 -0
  246. /package/{scripts → dist/scripts}/move_types.d.ts +0 -0
  247. /package/{scripts → dist/scripts}/move_types.js +0 -0
  248. /package/{scripts → dist/scripts}/read_schema.d.ts +0 -0
  249. /package/{scripts → dist/scripts}/read_schema.js +0 -0
  250. /package/{testutils → dist/testutils}/action/complex_schemas.d.ts +0 -0
  251. /package/{testutils → dist/testutils}/action/complex_schemas.js +0 -0
  252. /package/{testutils → dist/testutils}/builder.d.ts +0 -0
  253. /package/{testutils → dist/testutils}/builder.js +0 -0
  254. /package/{testutils → dist/testutils}/context/test_context.d.ts +0 -0
  255. /package/{testutils → dist/testutils}/context/test_context.js +0 -0
  256. /package/{testutils → dist/testutils}/db/fixture.d.ts +0 -0
  257. /package/{testutils → dist/testutils}/db/fixture.js +0 -0
  258. /package/{testutils → dist/testutils}/db/temp_db.d.ts +0 -0
  259. /package/{testutils → dist/testutils}/db/temp_db.js +0 -0
  260. /package/{testutils → dist/testutils}/db/value.d.ts +0 -0
  261. /package/{testutils → dist/testutils}/db/value.js +0 -0
  262. /package/{testutils → dist/testutils}/db_mock.d.ts +0 -0
  263. /package/{testutils → dist/testutils}/db_mock.js +0 -0
  264. /package/{testutils → dist/testutils}/db_time_zone.d.ts +0 -0
  265. /package/{testutils → dist/testutils}/db_time_zone.js +0 -0
  266. /package/{testutils → dist/testutils}/ent-graphql-tests/index.d.ts +0 -0
  267. /package/{testutils → dist/testutils}/ent-graphql-tests/index.js +0 -0
  268. /package/{testutils → dist/testutils}/fake_comms.d.ts +0 -0
  269. /package/{testutils → dist/testutils}/fake_comms.js +0 -0
  270. /package/{testutils → dist/testutils}/fake_data/const.d.ts +0 -0
  271. /package/{testutils → dist/testutils}/fake_data/const.js +0 -0
  272. /package/{testutils → dist/testutils}/fake_data/events_query.d.ts +0 -0
  273. /package/{testutils → dist/testutils}/fake_data/events_query.js +0 -0
  274. /package/{testutils → dist/testutils}/fake_data/fake_contact.d.ts +0 -0
  275. /package/{testutils → dist/testutils}/fake_data/fake_contact.js +0 -0
  276. /package/{testutils → dist/testutils}/fake_data/fake_event.d.ts +0 -0
  277. /package/{testutils → dist/testutils}/fake_data/fake_event.js +0 -0
  278. /package/{testutils → dist/testutils}/fake_data/fake_tag.d.ts +0 -0
  279. /package/{testutils → dist/testutils}/fake_data/fake_tag.js +0 -0
  280. /package/{testutils → dist/testutils}/fake_data/fake_user.d.ts +0 -0
  281. /package/{testutils → dist/testutils}/fake_data/fake_user.js +0 -0
  282. /package/{testutils → dist/testutils}/fake_data/index.d.ts +0 -0
  283. /package/{testutils → dist/testutils}/fake_data/index.js +0 -0
  284. /package/{testutils → dist/testutils}/fake_data/internal.d.ts +0 -0
  285. /package/{testutils → dist/testutils}/fake_data/internal.js +0 -0
  286. /package/{testutils → dist/testutils}/fake_data/tag_query.d.ts +0 -0
  287. /package/{testutils → dist/testutils}/fake_data/tag_query.js +0 -0
  288. /package/{testutils → dist/testutils}/fake_data/test_helpers.d.ts +0 -0
  289. /package/{testutils → dist/testutils}/fake_data/test_helpers.js +0 -0
  290. /package/{testutils → dist/testutils}/fake_data/user_query.d.ts +0 -0
  291. /package/{testutils → dist/testutils}/fake_data/user_query.js +0 -0
  292. /package/{testutils → dist/testutils}/fake_log.d.ts +0 -0
  293. /package/{testutils → dist/testutils}/fake_log.js +0 -0
  294. /package/{testutils → dist/testutils}/mock_date.d.ts +0 -0
  295. /package/{testutils → dist/testutils}/mock_date.js +0 -0
  296. /package/{testutils → dist/testutils}/mock_log.d.ts +0 -0
  297. /package/{testutils → dist/testutils}/mock_log.js +0 -0
  298. /package/{testutils → dist/testutils}/parse_sql.d.ts +0 -0
  299. /package/{testutils → dist/testutils}/parse_sql.js +0 -0
  300. /package/{testutils → dist/testutils}/test_edge_global_schema.d.ts +0 -0
  301. /package/{testutils → dist/testutils}/test_edge_global_schema.js +0 -0
  302. /package/{testutils → dist/testutils}/write.d.ts +0 -0
  303. /package/{testutils → dist/testutils}/write.js +0 -0
  304. /package/{tsc → dist/tsc}/ast.d.ts +0 -0
  305. /package/{tsc → dist/tsc}/ast.js +0 -0
  306. /package/{tsc → dist/tsc}/compilerOptions.d.ts +0 -0
  307. /package/{tsc → dist/tsc}/compilerOptions.js +0 -0
  308. /package/{tsc → dist/tsc}/move_generated.d.ts +0 -0
  309. /package/{tsc → dist/tsc}/move_generated.js +0 -0
  310. /package/{tsc → dist/tsc}/transform.d.ts +0 -0
  311. /package/{tsc → dist/tsc}/transform.js +0 -0
  312. /package/{tsc → dist/tsc}/transform_action.d.ts +0 -0
  313. /package/{tsc → dist/tsc}/transform_action.js +0 -0
  314. /package/{tsc → dist/tsc}/transform_ent.d.ts +0 -0
  315. /package/{tsc → dist/tsc}/transform_ent.js +0 -0
  316. /package/{tsc → dist/tsc}/transform_schema.d.ts +0 -0
  317. /package/{tsc → dist/tsc}/transform_schema.js +0 -0
@@ -0,0 +1,294 @@
1
+ import { v4 } from "uuid";
2
+ import { DBType, Field, Schema, Type } from "../../schema";
3
+ import {
4
+ DateType,
5
+ TimeType,
6
+ TimetzType,
7
+ TimestampType,
8
+ TimestamptzType,
9
+ JSONBType,
10
+ JSONType,
11
+ StringType,
12
+ } from "../../schema";
13
+
14
+ function random(): string {
15
+ return Math.random().toString(16).substring(2);
16
+ }
17
+
18
+ export function randomEmail(domain?: string): string {
19
+ domain = domain || "email.com";
20
+
21
+ return `test+${random()}@${domain}`;
22
+ }
23
+
24
+ function randomPhoneNumber(): string {
25
+ return `+1${Math.random().toString(10).substring(2, 11)}`;
26
+ }
27
+
28
+ function coinFlip() {
29
+ return Math.floor(Math.random() * 10) >= 5;
30
+ }
31
+
32
+ function specialType(typ: Type, col: string) {
33
+ let list = m.get(typ.dbType);
34
+ if (list?.length) {
35
+ for (const l of list) {
36
+ let regex: RegExp[] = [];
37
+ if (Array.isArray(l.regex)) {
38
+ regex = l.regex;
39
+ } else {
40
+ regex = [l.regex];
41
+ }
42
+
43
+ for (const r of regex) {
44
+ if (r.test(col)) {
45
+ return l.newValue();
46
+ }
47
+ }
48
+ }
49
+ }
50
+ return undefined;
51
+ }
52
+
53
+ interface Info {
54
+ schema: Schema;
55
+ }
56
+
57
+ export function getDefaultValue(
58
+ f: Field,
59
+ col: string,
60
+ infos?: Map<string, Info>,
61
+ ): any {
62
+ if (f.defaultValueOnCreate) {
63
+ // @ts-ignore
64
+ return f.defaultValueOnCreate();
65
+ }
66
+ // half the time, return null for nullable
67
+ if (f.nullable && coinFlip()) {
68
+ return null;
69
+ }
70
+
71
+ const specialVal = specialType(f.type, col);
72
+ if (specialVal !== undefined) {
73
+ return specialVal;
74
+ }
75
+
76
+ return getValueForType(f.type, f, infos);
77
+ }
78
+
79
+ function getValueForType(typ: Type, f: Field, infos?: Map<string, Info>) {
80
+ switch (typ.dbType) {
81
+ case DBType.UUID:
82
+ return v4();
83
+ case DBType.Boolean:
84
+ return coinFlip();
85
+ case DBType.Date:
86
+ return DateType().format(new Date());
87
+ case DBType.Time:
88
+ return TimeType().format(new Date());
89
+ case DBType.Timetz:
90
+ return TimetzType().format(new Date());
91
+ case DBType.Timestamp:
92
+ return TimestampType().format(new Date());
93
+ case DBType.Timestamptz:
94
+ return TimestamptzType().format(new Date());
95
+ case DBType.String:
96
+ return random();
97
+ case DBType.Int:
98
+ return Math.floor(Math.random() * 100000000);
99
+ case DBType.Float:
100
+ return Math.random() * 100000000;
101
+ case DBType.Enum:
102
+ case DBType.StringEnum:
103
+ if (typ.values) {
104
+ const idx = Math.floor(Math.random() * typ.values.length);
105
+ return typ.values[idx];
106
+ }
107
+ if (typ.enumMap) {
108
+ const vals = Object.values(typ.enumMap);
109
+ const idx = Math.floor(Math.random() * vals.length);
110
+ return vals[idx];
111
+ }
112
+ if (f.foreignKey) {
113
+ const schema = f.foreignKey.schema;
114
+ const col = f.foreignKey.column;
115
+ if (!infos) {
116
+ throw new Error(`infos required for enum with foreignKey`);
117
+ }
118
+ const info = infos.get(schema);
119
+ if (!info) {
120
+ throw new Error(`couldn't load data for schema ${schema}`);
121
+ }
122
+ if (!info.schema.dbRows) {
123
+ throw new Error(`no dbRows for schema ${schema}`);
124
+ }
125
+ const idx = Math.floor(Math.random() * info.schema.dbRows.length);
126
+ return info.schema.dbRows[idx][col];
127
+ }
128
+ throw new Error("TODO: enum without values not currently supported");
129
+
130
+ case DBType.IntEnum:
131
+ const vals = Object.values(typ.intEnumMap!);
132
+ const idx = Math.floor(Math.random() * vals.length);
133
+ return vals[idx];
134
+
135
+ case DBType.BigInt:
136
+ return BigInt(Math.floor(Math.random() * 100000000));
137
+ case DBType.JSONB:
138
+ // type as list
139
+ if (typ.listElemType?.dbType === DBType.JSONB) {
140
+ const values: any[] = [];
141
+
142
+ for (let i = 0; i < 10; i++) {
143
+ values.push(getValueForType(typ.listElemType, f, infos));
144
+ }
145
+ if (!f.format) {
146
+ throw new Error("invalid format");
147
+ }
148
+ return f.format(values);
149
+ }
150
+ return JSONBType().format({});
151
+ case DBType.JSON:
152
+ return JSONType().format({});
153
+ case DBType.List:
154
+ // just do 10
155
+ const values: any[] = [];
156
+
157
+ for (let i = 0; i < 10; i++) {
158
+ values.push(
159
+ getValueForType(f.type.listElemType!, f.__getElemField(), infos),
160
+ );
161
+ }
162
+ if (!f.format) {
163
+ throw new Error("invalid format");
164
+ }
165
+ return f.format(values);
166
+ default:
167
+ throw new Error(`unsupported type ${typ.dbType}`);
168
+ }
169
+ }
170
+
171
+ interface commonType {
172
+ dbType: DBType;
173
+ newValue: () => any;
174
+ regex: [RegExp] | RegExp;
175
+ }
176
+
177
+ const emailType = {
178
+ dbType: DBType.String,
179
+ newValue: () => {
180
+ return StringType().format(randomEmail().toLowerCase());
181
+ },
182
+ regex: /^email(_address)|_email$/,
183
+ };
184
+
185
+ const phoneType = {
186
+ dbType: DBType.String,
187
+ newValue: () => {
188
+ return randomPhoneNumber();
189
+ },
190
+ regex: /^phone(_number)?|_phone$|_phone_number$/,
191
+ };
192
+
193
+ const passwordType = {
194
+ dbType: DBType.String,
195
+ newValue: () => {
196
+ // we don't use password type because when we're generating so many rows, it's too slow...
197
+ return random();
198
+ },
199
+ regex: /^password/,
200
+ };
201
+
202
+ const firstNames = [
203
+ "Daenerys",
204
+ "Jon",
205
+ "Arya",
206
+ "Sansa",
207
+ "Eddard",
208
+ "Khal",
209
+ "Robb",
210
+ "Joffrey",
211
+ "Ramsay",
212
+ "Cersei",
213
+ "Bolton",
214
+ "Oberyn",
215
+ "Jojen",
216
+ "Petyr",
217
+ "Brienne",
218
+ "Ygritte",
219
+ "Missandei",
220
+ "Shae",
221
+ "Sandor",
222
+ "Theon",
223
+ "Catelyn",
224
+ "Gilly",
225
+ "Samwell",
226
+ "Jaime",
227
+ "Stannis",
228
+ "Tyene",
229
+ "Obara",
230
+ "Nymeria",
231
+ "Elia",
232
+ "Ellaria",
233
+ "Myrcella",
234
+ "Hodor",
235
+ "Osha",
236
+ "Meera",
237
+ "Davos",
238
+ "Gendry",
239
+ ];
240
+
241
+ const lastNames = [
242
+ "Stark",
243
+ "Targaryen",
244
+ "Lannister",
245
+ "Drogo",
246
+ "Baratheon",
247
+ "Reed",
248
+ "Martell",
249
+ "Tyrell",
250
+ "Clegane",
251
+ "Baelish",
252
+ "Greyjoy",
253
+ "Tarly",
254
+ "Sand",
255
+ "Snow",
256
+ "Bolton",
257
+ "Frey",
258
+ "Tarth",
259
+ "Payne",
260
+ "Seaworth",
261
+ ];
262
+
263
+ const firstNameType = {
264
+ dbType: DBType.String,
265
+ newValue: () => {
266
+ let idx = Math.floor(firstNames.length * Math.random());
267
+ return firstNames[idx];
268
+ },
269
+ regex: /^first_?(name)?/,
270
+ };
271
+
272
+ const lastNameType = {
273
+ dbType: DBType.String,
274
+ newValue: () => {
275
+ let idx = Math.floor(lastNames.length * Math.random());
276
+ return lastNames[idx];
277
+ },
278
+ regex: /^last_?(name)?/,
279
+ };
280
+
281
+ let types: commonType[] = [
282
+ phoneType,
283
+ emailType,
284
+ passwordType,
285
+ firstNameType,
286
+ lastNameType,
287
+ ];
288
+
289
+ let m: Map<DBType, commonType[]> = new Map();
290
+ for (const type of types) {
291
+ let list = m.get(type.dbType) || [];
292
+ list.push(type);
293
+ m.set(type.dbType, list);
294
+ }
@@ -0,0 +1,351 @@
1
+ import { v4 as uuidv4 } from "uuid";
2
+ import { Pool, PoolClient } from "pg";
3
+ import { mocked } from "jest-mock";
4
+ import { ID, Data } from "../core/base";
5
+ import { Clause } from "../core/clause";
6
+
7
+ import { performQuery, queryResult, getDataToReturn } from "./parse_sql";
8
+ import { MockLogs } from "./mock_log";
9
+
10
+ const eventEmitter = {
11
+ on: jest.fn(),
12
+ addListener: jest.fn(),
13
+ removeListener: jest.fn(),
14
+ once: jest.fn(),
15
+ off: jest.fn(),
16
+ removeAllListeners: jest.fn(),
17
+ setMaxListeners: jest.fn(),
18
+ getMaxListeners: jest.fn(),
19
+ listeners: jest.fn(),
20
+ rawListeners: jest.fn(),
21
+ emit: jest.fn(),
22
+ listenerCount: jest.fn(),
23
+ prependListener: jest.fn(),
24
+ prependOnceListener: jest.fn(),
25
+ eventNames: jest.fn(),
26
+ };
27
+
28
+ export interface queryOptions {
29
+ query: string;
30
+ values?: any[];
31
+ qs?: internalQueryStructure | null;
32
+ }
33
+
34
+ export interface mockOptions {
35
+ tableName: string;
36
+ // columns?: string[];
37
+ clause: Clause;
38
+ result: (values: any[]) => {};
39
+ }
40
+
41
+ export enum queryType {
42
+ SELECT,
43
+ INSERT,
44
+ UPDATE,
45
+ BEGIN,
46
+ COMMIT,
47
+ ROLLBACK,
48
+ DELETE,
49
+ }
50
+
51
+ export interface queryStructure {
52
+ tableName?: string;
53
+ type: queryType;
54
+ // columns?: string[];
55
+ values?: any[];
56
+ }
57
+
58
+ interface internalQueryStructure extends queryStructure {
59
+ query: string;
60
+ columns?: string[];
61
+ whereClause?: string;
62
+ suffix?: string;
63
+ setClause?: string;
64
+ }
65
+
66
+ /**
67
+ * @deprecated
68
+ */
69
+ export class QueryRecorder {
70
+ private static queries: queryOptions[] = [];
71
+ private static ids: ID[] = [];
72
+
73
+ // we need pkeys when storing...
74
+ private static data: Map<string, Data[]> = new Map();
75
+
76
+ // TODO kill use AST or just throw away
77
+ static getQueryStructure(query): internalQueryStructure | null {
78
+ // we parsing sql now??
79
+ // slowing building sqlshim?
80
+ // make it so that we return the values entered back when mocking the db
81
+
82
+ if (/^INSERT/.test(query)) {
83
+ let execArray = /INSERT INTO (.+) \((.+)\) VALUES \((.+)\) (.+)?/.exec(
84
+ query,
85
+ );
86
+ if (execArray) {
87
+ return {
88
+ tableName: execArray[1],
89
+ columns: execArray[2].split(", "),
90
+ type: queryType.INSERT,
91
+ query: execArray[0],
92
+ suffix: execArray[4],
93
+ };
94
+ }
95
+ return null;
96
+ }
97
+
98
+ if (/^SELECT/.test(query)) {
99
+ let execArray = /^SELECT (.+) FROM (.+) WHERE (.+)?/.exec(query);
100
+ if (execArray) {
101
+ return {
102
+ tableName: execArray[2],
103
+ whereClause: execArray[3],
104
+ type: queryType.SELECT,
105
+ query: execArray[0],
106
+ columns: execArray[1].split(", "),
107
+ };
108
+ }
109
+ }
110
+
111
+ if (/^UPDATE/.test(query)) {
112
+ // regex can't do returning
113
+ let execArray = /^UPDATE (.+) SET (.+) WHERE (.+) /.exec(query);
114
+ if (execArray) {
115
+ return {
116
+ tableName: execArray[1],
117
+ // not completely accurate
118
+ whereClause: execArray[3],
119
+ type: queryType.UPDATE,
120
+ query: execArray[0],
121
+ setClause: execArray[2],
122
+ // colummns: execArray[1].split(", "),
123
+ };
124
+ }
125
+ }
126
+
127
+ if (/^DELETE/.test(query)) {
128
+ // regex can't do returning
129
+ let execArray = /^DELETE FROM (.+) WHERE (.+) /.exec(query);
130
+ if (execArray) {
131
+ return {
132
+ tableName: execArray[1],
133
+ whereClause: execArray[2],
134
+ type: queryType.DELETE,
135
+ query: execArray[0],
136
+ };
137
+ }
138
+ }
139
+
140
+ if (/^BEGIN/.test(query)) {
141
+ return {
142
+ type: queryType.BEGIN,
143
+ query: query,
144
+ };
145
+ }
146
+
147
+ if (/^COMMIT/.test(query)) {
148
+ return {
149
+ type: queryType.COMMIT,
150
+ query: query,
151
+ };
152
+ }
153
+
154
+ if (/^ROLLBACK/.test(query)) {
155
+ return {
156
+ type: queryType.ROLLBACK,
157
+ query: query,
158
+ };
159
+ }
160
+ return null;
161
+ }
162
+
163
+ private static recordQuery(
164
+ query: string,
165
+ values: any[],
166
+ ): queryResult | undefined {
167
+ let qs = QueryRecorder.getQueryStructure(query);
168
+ QueryRecorder.queries.push({
169
+ query: query,
170
+ values: values,
171
+ qs: qs,
172
+ });
173
+
174
+ return performQuery(query, values, QueryRecorder.data);
175
+ }
176
+
177
+ static newID(): ID {
178
+ let id = uuidv4();
179
+ QueryRecorder.ids.push(id);
180
+ return id;
181
+ }
182
+
183
+ static getCurrentIDs(): ID[] {
184
+ return QueryRecorder.ids;
185
+ }
186
+
187
+ static getLastID(): ID {
188
+ const l = QueryRecorder.ids.length;
189
+ if (!l) {
190
+ throw new Error(`no ID`);
191
+ }
192
+ return QueryRecorder.ids[l - 1];
193
+ }
194
+
195
+ static getData() {
196
+ return QueryRecorder.data;
197
+ }
198
+
199
+ static filterData(tableName: string, filterfn: (row: Data) => boolean) {
200
+ const rows = QueryRecorder.data.get(tableName) || [];
201
+ let result: Data[] = [];
202
+ for (const row of rows) {
203
+ // transform the data into whatever the return value should be before filtering
204
+ const row2 = getDataToReturn(row, undefined, true);
205
+ if (filterfn(row2)) {
206
+ result.push(row2);
207
+ }
208
+ }
209
+ return result;
210
+ }
211
+
212
+ static clear() {
213
+ QueryRecorder.queries = [];
214
+ QueryRecorder.ids = [];
215
+ QueryRecorder.data = new Map();
216
+ }
217
+
218
+ static clearQueries() {
219
+ // clears queries but keeps data
220
+ // this is useful for situations like write this data before each test
221
+ // but each test shouldn't have to account for this
222
+ QueryRecorder.queries = [];
223
+ }
224
+
225
+ static getCurrentQueries(): queryOptions[] {
226
+ return QueryRecorder.queries;
227
+ }
228
+
229
+ /**
230
+ * @deprecated will keep this since it's just using ml.logs
231
+ * which we still want around (I think) but we should eventually
232
+ * probably stop using this
233
+ */
234
+ static validateQueryStructuresFromLogs(
235
+ ml: MockLogs,
236
+ expected: queryStructure[],
237
+ opts?: {
238
+ skipSelect?: boolean;
239
+ flagBeginCommit?: boolean;
240
+ },
241
+ ) {
242
+ const queries = ml.logs.map((log) => {
243
+ const qs = QueryRecorder.getQueryStructure(log.query);
244
+ if (!qs) {
245
+ throw new Error(`invalid query ${log.querya}`);
246
+ }
247
+ return {
248
+ query: log.query,
249
+ qs,
250
+ };
251
+ });
252
+
253
+ QueryRecorder.validateQueryStructuresImpl(expected, queries, opts);
254
+ }
255
+
256
+ private static validateQueryStructuresImpl(
257
+ expected: queryStructure[],
258
+ queries: queryOptions[],
259
+ opts?: {
260
+ skipSelect?: boolean;
261
+ flagBeginCommit?: boolean;
262
+ },
263
+ ) {
264
+ if (opts?.skipSelect) {
265
+ queries = queries.filter((query) => query.qs?.type !== queryType.SELECT);
266
+ }
267
+ if (!opts?.flagBeginCommit) {
268
+ queries = queries.filter(
269
+ (query) =>
270
+ query.qs?.type !== queryType.BEGIN &&
271
+ query.qs?.type !== queryType.COMMIT,
272
+ );
273
+ }
274
+ // console.log(queries, expected);
275
+ expect(queries.length).toBe(expected.length);
276
+
277
+ for (let i = 0; i < expected.length; i++) {
278
+ let expectedStructure = expected[i];
279
+ let query = queries[i];
280
+ switch (expectedStructure.type) {
281
+ case queryType.BEGIN:
282
+ expect(query.query).toBe("BEGIN");
283
+ expect(query.values).toBe(undefined);
284
+ break;
285
+ case queryType.ROLLBACK:
286
+ expect(query.query).toBe("ROLLBACK");
287
+ expect(query.values).toBe(undefined);
288
+ break;
289
+ case queryType.COMMIT:
290
+ expect(query.query).toBe("COMMIT");
291
+ expect(query.values).toBe(undefined);
292
+ break;
293
+ case queryType.SELECT:
294
+ if (!opts?.skipSelect) {
295
+ console.error(
296
+ "validating select query structure not supported yet",
297
+ );
298
+ }
299
+ // TODO INSERT and UPDATE tests here...
300
+ // should be easy...
301
+ break;
302
+ case queryType.INSERT:
303
+ expect(query.query.startsWith("INSERT")).toBe(true);
304
+ expect(query.qs?.tableName).toBe(expectedStructure.tableName);
305
+ break;
306
+ case queryType.UPDATE:
307
+ expect(query.query.startsWith("UPDATE")).toBe(true);
308
+ expect(query.qs?.tableName).toBe(expectedStructure.tableName);
309
+ break;
310
+ }
311
+ }
312
+ }
313
+
314
+ static mockPool(pool: typeof Pool) {
315
+ const mockedPool = mocked(pool);
316
+ // @ts-ignore
317
+ // TODO what changed in mockImplementation?
318
+ mockedPool.mockImplementation((): Pool => {
319
+ return {
320
+ totalCount: 1,
321
+ idleCount: 1,
322
+ waitingCount: 1,
323
+ connect: async (): Promise<PoolClient> => {
324
+ return {
325
+ connect: jest.fn(),
326
+ release: jest.fn(),
327
+ query: jest
328
+ .fn()
329
+ .mockImplementation((query: string, values: any[]) => {
330
+ return QueryRecorder.recordQuery(query, values);
331
+ }),
332
+ copyFrom: jest.fn(),
333
+ copyTo: jest.fn(),
334
+ pauseDrain: jest.fn(),
335
+ resumeDrain: jest.fn(),
336
+ escapeIdentifier: jest.fn(),
337
+ escapeLiteral: jest.fn(),
338
+
339
+ // EventEmitter
340
+ ...eventEmitter,
341
+ };
342
+ },
343
+ end: jest.fn(),
344
+ query: jest.fn().mockImplementation(QueryRecorder.recordQuery),
345
+
346
+ // EventEmitter
347
+ ...eventEmitter,
348
+ };
349
+ });
350
+ }
351
+ }
@@ -0,0 +1,40 @@
1
+ import { DateTime } from "luxon";
2
+ import { leftPad } from "../schema";
3
+ import DB from "../core/db";
4
+
5
+ let dbCurrentZone: string | null | undefined = undefined;
6
+ export class DBTimeZone {
7
+ private static async getVal(): Promise<string | null> {
8
+ if (dbCurrentZone !== undefined) {
9
+ return dbCurrentZone;
10
+ }
11
+
12
+ const r = await DB.getInstance()
13
+ .getPool()
14
+ .query("SELECT current_setting('TIMEZONE');");
15
+
16
+ if (r.rows.length) {
17
+ dbCurrentZone = r.rows[0].current_setting as string;
18
+ } else {
19
+ dbCurrentZone = null;
20
+ }
21
+ return dbCurrentZone;
22
+ }
23
+
24
+ static async getDateOffset(d: Date) {
25
+ let zone = await DBTimeZone.getVal();
26
+
27
+ let dt = DateTime.fromJSDate(d);
28
+ if (zone) {
29
+ dt = dt.setZone(zone);
30
+ }
31
+
32
+ // use
33
+ const val = leftPad(dt.get("offset") / 60);
34
+
35
+ if (val == "00") {
36
+ return "+00";
37
+ }
38
+ return val;
39
+ }
40
+ }