@snowtop/ent 0.1.0-alpha9 → 0.1.0-alpha90

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 (116) hide show
  1. package/action/action.d.ts +36 -31
  2. package/action/action.js +2 -6
  3. package/action/executor.d.ts +3 -3
  4. package/action/executor.js +2 -2
  5. package/action/experimental_action.d.ts +29 -22
  6. package/action/experimental_action.js +29 -6
  7. package/action/orchestrator.d.ts +38 -16
  8. package/action/orchestrator.js +219 -61
  9. package/action/privacy.d.ts +2 -2
  10. package/core/base.d.ts +45 -24
  11. package/core/base.js +7 -1
  12. package/core/clause.d.ts +68 -7
  13. package/core/clause.js +291 -62
  14. package/core/config.d.ts +8 -0
  15. package/core/context.d.ts +5 -3
  16. package/core/context.js +20 -2
  17. package/core/convert.d.ts +1 -1
  18. package/core/db.d.ts +2 -2
  19. package/core/db.js +1 -1
  20. package/core/ent.d.ts +79 -24
  21. package/core/ent.js +520 -168
  22. package/core/loaders/assoc_count_loader.d.ts +2 -2
  23. package/core/loaders/assoc_count_loader.js +6 -1
  24. package/core/loaders/assoc_edge_loader.d.ts +2 -2
  25. package/core/loaders/index.d.ts +1 -1
  26. package/core/loaders/index.js +1 -3
  27. package/core/loaders/index_loader.d.ts +2 -2
  28. package/core/loaders/loader.js +5 -5
  29. package/core/loaders/object_loader.d.ts +6 -5
  30. package/core/loaders/object_loader.js +62 -58
  31. package/core/loaders/query_loader.d.ts +6 -12
  32. package/core/loaders/query_loader.js +47 -10
  33. package/core/loaders/raw_count_loader.d.ts +2 -2
  34. package/core/logger.d.ts +1 -1
  35. package/core/logger.js +1 -0
  36. package/core/privacy.d.ts +26 -25
  37. package/core/privacy.js +21 -25
  38. package/core/query/assoc_query.d.ts +6 -6
  39. package/core/query/custom_clause_query.d.ts +24 -0
  40. package/core/query/custom_clause_query.js +72 -0
  41. package/core/query/custom_query.d.ts +20 -5
  42. package/core/query/custom_query.js +77 -10
  43. package/core/query/index.d.ts +1 -0
  44. package/core/query/index.js +3 -1
  45. package/core/query/query.d.ts +1 -1
  46. package/core/query/query.js +8 -1
  47. package/core/query/shared_assoc_test.d.ts +1 -1
  48. package/core/query/shared_assoc_test.js +17 -5
  49. package/core/query/shared_test.d.ts +3 -0
  50. package/core/query/shared_test.js +211 -30
  51. package/core/viewer.d.ts +3 -3
  52. package/core/viewer.js +1 -1
  53. package/graphql/graphql.js +6 -0
  54. package/graphql/query/edge_connection.d.ts +9 -9
  55. package/graphql/query/page_info.d.ts +1 -1
  56. package/index.d.ts +11 -5
  57. package/index.js +15 -6
  58. package/package.json +1 -1
  59. package/parse_schema/parse.d.ts +12 -3
  60. package/parse_schema/parse.js +70 -11
  61. package/schema/base_schema.js +3 -0
  62. package/schema/field.d.ts +44 -8
  63. package/schema/field.js +125 -9
  64. package/schema/index.d.ts +2 -2
  65. package/schema/json_field.d.ts +13 -1
  66. package/schema/json_field.js +28 -1
  67. package/schema/schema.d.ts +65 -11
  68. package/schema/schema.js +18 -4
  69. package/schema/struct_field.d.ts +11 -1
  70. package/schema/struct_field.js +44 -5
  71. package/scripts/custom_graphql.js +8 -3
  72. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  73. package/scripts/migrate_v0.1.js +36 -0
  74. package/scripts/read_schema.js +15 -4
  75. package/testutils/builder.d.ts +31 -21
  76. package/testutils/builder.js +83 -29
  77. package/testutils/db/fixture.d.ts +10 -0
  78. package/testutils/db/fixture.js +26 -0
  79. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +15 -3
  80. package/testutils/db/{test_db.js → temp_db.js} +70 -16
  81. package/testutils/db/value.d.ts +6 -0
  82. package/testutils/db/value.js +251 -0
  83. package/testutils/db_time_zone.d.ts +4 -0
  84. package/testutils/db_time_zone.js +41 -0
  85. package/testutils/fake_data/fake_contact.d.ts +5 -4
  86. package/testutils/fake_data/fake_contact.js +14 -6
  87. package/testutils/fake_data/fake_event.d.ts +5 -3
  88. package/testutils/fake_data/fake_event.js +8 -5
  89. package/testutils/fake_data/fake_user.d.ts +4 -4
  90. package/testutils/fake_data/fake_user.js +16 -13
  91. package/testutils/fake_data/test_helpers.d.ts +3 -2
  92. package/testutils/fake_data/test_helpers.js +8 -6
  93. package/testutils/fake_data/user_query.d.ts +8 -6
  94. package/testutils/fake_data/user_query.js +28 -21
  95. package/testutils/fake_log.d.ts +3 -3
  96. package/testutils/parse_sql.d.ts +6 -0
  97. package/testutils/parse_sql.js +16 -2
  98. package/testutils/test_edge_global_schema.d.ts +15 -0
  99. package/testutils/test_edge_global_schema.js +58 -0
  100. package/testutils/write.d.ts +2 -2
  101. package/testutils/write.js +29 -7
  102. package/tsc/ast.d.ts +44 -0
  103. package/tsc/ast.js +267 -0
  104. package/tsc/compilerOptions.d.ts +6 -0
  105. package/tsc/compilerOptions.js +40 -1
  106. package/tsc/move_generated.d.ts +1 -0
  107. package/tsc/move_generated.js +160 -0
  108. package/tsc/transform.d.ts +21 -0
  109. package/tsc/transform.js +167 -0
  110. package/tsc/transform_action.d.ts +22 -0
  111. package/tsc/transform_action.js +179 -0
  112. package/tsc/transform_ent.d.ts +17 -0
  113. package/tsc/transform_ent.js +59 -0
  114. package/tsc/transform_schema.d.ts +27 -0
  115. package/tsc/transform_schema.js +379 -0
  116. package/scripts/transform_schema.js +0 -445
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SimpleAction = exports.SimpleBuilder = exports.getTableName = exports.getSchemaName = exports.getBuilderSchemaTZFromFields = exports.getBuilderSchemaFromFields = exports.getBuilderSchema = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = void 0;
6
+ exports.SimpleAction = exports.SimpleBuilder = exports.getFieldInfo = exports.getTableName = exports.getSchemaName = exports.getBuilderSchemaTZFromFields = exports.getBuilderSchemaFromFields = exports.getBuilderSchema = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = void 0;
7
7
  const privacy_1 = require("../core/privacy");
8
8
  const orchestrator_1 = require("../action/orchestrator");
9
9
  const action_1 = require("../action");
@@ -15,18 +15,21 @@ const loaders_1 = require("../core/loaders");
15
15
  const convert_1 = require("../core/convert");
16
16
  const camel_case_1 = require("camel-case");
17
17
  const base_schema_1 = require("../schema/base_schema");
18
+ const schema_2 = require("../schema/schema");
18
19
  class User {
19
20
  constructor(viewer, data) {
20
21
  this.viewer = viewer;
21
22
  this.data = data;
22
23
  this.accountID = "";
23
24
  this.nodeType = "User";
24
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
25
25
  this.data.created_at = (0, convert_1.convertDate)(data.created_at);
26
26
  this.data.updated_at = (0, convert_1.convertDate)(data.updated_at);
27
27
  this.id = data.id;
28
28
  this.firstName = data.first_name;
29
29
  }
30
+ getPrivacyPolicy() {
31
+ return privacy_1.AlwaysAllowPrivacyPolicy;
32
+ }
30
33
  }
31
34
  exports.User = User;
32
35
  class Event {
@@ -35,9 +38,11 @@ class Event {
35
38
  this.data = data;
36
39
  this.accountID = "";
37
40
  this.nodeType = "Event";
38
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
39
41
  this.id = data.id;
40
42
  }
43
+ getPrivacyPolicy() {
44
+ return privacy_1.AlwaysAllowPrivacyPolicy;
45
+ }
41
46
  }
42
47
  exports.Event = Event;
43
48
  class Contact {
@@ -46,11 +51,13 @@ class Contact {
46
51
  this.data = data;
47
52
  this.accountID = "";
48
53
  this.nodeType = "Contact";
49
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
50
54
  this.data.created_at = (0, convert_1.convertDate)(data.created_at);
51
55
  this.data.updated_at = (0, convert_1.convertDate)(data.updated_at);
52
56
  this.id = data.id;
53
57
  }
58
+ getPrivacyPolicy() {
59
+ return privacy_1.AlwaysAllowPrivacyPolicy;
60
+ }
54
61
  }
55
62
  exports.Contact = Contact;
56
63
  class Group {
@@ -59,9 +66,11 @@ class Group {
59
66
  this.data = data;
60
67
  this.accountID = "";
61
68
  this.nodeType = "Group";
62
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
63
69
  this.id = data.id;
64
70
  }
71
+ getPrivacyPolicy() {
72
+ return privacy_1.AlwaysAllowPrivacyPolicy;
73
+ }
65
74
  }
66
75
  exports.Group = Group;
67
76
  class Message {
@@ -70,9 +79,11 @@ class Message {
70
79
  this.data = data;
71
80
  this.accountID = "";
72
81
  this.nodeType = "Message";
73
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
74
82
  this.id = data.id;
75
83
  }
84
+ getPrivacyPolicy() {
85
+ return privacy_1.AlwaysAllowPrivacyPolicy;
86
+ }
76
87
  }
77
88
  exports.Message = Message;
78
89
  class Address {
@@ -81,9 +92,11 @@ class Address {
81
92
  this.data = data;
82
93
  this.accountID = "";
83
94
  this.nodeType = "Address";
84
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
85
95
  this.id = data.id;
86
96
  }
97
+ getPrivacyPolicy() {
98
+ return privacy_1.AlwaysAllowPrivacyPolicy;
99
+ }
87
100
  }
88
101
  exports.Address = Address;
89
102
  function getBuilderSchema(cfg, ent) {
@@ -118,13 +131,27 @@ exports.getTableName = getTableName;
118
131
  function randomNum() {
119
132
  return Math.random().toString(10).substring(2);
120
133
  }
134
+ function getFieldInfo(value) {
135
+ const fields = (0, schema_1.getFields)(value);
136
+ let ret = {};
137
+ for (const [k, f] of fields) {
138
+ ret[k] = {
139
+ dbCol: (0, schema_2.getStorageKey)(f, k),
140
+ // in tests (anything using SimpleBuilder), make it be the same as the fieldName
141
+ inputKey: k,
142
+ };
143
+ }
144
+ return ret;
145
+ }
146
+ exports.getFieldInfo = getFieldInfo;
121
147
  // reuses orchestrator and standard things
122
148
  class SimpleBuilder {
123
- constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt = undefined, action) {
149
+ constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt, action) {
124
150
  this.viewer = viewer;
125
151
  this.schema = schema;
126
152
  this.operation = operation;
127
153
  this.existingEnt = existingEnt;
154
+ this.m = new Map();
128
155
  // create dynamic placeholder
129
156
  // TODO: do we need to use this as the node when there's an existingEnt
130
157
  // same for generated builders.
@@ -150,11 +177,13 @@ class SimpleBuilder {
150
177
  this.ent = schema.ent;
151
178
  const tableName = getTableName(schema);
152
179
  this.nodeType = (0, camel_case_1.camelCase)(schema.ent.name);
180
+ const fieldInfo = getFieldInfo(schema);
153
181
  this.orchestrator = new orchestrator_1.Orchestrator({
154
182
  viewer: this.viewer,
155
183
  operation: operation,
156
184
  tableName: tableName,
157
185
  key,
186
+ fieldInfo,
158
187
  loaderOptions: {
159
188
  loaderFactory: new loaders_1.ObjectLoaderFactory({
160
189
  tableName: tableName,
@@ -164,7 +193,7 @@ class SimpleBuilder {
164
193
  ent: schema.ent,
165
194
  tableName: tableName,
166
195
  fields: [],
167
- fieldPrivacy: (0, schema_1.getFieldsWithPrivacy)(schema),
196
+ fieldPrivacy: (0, schema_1.getFieldsWithPrivacy)(schema, fieldInfo),
168
197
  },
169
198
  builder: this,
170
199
  action: action,
@@ -177,24 +206,40 @@ class SimpleBuilder {
177
206
  }
178
207
  return m;
179
208
  },
180
- updateInput: (input) => {
181
- const knownFields = (0, schema_1.getFields)(this.schema);
182
- for (const k in input) {
183
- if (knownFields.has(k)) {
184
- this.fields.set(k, input[k]);
185
- }
186
- else {
187
- // related to #510. we do camelCase to pass fields in here but fields may be snakeCase and we want that to pass in tests
188
- // we do camelCase in
189
- const sc = (0, snake_case_1.snakeCase)(k);
190
- if (knownFields.has(sc)) {
191
- this.fields.set(sc, input[k]);
192
- }
193
- }
194
- }
195
- },
209
+ updateInput: this.updateInput.bind(this),
196
210
  });
197
211
  }
212
+ getInput() {
213
+ let ret = {};
214
+ for (const [k, v] of this.fields) {
215
+ ret[k] = v;
216
+ }
217
+ return ret;
218
+ }
219
+ updateInput(input) {
220
+ const knownFields = (0, schema_1.getFields)(this.schema);
221
+ for (const k in input) {
222
+ if (knownFields.has(k)) {
223
+ this.fields.set(k, input[k]);
224
+ }
225
+ else {
226
+ // related to #510. we do camelCase to pass fields in here but fields may be snakeCase and we want that to pass in tests
227
+ // we do camelCase in
228
+ const sc = (0, snake_case_1.snakeCase)(k);
229
+ if (knownFields.has(sc)) {
230
+ this.fields.set(sc, input[k]);
231
+ }
232
+ }
233
+ }
234
+ }
235
+ // store data in Builder that can be retrieved by another validator, trigger, observer later in the action
236
+ storeData(k, v) {
237
+ this.m.set(k, v);
238
+ }
239
+ // retrieve data stored in this Builder with key
240
+ getStoredData(k) {
241
+ return this.m.get(k);
242
+ }
198
243
  build() {
199
244
  return this.orchestrator.build();
200
245
  }
@@ -219,14 +264,20 @@ class SimpleBuilder {
219
264
  }
220
265
  exports.SimpleBuilder = SimpleBuilder;
221
266
  class SimpleAction {
222
- constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt = undefined) {
267
+ constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt) {
223
268
  this.viewer = viewer;
224
269
  this.fields = fields;
225
- this.validators = [];
226
- this.triggers = [];
227
- this.observers = [];
228
270
  this.builder = new SimpleBuilder(this.viewer, schema, fields, operation, existingEnt, this);
229
271
  }
272
+ getTriggers() {
273
+ return [];
274
+ }
275
+ getValidators() {
276
+ return [];
277
+ }
278
+ getObservers() {
279
+ return [];
280
+ }
230
281
  getPrivacyPolicy() {
231
282
  return privacy_1.AlwaysAllowPrivacyPolicy;
232
283
  }
@@ -246,6 +297,9 @@ class SimpleAction {
246
297
  validX() {
247
298
  return this.builder.orchestrator.validX();
248
299
  }
300
+ validWithErrors() {
301
+ return this.builder.orchestrator.validWithErrors();
302
+ }
249
303
  async save() {
250
304
  await (0, action_1.saveBuilder)(this.builder);
251
305
  if (this.builder.operation !== action_1.WriteOperation.Delete) {
@@ -0,0 +1,10 @@
1
+ import { Client } from "pg";
2
+ import { Data } from "../../core/base";
3
+ import { Schema } from "../../schema";
4
+ interface Options {
5
+ overrides?: Data;
6
+ client: Client;
7
+ tableName: string;
8
+ }
9
+ export declare function writeFixture(schema: Schema, opts: Options): Promise<void>;
10
+ export {};
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeFixture = void 0;
4
+ const schema_1 = require("../../schema");
5
+ const value_1 = require("./value");
6
+ const ent_1 = require("../../core/ent");
7
+ async function writeFixture(schema, opts) {
8
+ const fields = (0, schema_1.getFields)(schema);
9
+ const d = {};
10
+ for (const [fieldName, field] of fields) {
11
+ const col = (0, schema_1.getStorageKey)(field, fieldName);
12
+ const val = (0, value_1.getDefaultValue)(field, col);
13
+ d[col] = val;
14
+ }
15
+ if (opts.overrides) {
16
+ for (const k in opts.overrides) {
17
+ d[k] = opts.overrides[k];
18
+ }
19
+ }
20
+ const q = (0, ent_1.buildInsertQuery)({
21
+ tableName: opts.tableName,
22
+ fields: d,
23
+ });
24
+ await opts.client.query(q[0], q[1]);
25
+ }
26
+ exports.writeFixture = writeFixture;
@@ -1,6 +1,7 @@
1
1
  import { Client as PGClient } from "pg";
2
2
  import { Dialect } from "../../core/db";
3
3
  import { Database as SqliteDatabase } from "better-sqlite3";
4
+ import { Field } from "../../schema";
4
5
  import { BuilderSchema } from "../builder";
5
6
  import { Ent } from "../../core/base";
6
7
  interface SchemaItem {
@@ -12,6 +13,7 @@ interface Column extends SchemaItem {
12
13
  primaryKey?: boolean;
13
14
  unique?: boolean;
14
15
  default?: string;
16
+ index?: boolean | indexOptions;
15
17
  foreignKey?: {
16
18
  table: string;
17
19
  col: string;
@@ -20,21 +22,29 @@ interface Column extends SchemaItem {
20
22
  interface Constraint extends SchemaItem {
21
23
  generate(): string;
22
24
  }
25
+ interface Index extends SchemaItem {
26
+ generate(): string;
27
+ }
23
28
  export interface CoreConcept {
24
29
  name: string;
25
30
  create(): string;
31
+ postCreate?(): string[];
26
32
  drop(): string;
27
33
  }
28
34
  export interface Table extends CoreConcept {
29
35
  columns: Column[];
30
36
  constraints?: Constraint[];
31
37
  }
32
- declare type options = Pick<Column, "nullable" | "primaryKey" | "default" | "foreignKey" | "unique">;
38
+ declare type options = Pick<Column, "nullable" | "primaryKey" | "default" | "foreignKey" | "unique" | "index">;
33
39
  export declare function primaryKey(name: string, cols: string[]): Constraint;
34
40
  export declare function foreignKey(name: string, cols: string[], fkey: {
35
41
  table: string;
36
42
  cols: string[];
37
43
  }): Constraint;
44
+ interface indexOptions {
45
+ type: string;
46
+ }
47
+ export declare function index(tableName: string, cols: string[], opts?: indexOptions): Index;
38
48
  export declare function uniqueIndex(name: string): Constraint;
39
49
  export declare function uuid(name: string, opts?: options): Column;
40
50
  export declare function text(name: string, opts?: options): Column;
@@ -72,6 +82,7 @@ export declare class TempDB {
72
82
  getDialect(): Dialect;
73
83
  getTables(): Map<string, CoreConcept>;
74
84
  beforeAll(setupConnString?: boolean): Promise<void>;
85
+ createImpl(table: CoreConcept): Promise<void>;
75
86
  getSqliteClient(): SqliteDatabase;
76
87
  getPostgresClient(): PGClient;
77
88
  afterAll(): Promise<void>;
@@ -81,10 +92,11 @@ export declare class TempDB {
81
92
  create(...tables: CoreConcept[]): Promise<void>;
82
93
  }
83
94
  export declare function assoc_edge_config_table(): Table;
84
- export declare function assoc_edge_table(name: string): Table;
95
+ export declare function assoc_edge_table(name: string, global?: boolean): Table;
85
96
  interface sqliteSetupOptions {
86
97
  disableDeleteAfterEachTest?: boolean;
87
98
  }
88
- export declare function setupSqlite(connString: string, tables: () => Table[], opts?: sqliteSetupOptions): void;
99
+ export declare function setupSqlite(connString: string, tables: () => Table[], opts?: sqliteSetupOptions): TempDB;
89
100
  export declare function getSchemaTable(schema: BuilderSchema<Ent>, dialect: Dialect): Table;
101
+ export declare function getColumnFromField(fieldName: string, f: Field, dialect: Dialect): Column;
90
102
  export {};
@@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  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;
25
+ exports.getColumnFromField = 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.index = exports.foreignKey = exports.primaryKey = void 0;
26
26
  const pg_1 = require("pg");
27
27
  const db_1 = __importStar(require("../../core/db"));
28
28
  // this should only be used in tests so we expect to be able to import without shenanigans
@@ -32,6 +32,7 @@ const fs = __importStar(require("fs"));
32
32
  const schema_1 = require("../../schema");
33
33
  const snake_case_1 = require("snake-case");
34
34
  const builder_1 = require("../builder");
35
+ const test_edge_global_schema_1 = require("../test_edge_global_schema");
35
36
  function primaryKey(name, cols) {
36
37
  return {
37
38
  name: name,
@@ -50,6 +51,16 @@ function foreignKey(name, cols, fkey) {
50
51
  };
51
52
  }
52
53
  exports.foreignKey = foreignKey;
54
+ function index(tableName, cols, opts) {
55
+ const name = `${tableName}_${cols.join("_")}_idx`;
56
+ return {
57
+ name,
58
+ generate() {
59
+ return `CREATE INDEX ${name} ON ${tableName} USING ${opts?.type || "btree"} (${cols.join(",")});`;
60
+ },
61
+ };
62
+ }
63
+ exports.index = index;
53
64
  function uniqueIndex(name) {
54
65
  return {
55
66
  name: "",
@@ -248,9 +259,24 @@ exports.boolList = boolList;
248
259
  function table(name, ...items) {
249
260
  let cols = [];
250
261
  let constraints = [];
262
+ let indexes = [];
251
263
  for (const item of items) {
252
264
  if (item.datatype !== undefined) {
253
265
  const col = item;
266
+ if (col.index) {
267
+ let opts = {
268
+ type: "btree",
269
+ };
270
+ if (col.index === true) {
271
+ opts = {
272
+ type: "btree",
273
+ };
274
+ }
275
+ else {
276
+ opts = col.index;
277
+ }
278
+ indexes.push(index(name, [col.name], opts));
279
+ }
254
280
  // add it as a constraint
255
281
  if (col.foreignKey) {
256
282
  constraints.push(foreignKey(`${name}_${col.name}_fkey`, [col.name], {
@@ -278,7 +304,12 @@ function table(name, ...items) {
278
304
  parts.push("PRIMARY KEY");
279
305
  }
280
306
  if (col.default !== undefined) {
281
- parts.push(`DEFAULT ${col.default}`);
307
+ if (db_1.Dialect.SQLite === db_1.default.getDialect()) {
308
+ parts.push(`DEFAULT "${col.default}"`);
309
+ }
310
+ else {
311
+ parts.push(`DEFAULT ${col.default}`);
312
+ }
282
313
  }
283
314
  if (col.unique) {
284
315
  parts.push("UNIQUE");
@@ -288,6 +319,9 @@ function table(name, ...items) {
288
319
  constraints.forEach((constraint) => schemaStr.push(constraint.generate()));
289
320
  return `CREATE TABLE IF NOT EXISTS ${name} (\n ${schemaStr})`;
290
321
  },
322
+ postCreate() {
323
+ return indexes.map((index) => index.generate());
324
+ },
291
325
  drop() {
292
326
  return `DROP TABLE IF EXISTS ${name}`;
293
327
  },
@@ -355,6 +389,7 @@ class TempDB {
355
389
  else {
356
390
  process.env.DB_CONNECTION_STRING = `postgres://localhost/${this.db}?`;
357
391
  }
392
+ db_1.default.initDB();
358
393
  }
359
394
  else {
360
395
  // will probably be setup via loadConfig
@@ -376,11 +411,24 @@ class TempDB {
376
411
  this.sqlite = (0, better_sqlite3_1.default)(filePath);
377
412
  }
378
413
  for (const [_, table] of this.tables) {
379
- if (this.dialect == db_1.Dialect.Postgres) {
380
- await this.dbClient.query(table.create());
414
+ await this.createImpl(table);
415
+ }
416
+ }
417
+ async createImpl(table) {
418
+ if (this.dialect == db_1.Dialect.Postgres) {
419
+ await this.dbClient.query(table.create());
420
+ if (table.postCreate) {
421
+ for (const q of table.postCreate()) {
422
+ await this.dbClient.query(q);
423
+ }
381
424
  }
382
- else {
383
- this.sqlite.exec(table.create());
425
+ }
426
+ else {
427
+ this.sqlite.exec(table.create());
428
+ if (table.postCreate) {
429
+ for (const q of table.postCreate()) {
430
+ this.sqlite.exec(q);
431
+ }
384
432
  }
385
433
  }
386
434
  }
@@ -431,12 +479,7 @@ class TempDB {
431
479
  if (this.tables.has(table.name)) {
432
480
  throw new Error(`table with name ${table.name} already exists`);
433
481
  }
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
- }
482
+ await this.createImpl(table);
440
483
  this.tables.set(table.name, table);
441
484
  }
442
485
  }
@@ -448,18 +491,26 @@ function assoc_edge_config_table() {
448
491
  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
492
  }
450
493
  exports.assoc_edge_config_table = assoc_edge_config_table;
451
- function assoc_edge_table(name) {
452
- return table(name, uuid("id1"), text("id1_type"),
494
+ // if global flag is true, add any column from testEdgeGlobalSchema
495
+ // up to caller to set/clear that as needed
496
+ function assoc_edge_table(name, global) {
497
+ const t = table(name, uuid("id1"), text("id1_type"),
453
498
  // same as in assoc_edge_config_table
454
499
  text("edge_type"), uuid("id2"), text("id2_type"), timestamptz("time"), text("data", { nullable: true }), primaryKey(`${name}_pkey`, ["id1", "id2", "edge_type"]));
500
+ if (global) {
501
+ for (const k in test_edge_global_schema_1.testEdgeGlobalSchema.extraEdgeFields) {
502
+ const col = getColumnFromField(k, test_edge_global_schema_1.testEdgeGlobalSchema.extraEdgeFields[k], db_1.Dialect.Postgres);
503
+ t.columns.push(col);
504
+ }
505
+ }
506
+ return t;
455
507
  }
456
508
  exports.assoc_edge_table = assoc_edge_table;
457
509
  function setupSqlite(connString, tables, opts) {
458
- let tdb;
510
+ let tdb = new TempDB(db_1.Dialect.SQLite, tables());
459
511
  beforeAll(async () => {
460
512
  process.env.DB_CONNECTION_STRING = connString;
461
513
  (0, config_1.loadConfig)();
462
- tdb = new TempDB(db_1.Dialect.SQLite, tables());
463
514
  await tdb.beforeAll();
464
515
  const conn = db_1.default.getInstance().getConnection();
465
516
  expect(conn.db.memory).toBe(false);
@@ -482,7 +533,9 @@ function setupSqlite(connString, tables, opts) {
482
533
  afterAll(async () => {
483
534
  await tdb.afterAll();
484
535
  fs.rmSync(tdb.getSqliteClient().name);
536
+ delete process.env.DB_CONNECTION_STRING;
485
537
  });
538
+ return tdb;
486
539
  }
487
540
  exports.setupSqlite = setupSqlite;
488
541
  function getSchemaTable(schema, dialect) {
@@ -549,6 +602,7 @@ function getColumnFromField(fieldName, f, dialect) {
549
602
  return getColumn(fieldName, f, fn);
550
603
  }
551
604
  }
605
+ exports.getColumnFromField = getColumnFromField;
552
606
  function getColumn(fieldName, f, col) {
553
607
  return col(storageKey(fieldName, f), buildOpts(f));
554
608
  }
@@ -0,0 +1,6 @@
1
+ import { Field, Schema } from "../../schema";
2
+ interface Info {
3
+ schema: Schema;
4
+ }
5
+ export declare function getDefaultValue(f: Field, col: string, infos?: Map<string, Info>): any;
6
+ export {};