@snowtop/ent 0.1.0-alpha15 → 0.1.0-alpha150

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 (170) hide show
  1. package/action/action.d.ts +27 -16
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +16 -3
  4. package/action/executor.js +90 -23
  5. package/action/experimental_action.d.ts +25 -16
  6. package/action/experimental_action.js +35 -9
  7. package/action/index.d.ts +3 -1
  8. package/action/index.js +7 -1
  9. package/action/operations.d.ts +125 -0
  10. package/action/operations.js +684 -0
  11. package/action/orchestrator.d.ts +38 -12
  12. package/action/orchestrator.js +427 -102
  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 +51 -21
  19. package/core/base.js +7 -1
  20. package/core/clause.d.ts +85 -40
  21. package/core/clause.js +375 -64
  22. package/core/config.d.ts +15 -1
  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 +4 -2
  27. package/core/context.js +20 -2
  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 +18 -8
  32. package/core/ent.d.ts +68 -94
  33. package/core/ent.js +535 -586
  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 +1 -0
  37. package/core/loaders/assoc_count_loader.js +10 -2
  38. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  39. package/core/loaders/assoc_edge_loader.js +10 -13
  40. package/core/loaders/index.d.ts +1 -1
  41. package/core/loaders/index.js +1 -3
  42. package/core/loaders/index_loader.d.ts +3 -3
  43. package/core/loaders/loader.d.ts +2 -2
  44. package/core/loaders/loader.js +5 -5
  45. package/core/loaders/object_loader.d.ts +30 -9
  46. package/core/loaders/object_loader.js +225 -78
  47. package/core/loaders/query_loader.d.ts +6 -12
  48. package/core/loaders/query_loader.js +54 -13
  49. package/core/loaders/raw_count_loader.js +5 -1
  50. package/core/logger.d.ts +1 -1
  51. package/core/logger.js +1 -0
  52. package/core/privacy.d.ts +7 -6
  53. package/core/privacy.js +21 -25
  54. package/core/query/assoc_query.d.ts +3 -2
  55. package/core/query/assoc_query.js +9 -1
  56. package/core/query/custom_clause_query.d.ts +27 -0
  57. package/core/query/custom_clause_query.js +88 -0
  58. package/core/query/custom_query.d.ts +17 -2
  59. package/core/query/custom_query.js +88 -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 +15 -3
  63. package/core/query/query.js +128 -53
  64. package/core/query/shared_assoc_test.d.ts +2 -1
  65. package/core/query/shared_assoc_test.js +44 -54
  66. package/core/query/shared_test.d.ts +8 -1
  67. package/core/query/shared_test.js +532 -236
  68. package/core/viewer.d.ts +2 -0
  69. package/core/viewer.js +3 -1
  70. package/graphql/graphql.d.ts +52 -19
  71. package/graphql/graphql.js +174 -136
  72. package/graphql/graphql_field_helpers.d.ts +7 -1
  73. package/graphql/graphql_field_helpers.js +21 -1
  74. package/graphql/index.d.ts +2 -2
  75. package/graphql/index.js +3 -5
  76. package/graphql/query/connection_type.d.ts +9 -9
  77. package/graphql/query/shared_assoc_test.js +1 -1
  78. package/graphql/query/shared_edge_connection.js +1 -19
  79. package/graphql/scalars/orderby_direction.d.ts +2 -0
  80. package/graphql/scalars/orderby_direction.js +15 -0
  81. package/imports/dataz/example1/_auth.js +128 -47
  82. package/imports/dataz/example1/_viewer.js +87 -39
  83. package/imports/index.d.ts +7 -2
  84. package/imports/index.js +20 -5
  85. package/index.d.ts +14 -5
  86. package/index.js +26 -10
  87. package/package.json +18 -17
  88. package/parse_schema/parse.d.ts +31 -9
  89. package/parse_schema/parse.js +179 -32
  90. package/schema/base_schema.d.ts +13 -3
  91. package/schema/base_schema.js +13 -0
  92. package/schema/field.d.ts +78 -21
  93. package/schema/field.js +231 -71
  94. package/schema/index.d.ts +2 -2
  95. package/schema/index.js +7 -2
  96. package/schema/json_field.d.ts +16 -4
  97. package/schema/json_field.js +32 -2
  98. package/schema/schema.d.ts +109 -20
  99. package/schema/schema.js +42 -53
  100. package/schema/struct_field.d.ts +15 -3
  101. package/schema/struct_field.js +117 -22
  102. package/schema/union_field.d.ts +1 -1
  103. package/scripts/custom_compiler.js +12 -8
  104. package/scripts/custom_graphql.js +145 -34
  105. package/scripts/migrate_v0.1.js +36 -0
  106. package/scripts/move_types.js +120 -0
  107. package/scripts/read_schema.js +22 -7
  108. package/testutils/action/complex_schemas.d.ts +69 -0
  109. package/testutils/action/complex_schemas.js +405 -0
  110. package/testutils/builder.d.ts +39 -43
  111. package/testutils/builder.js +75 -49
  112. package/testutils/db/fixture.d.ts +10 -0
  113. package/testutils/db/fixture.js +26 -0
  114. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +32 -8
  115. package/testutils/db/{test_db.js → temp_db.js} +244 -48
  116. package/testutils/db/value.d.ts +7 -0
  117. package/testutils/db/value.js +251 -0
  118. package/testutils/db_mock.d.ts +16 -4
  119. package/testutils/db_mock.js +52 -9
  120. package/testutils/db_time_zone.d.ts +4 -0
  121. package/testutils/db_time_zone.js +41 -0
  122. package/testutils/ent-graphql-tests/index.d.ts +7 -1
  123. package/testutils/ent-graphql-tests/index.js +56 -26
  124. package/testutils/fake_comms.js +1 -1
  125. package/testutils/fake_data/const.d.ts +2 -1
  126. package/testutils/fake_data/const.js +3 -0
  127. package/testutils/fake_data/fake_contact.d.ts +7 -3
  128. package/testutils/fake_data/fake_contact.js +13 -7
  129. package/testutils/fake_data/fake_event.d.ts +4 -1
  130. package/testutils/fake_data/fake_event.js +7 -6
  131. package/testutils/fake_data/fake_tag.d.ts +36 -0
  132. package/testutils/fake_data/fake_tag.js +89 -0
  133. package/testutils/fake_data/fake_user.d.ts +8 -5
  134. package/testutils/fake_data/fake_user.js +16 -15
  135. package/testutils/fake_data/index.js +5 -1
  136. package/testutils/fake_data/internal.d.ts +2 -0
  137. package/testutils/fake_data/internal.js +7 -1
  138. package/testutils/fake_data/tag_query.d.ts +13 -0
  139. package/testutils/fake_data/tag_query.js +43 -0
  140. package/testutils/fake_data/test_helpers.d.ts +11 -4
  141. package/testutils/fake_data/test_helpers.js +29 -13
  142. package/testutils/fake_data/user_query.d.ts +11 -4
  143. package/testutils/fake_data/user_query.js +54 -22
  144. package/testutils/fake_log.js +1 -1
  145. package/testutils/parse_sql.d.ts +6 -0
  146. package/testutils/parse_sql.js +16 -2
  147. package/testutils/test_edge_global_schema.d.ts +15 -0
  148. package/testutils/test_edge_global_schema.js +62 -0
  149. package/testutils/write.d.ts +2 -2
  150. package/testutils/write.js +33 -7
  151. package/tsc/ast.d.ts +25 -2
  152. package/tsc/ast.js +141 -17
  153. package/tsc/compilerOptions.js +5 -1
  154. package/tsc/move_generated.d.ts +1 -0
  155. package/tsc/move_generated.js +164 -0
  156. package/tsc/transform.d.ts +22 -0
  157. package/tsc/transform.js +182 -0
  158. package/tsc/transform_action.d.ts +22 -0
  159. package/tsc/transform_action.js +183 -0
  160. package/tsc/transform_ent.d.ts +17 -0
  161. package/tsc/transform_ent.js +60 -0
  162. package/tsc/transform_schema.d.ts +27 -0
  163. package/{scripts → tsc}/transform_schema.js +146 -117
  164. package/graphql/enums.d.ts +0 -3
  165. package/graphql/enums.js +0 -25
  166. package/scripts/move_generated.js +0 -142
  167. package/scripts/transform_code.js +0 -113
  168. package/scripts/transform_schema.d.ts +0 -1
  169. /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
  170. /package/scripts/{transform_code.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,9 @@ 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
+ fs.rmSync(this.getSqliteClient().name);
500
+ }
396
501
  return;
397
502
  }
398
503
  // end our connection to db
@@ -401,6 +506,7 @@ class TempDB {
401
506
  await db_1.default.getInstance().endPool();
402
507
  // drop db
403
508
  await this.client.query(`DROP DATABASE ${this.db}`);
509
+ // console.log(this.db);
404
510
  await this.client.end();
405
511
  }
406
512
  getDB() {
@@ -431,12 +537,7 @@ class TempDB {
431
537
  if (this.tables.has(table.name)) {
432
538
  throw new Error(`table with name ${table.name} already exists`);
433
539
  }
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
- }
540
+ await this.createImpl(table);
440
541
  this.tables.set(table.name, table);
441
542
  }
442
543
  }
@@ -448,18 +549,43 @@ function assoc_edge_config_table() {
448
549
  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
550
  }
450
551
  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"]));
552
+ // if global flag is true, add any column from testEdgeGlobalSchema
553
+ // up to caller to set/clear that as needed
554
+ function assoc_edge_table(name, global, unique_edge) {
555
+ const items = [
556
+ uuid("id1"),
557
+ text("id1_type"),
558
+ // same as in assoc_edge_config_table
559
+ text("edge_type"),
560
+ uuid("id2"),
561
+ text("id2_type"),
562
+ timestamptz("time"),
563
+ text("data", { nullable: true }),
564
+ primaryKey(`${name}_pkey`, ["id1", "id2", "edge_type"]),
565
+ ];
566
+ if (unique_edge) {
567
+ items.push(unique(`${name}_unique_id1_edge_type`, ["id1", "edge_type"], name));
568
+ }
569
+ const t = table(name, ...items);
570
+ if (global) {
571
+ for (const k in test_edge_global_schema_1.testEdgeGlobalSchema.extraEdgeFields) {
572
+ const col = getColumnFromField(k, test_edge_global_schema_1.testEdgeGlobalSchema.extraEdgeFields[k], db_1.Dialect.Postgres);
573
+ t.columns.push(col);
574
+ }
575
+ }
576
+ return t;
455
577
  }
456
578
  exports.assoc_edge_table = assoc_edge_table;
457
579
  function setupSqlite(connString, tables, opts) {
458
- let tdb;
580
+ let tdb = new TempDB({
581
+ dialect: db_1.Dialect.SQLite,
582
+ tables,
583
+ sqliteConnString: connString,
584
+ });
459
585
  beforeAll(async () => {
460
- process.env.DB_CONNECTION_STRING = connString;
461
- (0, config_1.loadConfig)();
462
- tdb = new TempDB(db_1.Dialect.SQLite, tables());
586
+ (0, config_1.loadConfig)({
587
+ dbConnectionString: connString,
588
+ });
463
589
  await tdb.beforeAll();
464
590
  const conn = db_1.default.getInstance().getConnection();
465
591
  expect(conn.db.memory).toBe(false);
@@ -467,7 +593,7 @@ function setupSqlite(connString, tables, opts) {
467
593
  if (!opts?.disableDeleteAfterEachTest) {
468
594
  afterEach(async () => {
469
595
  const client = await db_1.default.getInstance().getNewClient();
470
- for (const [key, _] of tdb.getTables()) {
596
+ for (const [key, _] of tdb.__getTables()) {
471
597
  const query = `delete from ${key}`;
472
598
  if (isSyncClient(client))
473
599
  if (client.execSync) {
@@ -481,17 +607,83 @@ function setupSqlite(connString, tables, opts) {
481
607
  }
482
608
  afterAll(async () => {
483
609
  await tdb.afterAll();
484
- fs.rmSync(tdb.getSqliteClient().name);
610
+ delete process.env.DB_CONNECTION_STRING;
485
611
  });
612
+ return tdb;
486
613
  }
487
614
  exports.setupSqlite = setupSqlite;
615
+ function setupPostgres(tables, opts) {
616
+ let tdb;
617
+ beforeAll(async () => {
618
+ tdb = new TempDB(db_1.Dialect.Postgres, tables());
619
+ await tdb.beforeAll();
620
+ });
621
+ // TODO need to fix this implementation...
622
+ if (!opts?.disableDeleteAfterEachTest) {
623
+ afterEach(async () => {
624
+ const client = await db_1.default.getInstance().getNewClient();
625
+ for (const [key, _] of tdb.__getTables()) {
626
+ const query = `delete from ${key}`;
627
+ await client.exec(query);
628
+ }
629
+ client.release();
630
+ });
631
+ }
632
+ afterAll(async () => {
633
+ await tdb.afterAll();
634
+ });
635
+ }
636
+ exports.setupPostgres = setupPostgres;
637
+ async function doSQLiteTestFromSchemas(schemas, doTest, db) {
638
+ const connString = `sqlite:///${db || randomDB()}.db`;
639
+ const tables = schemas.map((schema) => getSchemaTable(schema, db_1.Dialect.SQLite));
640
+ let tdb = new TempDB(db_1.Dialect.SQLite, tables);
641
+ process.env.DB_CONNECTION_STRING = connString;
642
+ (0, config_1.loadConfig)();
643
+ await tdb.beforeAll();
644
+ await doTest();
645
+ await tdb.afterAll();
646
+ delete process.env.DB_CONNECTION_STRING;
647
+ return tdb;
648
+ }
649
+ exports.doSQLiteTestFromSchemas = doSQLiteTestFromSchemas;
488
650
  function getSchemaTable(schema, dialect) {
489
651
  const fields = (0, schema_1.getFields)(schema);
490
- const columns = [];
652
+ const items = [];
491
653
  for (const [fieldName, field] of fields) {
492
- columns.push(getColumnFromField(fieldName, field, dialect));
654
+ items.push(getColumnFromField(fieldName, field, dialect));
493
655
  }
494
- return table((0, builder_1.getTableName)(schema), ...columns);
656
+ const tableName = (0, builder_1.getTableName)(schema);
657
+ if (schema.constraints) {
658
+ for (const constraint of schema.constraints) {
659
+ switch (constraint.type) {
660
+ case schema_1.ConstraintType.PrimaryKey:
661
+ items.push(primaryKey(constraint.name, constraint.columns));
662
+ break;
663
+ case schema_1.ConstraintType.ForeignKey:
664
+ if (!constraint.fkey) {
665
+ throw new Error(`need 'fkey' field for foreign key constraint`);
666
+ }
667
+ items.push(foreignKey(constraint.name, constraint.columns, {
668
+ table: constraint.fkey.tableName,
669
+ cols: constraint.fkey.columns,
670
+ }));
671
+ break;
672
+ case schema_1.ConstraintType.Check:
673
+ if (!constraint.condition) {
674
+ throw new Error(`need 'condition' field for check constraint`);
675
+ }
676
+ items.push(check(constraint.name, constraint.condition));
677
+ break;
678
+ case schema_1.ConstraintType.Unique:
679
+ items.push(unique(constraint.name, constraint.columns, tableName));
680
+ break;
681
+ default:
682
+ throw new Error(`unknown constraint type ${constraint.type}`);
683
+ }
684
+ }
685
+ }
686
+ return table(tableName, ...items);
495
687
  }
496
688
  exports.getSchemaTable = getSchemaTable;
497
689
  function getColumnForDbType(t, dialect) {
@@ -549,6 +741,7 @@ function getColumnFromField(fieldName, f, dialect) {
549
741
  return getColumn(fieldName, f, fn);
550
742
  }
551
743
  }
744
+ exports.getColumnFromField = getColumnFromField;
552
745
  function getColumn(fieldName, f, col) {
553
746
  return col(storageKey(fieldName, f), buildOpts(f));
554
747
  }
@@ -567,6 +760,9 @@ function buildOpts(f) {
567
760
  if (f.serverDefault) {
568
761
  ret.default = f.serverDefault;
569
762
  }
763
+ if (f.unique) {
764
+ ret.unique = true;
765
+ }
570
766
  return ret;
571
767
  }
572
768
  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 {};