@snowtop/ent 0.1.0-alpha16 → 0.1.0-alpha160-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 (176) hide show
  1. package/action/action.d.ts +25 -14
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +16 -3
  4. package/action/executor.js +89 -28
  5. package/action/experimental_action.d.ts +25 -16
  6. package/action/experimental_action.js +34 -14
  7. package/action/index.d.ts +4 -1
  8. package/action/index.js +7 -1
  9. package/action/operations.d.ts +126 -0
  10. package/action/operations.js +686 -0
  11. package/action/orchestrator.d.ts +43 -12
  12. package/action/orchestrator.js +461 -101
  13. package/action/relative_value.d.ts +47 -0
  14. package/action/relative_value.js +125 -0
  15. package/action/transaction.d.ts +10 -0
  16. package/action/transaction.js +23 -0
  17. package/auth/auth.d.ts +1 -1
  18. package/core/base.d.ts +56 -23
  19. package/core/base.js +7 -1
  20. package/core/clause.d.ts +103 -39
  21. package/core/clause.js +430 -66
  22. package/core/config.d.ts +13 -3
  23. package/core/config.js +10 -1
  24. package/core/const.d.ts +3 -0
  25. package/core/const.js +6 -0
  26. package/core/context.d.ts +6 -3
  27. package/core/context.js +22 -3
  28. package/core/convert.d.ts +1 -1
  29. package/core/date.js +1 -5
  30. package/core/db.d.ts +12 -8
  31. package/core/db.js +21 -9
  32. package/core/ent.d.ts +99 -95
  33. package/core/ent.js +550 -602
  34. package/core/global_schema.d.ts +7 -0
  35. package/core/global_schema.js +51 -0
  36. package/core/loaders/assoc_count_loader.d.ts +5 -2
  37. package/core/loaders/assoc_count_loader.js +19 -3
  38. package/core/loaders/assoc_edge_loader.d.ts +2 -3
  39. package/core/loaders/assoc_edge_loader.js +23 -17
  40. package/core/loaders/index.d.ts +1 -2
  41. package/core/loaders/index.js +1 -5
  42. package/core/loaders/loader.d.ts +3 -3
  43. package/core/loaders/loader.js +4 -21
  44. package/core/loaders/object_loader.d.ts +30 -9
  45. package/core/loaders/object_loader.js +226 -79
  46. package/core/loaders/query_loader.d.ts +7 -13
  47. package/core/loaders/query_loader.js +60 -24
  48. package/core/loaders/raw_count_loader.d.ts +1 -0
  49. package/core/loaders/raw_count_loader.js +8 -3
  50. package/core/logger.d.ts +1 -1
  51. package/core/logger.js +1 -0
  52. package/core/privacy.d.ts +26 -16
  53. package/core/privacy.js +68 -51
  54. package/core/query/assoc_query.d.ts +3 -2
  55. package/core/query/assoc_query.js +10 -2
  56. package/core/query/custom_clause_query.d.ts +29 -0
  57. package/core/query/custom_clause_query.js +105 -0
  58. package/core/query/custom_query.d.ts +19 -2
  59. package/core/query/custom_query.js +111 -13
  60. package/core/query/index.d.ts +1 -0
  61. package/core/query/index.js +3 -1
  62. package/core/query/query.d.ts +18 -4
  63. package/core/query/query.js +135 -58
  64. package/core/query/shared_assoc_test.d.ts +2 -1
  65. package/core/query/shared_assoc_test.js +186 -55
  66. package/core/query/shared_test.d.ts +9 -2
  67. package/core/query/shared_test.js +529 -236
  68. package/core/query_impl.d.ts +8 -0
  69. package/core/query_impl.js +28 -0
  70. package/core/viewer.d.ts +2 -0
  71. package/core/viewer.js +3 -1
  72. package/graphql/graphql.d.ts +108 -22
  73. package/graphql/graphql.js +183 -137
  74. package/graphql/graphql_field_helpers.d.ts +9 -3
  75. package/graphql/graphql_field_helpers.js +22 -2
  76. package/graphql/index.d.ts +2 -2
  77. package/graphql/index.js +5 -5
  78. package/graphql/query/connection_type.d.ts +9 -9
  79. package/graphql/query/shared_assoc_test.js +1 -1
  80. package/graphql/query/shared_edge_connection.js +1 -19
  81. package/graphql/scalars/orderby_direction.d.ts +2 -0
  82. package/graphql/scalars/orderby_direction.js +15 -0
  83. package/imports/dataz/example1/_auth.js +128 -47
  84. package/imports/dataz/example1/_viewer.js +87 -39
  85. package/imports/index.d.ts +7 -2
  86. package/imports/index.js +20 -5
  87. package/index.d.ts +23 -5
  88. package/index.js +35 -10
  89. package/package.json +19 -19
  90. package/parse_schema/parse.d.ts +33 -9
  91. package/parse_schema/parse.js +182 -33
  92. package/schema/base_schema.d.ts +13 -3
  93. package/schema/base_schema.js +13 -0
  94. package/schema/field.d.ts +78 -21
  95. package/schema/field.js +232 -72
  96. package/schema/index.d.ts +2 -2
  97. package/schema/index.js +7 -2
  98. package/schema/json_field.d.ts +16 -4
  99. package/schema/json_field.js +32 -2
  100. package/schema/schema.d.ts +109 -20
  101. package/schema/schema.js +42 -53
  102. package/schema/struct_field.d.ts +15 -3
  103. package/schema/struct_field.js +117 -22
  104. package/schema/union_field.d.ts +1 -1
  105. package/scripts/custom_compiler.js +12 -8
  106. package/scripts/custom_graphql.js +171 -64
  107. package/scripts/migrate_v0.1.js +36 -0
  108. package/scripts/move_types.js +120 -0
  109. package/scripts/read_schema.js +22 -7
  110. package/testutils/action/complex_schemas.d.ts +69 -0
  111. package/testutils/action/complex_schemas.js +405 -0
  112. package/testutils/builder.d.ts +37 -41
  113. package/testutils/builder.js +66 -46
  114. package/testutils/db/fixture.d.ts +10 -0
  115. package/testutils/db/fixture.js +26 -0
  116. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +32 -8
  117. package/testutils/db/{test_db.js → temp_db.js} +251 -48
  118. package/testutils/db/value.d.ts +7 -0
  119. package/testutils/db/value.js +251 -0
  120. package/testutils/db_mock.d.ts +16 -4
  121. package/testutils/db_mock.js +52 -9
  122. package/testutils/db_time_zone.d.ts +4 -0
  123. package/testutils/db_time_zone.js +41 -0
  124. package/testutils/ent-graphql-tests/index.d.ts +7 -1
  125. package/testutils/ent-graphql-tests/index.js +56 -26
  126. package/testutils/fake_comms.js +1 -1
  127. package/testutils/fake_data/const.d.ts +2 -1
  128. package/testutils/fake_data/const.js +3 -0
  129. package/testutils/fake_data/fake_contact.d.ts +7 -3
  130. package/testutils/fake_data/fake_contact.js +13 -7
  131. package/testutils/fake_data/fake_event.d.ts +4 -1
  132. package/testutils/fake_data/fake_event.js +7 -6
  133. package/testutils/fake_data/fake_tag.d.ts +36 -0
  134. package/testutils/fake_data/fake_tag.js +89 -0
  135. package/testutils/fake_data/fake_user.d.ts +8 -5
  136. package/testutils/fake_data/fake_user.js +31 -19
  137. package/testutils/fake_data/index.js +5 -1
  138. package/testutils/fake_data/internal.d.ts +2 -0
  139. package/testutils/fake_data/internal.js +7 -1
  140. package/testutils/fake_data/tag_query.d.ts +13 -0
  141. package/testutils/fake_data/tag_query.js +48 -0
  142. package/testutils/fake_data/test_helpers.d.ts +14 -6
  143. package/testutils/fake_data/test_helpers.js +31 -15
  144. package/testutils/fake_data/user_query.d.ts +16 -6
  145. package/testutils/fake_data/user_query.js +72 -23
  146. package/testutils/fake_log.js +1 -1
  147. package/testutils/parse_sql.d.ts +6 -0
  148. package/testutils/parse_sql.js +16 -2
  149. package/testutils/test_edge_global_schema.d.ts +15 -0
  150. package/testutils/test_edge_global_schema.js +62 -0
  151. package/testutils/write.d.ts +2 -2
  152. package/testutils/write.js +33 -7
  153. package/tsc/ast.d.ts +15 -3
  154. package/tsc/ast.js +114 -23
  155. package/tsc/compilerOptions.js +5 -1
  156. package/tsc/move_generated.d.ts +1 -0
  157. package/tsc/move_generated.js +164 -0
  158. package/tsc/transform.d.ts +22 -0
  159. package/tsc/transform.js +182 -0
  160. package/tsc/transform_action.d.ts +22 -0
  161. package/tsc/transform_action.js +183 -0
  162. package/tsc/transform_ent.d.ts +17 -0
  163. package/tsc/transform_ent.js +60 -0
  164. package/tsc/transform_schema.d.ts +27 -0
  165. package/{scripts → tsc}/transform_schema.js +146 -117
  166. package/core/loaders/index_loader.d.ts +0 -14
  167. package/core/loaders/index_loader.js +0 -27
  168. package/graphql/enums.d.ts +0 -3
  169. package/graphql/enums.js +0 -25
  170. package/scripts/move_generated.js +0 -141
  171. package/scripts/transform_actions.js +0 -266
  172. package/scripts/transform_code.d.ts +0 -1
  173. package/scripts/transform_code.js +0 -111
  174. package/scripts/transform_schema.d.ts +0 -1
  175. /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
  176. /package/scripts/{transform_actions.d.ts → move_types.d.ts} +0 -0
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -22,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
27
  };
24
28
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.getSchemaTable = exports.setupSqlite = exports.assoc_edge_table = exports.assoc_edge_config_table = exports.TempDB = exports.enumType = exports.table = exports.boolList = exports.dateList = exports.timetzList = exports.timeList = exports.timestamptzList = exports.timestampList = exports.uuidList = exports.integerList = exports.textList = exports.jsonb = exports.json = exports.float = exports.integer = exports.bool = exports.date = exports.timetz = exports.time = exports.timestamptz = exports.timestamp = exports.enumCol = exports.text = exports.uuid = exports.uniqueIndex = exports.foreignKey = exports.primaryKey = void 0;
29
+ exports.getColumnFromField = exports.getSchemaTable = exports.doSQLiteTestFromSchemas = exports.setupPostgres = exports.setupSqlite = exports.assoc_edge_table = exports.assoc_edge_config_table = exports.TempDB = exports.enumType = exports.table = exports.boolList = exports.dateList = exports.timetzList = exports.timeList = exports.timestamptzList = exports.timestampList = exports.uuidList = exports.integerList = exports.textList = exports.jsonb = exports.json = exports.float = exports.integer = exports.bool = exports.date = exports.timetz = exports.time = exports.timestamptz = exports.timestamp = exports.enumCol = exports.text = exports.uuid = exports.index = exports.check = exports.foreignKey = exports.primaryKey = void 0;
26
30
  const pg_1 = require("pg");
27
31
  const db_1 = __importStar(require("../../core/db"));
28
32
  // this should only be used in tests so we expect to be able to import without shenanigans
@@ -32,6 +36,7 @@ const fs = __importStar(require("fs"));
32
36
  const schema_1 = require("../../schema");
33
37
  const snake_case_1 = require("snake-case");
34
38
  const builder_1 = require("../builder");
39
+ const test_edge_global_schema_1 = require("../test_edge_global_schema");
35
40
  function primaryKey(name, cols) {
36
41
  return {
37
42
  name: name,
@@ -50,15 +55,49 @@ function foreignKey(name, cols, fkey) {
50
55
  };
51
56
  }
52
57
  exports.foreignKey = foreignKey;
53
- function uniqueIndex(name) {
58
+ function check(name, condition) {
59
+ return {
60
+ name,
61
+ generate() {
62
+ return `CONSTRAINT ${name} CHECK(${condition})`;
63
+ },
64
+ };
65
+ }
66
+ exports.check = check;
67
+ function unique(name, cols, tableName) {
68
+ return {
69
+ name,
70
+ generate() {
71
+ if (db_1.Dialect.SQLite === db_1.default.getDialect()) {
72
+ return `UNIQUE (${cols.join(",")})`;
73
+ }
74
+ return `ALTER TABLE ${tableName} ADD CONSTRAINT ${name} UNIQUE (${cols.join(", ")});`;
75
+ },
76
+ postCreate() {
77
+ return db_1.Dialect.Postgres === db_1.default.getDialect();
78
+ },
79
+ };
80
+ }
81
+ function isPostCreateIndex(s) {
82
+ return (s.postCreate !== undefined &&
83
+ s.postCreate());
84
+ }
85
+ function index(tableName, cols, opts) {
86
+ const name = `${tableName}_${cols.join("_")}_idx`;
54
87
  return {
55
- name: "",
88
+ name,
56
89
  generate() {
57
- return `UNIQUE (${name})`;
90
+ if (opts?.unique && db_1.Dialect.SQLite === db_1.default.getDialect()) {
91
+ return `UNIQUE (${cols.join(",")})`;
92
+ }
93
+ return `CREATE ${opts?.unique ? "UNIQUE " : ""}INDEX ${name} ON ${tableName} USING ${opts?.type || "btree"} (${cols.join(",")});`;
94
+ },
95
+ postCreate() {
96
+ return db_1.Dialect.Postgres === db_1.default.getDialect() && !!opts?.unique;
58
97
  },
59
98
  };
60
99
  }
61
- exports.uniqueIndex = uniqueIndex;
100
+ exports.index = index;
62
101
  function uuid(name, opts) {
63
102
  return {
64
103
  name,
@@ -248,9 +287,24 @@ exports.boolList = boolList;
248
287
  function table(name, ...items) {
249
288
  let cols = [];
250
289
  let constraints = [];
290
+ let indexes = [];
251
291
  for (const item of items) {
252
292
  if (item.datatype !== undefined) {
253
293
  const col = item;
294
+ if (col.index) {
295
+ let opts = {
296
+ type: "btree",
297
+ };
298
+ if (col.index === true) {
299
+ opts = {
300
+ type: "btree",
301
+ };
302
+ }
303
+ else {
304
+ opts = col.index;
305
+ }
306
+ indexes.push(index(name, [col.name], opts));
307
+ }
254
308
  // add it as a constraint
255
309
  if (col.foreignKey) {
256
310
  constraints.push(foreignKey(`${name}_${col.name}_fkey`, [col.name], {
@@ -261,7 +315,12 @@ function table(name, ...items) {
261
315
  cols.push(item);
262
316
  }
263
317
  else if (item.generate !== undefined) {
264
- constraints.push(item);
318
+ if (isPostCreateIndex(item) && item.postCreate()) {
319
+ indexes.push(item);
320
+ }
321
+ else {
322
+ constraints.push(item);
323
+ }
265
324
  }
266
325
  }
267
326
  return {
@@ -278,7 +337,12 @@ function table(name, ...items) {
278
337
  parts.push("PRIMARY KEY");
279
338
  }
280
339
  if (col.default !== undefined) {
281
- parts.push(`DEFAULT ${col.default}`);
340
+ if (db_1.Dialect.SQLite === db_1.default.getDialect()) {
341
+ parts.push(`DEFAULT "${col.default}"`);
342
+ }
343
+ else {
344
+ parts.push(`DEFAULT ${col.default}`);
345
+ }
282
346
  }
283
347
  if (col.unique) {
284
348
  parts.push("UNIQUE");
@@ -288,6 +352,9 @@ function table(name, ...items) {
288
352
  constraints.forEach((constraint) => schemaStr.push(constraint.generate()));
289
353
  return `CREATE TABLE IF NOT EXISTS ${name} (\n ${schemaStr})`;
290
354
  },
355
+ postCreate() {
356
+ return indexes.map((index) => index.generate());
357
+ },
291
358
  drop() {
292
359
  return `DROP TABLE IF EXISTS ${name}`;
293
360
  },
@@ -311,29 +378,25 @@ function randomDB() {
311
378
  // always ensure it starts with an alpha character
312
379
  return "abcdefghijklmnopqrstuvwxyz"[Math.floor(Math.random() * 26)] + str;
313
380
  }
314
- function isDialect(dialect) {
315
- return !Array.isArray(dialect);
316
- }
317
381
  class TempDB {
318
382
  constructor(dialect, tables) {
319
383
  this.tables = new Map();
320
- let tbles = [];
321
- if (isDialect(dialect)) {
384
+ if (typeof dialect === "string") {
322
385
  this.dialect = dialect;
323
- if (tables) {
324
- tbles = tables;
325
- }
386
+ this.setTables = tables;
326
387
  }
327
388
  else {
328
- this.dialect = db_1.Dialect.Postgres;
329
- tbles = dialect;
389
+ this.dialect = dialect.dialect;
390
+ this.setTables = dialect.tables;
391
+ this.sqliteConnString = dialect.sqliteConnString;
330
392
  }
331
- tbles.forEach((table) => this.tables.set(table.name, table));
332
393
  }
333
394
  getDialect() {
334
395
  return this.dialect;
335
396
  }
336
- getTables() {
397
+ // NB: this won't be set until after beforeAll() is called since it depends on
398
+ // dialect being correctly set
399
+ __getTables() {
337
400
  return this.tables;
338
401
  }
339
402
  async beforeAll(setupConnString = true) {
@@ -349,12 +412,21 @@ class TempDB {
349
412
  this.db = randomDB();
350
413
  await this.client.query(`CREATE DATABASE ${this.db}`);
351
414
  if (setupConnString) {
415
+ delete process.env.DB_CONNECTION_STRING;
416
+ let connStr = "";
352
417
  if (user && password) {
353
- process.env.DB_CONNECTION_STRING = `postgres://${user}:${password}@localhost:5432/${this.db}`;
418
+ connStr = `postgres://${user}:${password}@localhost:5432/${this.db}`;
354
419
  }
355
420
  else {
356
- process.env.DB_CONNECTION_STRING = `postgres://localhost/${this.db}?`;
421
+ connStr = `postgres://localhost/${this.db}?`;
357
422
  }
423
+ db_1.default.initDB({
424
+ connectionString: connStr,
425
+ cfg: {
426
+ max: 100,
427
+ idleTimeoutMillis: 100,
428
+ },
429
+ });
358
430
  }
359
431
  else {
360
432
  // will probably be setup via loadConfig
@@ -369,18 +441,48 @@ class TempDB {
369
441
  await this.dbClient.connect();
370
442
  }
371
443
  else {
372
- if (process.env.DB_CONNECTION_STRING === undefined) {
373
- throw new Error(`DB_CONNECTION_STRING required for sqlite `);
444
+ let connString;
445
+ if (this.sqliteConnString) {
446
+ connString = this.sqliteConnString;
447
+ }
448
+ else {
449
+ if (process.env.DB_CONNECTION_STRING === undefined) {
450
+ throw new Error(`DB_CONNECTION_STRING required for sqlite if sqliteConnString is not set`);
451
+ }
452
+ connString = process.env.DB_CONNECTION_STRING;
374
453
  }
375
- const filePath = process.env.DB_CONNECTION_STRING.substr(10);
454
+ const filePath = connString.substr(10);
376
455
  this.sqlite = (0, better_sqlite3_1.default)(filePath);
377
456
  }
378
- for (const [_, table] of this.tables) {
379
- if (this.dialect == db_1.Dialect.Postgres) {
380
- await this.dbClient.query(table.create());
457
+ if (this.setTables) {
458
+ let tables = [];
459
+ if (typeof this.setTables === "function") {
460
+ tables = this.setTables();
381
461
  }
382
462
  else {
383
- this.sqlite.exec(table.create());
463
+ tables = this.setTables;
464
+ }
465
+ tables.forEach((table) => this.tables.set(table.name, table));
466
+ }
467
+ for (const [_, table] of this.tables) {
468
+ await this.createImpl(table);
469
+ }
470
+ }
471
+ async createImpl(table) {
472
+ if (this.dialect == db_1.Dialect.Postgres) {
473
+ await this.dbClient.query(table.create());
474
+ if (table.postCreate) {
475
+ for (const q of table.postCreate()) {
476
+ await this.dbClient.query(q);
477
+ }
478
+ }
479
+ }
480
+ else {
481
+ this.sqlite.exec(table.create());
482
+ if (table.postCreate) {
483
+ for (const q of table.postCreate()) {
484
+ this.sqlite.exec(q);
485
+ }
384
486
  }
385
487
  }
386
488
  }
@@ -393,6 +495,16 @@ class TempDB {
393
495
  async afterAll() {
394
496
  if (this.dialect === db_1.Dialect.SQLite) {
395
497
  this.sqlite.close();
498
+ if (!this.sqlite.memory) {
499
+ const f = this.getSqliteClient().name;
500
+ fs.rmSync(f);
501
+ fs.rmSync(`${f}-shm`, {
502
+ force: true,
503
+ });
504
+ fs.rmSync(`${f}-wal`, {
505
+ force: true,
506
+ });
507
+ }
396
508
  return;
397
509
  }
398
510
  // end our connection to db
@@ -401,6 +513,7 @@ class TempDB {
401
513
  await db_1.default.getInstance().endPool();
402
514
  // drop db
403
515
  await this.client.query(`DROP DATABASE ${this.db}`);
516
+ // console.log(this.db);
404
517
  await this.client.end();
405
518
  }
406
519
  getDB() {
@@ -431,12 +544,7 @@ class TempDB {
431
544
  if (this.tables.has(table.name)) {
432
545
  throw new Error(`table with name ${table.name} already exists`);
433
546
  }
434
- if (this.dialect === db_1.Dialect.Postgres) {
435
- await this.dbClient.query(table.create());
436
- }
437
- else {
438
- this.sqlite.exec(table.create());
439
- }
547
+ await this.createImpl(table);
440
548
  this.tables.set(table.name, table);
441
549
  }
442
550
  }
@@ -448,18 +556,43 @@ function assoc_edge_config_table() {
448
556
  text("edge_type", { primaryKey: true }), text("edge_name"), bool("symmetric_edge", { default: "FALSE" }), text("inverse_edge_type", { nullable: true }), text("edge_table"), timestamptz("created_at"), timestamptz("updated_at"));
449
557
  }
450
558
  exports.assoc_edge_config_table = assoc_edge_config_table;
451
- function assoc_edge_table(name) {
452
- return table(name, uuid("id1"), text("id1_type"),
453
- // same as in assoc_edge_config_table
454
- text("edge_type"), uuid("id2"), text("id2_type"), timestamptz("time"), text("data", { nullable: true }), primaryKey(`${name}_pkey`, ["id1", "id2", "edge_type"]));
559
+ // if global flag is true, add any column from testEdgeGlobalSchema
560
+ // up to caller to set/clear that as needed
561
+ function assoc_edge_table(name, global, unique_edge) {
562
+ const items = [
563
+ uuid("id1"),
564
+ text("id1_type"),
565
+ // same as in assoc_edge_config_table
566
+ text("edge_type"),
567
+ uuid("id2"),
568
+ text("id2_type"),
569
+ timestamptz("time"),
570
+ text("data", { nullable: true }),
571
+ primaryKey(`${name}_pkey`, ["id1", "id2", "edge_type"]),
572
+ ];
573
+ if (unique_edge) {
574
+ items.push(unique(`${name}_unique_id1_edge_type`, ["id1", "edge_type"], name));
575
+ }
576
+ const t = table(name, ...items);
577
+ if (global) {
578
+ for (const k in test_edge_global_schema_1.testEdgeGlobalSchema.extraEdgeFields) {
579
+ const col = getColumnFromField(k, test_edge_global_schema_1.testEdgeGlobalSchema.extraEdgeFields[k], db_1.Dialect.Postgres);
580
+ t.columns.push(col);
581
+ }
582
+ }
583
+ return t;
455
584
  }
456
585
  exports.assoc_edge_table = assoc_edge_table;
457
586
  function setupSqlite(connString, tables, opts) {
458
- let tdb;
587
+ let tdb = new TempDB({
588
+ dialect: db_1.Dialect.SQLite,
589
+ tables,
590
+ sqliteConnString: connString,
591
+ });
459
592
  beforeAll(async () => {
460
- process.env.DB_CONNECTION_STRING = connString;
461
- (0, config_1.loadConfig)();
462
- tdb = new TempDB(db_1.Dialect.SQLite, tables());
593
+ (0, config_1.loadConfig)({
594
+ dbConnectionString: connString,
595
+ });
463
596
  await tdb.beforeAll();
464
597
  const conn = db_1.default.getInstance().getConnection();
465
598
  expect(conn.db.memory).toBe(false);
@@ -467,7 +600,7 @@ function setupSqlite(connString, tables, opts) {
467
600
  if (!opts?.disableDeleteAfterEachTest) {
468
601
  afterEach(async () => {
469
602
  const client = await db_1.default.getInstance().getNewClient();
470
- for (const [key, _] of tdb.getTables()) {
603
+ for (const [key, _] of tdb.__getTables()) {
471
604
  const query = `delete from ${key}`;
472
605
  if (isSyncClient(client))
473
606
  if (client.execSync) {
@@ -481,17 +614,83 @@ function setupSqlite(connString, tables, opts) {
481
614
  }
482
615
  afterAll(async () => {
483
616
  await tdb.afterAll();
484
- fs.rmSync(tdb.getSqliteClient().name);
617
+ delete process.env.DB_CONNECTION_STRING;
485
618
  });
619
+ return tdb;
486
620
  }
487
621
  exports.setupSqlite = setupSqlite;
622
+ function setupPostgres(tables, opts) {
623
+ let tdb;
624
+ beforeAll(async () => {
625
+ tdb = new TempDB(db_1.Dialect.Postgres, tables());
626
+ await tdb.beforeAll();
627
+ });
628
+ // TODO need to fix this implementation...
629
+ if (!opts?.disableDeleteAfterEachTest) {
630
+ afterEach(async () => {
631
+ const client = await db_1.default.getInstance().getNewClient();
632
+ for (const [key, _] of tdb.__getTables()) {
633
+ const query = `delete from ${key}`;
634
+ await client.exec(query);
635
+ }
636
+ client.release();
637
+ });
638
+ }
639
+ afterAll(async () => {
640
+ await tdb.afterAll();
641
+ });
642
+ }
643
+ exports.setupPostgres = setupPostgres;
644
+ async function doSQLiteTestFromSchemas(schemas, doTest, db) {
645
+ const connString = `sqlite:///${db || randomDB()}.db`;
646
+ const tables = schemas.map((schema) => getSchemaTable(schema, db_1.Dialect.SQLite));
647
+ let tdb = new TempDB(db_1.Dialect.SQLite, tables);
648
+ process.env.DB_CONNECTION_STRING = connString;
649
+ (0, config_1.loadConfig)();
650
+ await tdb.beforeAll();
651
+ await doTest();
652
+ await tdb.afterAll();
653
+ delete process.env.DB_CONNECTION_STRING;
654
+ return tdb;
655
+ }
656
+ exports.doSQLiteTestFromSchemas = doSQLiteTestFromSchemas;
488
657
  function getSchemaTable(schema, dialect) {
489
658
  const fields = (0, schema_1.getFields)(schema);
490
- const columns = [];
659
+ const items = [];
491
660
  for (const [fieldName, field] of fields) {
492
- columns.push(getColumnFromField(fieldName, field, dialect));
661
+ items.push(getColumnFromField(fieldName, field, dialect));
493
662
  }
494
- return table((0, builder_1.getTableName)(schema), ...columns);
663
+ const tableName = (0, builder_1.getTableName)(schema);
664
+ if (schema.constraints) {
665
+ for (const constraint of schema.constraints) {
666
+ switch (constraint.type) {
667
+ case schema_1.ConstraintType.PrimaryKey:
668
+ items.push(primaryKey(constraint.name, constraint.columns));
669
+ break;
670
+ case schema_1.ConstraintType.ForeignKey:
671
+ if (!constraint.fkey) {
672
+ throw new Error(`need 'fkey' field for foreign key constraint`);
673
+ }
674
+ items.push(foreignKey(constraint.name, constraint.columns, {
675
+ table: constraint.fkey.tableName,
676
+ cols: constraint.fkey.columns,
677
+ }));
678
+ break;
679
+ case schema_1.ConstraintType.Check:
680
+ if (!constraint.condition) {
681
+ throw new Error(`need 'condition' field for check constraint`);
682
+ }
683
+ items.push(check(constraint.name, constraint.condition));
684
+ break;
685
+ case schema_1.ConstraintType.Unique:
686
+ items.push(unique(constraint.name, constraint.columns, tableName));
687
+ break;
688
+ default:
689
+ throw new Error(`unknown constraint type ${constraint.type}`);
690
+ }
691
+ }
692
+ }
693
+ return table(tableName, ...items);
495
694
  }
496
695
  exports.getSchemaTable = getSchemaTable;
497
696
  function getColumnForDbType(t, dialect) {
@@ -549,6 +748,7 @@ function getColumnFromField(fieldName, f, dialect) {
549
748
  return getColumn(fieldName, f, fn);
550
749
  }
551
750
  }
751
+ exports.getColumnFromField = getColumnFromField;
552
752
  function getColumn(fieldName, f, col) {
553
753
  return col(storageKey(fieldName, f), buildOpts(f));
554
754
  }
@@ -567,6 +767,9 @@ function buildOpts(f) {
567
767
  if (f.serverDefault) {
568
768
  ret.default = f.serverDefault;
569
769
  }
770
+ if (f.unique) {
771
+ ret.unique = true;
772
+ }
570
773
  return ret;
571
774
  }
572
775
  function storageKey(fieldName, f) {
@@ -0,0 +1,7 @@
1
+ import { Field, Schema } from "../../schema";
2
+ export declare function randomEmail(domain?: string): string;
3
+ interface Info {
4
+ schema: Schema;
5
+ }
6
+ export declare function getDefaultValue(f: Field, col: string, infos?: Map<string, Info>): any;
7
+ export {};