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

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/core → core}/query/assoc_query.d.ts +3 -0
  2. package/{dist/core → core}/query/assoc_query.js +71 -0
  3. package/{dist/core → core}/query/query.d.ts +3 -0
  4. package/{dist/core → core}/query/query.js +20 -6
  5. package/core/query/shared_assoc_test.d.ts +2 -0
  6. package/core/query/shared_assoc_test.js +1089 -0
  7. package/core/query/shared_test.d.ts +21 -0
  8. package/core/query/shared_test.js +736 -0
  9. package/graphql/query/shared_assoc_test.d.ts +1 -0
  10. package/graphql/query/shared_assoc_test.js +203 -0
  11. package/package.json +8 -53
  12. package/{dist/testutils → testutils}/fake_data/test_helpers.js +2 -1
  13. package/dist/package.json +0 -64
  14. package/src/action/action.ts +0 -330
  15. package/src/action/executor.ts +0 -453
  16. package/src/action/experimental_action.ts +0 -277
  17. package/src/action/index.ts +0 -31
  18. package/src/action/operations.ts +0 -967
  19. package/src/action/orchestrator.ts +0 -1527
  20. package/src/action/privacy.ts +0 -37
  21. package/src/action/relative_value.ts +0 -242
  22. package/src/action/transaction.ts +0 -38
  23. package/src/auth/auth.ts +0 -77
  24. package/src/auth/index.ts +0 -8
  25. package/src/core/base.ts +0 -367
  26. package/src/core/clause.ts +0 -1065
  27. package/src/core/config.ts +0 -219
  28. package/src/core/const.ts +0 -5
  29. package/src/core/context.ts +0 -135
  30. package/src/core/convert.ts +0 -106
  31. package/src/core/date.ts +0 -23
  32. package/src/core/db.ts +0 -498
  33. package/src/core/ent.ts +0 -1740
  34. package/src/core/global_schema.ts +0 -49
  35. package/src/core/loaders/assoc_count_loader.ts +0 -99
  36. package/src/core/loaders/assoc_edge_loader.ts +0 -250
  37. package/src/core/loaders/index.ts +0 -12
  38. package/src/core/loaders/loader.ts +0 -66
  39. package/src/core/loaders/object_loader.ts +0 -489
  40. package/src/core/loaders/query_loader.ts +0 -314
  41. package/src/core/loaders/raw_count_loader.ts +0 -175
  42. package/src/core/logger.ts +0 -49
  43. package/src/core/privacy.ts +0 -660
  44. package/src/core/query/assoc_query.ts +0 -240
  45. package/src/core/query/custom_clause_query.ts +0 -174
  46. package/src/core/query/custom_query.ts +0 -302
  47. package/src/core/query/index.ts +0 -9
  48. package/src/core/query/query.ts +0 -674
  49. package/src/core/query_impl.ts +0 -32
  50. package/src/core/viewer.ts +0 -52
  51. package/src/ent.code-workspace +0 -73
  52. package/src/graphql/builtins/connection.ts +0 -25
  53. package/src/graphql/builtins/edge.ts +0 -16
  54. package/src/graphql/builtins/node.ts +0 -12
  55. package/src/graphql/graphql.ts +0 -891
  56. package/src/graphql/graphql_field_helpers.ts +0 -221
  57. package/src/graphql/index.ts +0 -42
  58. package/src/graphql/mutations/union.ts +0 -39
  59. package/src/graphql/node_resolver.ts +0 -122
  60. package/src/graphql/query/connection_type.ts +0 -113
  61. package/src/graphql/query/edge_connection.ts +0 -171
  62. package/src/graphql/query/page_info.ts +0 -34
  63. package/src/graphql/query/shared_edge_connection.ts +0 -287
  64. package/src/graphql/scalars/orderby_direction.ts +0 -13
  65. package/src/graphql/scalars/time.ts +0 -38
  66. package/src/imports/dataz/example1/_auth.ts +0 -51
  67. package/src/imports/dataz/example1/_viewer.ts +0 -35
  68. package/src/imports/index.ts +0 -213
  69. package/src/index.ts +0 -145
  70. package/src/parse_schema/parse.ts +0 -585
  71. package/src/schema/base_schema.ts +0 -224
  72. package/src/schema/field.ts +0 -1087
  73. package/src/schema/index.ts +0 -53
  74. package/src/schema/json_field.ts +0 -94
  75. package/src/schema/schema.ts +0 -1028
  76. package/src/schema/struct_field.ts +0 -234
  77. package/src/schema/union_field.ts +0 -105
  78. package/src/scripts/custom_compiler.ts +0 -331
  79. package/src/scripts/custom_graphql.ts +0 -550
  80. package/src/scripts/migrate_v0.1.ts +0 -41
  81. package/src/scripts/move_types.ts +0 -131
  82. package/src/scripts/read_schema.ts +0 -67
  83. package/src/setupPackage.js +0 -42
  84. package/src/testutils/action/complex_schemas.ts +0 -517
  85. package/src/testutils/builder.ts +0 -422
  86. package/src/testutils/context/test_context.ts +0 -25
  87. package/src/testutils/db/fixture.ts +0 -32
  88. package/src/testutils/db/temp_db.ts +0 -941
  89. package/src/testutils/db/value.ts +0 -294
  90. package/src/testutils/db_mock.ts +0 -351
  91. package/src/testutils/db_time_zone.ts +0 -40
  92. package/src/testutils/ent-graphql-tests/index.ts +0 -653
  93. package/src/testutils/fake_comms.ts +0 -50
  94. package/src/testutils/fake_data/const.ts +0 -64
  95. package/src/testutils/fake_data/events_query.ts +0 -145
  96. package/src/testutils/fake_data/fake_contact.ts +0 -150
  97. package/src/testutils/fake_data/fake_event.ts +0 -150
  98. package/src/testutils/fake_data/fake_tag.ts +0 -139
  99. package/src/testutils/fake_data/fake_user.ts +0 -232
  100. package/src/testutils/fake_data/index.ts +0 -1
  101. package/src/testutils/fake_data/internal.ts +0 -8
  102. package/src/testutils/fake_data/tag_query.ts +0 -56
  103. package/src/testutils/fake_data/test_helpers.ts +0 -388
  104. package/src/testutils/fake_data/user_query.ts +0 -524
  105. package/src/testutils/fake_log.ts +0 -52
  106. package/src/testutils/mock_date.ts +0 -10
  107. package/src/testutils/mock_log.ts +0 -39
  108. package/src/testutils/parse_sql.ts +0 -685
  109. package/src/testutils/test_edge_global_schema.ts +0 -49
  110. package/src/testutils/write.ts +0 -70
  111. package/src/tsc/ast.ts +0 -351
  112. package/src/tsc/compilerOptions.ts +0 -85
  113. package/src/tsc/move_generated.ts +0 -191
  114. package/src/tsc/transform.ts +0 -226
  115. package/src/tsc/transform_action.ts +0 -224
  116. package/src/tsc/transform_ent.ts +0 -66
  117. package/src/tsc/transform_schema.ts +0 -546
  118. package/tsconfig.json +0 -20
  119. /package/{dist/action → action}/action.d.ts +0 -0
  120. /package/{dist/action → action}/action.js +0 -0
  121. /package/{dist/action → action}/executor.d.ts +0 -0
  122. /package/{dist/action → action}/executor.js +0 -0
  123. /package/{dist/action → action}/experimental_action.d.ts +0 -0
  124. /package/{dist/action → action}/experimental_action.js +0 -0
  125. /package/{dist/action → action}/index.d.ts +0 -0
  126. /package/{dist/action → action}/index.js +0 -0
  127. /package/{dist/action → action}/operations.d.ts +0 -0
  128. /package/{dist/action → action}/operations.js +0 -0
  129. /package/{dist/action → action}/orchestrator.d.ts +0 -0
  130. /package/{dist/action → action}/orchestrator.js +0 -0
  131. /package/{dist/action → action}/privacy.d.ts +0 -0
  132. /package/{dist/action → action}/privacy.js +0 -0
  133. /package/{dist/action → action}/relative_value.d.ts +0 -0
  134. /package/{dist/action → action}/relative_value.js +0 -0
  135. /package/{dist/action → action}/transaction.d.ts +0 -0
  136. /package/{dist/action → action}/transaction.js +0 -0
  137. /package/{dist/auth → auth}/auth.d.ts +0 -0
  138. /package/{dist/auth → auth}/auth.js +0 -0
  139. /package/{dist/auth → auth}/index.d.ts +0 -0
  140. /package/{dist/auth → auth}/index.js +0 -0
  141. /package/{dist/core → core}/base.d.ts +0 -0
  142. /package/{dist/core → core}/base.js +0 -0
  143. /package/{dist/core → core}/clause.d.ts +0 -0
  144. /package/{dist/core → core}/clause.js +0 -0
  145. /package/{dist/core → core}/config.d.ts +0 -0
  146. /package/{dist/core → core}/config.js +0 -0
  147. /package/{dist/core → core}/const.d.ts +0 -0
  148. /package/{dist/core → core}/const.js +0 -0
  149. /package/{dist/core → core}/context.d.ts +0 -0
  150. /package/{dist/core → core}/context.js +0 -0
  151. /package/{dist/core → core}/convert.d.ts +0 -0
  152. /package/{dist/core → core}/convert.js +0 -0
  153. /package/{dist/core → core}/date.d.ts +0 -0
  154. /package/{dist/core → core}/date.js +0 -0
  155. /package/{dist/core → core}/db.d.ts +0 -0
  156. /package/{dist/core → core}/db.js +0 -0
  157. /package/{dist/core → core}/ent.d.ts +0 -0
  158. /package/{dist/core → core}/ent.js +0 -0
  159. /package/{dist/core → core}/global_schema.d.ts +0 -0
  160. /package/{dist/core → core}/global_schema.js +0 -0
  161. /package/{dist/core → core}/loaders/assoc_count_loader.d.ts +0 -0
  162. /package/{dist/core → core}/loaders/assoc_count_loader.js +0 -0
  163. /package/{dist/core → core}/loaders/assoc_edge_loader.d.ts +0 -0
  164. /package/{dist/core → core}/loaders/assoc_edge_loader.js +0 -0
  165. /package/{dist/core → core}/loaders/index.d.ts +0 -0
  166. /package/{dist/core → core}/loaders/index.js +0 -0
  167. /package/{dist/core → core}/loaders/loader.d.ts +0 -0
  168. /package/{dist/core → core}/loaders/loader.js +0 -0
  169. /package/{dist/core → core}/loaders/object_loader.d.ts +0 -0
  170. /package/{dist/core → core}/loaders/object_loader.js +0 -0
  171. /package/{dist/core → core}/loaders/query_loader.d.ts +0 -0
  172. /package/{dist/core → core}/loaders/query_loader.js +0 -0
  173. /package/{dist/core → core}/loaders/raw_count_loader.d.ts +0 -0
  174. /package/{dist/core → core}/loaders/raw_count_loader.js +0 -0
  175. /package/{dist/core → core}/logger.d.ts +0 -0
  176. /package/{dist/core → core}/logger.js +0 -0
  177. /package/{dist/core → core}/privacy.d.ts +0 -0
  178. /package/{dist/core → core}/privacy.js +0 -0
  179. /package/{dist/core → core}/query/custom_clause_query.d.ts +0 -0
  180. /package/{dist/core → core}/query/custom_clause_query.js +0 -0
  181. /package/{dist/core → core}/query/custom_query.d.ts +0 -0
  182. /package/{dist/core → core}/query/custom_query.js +0 -0
  183. /package/{dist/core → core}/query/index.d.ts +0 -0
  184. /package/{dist/core → core}/query/index.js +0 -0
  185. /package/{dist/core → core}/query_impl.d.ts +0 -0
  186. /package/{dist/core → core}/query_impl.js +0 -0
  187. /package/{dist/core → core}/viewer.d.ts +0 -0
  188. /package/{dist/core → core}/viewer.js +0 -0
  189. /package/{dist/graphql → graphql}/builtins/connection.d.ts +0 -0
  190. /package/{dist/graphql → graphql}/builtins/connection.js +0 -0
  191. /package/{dist/graphql → graphql}/builtins/edge.d.ts +0 -0
  192. /package/{dist/graphql → graphql}/builtins/edge.js +0 -0
  193. /package/{dist/graphql → graphql}/builtins/node.d.ts +0 -0
  194. /package/{dist/graphql → graphql}/builtins/node.js +0 -0
  195. /package/{dist/graphql → graphql}/graphql.d.ts +0 -0
  196. /package/{dist/graphql → graphql}/graphql.js +0 -0
  197. /package/{dist/graphql → graphql}/graphql_field_helpers.d.ts +0 -0
  198. /package/{dist/graphql → graphql}/graphql_field_helpers.js +0 -0
  199. /package/{dist/graphql → graphql}/index.d.ts +0 -0
  200. /package/{dist/graphql → graphql}/index.js +0 -0
  201. /package/{dist/graphql → graphql}/mutations/union.d.ts +0 -0
  202. /package/{dist/graphql → graphql}/mutations/union.js +0 -0
  203. /package/{dist/graphql → graphql}/node_resolver.d.ts +0 -0
  204. /package/{dist/graphql → graphql}/node_resolver.js +0 -0
  205. /package/{dist/graphql → graphql}/query/connection_type.d.ts +0 -0
  206. /package/{dist/graphql → graphql}/query/connection_type.js +0 -0
  207. /package/{dist/graphql → graphql}/query/edge_connection.d.ts +0 -0
  208. /package/{dist/graphql → graphql}/query/edge_connection.js +0 -0
  209. /package/{dist/graphql → graphql}/query/page_info.d.ts +0 -0
  210. /package/{dist/graphql → graphql}/query/page_info.js +0 -0
  211. /package/{dist/graphql → graphql}/query/shared_edge_connection.d.ts +0 -0
  212. /package/{dist/graphql → graphql}/query/shared_edge_connection.js +0 -0
  213. /package/{dist/graphql → graphql}/scalars/orderby_direction.d.ts +0 -0
  214. /package/{dist/graphql → graphql}/scalars/orderby_direction.js +0 -0
  215. /package/{dist/graphql → graphql}/scalars/time.d.ts +0 -0
  216. /package/{dist/graphql → graphql}/scalars/time.js +0 -0
  217. /package/{dist/imports → imports}/dataz/example1/_auth.d.ts +0 -0
  218. /package/{dist/imports → imports}/dataz/example1/_auth.js +0 -0
  219. /package/{dist/imports → imports}/dataz/example1/_viewer.d.ts +0 -0
  220. /package/{dist/imports → imports}/dataz/example1/_viewer.js +0 -0
  221. /package/{dist/imports → imports}/index.d.ts +0 -0
  222. /package/{dist/imports → imports}/index.js +0 -0
  223. /package/{dist/index.d.ts → index.d.ts} +0 -0
  224. /package/{dist/index.js → index.js} +0 -0
  225. /package/{dist/parse_schema → parse_schema}/parse.d.ts +0 -0
  226. /package/{dist/parse_schema → parse_schema}/parse.js +0 -0
  227. /package/{dist/schema → schema}/base_schema.d.ts +0 -0
  228. /package/{dist/schema → schema}/base_schema.js +0 -0
  229. /package/{dist/schema → schema}/field.d.ts +0 -0
  230. /package/{dist/schema → schema}/field.js +0 -0
  231. /package/{dist/schema → schema}/index.d.ts +0 -0
  232. /package/{dist/schema → schema}/index.js +0 -0
  233. /package/{dist/schema → schema}/json_field.d.ts +0 -0
  234. /package/{dist/schema → schema}/json_field.js +0 -0
  235. /package/{dist/schema → schema}/schema.d.ts +0 -0
  236. /package/{dist/schema → schema}/schema.js +0 -0
  237. /package/{dist/schema → schema}/struct_field.d.ts +0 -0
  238. /package/{dist/schema → schema}/struct_field.js +0 -0
  239. /package/{dist/schema → schema}/union_field.d.ts +0 -0
  240. /package/{dist/schema → schema}/union_field.js +0 -0
  241. /package/{dist/scripts → scripts}/custom_compiler.d.ts +0 -0
  242. /package/{dist/scripts → scripts}/custom_compiler.js +0 -0
  243. /package/{dist/scripts → scripts}/custom_graphql.d.ts +0 -0
  244. /package/{dist/scripts → scripts}/custom_graphql.js +0 -0
  245. /package/{dist/scripts → scripts}/migrate_v0.1.d.ts +0 -0
  246. /package/{dist/scripts → scripts}/migrate_v0.1.js +0 -0
  247. /package/{dist/scripts → scripts}/move_types.d.ts +0 -0
  248. /package/{dist/scripts → scripts}/move_types.js +0 -0
  249. /package/{dist/scripts → scripts}/read_schema.d.ts +0 -0
  250. /package/{dist/scripts → scripts}/read_schema.js +0 -0
  251. /package/{dist/testutils → testutils}/action/complex_schemas.d.ts +0 -0
  252. /package/{dist/testutils → testutils}/action/complex_schemas.js +0 -0
  253. /package/{dist/testutils → testutils}/builder.d.ts +0 -0
  254. /package/{dist/testutils → testutils}/builder.js +0 -0
  255. /package/{dist/testutils → testutils}/context/test_context.d.ts +0 -0
  256. /package/{dist/testutils → testutils}/context/test_context.js +0 -0
  257. /package/{dist/testutils → testutils}/db/fixture.d.ts +0 -0
  258. /package/{dist/testutils → testutils}/db/fixture.js +0 -0
  259. /package/{dist/testutils → testutils}/db/temp_db.d.ts +0 -0
  260. /package/{dist/testutils → testutils}/db/temp_db.js +0 -0
  261. /package/{dist/testutils → testutils}/db/value.d.ts +0 -0
  262. /package/{dist/testutils → testutils}/db/value.js +0 -0
  263. /package/{dist/testutils → testutils}/db_mock.d.ts +0 -0
  264. /package/{dist/testutils → testutils}/db_mock.js +0 -0
  265. /package/{dist/testutils → testutils}/db_time_zone.d.ts +0 -0
  266. /package/{dist/testutils → testutils}/db_time_zone.js +0 -0
  267. /package/{dist/testutils → testutils}/ent-graphql-tests/index.d.ts +0 -0
  268. /package/{dist/testutils → testutils}/ent-graphql-tests/index.js +0 -0
  269. /package/{dist/testutils → testutils}/fake_comms.d.ts +0 -0
  270. /package/{dist/testutils → testutils}/fake_comms.js +0 -0
  271. /package/{dist/testutils → testutils}/fake_data/const.d.ts +0 -0
  272. /package/{dist/testutils → testutils}/fake_data/const.js +0 -0
  273. /package/{dist/testutils → testutils}/fake_data/events_query.d.ts +0 -0
  274. /package/{dist/testutils → testutils}/fake_data/events_query.js +0 -0
  275. /package/{dist/testutils → testutils}/fake_data/fake_contact.d.ts +0 -0
  276. /package/{dist/testutils → testutils}/fake_data/fake_contact.js +0 -0
  277. /package/{dist/testutils → testutils}/fake_data/fake_event.d.ts +0 -0
  278. /package/{dist/testutils → testutils}/fake_data/fake_event.js +0 -0
  279. /package/{dist/testutils → testutils}/fake_data/fake_tag.d.ts +0 -0
  280. /package/{dist/testutils → testutils}/fake_data/fake_tag.js +0 -0
  281. /package/{dist/testutils → testutils}/fake_data/fake_user.d.ts +0 -0
  282. /package/{dist/testutils → testutils}/fake_data/fake_user.js +0 -0
  283. /package/{dist/testutils → testutils}/fake_data/index.d.ts +0 -0
  284. /package/{dist/testutils → testutils}/fake_data/index.js +0 -0
  285. /package/{dist/testutils → testutils}/fake_data/internal.d.ts +0 -0
  286. /package/{dist/testutils → testutils}/fake_data/internal.js +0 -0
  287. /package/{dist/testutils → testutils}/fake_data/tag_query.d.ts +0 -0
  288. /package/{dist/testutils → testutils}/fake_data/tag_query.js +0 -0
  289. /package/{dist/testutils → testutils}/fake_data/test_helpers.d.ts +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
package/src/core/db.ts DELETED
@@ -1,498 +0,0 @@
1
- import pg, { Pool, PoolClient, PoolConfig } from "pg";
2
- import * as fs from "fs";
3
- import { load } from "js-yaml";
4
- import { log } from "./logger";
5
- import { DateTime } from "luxon";
6
-
7
- export interface Database extends PoolConfig {
8
- database?: string;
9
- user?: string;
10
- password?: string;
11
- host?: string;
12
- port?: number;
13
- sslmode?: string;
14
- }
15
-
16
- // probably should just be string?
17
- // depends on NODE_ENV values.
18
- export type env = "production" | "test" | "development";
19
- export declare type DBDict = Partial<Record<env, Database>>;
20
-
21
- function isDbDict(v: Database | DBDict): v is DBDict {
22
- return (
23
- v["production"] !== undefined ||
24
- v["development"] !== undefined ||
25
- v["test"] !== undefined
26
- );
27
- }
28
-
29
- interface customPoolConfig extends PoolConfig {
30
- sslmode?: string;
31
- }
32
-
33
- export enum Dialect {
34
- Postgres = "postgres",
35
- SQLite = "sqlite",
36
- }
37
-
38
- function parseConnectionString(
39
- str: string,
40
- args?: clientConfigArgs,
41
- ): DatabaseInfo {
42
- if (str.startsWith("sqlite:///")) {
43
- let filePath = str.substr(10);
44
-
45
- return {
46
- dialect: Dialect.SQLite,
47
- config: {
48
- connectionString: str,
49
- ...args?.cfg,
50
- },
51
- filePath,
52
- };
53
- }
54
-
55
- return {
56
- dialect: Dialect.Postgres,
57
- config: {
58
- ...args?.cfg,
59
- connectionString: str,
60
- },
61
- };
62
- }
63
-
64
- interface DatabaseInfo {
65
- dialect: Dialect;
66
- config: PoolConfig;
67
- /// filePath for sqlite
68
- filePath?: string;
69
- }
70
-
71
- interface clientConfigArgs {
72
- connectionString?: string;
73
- dbFile?: string;
74
- db?: Database | DBDict;
75
- cfg?: PoolConfig;
76
- }
77
- // order
78
- // env variable
79
- // connString in config
80
- // db in Config file (helpful for test vs development)
81
- // database file in yml file
82
- // database/config.yml
83
- function getClientConfig(args?: clientConfigArgs): DatabaseInfo | null {
84
- // if there's a db connection string, use that first
85
- const str = process.env.DB_CONNECTION_STRING;
86
- if (str) {
87
- return parseConnectionString(str, args);
88
- }
89
-
90
- let file = "config/database.yml";
91
- if (args) {
92
- if (args.connectionString) {
93
- return parseConnectionString(args.connectionString, args);
94
- }
95
-
96
- if (args.db) {
97
- let db: Database;
98
- if (isDbDict(args.db)) {
99
- if (!process.env.NODE_ENV) {
100
- throw new Error(`process.env.NODE_ENV is undefined`);
101
- }
102
- db = args.db[process.env.NODE_ENV];
103
- } else {
104
- db = args.db;
105
- }
106
- return {
107
- dialect: Dialect.Postgres,
108
- config: db,
109
- };
110
- }
111
-
112
- if (args.dbFile) {
113
- file = args.dbFile;
114
- }
115
- }
116
-
117
- if (!fs.existsSync(file)) {
118
- return null;
119
- }
120
-
121
- try {
122
- // TODO support multiple environments in database/config.yaml file.
123
- // if needed for now, general yaml file should be used
124
- let data = fs.readFileSync(file, { encoding: "utf8" });
125
- let yaml = load(data);
126
- if (yaml && typeof yaml === "object") {
127
- let cfg: customPoolConfig = yaml;
128
- return {
129
- dialect: Dialect.Postgres,
130
- config: {
131
- database: cfg.database,
132
- user: cfg.user,
133
- password: cfg.password,
134
- host: cfg.host,
135
- port: cfg.port,
136
- ssl: cfg.sslmode == "enable",
137
- // max, min, etc
138
- ...cfg,
139
- },
140
- };
141
- }
142
- throw new Error(`invalid yaml configuration in file`);
143
- } catch (e) {
144
- console.error("error reading file" + e.message);
145
- return null;
146
- }
147
- }
148
-
149
- export default class DB {
150
- static instance: DB;
151
- static dialect: Dialect;
152
-
153
- private pool: Pool;
154
- private q: Connection;
155
- private constructor(public db: DatabaseInfo) {
156
- if (db.dialect === Dialect.Postgres) {
157
- this.pool = new Pool(db.config);
158
- this.q = new Postgres(this.pool);
159
-
160
- this.pool.on("error", (err, client) => {
161
- log("error", err);
162
- });
163
- } else {
164
- let sqlite = require("better-sqlite3");
165
- const dbb = sqlite(db.filePath || "");
166
- dbb.pragma("journal_mode = WAL");
167
- this.q = new Sqlite(dbb);
168
- }
169
- }
170
-
171
- getConnection(): Connection {
172
- return this.q;
173
- }
174
-
175
- // TODO rename all these...
176
- getPool(): Queryer {
177
- return this.q.self();
178
- }
179
-
180
- // TODO rename
181
- // expect to release client as needed
182
- async getNewClient(): Promise<Client> {
183
- return this.q.newClient();
184
- }
185
-
186
- getSQLiteClient(): Sqlite {
187
- if (this.db.dialect == Dialect.Postgres) {
188
- throw new Error(`can't call getSQLiteClient when dialect is postgres`);
189
- }
190
- return this.q as Sqlite;
191
- }
192
-
193
- // this should be called when the server is shutting down or end of tests.
194
- async endPool(): Promise<void> {
195
- return this.q.close();
196
- }
197
-
198
- emitsExplicitTransactionStatements() {
199
- const instance = DB.getInstance();
200
- return instance.q.runInTransaction === undefined;
201
- }
202
-
203
- // throws if invalid
204
- static getInstance(): DB {
205
- if (DB.instance) {
206
- return DB.instance;
207
- }
208
-
209
- const clientConfig = getClientConfig();
210
- if (!clientConfig) {
211
- throw new Error("could not load client config");
212
- }
213
- DB.instance = new DB(clientConfig);
214
- DB.dialect = DB.instance.db.dialect;
215
- return DB.instance;
216
- }
217
-
218
- static getDialect(): Dialect {
219
- if (DB.dialect) {
220
- return DB.dialect;
221
- }
222
- // default to postgres
223
- return Dialect.Postgres;
224
- }
225
-
226
- static initDB(args?: clientConfigArgs) {
227
- const config = getClientConfig(args);
228
- if (config) {
229
- DB.instance = new DB(config);
230
- DB.dialect = DB.instance.db.dialect;
231
- }
232
- }
233
- }
234
-
235
- export const defaultTimestampParser = pg.types.getTypeParser(
236
- pg.types.builtins.TIMESTAMP,
237
- );
238
-
239
- // this is stored in the db without timezone but we want to make sure
240
- // it's parsed as UTC time as opposed to the local time
241
- pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, function (val: string) {
242
- return DateTime.fromSQL(val + "Z").toJSDate();
243
- });
244
-
245
- export interface Queryer {
246
- query(query: string, values?: any[]): Promise<QueryResult<QueryResultRow>>;
247
- queryAll(query: string, values?: any[]): Promise<QueryResult<QueryResultRow>>;
248
- // exec a query with no result
249
- // e.g. insert in sqlite etc
250
- // exec has no async/await in sqlite...
251
- exec(query: string, values?: any[]): Promise<ExecResult>;
252
- }
253
-
254
- // if this exists, we don't want to use async/await
255
- // e.g. SQLite
256
- export interface SyncQueryer extends Queryer {
257
- execSync(query: string, values?: any[]): ExecResult;
258
- queryAllSync(query: string, values?: any[]): QueryResult<QueryResultRow>;
259
- querySync(query: string, values?: any[]): QueryResult<QueryResultRow>;
260
- }
261
-
262
- export interface Connection extends Queryer {
263
- self(): Queryer;
264
- newClient(): Promise<Client>;
265
- close(): Promise<void>;
266
- runInTransaction?(cb: () => void | Promise<void>);
267
- }
268
-
269
- export interface QueryResultRow {
270
- [column: string]: any;
271
- }
272
-
273
- export interface QueryResult<R extends QueryResultRow = any> {
274
- rows: R[];
275
- rowCount: number;
276
- // postgres fields
277
- // command: string;
278
- // oid: number;
279
- // fields: FieldDef[];
280
- }
281
-
282
- interface ExecResult {
283
- rows: QueryResultRow[];
284
- rowCount: number;
285
- }
286
-
287
- export interface Client extends Queryer {
288
- release(err?: Error | boolean): void;
289
- }
290
-
291
- export interface SyncClient extends Client, SyncQueryer {
292
- runInTransaction(cb: () => void): void;
293
- }
294
-
295
- // interfaces we provide to have strong typing here so as to not import
296
- // better-sqlite3. ideally, import type would be used but that seems
297
- // to have downstream issues when this library is used
298
- interface SqliteRunResult {
299
- changes: number;
300
- lastInsertRowid: number;
301
- }
302
-
303
- interface SqliteDatabase {
304
- memory: boolean;
305
- exec(query: string): SqliteStatement;
306
- prepare(query: string): SqliteStatement;
307
- close(): void;
308
- transaction(fn: (...params: any[]) => any): SqliteTransaction;
309
- }
310
-
311
- interface SqliteStatement {
312
- get(...params: any): SqliteRunResult;
313
- all(...params: any): SqliteRunResult[];
314
- run(...params: any): SqliteRunResult;
315
- }
316
-
317
- interface SqliteTransaction {
318
- (...params: any): void;
319
- }
320
-
321
- export class Sqlite implements Connection, SyncClient {
322
- constructor(public db: SqliteDatabase) {}
323
-
324
- self() {
325
- return this;
326
- }
327
-
328
- // returns self
329
- async newClient() {
330
- return this;
331
- }
332
-
333
- async query(
334
- query: string,
335
- values?: any[],
336
- ): Promise<QueryResult<QueryResultRow>> {
337
- return this.querySync(query, values);
338
- }
339
-
340
- private convertValues(values: any[]) {
341
- for (const key in values) {
342
- let value = values[key];
343
- if (value === true) {
344
- values[key] = 1;
345
- } else if (value === false) {
346
- values[key] = 0;
347
- } else if (value instanceof Date) {
348
- values[key] = value.toISOString();
349
- }
350
- }
351
- return values;
352
- }
353
-
354
- querySync(query: string, values?: any[]): QueryResult<QueryResultRow> {
355
- let r: SqliteRunResult;
356
-
357
- if (values) {
358
- r = this.db.prepare(query).get(this.convertValues(values));
359
- } else {
360
- // TODO querySync() with no values seems to do the wrong thing...
361
- // e.g. querySync('select count(*) as count from table') returns nonsense
362
- r = this.db.prepare(query).run();
363
- }
364
- return {
365
- rowCount: r === undefined ? 0 : 1,
366
- rows: r ? [r] : [],
367
- };
368
- }
369
-
370
- async queryAll(
371
- query: string,
372
- values?: any[],
373
- ): Promise<QueryResult<QueryResultRow>> {
374
- return this.queryAllSync(query, values);
375
- }
376
-
377
- queryAllSync(query: string, values?: any[]): QueryResult<QueryResultRow> {
378
- let r: any[];
379
- if (values) {
380
- r = this.db.prepare(query).all(this.convertValues(values));
381
- } else {
382
- r = this.db.prepare(query).all();
383
- }
384
- return {
385
- rowCount: r.length,
386
- rows: r,
387
- };
388
- }
389
-
390
- async exec(query: string, values?: any[]): Promise<ExecResult> {
391
- throw new Error(
392
- `exec shouldn't be called. execSync() should be called instead`,
393
- );
394
- }
395
-
396
- execSync(query: string, values?: any[]): ExecResult {
397
- let r: SqliteRunResult;
398
- if (values) {
399
- r = this.db.prepare(query).run(this.convertValues(values));
400
- } else {
401
- r = this.db.prepare(query).run();
402
- }
403
- return {
404
- rowCount: r!.changes,
405
- rows: [],
406
- };
407
- }
408
-
409
- async close() {
410
- this.db.close();
411
- }
412
-
413
- async release(err?: Error | boolean) {}
414
-
415
- runInTransaction(cb: () => void | Promise<void>) {
416
- const tr = this.db.transaction(() => {
417
- cb();
418
- });
419
- tr();
420
- }
421
- }
422
-
423
- export class Postgres implements Connection {
424
- constructor(private pool: Pool) {}
425
-
426
- self() {
427
- return this;
428
- }
429
-
430
- // returns new Pool client
431
- async newClient() {
432
- const client = await this.pool.connect();
433
- if (!client) {
434
- throw new Error(`couldn't get new client`);
435
- }
436
- return new PostgresClient(client);
437
- }
438
-
439
- async query(
440
- query: string,
441
- values?: any[],
442
- ): Promise<QueryResult<QueryResultRow>> {
443
- const r = await this.pool.query(query, values);
444
- return r;
445
- }
446
-
447
- async queryAll(
448
- query: string,
449
- values?: any[],
450
- ): Promise<QueryResult<QueryResultRow>> {
451
- const r = await this.pool.query(query, values);
452
- return r;
453
- }
454
-
455
- async exec(query: string, values?: any[]): Promise<ExecResult> {
456
- const r = await this.pool.query(query, values);
457
- return {
458
- rowCount: r?.rowCount || 0,
459
- rows: r?.rows || [],
460
- };
461
- }
462
-
463
- async close() {
464
- return await this.pool.end();
465
- }
466
- }
467
-
468
- export class PostgresClient implements Client {
469
- constructor(private client: PoolClient) {}
470
-
471
- async query(
472
- query: string,
473
- values?: any[],
474
- ): Promise<QueryResult<QueryResultRow>> {
475
- const r = await this.client.query(query, values);
476
- return r;
477
- }
478
-
479
- async queryAll(
480
- query: string,
481
- values?: any[],
482
- ): Promise<QueryResult<QueryResultRow>> {
483
- const r = await this.client.query(query, values);
484
- return r;
485
- }
486
-
487
- async exec(query: string, values?: any[]): Promise<ExecResult> {
488
- const r = await this.client.query(query, values);
489
- return {
490
- rowCount: r?.rowCount || 0,
491
- rows: r?.rows || [],
492
- };
493
- }
494
-
495
- async release(err?: Error | boolean) {
496
- return this.client.release(err);
497
- }
498
- }