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

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/package.json +52 -7
  3. package/src/action/action.ts +330 -0
  4. package/src/action/executor.ts +453 -0
  5. package/src/action/experimental_action.ts +277 -0
  6. package/src/action/index.ts +31 -0
  7. package/src/action/operations.ts +967 -0
  8. package/src/action/orchestrator.ts +1527 -0
  9. package/src/action/privacy.ts +37 -0
  10. package/src/action/relative_value.ts +242 -0
  11. package/src/action/transaction.ts +38 -0
  12. package/src/auth/auth.ts +77 -0
  13. package/src/auth/index.ts +8 -0
  14. package/src/core/base.ts +367 -0
  15. package/src/core/clause.ts +1065 -0
  16. package/src/core/config.ts +219 -0
  17. package/src/core/const.ts +5 -0
  18. package/src/core/context.ts +135 -0
  19. package/src/core/convert.ts +106 -0
  20. package/src/core/date.ts +23 -0
  21. package/src/core/db.ts +498 -0
  22. package/src/core/ent.ts +1740 -0
  23. package/src/core/global_schema.ts +49 -0
  24. package/src/core/loaders/assoc_count_loader.ts +99 -0
  25. package/src/core/loaders/assoc_edge_loader.ts +250 -0
  26. package/src/core/loaders/index.ts +12 -0
  27. package/src/core/loaders/loader.ts +66 -0
  28. package/src/core/loaders/object_loader.ts +489 -0
  29. package/src/core/loaders/query_loader.ts +314 -0
  30. package/src/core/loaders/raw_count_loader.ts +175 -0
  31. package/src/core/logger.ts +49 -0
  32. package/src/core/privacy.ts +660 -0
  33. package/src/core/query/assoc_query.ts +240 -0
  34. package/src/core/query/custom_clause_query.ts +174 -0
  35. package/src/core/query/custom_query.ts +302 -0
  36. package/src/core/query/index.ts +9 -0
  37. package/src/core/query/query.ts +674 -0
  38. package/src/core/query_impl.ts +32 -0
  39. package/src/core/viewer.ts +52 -0
  40. package/src/ent.code-workspace +73 -0
  41. package/src/graphql/builtins/connection.ts +25 -0
  42. package/src/graphql/builtins/edge.ts +16 -0
  43. package/src/graphql/builtins/node.ts +12 -0
  44. package/src/graphql/graphql.ts +891 -0
  45. package/src/graphql/graphql_field_helpers.ts +221 -0
  46. package/src/graphql/index.ts +42 -0
  47. package/src/graphql/mutations/union.ts +39 -0
  48. package/src/graphql/node_resolver.ts +122 -0
  49. package/src/graphql/query/connection_type.ts +113 -0
  50. package/src/graphql/query/edge_connection.ts +171 -0
  51. package/src/graphql/query/page_info.ts +34 -0
  52. package/src/graphql/query/shared_edge_connection.ts +287 -0
  53. package/src/graphql/scalars/orderby_direction.ts +13 -0
  54. package/src/graphql/scalars/time.ts +38 -0
  55. package/src/imports/dataz/example1/_auth.ts +51 -0
  56. package/src/imports/dataz/example1/_viewer.ts +35 -0
  57. package/src/imports/index.ts +213 -0
  58. package/src/index.ts +145 -0
  59. package/src/parse_schema/parse.ts +585 -0
  60. package/src/schema/base_schema.ts +224 -0
  61. package/src/schema/field.ts +1087 -0
  62. package/src/schema/index.ts +53 -0
  63. package/src/schema/json_field.ts +94 -0
  64. package/src/schema/schema.ts +1028 -0
  65. package/src/schema/struct_field.ts +234 -0
  66. package/src/schema/union_field.ts +105 -0
  67. package/src/scripts/custom_compiler.ts +331 -0
  68. package/src/scripts/custom_graphql.ts +550 -0
  69. package/src/scripts/migrate_v0.1.ts +41 -0
  70. package/src/scripts/move_types.ts +131 -0
  71. package/src/scripts/read_schema.ts +67 -0
  72. package/src/setupPackage.js +42 -0
  73. package/src/testutils/action/complex_schemas.ts +517 -0
  74. package/src/testutils/builder.ts +422 -0
  75. package/src/testutils/context/test_context.ts +25 -0
  76. package/src/testutils/db/fixture.ts +32 -0
  77. package/src/testutils/db/temp_db.ts +941 -0
  78. package/src/testutils/db/value.ts +294 -0
  79. package/src/testutils/db_mock.ts +351 -0
  80. package/src/testutils/db_time_zone.ts +40 -0
  81. package/src/testutils/ent-graphql-tests/index.ts +653 -0
  82. package/src/testutils/fake_comms.ts +50 -0
  83. package/src/testutils/fake_data/const.ts +64 -0
  84. package/src/testutils/fake_data/events_query.ts +145 -0
  85. package/src/testutils/fake_data/fake_contact.ts +150 -0
  86. package/src/testutils/fake_data/fake_event.ts +150 -0
  87. package/src/testutils/fake_data/fake_tag.ts +139 -0
  88. package/src/testutils/fake_data/fake_user.ts +232 -0
  89. package/src/testutils/fake_data/index.ts +1 -0
  90. package/src/testutils/fake_data/internal.ts +8 -0
  91. package/src/testutils/fake_data/tag_query.ts +56 -0
  92. package/src/testutils/fake_data/test_helpers.ts +388 -0
  93. package/src/testutils/fake_data/user_query.ts +524 -0
  94. package/src/testutils/fake_log.ts +52 -0
  95. package/src/testutils/mock_date.ts +10 -0
  96. package/src/testutils/mock_log.ts +39 -0
  97. package/src/testutils/parse_sql.ts +685 -0
  98. package/src/testutils/test_edge_global_schema.ts +49 -0
  99. package/src/testutils/write.ts +70 -0
  100. package/src/tsc/ast.ts +351 -0
  101. package/src/tsc/compilerOptions.ts +85 -0
  102. package/src/tsc/move_generated.ts +191 -0
  103. package/src/tsc/transform.ts +226 -0
  104. package/src/tsc/transform_action.ts +224 -0
  105. package/src/tsc/transform_ent.ts +66 -0
  106. package/src/tsc/transform_schema.ts +546 -0
  107. package/tsconfig.json +20 -0
  108. package/core/query/shared_assoc_test.d.ts +0 -2
  109. package/core/query/shared_assoc_test.js +0 -804
  110. package/core/query/shared_test.d.ts +0 -21
  111. package/core/query/shared_test.js +0 -736
  112. package/graphql/query/shared_assoc_test.d.ts +0 -1
  113. package/graphql/query/shared_assoc_test.js +0 -203
  114. /package/{action → dist/action}/action.d.ts +0 -0
  115. /package/{action → dist/action}/action.js +0 -0
  116. /package/{action → dist/action}/executor.d.ts +0 -0
  117. /package/{action → dist/action}/executor.js +0 -0
  118. /package/{action → dist/action}/experimental_action.d.ts +0 -0
  119. /package/{action → dist/action}/experimental_action.js +0 -0
  120. /package/{action → dist/action}/index.d.ts +0 -0
  121. /package/{action → dist/action}/index.js +0 -0
  122. /package/{action → dist/action}/operations.d.ts +0 -0
  123. /package/{action → dist/action}/operations.js +0 -0
  124. /package/{action → dist/action}/orchestrator.d.ts +0 -0
  125. /package/{action → dist/action}/orchestrator.js +0 -0
  126. /package/{action → dist/action}/privacy.d.ts +0 -0
  127. /package/{action → dist/action}/privacy.js +0 -0
  128. /package/{action → dist/action}/relative_value.d.ts +0 -0
  129. /package/{action → dist/action}/relative_value.js +0 -0
  130. /package/{action → dist/action}/transaction.d.ts +0 -0
  131. /package/{action → dist/action}/transaction.js +0 -0
  132. /package/{auth → dist/auth}/auth.d.ts +0 -0
  133. /package/{auth → dist/auth}/auth.js +0 -0
  134. /package/{auth → dist/auth}/index.d.ts +0 -0
  135. /package/{auth → dist/auth}/index.js +0 -0
  136. /package/{core → dist/core}/base.d.ts +0 -0
  137. /package/{core → dist/core}/base.js +0 -0
  138. /package/{core → dist/core}/clause.d.ts +0 -0
  139. /package/{core → dist/core}/clause.js +0 -0
  140. /package/{core → dist/core}/config.d.ts +0 -0
  141. /package/{core → dist/core}/config.js +0 -0
  142. /package/{core → dist/core}/const.d.ts +0 -0
  143. /package/{core → dist/core}/const.js +0 -0
  144. /package/{core → dist/core}/context.d.ts +0 -0
  145. /package/{core → dist/core}/context.js +0 -0
  146. /package/{core → dist/core}/convert.d.ts +0 -0
  147. /package/{core → dist/core}/convert.js +0 -0
  148. /package/{core → dist/core}/date.d.ts +0 -0
  149. /package/{core → dist/core}/date.js +0 -0
  150. /package/{core → dist/core}/db.d.ts +0 -0
  151. /package/{core → dist/core}/db.js +0 -0
  152. /package/{core → dist/core}/ent.d.ts +0 -0
  153. /package/{core → dist/core}/ent.js +0 -0
  154. /package/{core → dist/core}/global_schema.d.ts +0 -0
  155. /package/{core → dist/core}/global_schema.js +0 -0
  156. /package/{core → dist/core}/loaders/assoc_count_loader.d.ts +0 -0
  157. /package/{core → dist/core}/loaders/assoc_count_loader.js +0 -0
  158. /package/{core → dist/core}/loaders/assoc_edge_loader.d.ts +0 -0
  159. /package/{core → dist/core}/loaders/assoc_edge_loader.js +0 -0
  160. /package/{core → dist/core}/loaders/index.d.ts +0 -0
  161. /package/{core → dist/core}/loaders/index.js +0 -0
  162. /package/{core → dist/core}/loaders/loader.d.ts +0 -0
  163. /package/{core → dist/core}/loaders/loader.js +0 -0
  164. /package/{core → dist/core}/loaders/object_loader.d.ts +0 -0
  165. /package/{core → dist/core}/loaders/object_loader.js +0 -0
  166. /package/{core → dist/core}/loaders/query_loader.d.ts +0 -0
  167. /package/{core → dist/core}/loaders/query_loader.js +0 -0
  168. /package/{core → dist/core}/loaders/raw_count_loader.d.ts +0 -0
  169. /package/{core → dist/core}/loaders/raw_count_loader.js +0 -0
  170. /package/{core → dist/core}/logger.d.ts +0 -0
  171. /package/{core → dist/core}/logger.js +0 -0
  172. /package/{core → dist/core}/privacy.d.ts +0 -0
  173. /package/{core → dist/core}/privacy.js +0 -0
  174. /package/{core → dist/core}/query/assoc_query.d.ts +0 -0
  175. /package/{core → dist/core}/query/assoc_query.js +0 -0
  176. /package/{core → dist/core}/query/custom_clause_query.d.ts +0 -0
  177. /package/{core → dist/core}/query/custom_clause_query.js +0 -0
  178. /package/{core → dist/core}/query/custom_query.d.ts +0 -0
  179. /package/{core → dist/core}/query/custom_query.js +0 -0
  180. /package/{core → dist/core}/query/index.d.ts +0 -0
  181. /package/{core → dist/core}/query/index.js +0 -0
  182. /package/{core → dist/core}/query/query.d.ts +0 -0
  183. /package/{core → dist/core}/query/query.js +0 -0
  184. /package/{core → dist/core}/query_impl.d.ts +0 -0
  185. /package/{core → dist/core}/query_impl.js +0 -0
  186. /package/{core → dist/core}/viewer.d.ts +0 -0
  187. /package/{core → dist/core}/viewer.js +0 -0
  188. /package/{graphql → dist/graphql}/builtins/connection.d.ts +0 -0
  189. /package/{graphql → dist/graphql}/builtins/connection.js +0 -0
  190. /package/{graphql → dist/graphql}/builtins/edge.d.ts +0 -0
  191. /package/{graphql → dist/graphql}/builtins/edge.js +0 -0
  192. /package/{graphql → dist/graphql}/builtins/node.d.ts +0 -0
  193. /package/{graphql → dist/graphql}/builtins/node.js +0 -0
  194. /package/{graphql → dist/graphql}/graphql.d.ts +0 -0
  195. /package/{graphql → dist/graphql}/graphql.js +0 -0
  196. /package/{graphql → dist/graphql}/graphql_field_helpers.d.ts +0 -0
  197. /package/{graphql → dist/graphql}/graphql_field_helpers.js +0 -0
  198. /package/{graphql → dist/graphql}/index.d.ts +0 -0
  199. /package/{graphql → dist/graphql}/index.js +0 -0
  200. /package/{graphql → dist/graphql}/mutations/union.d.ts +0 -0
  201. /package/{graphql → dist/graphql}/mutations/union.js +0 -0
  202. /package/{graphql → dist/graphql}/node_resolver.d.ts +0 -0
  203. /package/{graphql → dist/graphql}/node_resolver.js +0 -0
  204. /package/{graphql → dist/graphql}/query/connection_type.d.ts +0 -0
  205. /package/{graphql → dist/graphql}/query/connection_type.js +0 -0
  206. /package/{graphql → dist/graphql}/query/edge_connection.d.ts +0 -0
  207. /package/{graphql → dist/graphql}/query/edge_connection.js +0 -0
  208. /package/{graphql → dist/graphql}/query/page_info.d.ts +0 -0
  209. /package/{graphql → dist/graphql}/query/page_info.js +0 -0
  210. /package/{graphql → dist/graphql}/query/shared_edge_connection.d.ts +0 -0
  211. /package/{graphql → dist/graphql}/query/shared_edge_connection.js +0 -0
  212. /package/{graphql → dist/graphql}/scalars/orderby_direction.d.ts +0 -0
  213. /package/{graphql → dist/graphql}/scalars/orderby_direction.js +0 -0
  214. /package/{graphql → dist/graphql}/scalars/time.d.ts +0 -0
  215. /package/{graphql → dist/graphql}/scalars/time.js +0 -0
  216. /package/{imports → dist/imports}/dataz/example1/_auth.d.ts +0 -0
  217. /package/{imports → dist/imports}/dataz/example1/_auth.js +0 -0
  218. /package/{imports → dist/imports}/dataz/example1/_viewer.d.ts +0 -0
  219. /package/{imports → dist/imports}/dataz/example1/_viewer.js +0 -0
  220. /package/{imports → dist/imports}/index.d.ts +0 -0
  221. /package/{imports → dist/imports}/index.js +0 -0
  222. /package/{index.d.ts → dist/index.d.ts} +0 -0
  223. /package/{index.js → dist/index.js} +0 -0
  224. /package/{parse_schema → dist/parse_schema}/parse.d.ts +0 -0
  225. /package/{parse_schema → dist/parse_schema}/parse.js +0 -0
  226. /package/{schema → dist/schema}/base_schema.d.ts +0 -0
  227. /package/{schema → dist/schema}/base_schema.js +0 -0
  228. /package/{schema → dist/schema}/field.d.ts +0 -0
  229. /package/{schema → dist/schema}/field.js +0 -0
  230. /package/{schema → dist/schema}/index.d.ts +0 -0
  231. /package/{schema → dist/schema}/index.js +0 -0
  232. /package/{schema → dist/schema}/json_field.d.ts +0 -0
  233. /package/{schema → dist/schema}/json_field.js +0 -0
  234. /package/{schema → dist/schema}/schema.d.ts +0 -0
  235. /package/{schema → dist/schema}/schema.js +0 -0
  236. /package/{schema → dist/schema}/struct_field.d.ts +0 -0
  237. /package/{schema → dist/schema}/struct_field.js +0 -0
  238. /package/{schema → dist/schema}/union_field.d.ts +0 -0
  239. /package/{schema → dist/schema}/union_field.js +0 -0
  240. /package/{scripts → dist/scripts}/custom_compiler.d.ts +0 -0
  241. /package/{scripts → dist/scripts}/custom_compiler.js +0 -0
  242. /package/{scripts → dist/scripts}/custom_graphql.d.ts +0 -0
  243. /package/{scripts → dist/scripts}/custom_graphql.js +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,422 @@
1
+ import {
2
+ Ent,
3
+ ID,
4
+ Viewer,
5
+ Data,
6
+ EntConstructor,
7
+ PrivacyPolicy,
8
+ } from "../core/base";
9
+ import { AlwaysAllowPrivacyPolicy } from "../core/privacy";
10
+ import { Orchestrator } from "../action/orchestrator";
11
+ import {
12
+ Action,
13
+ Builder,
14
+ Changeset,
15
+ WriteOperation,
16
+ Validator,
17
+ Trigger,
18
+ saveBuilder,
19
+ saveBuilderX,
20
+ Observer,
21
+ } from "../action";
22
+ import { getFields, getFieldsWithPrivacy, FieldMap, Schema } from "../schema";
23
+ import { QueryRecorder } from "./db_mock";
24
+ import pluralize from "pluralize";
25
+ import { snakeCase } from "snake-case";
26
+ import { ObjectLoaderFactory } from "../core/loaders";
27
+ import { convertDate } from "../core/convert";
28
+ import { camelCase } from "camel-case";
29
+ import {
30
+ SchemaConfig,
31
+ EntSchema,
32
+ EntSchemaWithTZ,
33
+ } from "../schema/base_schema";
34
+ import { FieldInfoMap, getStorageKey } from "../schema/schema";
35
+ import { Clause } from "src/core/clause";
36
+ import { ChangesetOptions } from "../action/action";
37
+
38
+ export class BaseEnt {
39
+ readonly id: ID;
40
+
41
+ constructor(public viewer: Viewer, public readonly data: Data) {
42
+ this.data.created_at = convertDate(data.created_at);
43
+ this.data.updated_at = convertDate(data.updated_at);
44
+ this.id = data[this.getKey()];
45
+ }
46
+
47
+ getKey() {
48
+ return "id";
49
+ }
50
+
51
+ getPrivacyPolicy(): PrivacyPolicy {
52
+ return AlwaysAllowPrivacyPolicy;
53
+ }
54
+ __setRawDBData(data: Data) {
55
+ // doesn't apply here so ignore...
56
+ }
57
+ }
58
+
59
+ export class User extends BaseEnt implements Ent {
60
+ accountID: string = "";
61
+ nodeType = "User";
62
+ firstName: string;
63
+
64
+ constructor(public viewer: Viewer, public data: Data) {
65
+ super(viewer, data);
66
+ this.firstName = data.first_name;
67
+ }
68
+ }
69
+
70
+ export class Event extends BaseEnt implements Ent {
71
+ accountID: string = "";
72
+ nodeType = "Event";
73
+ }
74
+
75
+ export class Contact extends BaseEnt implements Ent {
76
+ accountID: string = "";
77
+ nodeType = "Contact";
78
+ getPrivacyPolicy(): PrivacyPolicy<this> {
79
+ return AlwaysAllowPrivacyPolicy;
80
+ }
81
+ }
82
+
83
+ export class Group extends BaseEnt implements Ent {
84
+ accountID: string = "";
85
+ nodeType = "Group";
86
+ getPrivacyPolicy(): PrivacyPolicy<this> {
87
+ return AlwaysAllowPrivacyPolicy;
88
+ }
89
+ }
90
+
91
+ export class Message extends BaseEnt implements Ent {
92
+ accountID: string = "";
93
+ nodeType = "Message";
94
+ }
95
+
96
+ export class Address extends BaseEnt implements Ent {
97
+ accountID: string = "";
98
+ nodeType = "Address";
99
+ }
100
+
101
+ export interface BuilderSchema<T extends Ent> extends Schema {
102
+ ent: EntConstructor<T>;
103
+ }
104
+
105
+ export class EntBuilderSchema<T extends Ent> extends EntSchema {
106
+ constructor(public ent: EntConstructor<T>, cfg: SchemaConfig) {
107
+ super(cfg);
108
+ }
109
+ }
110
+
111
+ export function getBuilderSchema<T extends Ent>(
112
+ cfg: SchemaConfig,
113
+ ent: EntConstructor<T>,
114
+ ): BuilderSchema<T> {
115
+ return {
116
+ ...new EntSchema(cfg),
117
+ ent,
118
+ };
119
+ }
120
+
121
+ export function getBuilderSchemaFromFields<T extends Ent>(
122
+ fields: FieldMap,
123
+ ent: EntConstructor<T>,
124
+ opts?: Partial<Exclude<SchemaConfig, "fields">>,
125
+ ): BuilderSchema<T> {
126
+ return {
127
+ ...new EntSchema({ ...opts, fields }),
128
+ ent,
129
+ };
130
+ }
131
+
132
+ export function getBuilderSchemaTZFromFields<T extends Ent>(
133
+ fields: FieldMap,
134
+ ent: EntConstructor<T>,
135
+ ): BuilderSchema<T> {
136
+ return {
137
+ ...new EntSchemaWithTZ({ fields }),
138
+ ent,
139
+ };
140
+ }
141
+
142
+ export function getSchemaName(value: BuilderSchema<Ent>) {
143
+ return value.ent.name;
144
+ }
145
+
146
+ export function getTableName(value: BuilderSchema<Ent>) {
147
+ return pluralize(snakeCase(value.ent.name)).toLowerCase();
148
+ }
149
+
150
+ function randomNum(): string {
151
+ return Math.random().toString(10).substring(2);
152
+ }
153
+
154
+ export function getFieldInfo(value: BuilderSchema<Ent>) {
155
+ const fields = getFields(value);
156
+ let ret: FieldInfoMap = {};
157
+ for (const [k, f] of fields) {
158
+ ret[k] = {
159
+ dbCol: getStorageKey(f, k),
160
+ // in tests (anything using SimpleBuilder), make it be the same as the fieldName
161
+ inputKey: k,
162
+ };
163
+ }
164
+ return ret;
165
+ }
166
+
167
+ type MaybeNull<T extends Ent> = T | null;
168
+ type TMaybleNullableEnt<T extends Ent> = T | MaybeNull<T>;
169
+
170
+ // reuses orchestrator and standard things
171
+ export class SimpleBuilder<
172
+ T extends Ent,
173
+ TExistingEnt extends TMaybleNullableEnt<T> = MaybeNull<T>,
174
+ > implements Builder<T, Viewer, TExistingEnt>
175
+ {
176
+ ent: EntConstructor<T, Viewer>;
177
+ placeholderID: ID;
178
+ public orchestrator: Orchestrator<T, Data, Viewer, TExistingEnt>;
179
+ public fields: Map<string, any>;
180
+ nodeType: string;
181
+ m: Map<string, any> = new Map();
182
+
183
+ constructor(
184
+ public viewer: Viewer,
185
+ private schema: BuilderSchema<T>,
186
+ fields: Map<string, any>,
187
+ public operation: WriteOperation = WriteOperation.Insert,
188
+ public existingEnt: TExistingEnt,
189
+ action?:
190
+ | Action<T, SimpleBuilder<T, TExistingEnt>, Viewer, Data, TExistingEnt>
191
+ | undefined,
192
+ expressions?: Map<string, Clause>,
193
+ ) {
194
+ // create dynamic placeholder
195
+ // TODO: do we need to use this as the node when there's an existingEnt
196
+ // same for generated builders.
197
+ this.placeholderID = `$ent.idPlaceholderID$ ${randomNum()}-${
198
+ schema.ent?.name
199
+ }`;
200
+
201
+ if (this.operation === WriteOperation.Insert) {
202
+ for (const [key, value] of fields) {
203
+ if (key === "id" && value === "{id}") {
204
+ fields.set(key, QueryRecorder.newID());
205
+ }
206
+ }
207
+ }
208
+ this.fields = fields;
209
+
210
+ const schemaFields = getFields(schema);
211
+ let key = "id";
212
+ if (!schemaFields.has("id") && !schemaFields.has("ID")) {
213
+ if (schemaFields.size !== 1) {
214
+ throw new Error(
215
+ `no id field and multiple fields so can't deduce key. add an id field to schema`,
216
+ );
217
+ }
218
+ for (const [name, _] of fields) {
219
+ key = snakeCase(name);
220
+ }
221
+ }
222
+ this.ent = schema.ent;
223
+ const tableName = getTableName(schema);
224
+ this.nodeType = camelCase(schema.ent.name);
225
+ const fieldInfo = getFieldInfo(schema);
226
+ this.orchestrator = new Orchestrator<T, Data, Viewer, TExistingEnt>({
227
+ viewer: this.viewer,
228
+ operation: operation,
229
+ tableName: tableName,
230
+ key,
231
+ fieldInfo,
232
+ loaderOptions: {
233
+ loaderFactory: new ObjectLoaderFactory({
234
+ tableName: tableName,
235
+ fields: [],
236
+ key,
237
+ }),
238
+ ent: schema.ent,
239
+ tableName: tableName,
240
+ fields: [],
241
+ fieldPrivacy: getFieldsWithPrivacy(schema, fieldInfo),
242
+ },
243
+ builder: this,
244
+ action: action,
245
+ expressions,
246
+ schema: this.schema,
247
+ editedFields: () => {
248
+ // to simulate what we do in generated builders where we return a new Map
249
+ const m = new Map();
250
+ for (const [k, v] of this.fields) {
251
+ m.set(k, v);
252
+ }
253
+ return m;
254
+ },
255
+ updateInput: this.updateInput.bind(this),
256
+ });
257
+ }
258
+
259
+ getInput(): Data {
260
+ let ret: Data = {};
261
+ for (const [k, v] of this.fields) {
262
+ ret[k] = v;
263
+ }
264
+ return ret;
265
+ }
266
+ updateInput(input: Data) {
267
+ const knownFields = getFields(this.schema);
268
+ for (const k in input) {
269
+ if (knownFields.has(k)) {
270
+ this.fields.set(k, input[k]);
271
+ } else {
272
+ // related to #510. we do camelCase to pass fields in here but fields may be snakeCase and we want that to pass in tests
273
+ // we do camelCase in
274
+ const sc = snakeCase(k);
275
+ if (knownFields.has(sc)) {
276
+ this.fields.set(sc, input[k]);
277
+ }
278
+ }
279
+ }
280
+ }
281
+
282
+ // store data in Builder that can be retrieved by another validator, trigger, observer later in the action
283
+ storeData(k: string, v: any) {
284
+ this.m.set(k, v);
285
+ }
286
+
287
+ // retrieve data stored in this Builder with key
288
+ getStoredData(k: string) {
289
+ return this.m.get(k);
290
+ }
291
+
292
+ build(): Promise<Changeset> {
293
+ return this.orchestrator.build();
294
+ }
295
+
296
+ buildWithOptions_BETA(options: ChangesetOptions): Promise<Changeset> {
297
+ return this.orchestrator.buildWithOptions_BETA(options);
298
+ }
299
+
300
+ async editedEnt(): Promise<T | null> {
301
+ return await this.orchestrator.editedEnt();
302
+ }
303
+
304
+ async editedEntX(): Promise<T> {
305
+ return await this.orchestrator.editedEntX();
306
+ }
307
+
308
+ async save(): Promise<void> {
309
+ await saveBuilder(this);
310
+ }
311
+
312
+ async saveX(): Promise<void> {
313
+ await saveBuilderX(this);
314
+ }
315
+
316
+ async valid(): Promise<boolean> {
317
+ return await this.orchestrator.valid();
318
+ }
319
+
320
+ async validX(): Promise<void> {
321
+ return await this.orchestrator.validX();
322
+ }
323
+ }
324
+
325
+ interface viewerEntLoadFunc {
326
+ (data: Data): Viewer | Promise<Viewer>;
327
+ }
328
+
329
+ export class SimpleAction<
330
+ T extends Ent,
331
+ TExistingEnt extends TMaybleNullableEnt<T> = MaybeNull<T>,
332
+ > implements
333
+ Action<T, SimpleBuilder<T, TExistingEnt>, Viewer, Data, TExistingEnt>
334
+ {
335
+ builder: SimpleBuilder<T, TExistingEnt>;
336
+ viewerForEntLoad: viewerEntLoadFunc | undefined;
337
+
338
+ constructor(
339
+ public viewer: Viewer,
340
+ schema: BuilderSchema<T>,
341
+ private fields: Map<string, any>,
342
+ operation: WriteOperation = WriteOperation.Insert,
343
+ existingEnt: TExistingEnt,
344
+ expressions?: Map<string, Clause>,
345
+ ) {
346
+ this.builder = new SimpleBuilder<T, TExistingEnt>(
347
+ this.viewer,
348
+ schema,
349
+ fields,
350
+ operation,
351
+ existingEnt,
352
+ this,
353
+ expressions,
354
+ );
355
+ }
356
+
357
+ getTriggers():
358
+ | (Trigger<T, SimpleBuilder<T>> | Array<Trigger<T, SimpleBuilder<T>>>)[] {
359
+ return [];
360
+ }
361
+
362
+ getValidators(): Validator<T, SimpleBuilder<T>>[] {
363
+ return [];
364
+ }
365
+
366
+ getObservers(): Observer<T, SimpleBuilder<T>>[] {
367
+ return [];
368
+ }
369
+
370
+ getPrivacyPolicy() {
371
+ return AlwaysAllowPrivacyPolicy;
372
+ }
373
+
374
+ getInput() {
375
+ const ret: Data = {};
376
+ for (const [k, v] of this.fields) {
377
+ ret[k] = v;
378
+ }
379
+ return ret;
380
+ }
381
+
382
+ changeset(): Promise<Changeset> {
383
+ return this.builder.build();
384
+ }
385
+
386
+ changesetWithOptions_BETA(options: ChangesetOptions): Promise<Changeset> {
387
+ return this.builder.buildWithOptions_BETA(options);
388
+ }
389
+
390
+ valid(): Promise<boolean> {
391
+ return this.builder.orchestrator.valid();
392
+ }
393
+
394
+ validX(): Promise<void> {
395
+ return this.builder.orchestrator.validX();
396
+ }
397
+
398
+ validWithErrors() {
399
+ return this.builder.orchestrator.validWithErrors();
400
+ }
401
+
402
+ async save(): Promise<T | null> {
403
+ await saveBuilder(this.builder);
404
+ if (this.builder.operation !== WriteOperation.Delete) {
405
+ return this.builder.orchestrator.editedEnt();
406
+ }
407
+ return null;
408
+ }
409
+
410
+ async saveX(): Promise<T> {
411
+ await saveBuilderX(this.builder);
412
+ return this.builder.orchestrator.editedEntX();
413
+ }
414
+
415
+ async editedEnt(): Promise<T | null> {
416
+ return this.builder.orchestrator.editedEnt();
417
+ }
418
+
419
+ async editedEntX(): Promise<T> {
420
+ return this.builder.orchestrator.editedEntX();
421
+ }
422
+ }
@@ -0,0 +1,25 @@
1
+ import { Context, Viewer } from "../../core/base";
2
+ import { ContextCache } from "../../core/context";
3
+ import { LoggedOutViewer } from "../../core/viewer";
4
+
5
+ interface viewerWithContext extends Viewer {
6
+ setContext(ctx: Context): any;
7
+ }
8
+
9
+ export class TestContext implements Context {
10
+ constructor(viewer?: Viewer) {
11
+ if (viewer) {
12
+ this.viewer = viewer;
13
+ if ((viewer as viewerWithContext).setContext !== undefined) {
14
+ (viewer as viewerWithContext).setContext(this);
15
+ }
16
+ }
17
+ }
18
+
19
+ cache: ContextCache = new ContextCache();
20
+ viewer: Viewer = new LoggedOutViewer(this);
21
+
22
+ getViewer(): Viewer {
23
+ return this.viewer;
24
+ }
25
+ }
@@ -0,0 +1,32 @@
1
+ import { Client } from "pg";
2
+ import { Data } from "../../core/base";
3
+ import { getFields, getStorageKey, Schema } from "../../schema";
4
+ import { getDefaultValue } from "./value";
5
+ import { buildInsertQuery } from "../../core/ent";
6
+
7
+ interface Options {
8
+ overrides?: Data;
9
+ client: Client;
10
+ tableName: string;
11
+ }
12
+
13
+ export async function writeFixture(schema: Schema, opts: Options) {
14
+ const fields = getFields(schema);
15
+ const d: Data = {};
16
+ for (const [fieldName, field] of fields) {
17
+ const col = getStorageKey(field, fieldName);
18
+ const val = getDefaultValue(field, col);
19
+ d[col] = val;
20
+ }
21
+ if (opts.overrides) {
22
+ for (const k in opts.overrides) {
23
+ d[k] = opts.overrides[k];
24
+ }
25
+ }
26
+
27
+ const q = buildInsertQuery({
28
+ tableName: opts.tableName,
29
+ fields: d,
30
+ });
31
+ await opts.client.query(q[0], q[1]);
32
+ }