@snowtop/ent 0.1.0-alpha95 → 0.1.0-alpha96
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.
- package/action/action.d.ts +3 -3
- package/action/executor.js +6 -1
- package/action/experimental_action.d.ts +5 -2
- package/action/experimental_action.js +15 -12
- package/action/index.d.ts +2 -0
- package/action/index.js +7 -1
- package/action/orchestrator.d.ts +4 -2
- package/action/orchestrator.js +6 -0
- package/action/relative_value.d.ts +47 -0
- package/action/relative_value.js +125 -0
- package/action/transaction.d.ts +10 -0
- package/action/transaction.js +23 -0
- package/auth/auth.d.ts +1 -1
- package/core/base.d.ts +4 -2
- package/core/clause.d.ts +6 -1
- package/core/clause.js +25 -4
- package/core/config.d.ts +2 -1
- package/core/config.js +2 -0
- package/core/date.js +1 -5
- package/core/db.d.ts +9 -6
- package/core/db.js +14 -6
- package/core/ent.d.ts +3 -1
- package/core/ent.js +76 -26
- package/core/logger.d.ts +1 -1
- package/core/query/assoc_query.d.ts +2 -2
- package/core/query/shared_assoc_test.js +1 -2
- package/core/query/shared_test.js +0 -1
- package/graphql/graphql.d.ts +6 -6
- package/graphql/graphql.js +1 -0
- package/graphql/query/connection_type.d.ts +1 -1
- package/graphql/query/shared_assoc_test.js +1 -1
- package/graphql/query/shared_edge_connection.js +0 -4
- package/imports/index.d.ts +6 -1
- package/imports/index.js +14 -3
- package/index.d.ts +1 -0
- package/package.json +16 -16
- package/parse_schema/parse.d.ts +7 -7
- package/schema/base_schema.d.ts +3 -3
- package/schema/field.js +2 -2
- package/schema/schema.d.ts +11 -10
- package/schema/schema.js +3 -13
- package/scripts/custom_graphql.js +30 -4
- package/testutils/action/complex_schemas.d.ts +69 -0
- package/testutils/action/complex_schemas.js +398 -0
- package/testutils/builder.d.ts +21 -36
- package/testutils/builder.js +39 -45
- package/testutils/db/temp_db.d.ts +6 -3
- package/testutils/db/temp_db.js +79 -7
- package/testutils/db/value.d.ts +1 -0
- package/testutils/db/value.js +2 -2
- package/testutils/db_mock.d.ts +16 -4
- package/testutils/db_mock.js +48 -5
- package/testutils/ent-graphql-tests/index.d.ts +7 -1
- package/testutils/ent-graphql-tests/index.js +17 -5
- package/testutils/fake_data/fake_contact.d.ts +1 -0
- package/testutils/fake_data/fake_contact.js +6 -5
- package/testutils/fake_data/fake_event.d.ts +1 -0
- package/testutils/fake_data/fake_event.js +4 -3
- package/testutils/fake_data/fake_tag.d.ts +2 -1
- package/testutils/fake_data/fake_tag.js +6 -5
- package/testutils/fake_data/fake_user.d.ts +2 -1
- package/testutils/fake_data/fake_user.js +14 -13
- package/tsc/ast.d.ts +1 -1
package/testutils/builder.d.ts
CHANGED
|
@@ -4,60 +4,45 @@ import { Action, Builder, Changeset, WriteOperation, Validator, Trigger, Observe
|
|
|
4
4
|
import { FieldMap, Schema } from "../schema";
|
|
5
5
|
import { SchemaConfig, EntSchema } from "../schema/base_schema";
|
|
6
6
|
import { FieldInfoMap } from "../schema/schema";
|
|
7
|
-
|
|
7
|
+
import { Clause } from "src/core/clause";
|
|
8
|
+
export declare class BaseEnt {
|
|
9
|
+
viewer: Viewer;
|
|
10
|
+
readonly data: Data;
|
|
11
|
+
readonly id: ID;
|
|
12
|
+
constructor(viewer: Viewer, data: Data);
|
|
13
|
+
getKey(): string;
|
|
14
|
+
getPrivacyPolicy(): PrivacyPolicy;
|
|
15
|
+
__setRawDBData(data: Data): void;
|
|
16
|
+
}
|
|
17
|
+
export declare class User extends BaseEnt implements Ent {
|
|
8
18
|
viewer: Viewer;
|
|
9
19
|
data: Data;
|
|
10
|
-
id: ID;
|
|
11
20
|
accountID: string;
|
|
12
21
|
nodeType: string;
|
|
13
|
-
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
14
22
|
firstName: string;
|
|
15
23
|
constructor(viewer: Viewer, data: Data);
|
|
16
24
|
}
|
|
17
|
-
export declare class Event implements Ent {
|
|
18
|
-
viewer: Viewer;
|
|
19
|
-
data: Data;
|
|
20
|
-
id: ID;
|
|
25
|
+
export declare class Event extends BaseEnt implements Ent {
|
|
21
26
|
accountID: string;
|
|
22
27
|
nodeType: string;
|
|
23
|
-
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
24
|
-
constructor(viewer: Viewer, data: Data);
|
|
25
28
|
}
|
|
26
|
-
export declare class Contact implements Ent {
|
|
27
|
-
viewer: Viewer;
|
|
28
|
-
data: Data;
|
|
29
|
-
id: ID;
|
|
29
|
+
export declare class Contact extends BaseEnt implements Ent {
|
|
30
30
|
accountID: string;
|
|
31
31
|
nodeType: string;
|
|
32
32
|
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
33
|
-
constructor(viewer: Viewer, data: Data);
|
|
34
33
|
}
|
|
35
|
-
export declare class Group implements Ent {
|
|
36
|
-
viewer: Viewer;
|
|
37
|
-
data: Data;
|
|
38
|
-
id: ID;
|
|
34
|
+
export declare class Group extends BaseEnt implements Ent {
|
|
39
35
|
accountID: string;
|
|
40
36
|
nodeType: string;
|
|
41
37
|
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
42
|
-
constructor(viewer: Viewer, data: Data);
|
|
43
38
|
}
|
|
44
|
-
export declare class Message implements Ent {
|
|
45
|
-
viewer: Viewer;
|
|
46
|
-
data: Data;
|
|
47
|
-
id: ID;
|
|
39
|
+
export declare class Message extends BaseEnt implements Ent {
|
|
48
40
|
accountID: string;
|
|
49
41
|
nodeType: string;
|
|
50
|
-
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
51
|
-
constructor(viewer: Viewer, data: Data);
|
|
52
42
|
}
|
|
53
|
-
export declare class Address implements Ent {
|
|
54
|
-
viewer: Viewer;
|
|
55
|
-
data: Data;
|
|
56
|
-
id: ID;
|
|
43
|
+
export declare class Address extends BaseEnt implements Ent {
|
|
57
44
|
accountID: string;
|
|
58
45
|
nodeType: string;
|
|
59
|
-
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
60
|
-
constructor(viewer: Viewer, data: Data);
|
|
61
46
|
}
|
|
62
47
|
export interface BuilderSchema<T extends Ent> extends Schema {
|
|
63
48
|
ent: EntConstructor<T>;
|
|
@@ -67,13 +52,13 @@ export declare class EntBuilderSchema<T extends Ent> extends EntSchema {
|
|
|
67
52
|
constructor(ent: EntConstructor<T>, cfg: SchemaConfig);
|
|
68
53
|
}
|
|
69
54
|
export declare function getBuilderSchema<T extends Ent>(cfg: SchemaConfig, ent: EntConstructor<T>): BuilderSchema<T>;
|
|
70
|
-
export declare function getBuilderSchemaFromFields<T extends Ent>(fields: FieldMap, ent: EntConstructor<T
|
|
55
|
+
export declare function getBuilderSchemaFromFields<T extends Ent>(fields: FieldMap, ent: EntConstructor<T>, opts?: Partial<Exclude<SchemaConfig, "fields">>): BuilderSchema<T>;
|
|
71
56
|
export declare function getBuilderSchemaTZFromFields<T extends Ent>(fields: FieldMap, ent: EntConstructor<T>): BuilderSchema<T>;
|
|
72
57
|
export declare function getSchemaName(value: BuilderSchema<Ent>): string;
|
|
73
58
|
export declare function getTableName(value: BuilderSchema<Ent>): string;
|
|
74
59
|
export declare function getFieldInfo(value: BuilderSchema<Ent>): FieldInfoMap;
|
|
75
|
-
|
|
76
|
-
|
|
60
|
+
type MaybeNull<T extends Ent> = T | null;
|
|
61
|
+
type TMaybleNullableEnt<T extends Ent> = T | MaybeNull<T>;
|
|
77
62
|
export declare class SimpleBuilder<T extends Ent, TExistingEnt extends TMaybleNullableEnt<T> = MaybeNull<T>> implements Builder<T, Viewer, TExistingEnt> {
|
|
78
63
|
viewer: Viewer;
|
|
79
64
|
private schema;
|
|
@@ -85,7 +70,7 @@ export declare class SimpleBuilder<T extends Ent, TExistingEnt extends TMaybleNu
|
|
|
85
70
|
fields: Map<string, any>;
|
|
86
71
|
nodeType: string;
|
|
87
72
|
m: Map<string, any>;
|
|
88
|
-
constructor(viewer: Viewer, schema: BuilderSchema<T>, fields: Map<string, any>, operation: WriteOperation, existingEnt: TExistingEnt, action?: Action<T, SimpleBuilder<T, TExistingEnt>, Viewer, Data, TExistingEnt> | undefined);
|
|
73
|
+
constructor(viewer: Viewer, schema: BuilderSchema<T>, fields: Map<string, any>, operation: WriteOperation, existingEnt: TExistingEnt, action?: Action<T, SimpleBuilder<T, TExistingEnt>, Viewer, Data, TExistingEnt> | undefined, expressions?: Map<string, Clause>);
|
|
89
74
|
getInput(): Data;
|
|
90
75
|
updateInput(input: Data): void;
|
|
91
76
|
storeData(k: string, v: any): void;
|
|
@@ -106,7 +91,7 @@ export declare class SimpleAction<T extends Ent, TExistingEnt extends TMaybleNul
|
|
|
106
91
|
private fields;
|
|
107
92
|
builder: SimpleBuilder<T, TExistingEnt>;
|
|
108
93
|
viewerForEntLoad: viewerEntLoadFunc | undefined;
|
|
109
|
-
constructor(viewer: Viewer, schema: BuilderSchema<T>, fields: Map<string, any>, operation: WriteOperation | undefined, existingEnt: TExistingEnt);
|
|
94
|
+
constructor(viewer: Viewer, schema: BuilderSchema<T>, fields: Map<string, any>, operation: WriteOperation | undefined, existingEnt: TExistingEnt, expressions?: Map<string, Clause>);
|
|
110
95
|
getTriggers(): (Trigger<T, SimpleBuilder<T>> | Array<Trigger<T, SimpleBuilder<T>>>)[];
|
|
111
96
|
getValidators(): Validator<T, SimpleBuilder<T>>[];
|
|
112
97
|
getObservers(): Observer<T, SimpleBuilder<T>>[];
|
package/testutils/builder.js
CHANGED
|
@@ -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.getFieldInfo = exports.getTableName = exports.getSchemaName = exports.getBuilderSchemaTZFromFields = exports.getBuilderSchemaFromFields = exports.getBuilderSchema = exports.EntBuilderSchema = 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.EntBuilderSchema = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = exports.BaseEnt = 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");
|
|
@@ -16,86 +16,79 @@ 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
18
|
const schema_2 = require("../schema/schema");
|
|
19
|
-
class
|
|
19
|
+
class BaseEnt {
|
|
20
20
|
constructor(viewer, data) {
|
|
21
21
|
this.viewer = viewer;
|
|
22
22
|
this.data = data;
|
|
23
|
-
this.accountID = "";
|
|
24
|
-
this.nodeType = "User";
|
|
25
23
|
this.data.created_at = (0, convert_1.convertDate)(data.created_at);
|
|
26
24
|
this.data.updated_at = (0, convert_1.convertDate)(data.updated_at);
|
|
27
|
-
this.id = data.
|
|
28
|
-
|
|
25
|
+
this.id = data[this.getKey()];
|
|
26
|
+
}
|
|
27
|
+
getKey() {
|
|
28
|
+
return "id";
|
|
29
29
|
}
|
|
30
30
|
getPrivacyPolicy() {
|
|
31
31
|
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
32
32
|
}
|
|
33
|
+
__setRawDBData(data) {
|
|
34
|
+
// doesn't apply here so ignore...
|
|
35
|
+
}
|
|
33
36
|
}
|
|
34
|
-
exports.
|
|
35
|
-
class
|
|
37
|
+
exports.BaseEnt = BaseEnt;
|
|
38
|
+
class User extends BaseEnt {
|
|
36
39
|
constructor(viewer, data) {
|
|
40
|
+
super(viewer, data);
|
|
37
41
|
this.viewer = viewer;
|
|
38
42
|
this.data = data;
|
|
39
43
|
this.accountID = "";
|
|
40
|
-
this.nodeType = "
|
|
41
|
-
this.
|
|
44
|
+
this.nodeType = "User";
|
|
45
|
+
this.firstName = data.first_name;
|
|
42
46
|
}
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
}
|
|
48
|
+
exports.User = User;
|
|
49
|
+
class Event extends BaseEnt {
|
|
50
|
+
constructor() {
|
|
51
|
+
super(...arguments);
|
|
52
|
+
this.accountID = "";
|
|
53
|
+
this.nodeType = "Event";
|
|
45
54
|
}
|
|
46
55
|
}
|
|
47
56
|
exports.Event = Event;
|
|
48
|
-
class Contact {
|
|
49
|
-
constructor(
|
|
50
|
-
|
|
51
|
-
this.data = data;
|
|
57
|
+
class Contact extends BaseEnt {
|
|
58
|
+
constructor() {
|
|
59
|
+
super(...arguments);
|
|
52
60
|
this.accountID = "";
|
|
53
61
|
this.nodeType = "Contact";
|
|
54
|
-
this.data.created_at = (0, convert_1.convertDate)(data.created_at);
|
|
55
|
-
this.data.updated_at = (0, convert_1.convertDate)(data.updated_at);
|
|
56
|
-
this.id = data.id;
|
|
57
62
|
}
|
|
58
63
|
getPrivacyPolicy() {
|
|
59
64
|
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
60
65
|
}
|
|
61
66
|
}
|
|
62
67
|
exports.Contact = Contact;
|
|
63
|
-
class Group {
|
|
64
|
-
constructor(
|
|
65
|
-
|
|
66
|
-
this.data = data;
|
|
68
|
+
class Group extends BaseEnt {
|
|
69
|
+
constructor() {
|
|
70
|
+
super(...arguments);
|
|
67
71
|
this.accountID = "";
|
|
68
72
|
this.nodeType = "Group";
|
|
69
|
-
this.id = data.id;
|
|
70
73
|
}
|
|
71
74
|
getPrivacyPolicy() {
|
|
72
75
|
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
78
|
exports.Group = Group;
|
|
76
|
-
class Message {
|
|
77
|
-
constructor(
|
|
78
|
-
|
|
79
|
-
this.data = data;
|
|
79
|
+
class Message extends BaseEnt {
|
|
80
|
+
constructor() {
|
|
81
|
+
super(...arguments);
|
|
80
82
|
this.accountID = "";
|
|
81
83
|
this.nodeType = "Message";
|
|
82
|
-
this.id = data.id;
|
|
83
|
-
}
|
|
84
|
-
getPrivacyPolicy() {
|
|
85
|
-
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
86
84
|
}
|
|
87
85
|
}
|
|
88
86
|
exports.Message = Message;
|
|
89
|
-
class Address {
|
|
90
|
-
constructor(
|
|
91
|
-
|
|
92
|
-
this.data = data;
|
|
87
|
+
class Address extends BaseEnt {
|
|
88
|
+
constructor() {
|
|
89
|
+
super(...arguments);
|
|
93
90
|
this.accountID = "";
|
|
94
91
|
this.nodeType = "Address";
|
|
95
|
-
this.id = data.id;
|
|
96
|
-
}
|
|
97
|
-
getPrivacyPolicy() {
|
|
98
|
-
return privacy_1.AlwaysAllowPrivacyPolicy;
|
|
99
92
|
}
|
|
100
93
|
}
|
|
101
94
|
exports.Address = Address;
|
|
@@ -113,9 +106,9 @@ function getBuilderSchema(cfg, ent) {
|
|
|
113
106
|
};
|
|
114
107
|
}
|
|
115
108
|
exports.getBuilderSchema = getBuilderSchema;
|
|
116
|
-
function getBuilderSchemaFromFields(fields, ent) {
|
|
109
|
+
function getBuilderSchemaFromFields(fields, ent, opts) {
|
|
117
110
|
return {
|
|
118
|
-
...new base_schema_1.EntSchema({ fields }),
|
|
111
|
+
...new base_schema_1.EntSchema({ ...opts, fields }),
|
|
119
112
|
ent,
|
|
120
113
|
};
|
|
121
114
|
}
|
|
@@ -153,7 +146,7 @@ function getFieldInfo(value) {
|
|
|
153
146
|
exports.getFieldInfo = getFieldInfo;
|
|
154
147
|
// reuses orchestrator and standard things
|
|
155
148
|
class SimpleBuilder {
|
|
156
|
-
constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt, action) {
|
|
149
|
+
constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt, action, expressions) {
|
|
157
150
|
this.viewer = viewer;
|
|
158
151
|
this.schema = schema;
|
|
159
152
|
this.operation = operation;
|
|
@@ -204,6 +197,7 @@ class SimpleBuilder {
|
|
|
204
197
|
},
|
|
205
198
|
builder: this,
|
|
206
199
|
action: action,
|
|
200
|
+
expressions,
|
|
207
201
|
schema: this.schema,
|
|
208
202
|
editedFields: () => {
|
|
209
203
|
// to simulate what we do in generated builders where we return a new Map
|
|
@@ -271,10 +265,10 @@ class SimpleBuilder {
|
|
|
271
265
|
}
|
|
272
266
|
exports.SimpleBuilder = SimpleBuilder;
|
|
273
267
|
class SimpleAction {
|
|
274
|
-
constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt) {
|
|
268
|
+
constructor(viewer, schema, fields, operation = action_1.WriteOperation.Insert, existingEnt, expressions) {
|
|
275
269
|
this.viewer = viewer;
|
|
276
270
|
this.fields = fields;
|
|
277
|
-
this.builder = new SimpleBuilder(this.viewer, schema, fields, operation, existingEnt, this);
|
|
271
|
+
this.builder = new SimpleBuilder(this.viewer, schema, fields, operation, existingEnt, this, expressions);
|
|
278
272
|
}
|
|
279
273
|
getTriggers() {
|
|
280
274
|
return [];
|
|
@@ -36,12 +36,13 @@ export interface Table extends CoreConcept {
|
|
|
36
36
|
columns: Column[];
|
|
37
37
|
constraints?: Constraint[];
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
type options = Pick<Column, "nullable" | "primaryKey" | "default" | "foreignKey" | "unique" | "index">;
|
|
40
40
|
export declare function primaryKey(name: string, cols: string[]): Constraint;
|
|
41
41
|
export declare function foreignKey(name: string, cols: string[], fkey: {
|
|
42
42
|
table: string;
|
|
43
43
|
cols: string[];
|
|
44
44
|
}): Constraint;
|
|
45
|
+
export declare function check(name: string, condition: string): Constraint;
|
|
45
46
|
interface indexOptions {
|
|
46
47
|
type?: string;
|
|
47
48
|
unique?: boolean;
|
|
@@ -94,10 +95,12 @@ export declare class TempDB {
|
|
|
94
95
|
}
|
|
95
96
|
export declare function assoc_edge_config_table(): Table;
|
|
96
97
|
export declare function assoc_edge_table(name: string, global?: boolean): Table;
|
|
97
|
-
interface
|
|
98
|
+
interface setupOptions {
|
|
98
99
|
disableDeleteAfterEachTest?: boolean;
|
|
99
100
|
}
|
|
100
|
-
export declare function setupSqlite(connString: string, tables: () => Table[], opts?:
|
|
101
|
+
export declare function setupSqlite(connString: string, tables: () => Table[], opts?: setupOptions): TempDB;
|
|
102
|
+
export declare function setupPostgres(tables: () => Table[], opts?: setupOptions): void;
|
|
103
|
+
export declare function doSQLiteTestFromSchemas(schemas: BuilderSchema<Ent>[], doTest: () => Promise<void>, db?: string): Promise<TempDB>;
|
|
101
104
|
export declare function getSchemaTable(schema: BuilderSchema<Ent>, dialect: Dialect): Table;
|
|
102
105
|
export declare function getColumnFromField(fieldName: string, f: Field, dialect: Dialect): Column;
|
|
103
106
|
export {};
|
package/testutils/db/temp_db.js
CHANGED
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
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.index = 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;
|
|
30
30
|
const pg_1 = require("pg");
|
|
31
31
|
const db_1 = __importStar(require("../../core/db"));
|
|
32
32
|
// this should only be used in tests so we expect to be able to import without shenanigans
|
|
@@ -55,6 +55,15 @@ function foreignKey(name, cols, fkey) {
|
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
57
|
exports.foreignKey = foreignKey;
|
|
58
|
+
function check(name, condition) {
|
|
59
|
+
return {
|
|
60
|
+
name,
|
|
61
|
+
generate() {
|
|
62
|
+
return `CONSTRAINT ${name} CHECK(${condition})`;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
exports.check = check;
|
|
58
67
|
function isPostCreateIndex(s) {
|
|
59
68
|
return (s.postCreate !== undefined &&
|
|
60
69
|
s.postCreate());
|
|
@@ -382,13 +391,17 @@ class TempDB {
|
|
|
382
391
|
this.db = randomDB();
|
|
383
392
|
await this.client.query(`CREATE DATABASE ${this.db}`);
|
|
384
393
|
if (setupConnString) {
|
|
394
|
+
delete process.env.DB_CONNECTION_STRING;
|
|
395
|
+
let connStr = "";
|
|
385
396
|
if (user && password) {
|
|
386
|
-
|
|
397
|
+
connStr = `postgres://${user}:${password}@localhost:5432/${this.db}`;
|
|
387
398
|
}
|
|
388
399
|
else {
|
|
389
|
-
|
|
400
|
+
connStr = `postgres://localhost/${this.db}?`;
|
|
390
401
|
}
|
|
391
|
-
db_1.default.initDB(
|
|
402
|
+
db_1.default.initDB({
|
|
403
|
+
connectionString: connStr,
|
|
404
|
+
});
|
|
392
405
|
}
|
|
393
406
|
else {
|
|
394
407
|
// will probably be setup via loadConfig
|
|
@@ -547,13 +560,72 @@ function setupSqlite(connString, tables, opts) {
|
|
|
547
560
|
return tdb;
|
|
548
561
|
}
|
|
549
562
|
exports.setupSqlite = setupSqlite;
|
|
563
|
+
function setupPostgres(tables, opts) {
|
|
564
|
+
let tdb;
|
|
565
|
+
beforeAll(async () => {
|
|
566
|
+
tdb = new TempDB(db_1.Dialect.Postgres, tables());
|
|
567
|
+
await tdb.beforeAll();
|
|
568
|
+
});
|
|
569
|
+
// TODO need to fix this implementation...
|
|
570
|
+
if (!opts?.disableDeleteAfterEachTest) {
|
|
571
|
+
afterEach(async () => {
|
|
572
|
+
const client = await db_1.default.getInstance().getNewClient();
|
|
573
|
+
for (const [key, _] of tdb.__getTables()) {
|
|
574
|
+
const query = `delete from ${key}`;
|
|
575
|
+
await client.exec(query);
|
|
576
|
+
}
|
|
577
|
+
client.release();
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
afterAll(async () => {
|
|
581
|
+
await tdb.afterAll();
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
exports.setupPostgres = setupPostgres;
|
|
585
|
+
async function doSQLiteTestFromSchemas(schemas, doTest, db) {
|
|
586
|
+
const connString = `sqlite:///${db || randomDB()}.db`;
|
|
587
|
+
const tables = schemas.map((schema) => getSchemaTable(schema, db_1.Dialect.SQLite));
|
|
588
|
+
let tdb = new TempDB(db_1.Dialect.SQLite, tables);
|
|
589
|
+
process.env.DB_CONNECTION_STRING = connString;
|
|
590
|
+
(0, config_1.loadConfig)();
|
|
591
|
+
await tdb.beforeAll();
|
|
592
|
+
await doTest();
|
|
593
|
+
await tdb.afterAll();
|
|
594
|
+
delete process.env.DB_CONNECTION_STRING;
|
|
595
|
+
return tdb;
|
|
596
|
+
}
|
|
597
|
+
exports.doSQLiteTestFromSchemas = doSQLiteTestFromSchemas;
|
|
550
598
|
function getSchemaTable(schema, dialect) {
|
|
551
599
|
const fields = (0, schema_1.getFields)(schema);
|
|
552
|
-
const
|
|
600
|
+
const items = [];
|
|
553
601
|
for (const [fieldName, field] of fields) {
|
|
554
|
-
|
|
602
|
+
items.push(getColumnFromField(fieldName, field, dialect));
|
|
603
|
+
}
|
|
604
|
+
if (schema.constraints) {
|
|
605
|
+
for (const constraint of schema.constraints) {
|
|
606
|
+
switch (constraint.type) {
|
|
607
|
+
case schema_1.ConstraintType.PrimaryKey:
|
|
608
|
+
items.push(primaryKey(constraint.name, constraint.columns));
|
|
609
|
+
break;
|
|
610
|
+
case schema_1.ConstraintType.ForeignKey:
|
|
611
|
+
if (!constraint.fkey) {
|
|
612
|
+
throw new Error(`need 'fkey' field for foreign key constraint`);
|
|
613
|
+
}
|
|
614
|
+
items.push(foreignKey(constraint.name, constraint.columns, {
|
|
615
|
+
table: constraint.fkey.tableName,
|
|
616
|
+
cols: constraint.fkey.columns,
|
|
617
|
+
}));
|
|
618
|
+
break;
|
|
619
|
+
case schema_1.ConstraintType.Check:
|
|
620
|
+
if (!constraint.condition) {
|
|
621
|
+
throw new Error(`need 'condition' field for check constraint`);
|
|
622
|
+
}
|
|
623
|
+
items.push(check(constraint.name, constraint.condition));
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
555
627
|
}
|
|
556
|
-
return table((0, builder_1.getTableName)(schema), ...
|
|
628
|
+
return table((0, builder_1.getTableName)(schema), ...items);
|
|
557
629
|
}
|
|
558
630
|
exports.getSchemaTable = getSchemaTable;
|
|
559
631
|
function getColumnForDbType(t, dialect) {
|
package/testutils/db/value.d.ts
CHANGED
package/testutils/db/value.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getDefaultValue = void 0;
|
|
3
|
+
exports.getDefaultValue = exports.randomEmail = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
5
|
const schema_1 = require("../../schema");
|
|
6
6
|
const schema_2 = require("../../schema");
|
|
@@ -11,6 +11,7 @@ function randomEmail(domain) {
|
|
|
11
11
|
domain = domain || "email.com";
|
|
12
12
|
return `test+${random()}@${domain}`;
|
|
13
13
|
}
|
|
14
|
+
exports.randomEmail = randomEmail;
|
|
14
15
|
function randomPhoneNumber() {
|
|
15
16
|
return `+1${Math.random().toString(10).substring(2, 11)}`;
|
|
16
17
|
}
|
|
@@ -145,7 +146,6 @@ const emailType = {
|
|
|
145
146
|
},
|
|
146
147
|
regex: /^email(_address)|_email$/,
|
|
147
148
|
};
|
|
148
|
-
const pdt = (0, schema_2.StringType)();
|
|
149
149
|
const phoneType = {
|
|
150
150
|
dbType: schema_1.DBType.String,
|
|
151
151
|
newValue: () => {
|
package/testutils/db_mock.d.ts
CHANGED
|
@@ -18,7 +18,8 @@ export declare enum queryType {
|
|
|
18
18
|
UPDATE = 2,
|
|
19
19
|
BEGIN = 3,
|
|
20
20
|
COMMIT = 4,
|
|
21
|
-
ROLLBACK = 5
|
|
21
|
+
ROLLBACK = 5,
|
|
22
|
+
DELETE = 6
|
|
22
23
|
}
|
|
23
24
|
export interface queryStructure {
|
|
24
25
|
tableName?: string;
|
|
@@ -32,11 +33,14 @@ interface internalQueryStructure extends queryStructure {
|
|
|
32
33
|
suffix?: string;
|
|
33
34
|
setClause?: string;
|
|
34
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated
|
|
38
|
+
*/
|
|
35
39
|
export declare class QueryRecorder {
|
|
36
40
|
private static queries;
|
|
37
41
|
private static ids;
|
|
38
42
|
private static data;
|
|
39
|
-
|
|
43
|
+
static getQueryStructure(query: any): internalQueryStructure | null;
|
|
40
44
|
private static recordQuery;
|
|
41
45
|
static newID(): ID;
|
|
42
46
|
static getCurrentIDs(): ID[];
|
|
@@ -46,8 +50,16 @@ export declare class QueryRecorder {
|
|
|
46
50
|
static clear(): void;
|
|
47
51
|
static clearQueries(): void;
|
|
48
52
|
static getCurrentQueries(): queryOptions[];
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated will keep this since it's just using ml.logs
|
|
55
|
+
* which we still want around (I think) but we should eventually
|
|
56
|
+
* probably stop using this
|
|
57
|
+
*/
|
|
58
|
+
static validateQueryStructuresFromLogs(ml: MockLogs, expected: queryStructure[], opts?: {
|
|
59
|
+
skipSelect?: boolean;
|
|
60
|
+
flagBeginCommit?: boolean;
|
|
61
|
+
}): void;
|
|
62
|
+
private static validateQueryStructuresImpl;
|
|
51
63
|
static mockPool(pool: typeof Pool): void;
|
|
52
64
|
}
|
|
53
65
|
export {};
|
package/testutils/db_mock.js
CHANGED
|
@@ -29,7 +29,11 @@ var queryType;
|
|
|
29
29
|
queryType[queryType["BEGIN"] = 3] = "BEGIN";
|
|
30
30
|
queryType[queryType["COMMIT"] = 4] = "COMMIT";
|
|
31
31
|
queryType[queryType["ROLLBACK"] = 5] = "ROLLBACK";
|
|
32
|
+
queryType[queryType["DELETE"] = 6] = "DELETE";
|
|
32
33
|
})(queryType = exports.queryType || (exports.queryType = {}));
|
|
34
|
+
/**
|
|
35
|
+
* @deprecated
|
|
36
|
+
*/
|
|
33
37
|
class QueryRecorder {
|
|
34
38
|
// TODO kill use AST or just throw away
|
|
35
39
|
static getQueryStructure(query) {
|
|
@@ -76,6 +80,36 @@ class QueryRecorder {
|
|
|
76
80
|
};
|
|
77
81
|
}
|
|
78
82
|
}
|
|
83
|
+
if (/^DELETE/.test(query)) {
|
|
84
|
+
// regex can't do returning
|
|
85
|
+
let execArray = /^DELETE FROM (.+) WHERE (.+) /.exec(query);
|
|
86
|
+
if (execArray) {
|
|
87
|
+
return {
|
|
88
|
+
tableName: execArray[1],
|
|
89
|
+
whereClause: execArray[2],
|
|
90
|
+
type: queryType.DELETE,
|
|
91
|
+
query: execArray[0],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (/^BEGIN/.test(query)) {
|
|
96
|
+
return {
|
|
97
|
+
type: queryType.BEGIN,
|
|
98
|
+
query: query,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (/^COMMIT/.test(query)) {
|
|
102
|
+
return {
|
|
103
|
+
type: queryType.COMMIT,
|
|
104
|
+
query: query,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (/^ROLLBACK/.test(query)) {
|
|
108
|
+
return {
|
|
109
|
+
type: queryType.ROLLBACK,
|
|
110
|
+
query: query,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
79
113
|
return null;
|
|
80
114
|
}
|
|
81
115
|
static recordQuery(query, values) {
|
|
@@ -131,7 +165,12 @@ class QueryRecorder {
|
|
|
131
165
|
static getCurrentQueries() {
|
|
132
166
|
return QueryRecorder.queries;
|
|
133
167
|
}
|
|
134
|
-
|
|
168
|
+
/**
|
|
169
|
+
* @deprecated will keep this since it's just using ml.logs
|
|
170
|
+
* which we still want around (I think) but we should eventually
|
|
171
|
+
* probably stop using this
|
|
172
|
+
*/
|
|
173
|
+
static validateQueryStructuresFromLogs(ml, expected, opts) {
|
|
135
174
|
const queries = ml.logs.map((log) => {
|
|
136
175
|
const qs = QueryRecorder.getQueryStructure(log.query);
|
|
137
176
|
if (!qs) {
|
|
@@ -142,12 +181,16 @@ class QueryRecorder {
|
|
|
142
181
|
qs,
|
|
143
182
|
};
|
|
144
183
|
});
|
|
145
|
-
QueryRecorder.
|
|
184
|
+
QueryRecorder.validateQueryStructuresImpl(expected, queries, opts);
|
|
146
185
|
}
|
|
147
|
-
static
|
|
148
|
-
if (skipSelect) {
|
|
186
|
+
static validateQueryStructuresImpl(expected, queries, opts) {
|
|
187
|
+
if (opts?.skipSelect) {
|
|
149
188
|
queries = queries.filter((query) => query.qs?.type !== queryType.SELECT);
|
|
150
189
|
}
|
|
190
|
+
if (!opts?.flagBeginCommit) {
|
|
191
|
+
queries = queries.filter((query) => query.qs?.type !== queryType.BEGIN &&
|
|
192
|
+
query.qs?.type !== queryType.COMMIT);
|
|
193
|
+
}
|
|
151
194
|
// console.log(queries, expected);
|
|
152
195
|
expect(queries.length).toBe(expected.length);
|
|
153
196
|
for (let i = 0; i < expected.length; i++) {
|
|
@@ -167,7 +210,7 @@ class QueryRecorder {
|
|
|
167
210
|
expect(query.values).toBe(undefined);
|
|
168
211
|
break;
|
|
169
212
|
case queryType.SELECT:
|
|
170
|
-
if (!skipSelect) {
|
|
213
|
+
if (!opts?.skipSelect) {
|
|
171
214
|
console.error("validating select query structure not supported yet");
|
|
172
215
|
}
|
|
173
216
|
// TODO INSERT and UPDATE tests here...
|
|
@@ -2,7 +2,7 @@ import { Express, RequestHandler } from "express";
|
|
|
2
2
|
import { Viewer } from "../../core/base";
|
|
3
3
|
import { GraphQLSchema } from "graphql";
|
|
4
4
|
import supertest from "supertest";
|
|
5
|
-
export
|
|
5
|
+
export type Option = [string, any];
|
|
6
6
|
interface queryConfig {
|
|
7
7
|
viewer?: Viewer;
|
|
8
8
|
init?: (app: Express) => void;
|
|
@@ -11,6 +11,12 @@ interface queryConfig {
|
|
|
11
11
|
headers?: object;
|
|
12
12
|
debugMode?: boolean;
|
|
13
13
|
args: {};
|
|
14
|
+
extraVariables?: {
|
|
15
|
+
[key: string]: {
|
|
16
|
+
graphqlType: string;
|
|
17
|
+
value: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
14
20
|
expectedStatus?: number;
|
|
15
21
|
expectedError?: string | RegExp;
|
|
16
22
|
callback?: (res: supertest.Response) => void;
|