-
Notifications
You must be signed in to change notification settings - Fork 6
/
GenTypescriptQuery.test.ts
291 lines (247 loc) · 7.24 KB
/
GenTypescriptQuery.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { SchemaNode } from '@aphro/schema-api';
import { createCompiler } from '@aphro/schema';
import GenTypescriptQuery from '../GenTypescriptQuery';
const { compileFromString } = createCompiler();
const NoEdgesSchema = `
engine: postgres
db: test
Foo as Node {}
`;
const OutboundEdgeViaFieldSchema = `
engine: postgres
db: test
Foo as Node {
barId: ID<Bar>
} & OutboundEdges {
bar: Edge<Foo.barId>
}
`;
const OutboundThroughForeignFieldSchema = `
engine: postgres
db: test
Foo as Node {} & OutboundEdges {
bars: Edge<Bar.fooId>
}
`;
const InboundOnFieldEdgeSchema = `
engine: postgres
db: test
Foo as Node {
barId: ID<Bar>
} & InboundEdges {
fromBar: Edge<Foo.barId>
}
`;
const InlineJunction = `
engine: postgres
db: test
Foo as Node {
} & OutboundEdges {
bars: Edge<Bar>
}
`;
const BidiInlineJunction = `
engine: postgres
db: test
Foo as Node {
} & OutboundEdges {
bars: Edge<Bar>
}
Bar as Node {
} & OutboundEdges {
foos: InverseEdge<Foo.bar>
}
`;
const BidiNamedJunction = `
engine: postgres
db: test
Foo as Node {
} & OutboundEdges {
bars: FooToBarEdge
}
Bar as Node {
} & OutboundEdges {
foos: BarToFooEdge
}
FooToBarEdge as Edge<Foo, Bar> {}
BarToFooEdge as InverseEdge<FooToBarEdge> {}
`;
const InboundThroughLocalFieldSchema = ``;
test('NoEdgesSchema', async () => {
const contents = (await genIt(compileFromString(NoEdgesSchema)[1].nodes.Foo)).contents;
// TODO: remove unneeded imports
// Validation should require that a primary key field exists
expect(contents).toEqual(`// SIGNED-SOURCE: <86219b43127d361c4e0130aff0df9ad0>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
*/
import { Context } from "@aphro/runtime-ts";
import { DerivedQuery } from "@aphro/runtime-ts";
import { QueryFactory } from "@aphro/runtime-ts";
import { modelLoad } from "@aphro/runtime-ts";
import { filter } from "@aphro/runtime-ts";
import { Predicate } from "@aphro/runtime-ts";
import { take } from "@aphro/runtime-ts";
import { orderBy } from "@aphro/runtime-ts";
import { P } from "@aphro/runtime-ts";
import { ModelFieldGetter } from "@aphro/runtime-ts";
import { Expression } from "@aphro/runtime-ts";
import { EmptyQuery } from "@aphro/runtime-ts";
import { SID_of } from "@aphro/runtime-ts";
import Foo from "../Foo.js";
import { Data } from "./FooBase.js";
import FooSpec from "./FooSpec.js";
export default class FooQuery extends DerivedQuery<Foo> {
static create(ctx: Context) {
return new FooQuery(
ctx,
QueryFactory.createSourceQueryFor(ctx, FooSpec),
modelLoad(ctx, FooSpec.createFrom)
);
}
static empty(ctx: Context) {
return new FooQuery(ctx, new EmptyQuery(ctx));
}
protected derive(expression: Expression): FooQuery {
return new FooQuery(this.ctx, this, expression);
}
static fromId(ctx: Context, id: SID_of<Foo>) {
return this.create(ctx).whereId(P.equals(id));
}
take(n: number) {
return new FooQuery(this.ctx, this, take(n));
}
}
`);
});
test('OutboundEdgeViaFieldSchema', async () => {
const contents = (await genIt(compileFromString(OutboundEdgeViaFieldSchema)[1].nodes.Foo))
.contents;
expect(contents).toEqual(`// SIGNED-SOURCE: <afc38a702ee61fa481fd19383e09e039>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
*/
import { Context } from "@aphro/runtime-ts";
import { DerivedQuery } from "@aphro/runtime-ts";
import { QueryFactory } from "@aphro/runtime-ts";
import { modelLoad } from "@aphro/runtime-ts";
import { filter } from "@aphro/runtime-ts";
import { Predicate } from "@aphro/runtime-ts";
import { take } from "@aphro/runtime-ts";
import { orderBy } from "@aphro/runtime-ts";
import { P } from "@aphro/runtime-ts";
import { ModelFieldGetter } from "@aphro/runtime-ts";
import { Expression } from "@aphro/runtime-ts";
import { EmptyQuery } from "@aphro/runtime-ts";
import { SID_of } from "@aphro/runtime-ts";
import Foo from "../Foo.js";
import { Data } from "./FooBase.js";
import FooSpec from "./FooSpec.js";
import Bar from "../Bar.js";
import BarSpec from "./BarSpec.js";
import BarQuery from "./BarQuery.js";
export default class FooQuery extends DerivedQuery<Foo> {
static create(ctx: Context) {
return new FooQuery(
ctx,
QueryFactory.createSourceQueryFor(ctx, FooSpec),
modelLoad(ctx, FooSpec.createFrom)
);
}
static empty(ctx: Context) {
return new FooQuery(ctx, new EmptyQuery(ctx));
}
protected derive(expression: Expression): FooQuery {
return new FooQuery(this.ctx, this, expression);
}
static fromId(ctx: Context, id: SID_of<Foo>) {
return this.create(ctx).whereId(P.equals(id));
}
whereBarId(p: Predicate<Data["barId"]>) {
return this.derive(
filter(new ModelFieldGetter<"barId", Data, Foo>("barId"), p)
);
}
queryBar(): BarQuery {
return new BarQuery(
this.ctx,
QueryFactory.createHopQueryFor(this.ctx, this, FooSpec.outboundEdges.bar),
modelLoad(this.ctx, BarSpec.createFrom)
);
}
take(n: number) {
return new FooQuery(this.ctx, this, take(n));
}
orderByBarId(direction: "asc" | "desc" = "asc") {
return this.derive(
orderBy(new ModelFieldGetter<"barId", Data, Foo>("barId"), direction)
);
}
}
`);
});
test('OutboundThroughForeignFieldSchema', async () => {
const contents = (await genIt(compileFromString(OutboundThroughForeignFieldSchema)[1].nodes.Foo))
.contents;
expect(contents).toEqual(`// SIGNED-SOURCE: <1222775915aac9b251cdfbb9ce058982>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
*/
import { Context } from "@aphro/runtime-ts";
import { DerivedQuery } from "@aphro/runtime-ts";
import { QueryFactory } from "@aphro/runtime-ts";
import { modelLoad } from "@aphro/runtime-ts";
import { filter } from "@aphro/runtime-ts";
import { Predicate } from "@aphro/runtime-ts";
import { take } from "@aphro/runtime-ts";
import { orderBy } from "@aphro/runtime-ts";
import { P } from "@aphro/runtime-ts";
import { ModelFieldGetter } from "@aphro/runtime-ts";
import { Expression } from "@aphro/runtime-ts";
import { EmptyQuery } from "@aphro/runtime-ts";
import { SID_of } from "@aphro/runtime-ts";
import Foo from "../Foo.js";
import { Data } from "./FooBase.js";
import FooSpec from "./FooSpec.js";
import BarSpec from "./BarSpec.js";
import BarQuery from "./BarQuery.js";
export default class FooQuery extends DerivedQuery<Foo> {
static create(ctx: Context) {
return new FooQuery(
ctx,
QueryFactory.createSourceQueryFor(ctx, FooSpec),
modelLoad(ctx, FooSpec.createFrom)
);
}
static empty(ctx: Context) {
return new FooQuery(ctx, new EmptyQuery(ctx));
}
protected derive(expression: Expression): FooQuery {
return new FooQuery(this.ctx, this, expression);
}
static fromId(ctx: Context, id: SID_of<Foo>) {
return this.create(ctx).whereId(P.equals(id));
}
queryBars(): BarQuery {
return new BarQuery(
this.ctx,
QueryFactory.createHopQueryFor(
this.ctx,
this,
FooSpec.outboundEdges.bars
),
modelLoad(this.ctx, BarSpec.createFrom)
);
}
take(n: number) {
return new FooQuery(this.ctx, this, take(n));
}
}
`);
});
function genIt(schema: SchemaNode) {
return new GenTypescriptQuery({ nodeOrEdge: schema, edges: {}, dest: '' }).gen();
}