@generazioneai/genquery 0.1.0 → 0.2.0
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/README.md +21 -10
- package/dist/adapters/base.d.ts +10 -1
- package/dist/adapters/base.d.ts.map +1 -1
- package/dist/adapters/prisma/adapter.d.ts +40 -0
- package/dist/adapters/prisma/adapter.d.ts.map +1 -0
- package/dist/adapters/prisma/adapter.js +92 -0
- package/dist/adapters/prisma/adapter.js.map +1 -0
- package/dist/adapters/prisma/create.d.ts +28 -0
- package/dist/adapters/prisma/create.d.ts.map +1 -0
- package/dist/adapters/prisma/create.js +28 -0
- package/dist/adapters/prisma/create.js.map +1 -0
- package/dist/adapters/prisma/index.d.ts +5 -0
- package/dist/adapters/prisma/index.d.ts.map +1 -0
- package/dist/adapters/prisma/index.js +10 -0
- package/dist/adapters/prisma/index.js.map +1 -0
- package/dist/adapters/prisma/schema-from-prisma.d.ts +43 -0
- package/dist/adapters/prisma/schema-from-prisma.d.ts.map +1 -0
- package/dist/adapters/prisma/schema-from-prisma.js +128 -0
- package/dist/adapters/prisma/schema-from-prisma.js.map +1 -0
- package/dist/adapters/prisma/select.d.ts +18 -0
- package/dist/adapters/prisma/select.d.ts.map +1 -0
- package/dist/adapters/prisma/select.js +94 -0
- package/dist/adapters/prisma/select.js.map +1 -0
- package/dist/adapters/prisma/types.d.ts +79 -0
- package/dist/adapters/prisma/types.d.ts.map +1 -0
- package/dist/adapters/prisma/types.js +10 -0
- package/dist/adapters/prisma/types.js.map +1 -0
- package/dist/adapters/prisma/where.d.ts +13 -0
- package/dist/adapters/prisma/where.d.ts.map +1 -0
- package/dist/adapters/prisma/where.js +169 -0
- package/dist/adapters/prisma/where.js.map +1 -0
- package/dist/adapters/typeorm/adapter.d.ts +8 -1
- package/dist/adapters/typeorm/adapter.d.ts.map +1 -1
- package/dist/adapters/typeorm/adapter.js +24 -0
- package/dist/adapters/typeorm/adapter.js.map +1 -1
- package/dist/engine.d.ts +21 -7
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +11 -3
- package/dist/engine.js.map +1 -1
- package/dist/parsed.d.ts +21 -0
- package/dist/parsed.d.ts.map +1 -1
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +21 -8
- package/dist/parser.js.map +1 -1
- package/dist/tests/prisma-adapter.test.d.ts +2 -0
- package/dist/tests/prisma-adapter.test.d.ts.map +1 -0
- package/dist/tests/prisma-adapter.test.js +351 -0
- package/dist/tests/prisma-adapter.test.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +14 -5
- package/spec.md +11 -0
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ const engine = createTypeORMEngine(dataSource);
|
|
|
29
29
|
// 3. Run a query from a request body
|
|
30
30
|
const qb = dataSource.getRepository(User).createQueryBuilder("User");
|
|
31
31
|
|
|
32
|
-
const
|
|
32
|
+
const { data, current, total } = await engine.run(
|
|
33
33
|
{
|
|
34
34
|
searchBy: { firstName: "mario" },
|
|
35
35
|
orderBy: "createdAt",
|
|
@@ -37,11 +37,22 @@ const result = engine.run(
|
|
|
37
37
|
},
|
|
38
38
|
qb, // target QueryBuilder — entity name + entity type both read from this
|
|
39
39
|
);
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
// data: User[]
|
|
41
|
+
// current: data.length (omitted if pagination.showNumber === false)
|
|
42
|
+
// total: match count (via getManyAndCount; omitted if pagination.showTotal === false)
|
|
42
43
|
```
|
|
43
44
|
|
|
44
|
-
`
|
|
45
|
+
`engine.run` is async and returns `{ data, current?, total? }`. `current` and `total` are populated according to `pagination.showNumber` / `pagination.showTotal` (both default to `true`); setting `showTotal: false` skips the extra `SELECT COUNT(*)`.
|
|
46
|
+
|
|
47
|
+
`createTypeORMEngine` is a thin wrapper around `schemaFromTypeORM` → `new TypeORMAdapter` → `new GenQueryEngine`. The root entity (`"User"` in this case) is derived from `qb.expressionMap.mainAlias.metadata.name` at runtime, and the TS entity type is read from `SelectQueryBuilder<User>`. If you need to override or your adapter can't introspect, the 3-arg form still works: `await engine.run(input, "User", qb)`.
|
|
48
|
+
|
|
49
|
+
If you need raw `SelectQueryBuilder` access (custom chaining, `.getRawMany()`, transactions), parse separately and call `runParsed`, which returns the mutated builder without executing:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const parsed = engine.parse(input, "User");
|
|
53
|
+
const built = engine.runParsed(parsed, qb);
|
|
54
|
+
const rows = await built.getRawMany();
|
|
55
|
+
```
|
|
45
56
|
|
|
46
57
|
Need fine-grained control? You can still build it manually:
|
|
47
58
|
|
|
@@ -109,7 +120,7 @@ The entity type is inferred automatically from the `target` argument when it has
|
|
|
109
120
|
const qb = dataSource.getRepository(User).createQueryBuilder("User");
|
|
110
121
|
// qb is SelectQueryBuilder<User> — entity type flows into the call below
|
|
111
122
|
|
|
112
|
-
engine.run(
|
|
123
|
+
await engine.run(
|
|
113
124
|
{
|
|
114
125
|
searchBy: {
|
|
115
126
|
firstName: "mario", // OK
|
|
@@ -150,15 +161,15 @@ Full query language reference: [docs/query-reference.md](docs/query-reference.md
|
|
|
150
161
|
```typescript
|
|
151
162
|
const engine = new GenQueryEngine({ adapter }); // schema comes from the adapter
|
|
152
163
|
|
|
153
|
-
// parse + apply
|
|
154
|
-
engine.run(input, target);
|
|
164
|
+
// parse + apply + execute → Promise<{ data, current?, total? }>
|
|
165
|
+
await engine.run(input, target);
|
|
155
166
|
// or explicit rootEntity:
|
|
156
|
-
engine.run(input, rootEntity, target);
|
|
167
|
+
await engine.run(input, rootEntity, target);
|
|
157
168
|
|
|
158
169
|
// parse only (requires explicit rootEntity — no target to infer from)
|
|
159
170
|
const parsed = engine.parse(input, rootEntity);
|
|
160
171
|
|
|
161
|
-
// apply a previously parsed query
|
|
172
|
+
// apply a previously parsed query without executing (returns the raw target)
|
|
162
173
|
engine.runParsed(parsed, target);
|
|
163
174
|
```
|
|
164
175
|
|
|
@@ -170,7 +181,7 @@ Parse failures throw `QueryValidationError` with a `path` field pointing to the
|
|
|
170
181
|
import { QueryValidationError } from "@generazioneai/genquery";
|
|
171
182
|
|
|
172
183
|
try {
|
|
173
|
-
engine.run(input, "User", qb);
|
|
184
|
+
await engine.run(input, "User", qb);
|
|
174
185
|
} catch (e) {
|
|
175
186
|
if (e instanceof QueryValidationError) {
|
|
176
187
|
console.error(e.path, e.message);
|
package/dist/adapters/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ParsedQuery } from "../parsed";
|
|
1
|
+
import type { PaginatedResult, ParsedQuery } from "../parsed";
|
|
2
2
|
import type { Schema } from "../schema";
|
|
3
3
|
/**
|
|
4
4
|
* Adapters translate a `ParsedQuery` into ORM-specific operations.
|
|
@@ -28,5 +28,14 @@ export interface Adapter<TTarget, TResult> {
|
|
|
28
28
|
* throw a helpful error.
|
|
29
29
|
*/
|
|
30
30
|
getRootEntity?(target: TTarget): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Run the query end-to-end and return `{ data, current?, total? }` shaped
|
|
33
|
+
* by `pagination.showNumber` / `pagination.showTotal`. Implemented by
|
|
34
|
+
* adapters whose target can be executed directly (e.g. a TypeORM query
|
|
35
|
+
* builder). `engine.run` delegates to this when present; pure args-builder
|
|
36
|
+
* adapters (Prisma, args-only Mongo) leave it unset and callers consume
|
|
37
|
+
* the apply result themselves.
|
|
38
|
+
*/
|
|
39
|
+
execute?(target: TTarget, query: ParsedQuery): Promise<PaginatedResult<unknown>>;
|
|
31
40
|
}
|
|
32
41
|
//# sourceMappingURL=base.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/adapters/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/adapters/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC;;;;;;;;GAQG;AACH,MAAM,WAAW,OAAO,CAAC,OAAO,EAAE,OAAO;IACvC,gFAAgF;IAChF,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,wDAAwD;IACxD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC;IAEpD;;;;;;;OAOG;IACH,aAAa,CAAC,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAAC;IAEpD;;;;;;;OAOG;IACH,OAAO,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;CAClF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { PaginatedResult, ParsedQuery } from "../../parsed";
|
|
2
|
+
import type { Schema } from "../../schema";
|
|
3
|
+
import type { Adapter } from "../base";
|
|
4
|
+
import type { PrismaFindManyArgs, PrismaModelDelegate } from "./types";
|
|
5
|
+
export interface PrismaAdapterOptions {
|
|
6
|
+
/**
|
|
7
|
+
* When `pagination.showTotal` is true, `execute` issues a parallel `count`
|
|
8
|
+
* query. Set to `false` to run them sequentially (useful when the underlying
|
|
9
|
+
* Prisma client doesn't pool well under concurrent reads). Defaults to true.
|
|
10
|
+
*/
|
|
11
|
+
parallelCount?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Applies a parsed GenQuery to a Prisma model delegate (e.g. `prisma.user`).
|
|
15
|
+
* Produces a Prisma `findMany` / `findFirst` args object and, on `execute`,
|
|
16
|
+
* runs the query and an optional parallel `count`.
|
|
17
|
+
*
|
|
18
|
+
* Typical use through the engine:
|
|
19
|
+
*
|
|
20
|
+
* const result = await engine.run(input, "User", prisma.user);
|
|
21
|
+
*
|
|
22
|
+
* The root entity name must be passed explicitly — Prisma delegates don't
|
|
23
|
+
* expose their model name on a stable public API, so `getRootEntity` is not
|
|
24
|
+
* implemented.
|
|
25
|
+
*/
|
|
26
|
+
export declare class PrismaAdapter implements Adapter<PrismaModelDelegate, PrismaFindManyArgs> {
|
|
27
|
+
readonly name = "prisma";
|
|
28
|
+
readonly schema: Schema;
|
|
29
|
+
private readonly options;
|
|
30
|
+
constructor(schema: Schema, options?: PrismaAdapterOptions);
|
|
31
|
+
apply(_delegate: PrismaModelDelegate, query: ParsedQuery): PrismaFindManyArgs;
|
|
32
|
+
/**
|
|
33
|
+
* Build a Prisma args object from a parsed query without needing a delegate.
|
|
34
|
+
* Useful for tests / callers who already hold the args path.
|
|
35
|
+
*/
|
|
36
|
+
buildArgs(query: ParsedQuery): PrismaFindManyArgs;
|
|
37
|
+
execute(delegate: PrismaModelDelegate, query: ParsedQuery): Promise<PaginatedResult<unknown>>;
|
|
38
|
+
private build;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvC,OAAO,KAAK,EACV,kBAAkB,EAClB,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AAGjB,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,aACX,YAAW,OAAO,CAAC,mBAAmB,EAAE,kBAAkB,CAAC;IAE3D,QAAQ,CAAC,IAAI,YAAY;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuB;gBAEnC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,oBAAyB;IAK9D,KAAK,CACH,SAAS,EAAE,mBAAmB,EAC9B,KAAK,EAAE,WAAW,GACjB,kBAAkB;IAIrB;;;OAGG;IACH,SAAS,CAAC,KAAK,EAAE,WAAW,GAAG,kBAAkB;IAI3C,OAAO,CACX,QAAQ,EAAE,mBAAmB,EAC7B,KAAK,EAAE,WAAW,GACjB,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IAyBpC,OAAO,CAAC,KAAK;CAsBd"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrismaAdapter = void 0;
|
|
4
|
+
const select_1 = require("./select");
|
|
5
|
+
const where_1 = require("./where");
|
|
6
|
+
/**
|
|
7
|
+
* Applies a parsed GenQuery to a Prisma model delegate (e.g. `prisma.user`).
|
|
8
|
+
* Produces a Prisma `findMany` / `findFirst` args object and, on `execute`,
|
|
9
|
+
* runs the query and an optional parallel `count`.
|
|
10
|
+
*
|
|
11
|
+
* Typical use through the engine:
|
|
12
|
+
*
|
|
13
|
+
* const result = await engine.run(input, "User", prisma.user);
|
|
14
|
+
*
|
|
15
|
+
* The root entity name must be passed explicitly — Prisma delegates don't
|
|
16
|
+
* expose their model name on a stable public API, so `getRootEntity` is not
|
|
17
|
+
* implemented.
|
|
18
|
+
*/
|
|
19
|
+
class PrismaAdapter {
|
|
20
|
+
constructor(schema, options = {}) {
|
|
21
|
+
this.name = "prisma";
|
|
22
|
+
this.schema = schema;
|
|
23
|
+
this.options = options;
|
|
24
|
+
}
|
|
25
|
+
apply(_delegate, query) {
|
|
26
|
+
return this.build(query);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Build a Prisma args object from a parsed query without needing a delegate.
|
|
30
|
+
* Useful for tests / callers who already hold the args path.
|
|
31
|
+
*/
|
|
32
|
+
buildArgs(query) {
|
|
33
|
+
return this.build(query);
|
|
34
|
+
}
|
|
35
|
+
async execute(delegate, query) {
|
|
36
|
+
const args = this.build(query);
|
|
37
|
+
const { kind, showNumber, showTotal } = query.pagination;
|
|
38
|
+
const parallel = this.options.parallelCount ?? true;
|
|
39
|
+
const dataPromise = kind === "first"
|
|
40
|
+
? delegate.findFirst(args).then((row) => (row ? [row] : []))
|
|
41
|
+
: delegate.findMany(args);
|
|
42
|
+
let total;
|
|
43
|
+
if (showTotal && parallel) {
|
|
44
|
+
const [data, count] = await Promise.all([
|
|
45
|
+
dataPromise,
|
|
46
|
+
delegate.count({ where: args.where }),
|
|
47
|
+
]);
|
|
48
|
+
total = count;
|
|
49
|
+
return assemble(data, showNumber, showTotal, total);
|
|
50
|
+
}
|
|
51
|
+
const data = await dataPromise;
|
|
52
|
+
if (showTotal)
|
|
53
|
+
total = await delegate.count({ where: args.where });
|
|
54
|
+
return assemble(data, showNumber, showTotal, total);
|
|
55
|
+
}
|
|
56
|
+
build(query) {
|
|
57
|
+
const args = {};
|
|
58
|
+
if (query.searchBy) {
|
|
59
|
+
const where = (0, where_1.buildWhere)(query.searchBy, this.schema, query.rootEntity);
|
|
60
|
+
if (Object.keys(where).length > 0)
|
|
61
|
+
args.where = where;
|
|
62
|
+
}
|
|
63
|
+
if (query.orderBy) {
|
|
64
|
+
args.orderBy = {
|
|
65
|
+
[query.orderBy.field]: query.orderBy.order,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
applyPagination(args, query.pagination);
|
|
69
|
+
(0, select_1.applySelectAndInclude)(args, query, this.schema);
|
|
70
|
+
return args;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.PrismaAdapter = PrismaAdapter;
|
|
74
|
+
function applyPagination(args, pagination) {
|
|
75
|
+
if (pagination.kind === "all")
|
|
76
|
+
return;
|
|
77
|
+
if (pagination.kind === "first") {
|
|
78
|
+
args.take = 1;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
args.skip = pagination.page * pagination.perPage;
|
|
82
|
+
args.take = pagination.perPage;
|
|
83
|
+
}
|
|
84
|
+
function assemble(data, showNumber, showTotal, total) {
|
|
85
|
+
const result = { data };
|
|
86
|
+
if (showNumber)
|
|
87
|
+
result.current = data.length;
|
|
88
|
+
if (showTotal)
|
|
89
|
+
result.total = total;
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../../src/adapters/prisma/adapter.ts"],"names":[],"mappings":";;;AAGA,qCAAiD;AAMjD,mCAAqC;AAWrC;;;;;;;;;;;;GAYG;AACH,MAAa,aAAa;IAOxB,YAAY,MAAc,EAAE,UAAgC,EAAE;QAJrD,SAAI,GAAG,QAAQ,CAAC;QAKvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CACH,SAA8B,EAC9B,KAAkB;QAElB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACH,SAAS,CAAC,KAAkB;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED,KAAK,CAAC,OAAO,CACX,QAA6B,EAC7B,KAAkB;QAElB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC/B,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC;QACzD,MAAM,QAAQ,GAAG,IAAI,CAAC,OA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romPrismaOptions } from "./schema-from-prisma";
|
|
4
|
+
import type { PrismaDatamodel, PrismaFindManyArgs, PrismaModelDelegate } from "./types";
|
|
5
|
+
export interface CreatePrismaEngineOptions {
|
|
6
|
+
/** Options forwarded to `schemaFromPrisma` (model filter, overrides, ...). */
|
|
7
|
+
schema?: SchemaFromPrismaOptions;
|
|
8
|
+
/** Options forwarded to the `PrismaAdapter` constructor (parallelCount, ...). */
|
|
9
|
+
adapter?: PrismaAdapterOptions;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* One-line setup mirroring `createTypeORMEngine`: read the schema from a
|
|
13
|
+
* Prisma DMMF datamodel, build the adapter and the engine.
|
|
14
|
+
*
|
|
15
|
+
* import { Prisma, PrismaClient } from "@prisma/client";
|
|
16
|
+
* const prisma = new PrismaClient();
|
|
17
|
+
* const engine = createPrismaEngine(Prisma.dmmf.datamodel);
|
|
18
|
+
* await engine.run(input, "User", prisma.user);
|
|
19
|
+
*
|
|
20
|
+
* Restrict to specific models or override scalar types:
|
|
21
|
+
*
|
|
22
|
+
* const engine = createPrismaEngine(Prisma.dmmf.datamodel, {
|
|
23
|
+
* schema: { models: ["User", "Post"] },
|
|
24
|
+
* adapter: { parallelCount: false },
|
|
25
|
+
* });
|
|
26
|
+
*/
|
|
27
|
+
export declare function createPrismaEngine(datamodel: PrismaDatamodel, options?: CreatePrismaEngineOptions): GenQueryEngine<PrismaModelDelegate, PrismaFindManyArgs>;
|
|
28
|
+
//# sourceMappingURL=create.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EACV,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAEjB,MAAM,WAAW,yBAAyB;IACxC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,uBAAuB,CAAC;IACjC,iFAAiF;IACjF,OAAO,CAAC,EAAE,oBAAoB,CAAC;CAChC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,eAAe,EAC1B,OAAO,GAAE,yBAA8B,GACtC,cAAc,CAAC,mBAAmB,EAAE,kBAAkB,CAAC,CAIzD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPrismaEngine = createPrismaEngine;
|
|
4
|
+
const engine_1 = require("../../engine");
|
|
5
|
+
const adapter_1 = require("./adapter");
|
|
6
|
+
const schema_from_prisma_1 = require("./schema-from-prisma");
|
|
7
|
+
/**
|
|
8
|
+
* One-line setup mirroring `createTypeORMEngine`: read the schema from a
|
|
9
|
+
* Prisma DMMF datamodel, build the adapter and the engine.
|
|
10
|
+
*
|
|
11
|
+
* import { Prisma, PrismaClient } from "@prisma/client";
|
|
12
|
+
* const prisma = new PrismaClient();
|
|
13
|
+
* const engine = createPrismaEngine(Prisma.dmmf.datamodel);
|
|
14
|
+
* await engine.run(input, "User", prisma.user);
|
|
15
|
+
*
|
|
16
|
+
* Restrict to specific models or override scalar types:
|
|
17
|
+
*
|
|
18
|
+
* const engine = createPrismaEngine(Prisma.dmmf.datamodel, {
|
|
19
|
+
* schema: { models: ["User", "Post"] },
|
|
20
|
+
* adapter: { parallelCount: false },
|
|
21
|
+
* });
|
|
22
|
+
*/
|
|
23
|
+
function createPrismaEngine(datamodel, options = {}) {
|
|
24
|
+
const schema = (0, schema_from_prisma_1.schemaFromPrisma)(datamodel, options.schema);
|
|
25
|
+
const adapter = new adapter_1.PrismaAdapter(schema, options.adapter);
|
|
26
|
+
return new engine_1.GenQueryEngine({ adapter });
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../../../src/adapters/prisma/create.ts"],"names":[],"mappings":";;AAmCA,gDAOC;AA1CD,yCAA8C;AAC9C,uCAAqE;AACrE,6DAG8B;AAc9B;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,kBAAkB,CAChC,SAA0B,EAC1B,UAAqC,EAAE;IAEvC,MAAM,MAAM,GAAG,IAAA,qCAAgB,EAAC,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAC3D,MAAM,OAAO,GAAG,IAAI,uBAAa,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,OAAO,IAAI,uBAAc,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { PrismaAdapter, type PrismaAdapterOptions } from "./adapter";
|
|
2
|
+
export { schemaFromPrisma, type SchemaFromPrismaOptions, } from "./schema-from-prisma";
|
|
3
|
+
export { createPrismaEngine, type CreatePrismaEngineOptions, } from "./create";
|
|
4
|
+
export type { PrismaDatamodel, PrismaEnumDef, PrismaFieldDef, PrismaFindManyArgs, PrismaInclude, PrismaModelDef, PrismaModelDelegate, PrismaSelect, PrismaWhere, } from "./types";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,KAAK,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EACL,gBAAgB,EAChB,KAAK,uBAAuB,GAC7B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,UAAU,CAAC;AAClB,YAAY,EACV,eAAe,EACf,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,WAAW,GACZ,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPrismaEngine = exports.schemaFromPrisma = exports.PrismaAdapter = void 0;
|
|
4
|
+
var adapter_1 = require("./adapter");
|
|
5
|
+
Object.defineProperty(exports, "PrismaAdapter", { enumerable: true, get: function () { return adapter_1.PrismaAdapter; } });
|
|
6
|
+
var schema_from_prisma_1 = require("./schema-from-prisma");
|
|
7
|
+
Object.defineProperty(exports, "schemaFromPrisma", { enumerable: true, get: function () { return schema_from_prisma_1.schemaFromPrisma; } });
|
|
8
|
+
var create_1 = require("./create");
|
|
9
|
+
Object.defineProperty(exports, "createPrismaEngine", { enumerable: true, get: function () { return create_1.createPrismaEngine; } });
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":";;;AAAA,qCAAqE;AAA5D,wGAAA,aAAa,OAAA;AACtB,2DAG8B;AAF5B,sHAAA,gBAAgB,OAAA;AAGlB,mCAGkB;AAFhB,4GAAA,kBAAkB,OAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { FieldType, Schema } from "../../schema";
|
|
2
|
+
import type { PrismaDatamodel } from "./types";
|
|
3
|
+
/** FieldTypes that don't require additional metadata (excludes `"enum"`). */
|
|
4
|
+
type ScalarFieldType = Exclude<FieldType, "enum">;
|
|
5
|
+
export interface SchemaFromPrismaOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Restrict to specific models. Accepts model names. Defaults to all models
|
|
8
|
+
* in the datamodel.
|
|
9
|
+
*/
|
|
10
|
+
models?: readonly string[];
|
|
11
|
+
/**
|
|
12
|
+
* Per-model, per-field type overrides. Use this to map Prisma scalars that
|
|
13
|
+
* the default mapper skips (e.g. `Json`, `Bytes`) to a genquery FieldType.
|
|
14
|
+
*
|
|
15
|
+
* overrides: { User: { metadata: "string" } }
|
|
16
|
+
*/
|
|
17
|
+
overrides?: Record<string, Record<string, ScalarFieldType>>;
|
|
18
|
+
/**
|
|
19
|
+
* Called when a scalar type is not recognized by the default mapping.
|
|
20
|
+
* Return a FieldType to include the field, or `undefined` to skip it.
|
|
21
|
+
* Defaults to skipping unknown types.
|
|
22
|
+
*/
|
|
23
|
+
fallback?: (modelName: string, fieldName: string, scalarType: string) => ScalarFieldType | undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Build a genquery `Schema` from a Prisma DMMF datamodel. Accepts the
|
|
27
|
+
* datamodel structurally so the lib doesn't depend on a specific Prisma
|
|
28
|
+
* version — pass `Prisma.dmmf.datamodel` (or whatever your version exposes)
|
|
29
|
+
* directly.
|
|
30
|
+
*
|
|
31
|
+
* import { Prisma } from "@prisma/client";
|
|
32
|
+
* const schema = schemaFromPrisma(Prisma.dmmf.datamodel);
|
|
33
|
+
* const adapter = new PrismaAdapter(schema);
|
|
34
|
+
*
|
|
35
|
+
* Restrict to specific models:
|
|
36
|
+
*
|
|
37
|
+
* const schema = schemaFromPrisma(Prisma.dmmf.datamodel, {
|
|
38
|
+
* models: ["User", "Post"],
|
|
39
|
+
* });
|
|
40
|
+
*/
|
|
41
|
+
export declare function schemaFromPrisma(datamodel: PrismaDatamodel, options?: SchemaFromPrismaOptions): Schema;
|
|
42
|
+
export {};
|
|
43
|
+
//# sourceMappingURL=schema-from-prisma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-from-prisma.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/schema-from-prisma.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,SAAS,EAET,MAAM,EACP,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EACV,eAAe,EAIhB,MAAM,SAAS,CAAC;AAEjB,6EAA6E;AAC7E,KAAK,eAAe,GAAG,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AAElD,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3B;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;IAC5D;;;;OAIG;IACH,QAAQ,CAAC,EAAE,CACT,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,KACf,eAAe,GAAG,SAAS,CAAC;CAClC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,eAAe,EAC1B,OAAO,GAAE,uBAA4B,GACpC,MAAM,CAaR"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.schemaFromPrisma = schemaFromPrisma;
|
|
4
|
+
/**
|
|
5
|
+
* Build a genquery `Schema` from a Prisma DMMF datamodel. Accepts the
|
|
6
|
+
* datamodel structurally so the lib doesn't depend on a specific Prisma
|
|
7
|
+
* version — pass `Prisma.dmmf.datamodel` (or whatever your version exposes)
|
|
8
|
+
* directly.
|
|
9
|
+
*
|
|
10
|
+
* import { Prisma } from "@prisma/client";
|
|
11
|
+
* const schema = schemaFromPrisma(Prisma.dmmf.datamodel);
|
|
12
|
+
* const adapter = new PrismaAdapter(schema);
|
|
13
|
+
*
|
|
14
|
+
* Restrict to specific models:
|
|
15
|
+
*
|
|
16
|
+
* const schema = schemaFromPrisma(Prisma.dmmf.datamodel, {
|
|
17
|
+
* models: ["User", "Post"],
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
function schemaFromPrisma(datamodel, options = {}) {
|
|
21
|
+
const enumValues = indexEnums(datamodel.enums);
|
|
22
|
+
const filter = options.models;
|
|
23
|
+
const selected = filter
|
|
24
|
+
? datamodel.models.filter((m) => filter.includes(m.name))
|
|
25
|
+
: datamodel.models;
|
|
26
|
+
const entities = {};
|
|
27
|
+
for (const model of selected) {
|
|
28
|
+
entities[model.name] = buildEntity(model, enumValues, options);
|
|
29
|
+
}
|
|
30
|
+
return { entities };
|
|
31
|
+
}
|
|
32
|
+
function indexEnums(enums) {
|
|
33
|
+
const out = new Map();
|
|
34
|
+
for (const e of enums) {
|
|
35
|
+
out.set(e.name, e.values.map((v) => v.name));
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
function buildEntity(model, enumValues, options) {
|
|
40
|
+
const fields = {};
|
|
41
|
+
const relations = {};
|
|
42
|
+
const overrides = options.overrides?.[model.name] ?? {};
|
|
43
|
+
for (const f of model.fields) {
|
|
44
|
+
if (f.kind === "object") {
|
|
45
|
+
relations[f.name] = {
|
|
46
|
+
target: f.type,
|
|
47
|
+
kind: f.isList ? "many" : "one",
|
|
48
|
+
};
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (f.kind === "unsupported")
|
|
52
|
+
continue;
|
|
53
|
+
const explicit = overrides[f.name];
|
|
54
|
+
if (explicit) {
|
|
55
|
+
fields[f.name] = {
|
|
56
|
+
type: explicit,
|
|
57
|
+
nullable: !f.isRequired,
|
|
58
|
+
};
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (f.kind === "enum") {
|
|
62
|
+
const values = enumValues.get(f.type);
|
|
63
|
+
if (!values)
|
|
64
|
+
continue;
|
|
65
|
+
fields[f.name] = {
|
|
66
|
+
type: "enum",
|
|
67
|
+
values: values.slice(),
|
|
68
|
+
nullable: !f.isRequired,
|
|
69
|
+
};
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
// scalar
|
|
73
|
+
const fieldType = mapScalar(f, model.name, options);
|
|
74
|
+
if (!fieldType)
|
|
75
|
+
continue;
|
|
76
|
+
fields[f.name] = {
|
|
77
|
+
type: fieldType,
|
|
78
|
+
nullable: !f.isRequired,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// Add virtual override fields not present in the model.
|
|
82
|
+
for (const [name, type] of Object.entries(overrides)) {
|
|
83
|
+
if (!fields[name])
|
|
84
|
+
fields[name] = { type };
|
|
85
|
+
}
|
|
86
|
+
const definition = {
|
|
87
|
+
name: model.name,
|
|
88
|
+
fields,
|
|
89
|
+
};
|
|
90
|
+
if (Object.keys(relations).length > 0)
|
|
91
|
+
definition.relations = relations;
|
|
92
|
+
const pk = derivePrimaryKey(model);
|
|
93
|
+
if (pk)
|
|
94
|
+
definition.primaryKey = pk;
|
|
95
|
+
return definition;
|
|
96
|
+
}
|
|
97
|
+
function derivePrimaryKey(model) {
|
|
98
|
+
const idField = model.fields.find((f) => f.isId === true);
|
|
99
|
+
if (idField)
|
|
100
|
+
return idField.name;
|
|
101
|
+
const pk = model.primaryKey?.fields?.[0];
|
|
102
|
+
if (pk)
|
|
103
|
+
return pk;
|
|
104
|
+
// Fallback for minimal DMMF shapes (Prisma 7+) that strip `isId`. The
|
|
105
|
+
// convention in `schema.prisma` is to name the PK field `id`.
|
|
106
|
+
const conventionalId = model.fields.find((f) => f.name === "id");
|
|
107
|
+
return conventionalId?.name;
|
|
108
|
+
}
|
|
109
|
+
function mapScalar(field, modelName, options) {
|
|
110
|
+
if (field.isList)
|
|
111
|
+
return undefined; // genquery doesn't model scalar arrays
|
|
112
|
+
switch (field.type) {
|
|
113
|
+
case "String":
|
|
114
|
+
return "string";
|
|
115
|
+
case "Int":
|
|
116
|
+
case "Float":
|
|
117
|
+
case "Decimal":
|
|
118
|
+
case "BigInt":
|
|
119
|
+
return "number";
|
|
120
|
+
case "Boolean":
|
|
121
|
+
return "boolean";
|
|
122
|
+
case "DateTime":
|
|
123
|
+
return "date";
|
|
124
|
+
default:
|
|
125
|
+
return options.fallback?.(modelName, field.name, field.type);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=schema-from-prisma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-from-prisma.js","sourceRoot":"","sources":["../../../src/adapters/prisma/schema-from-prisma.ts"],"names":[],"mappings":";;AA0DA,4CAgBC;AAhCD;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,gBAAgB,CAC9B,SAA0B,EAC1B,UAAmC,EAAE;IAErC,MAAM,UAAU,GAAG,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAE/C,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,MAAM,QAAQ,GAAG,MAAM;QACrB,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACzD,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC;IAErB,MAAM,QAAQ,GAAqC,EAAE,CAAC;IACtD,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;QAC7B,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,CAAC;AACtB,CAAC;AAED,SAAS,UAAU,CACjB,KAA+B;IAE/B,MAAM,GAAG,GAAG,IAAI,GAAG,EAA6B,CAAC;IACjD,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;QACtB,GAAG,CAAC,GAAG,CACL,CAAC,CAAC,IAAI,EACN,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,C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O,EAAE,CAAC;IAClB,sEAAsE;IACtE,8DAA8D;IAC9D,MAAM,cAAc,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;IACjE,OAAO,cAAc,EAAE,IAAI,CAAC;AAC9B,CAAC;AAED,SAAS,SAAS,CAChB,KAAqB,EACrB,SAAiB,EACjB,OAAgC;IAEhC,IAAI,KAAK,CAAC,MAAM;QAAE,OAAO,SAAS,CAAC,CAAC,uCAAuC;IAC3E,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,QAAQ;YACX,OAAO,QAAQ,CAAC;QAClB,KAAK,KAAK,CAAC;QACX,KAAK,OAAO,CAAC;QACb,KAAK,SAAS,CAAC;QACf,KAAK,QAAQ;YACX,OAAO,QAAQ,CAAC;QAClB,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;QACnB,KAAK,UAAU;YACb,OAAO,MAAM,CAAC;QAChB;YACE,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IACjE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ParsedQuery } from "../../parsed";
|
|
2
|
+
import { type Schema } from "../../schema";
|
|
3
|
+
import type { PrismaFindManyArgs } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Translate ParsedSelect + ParsedInclude into Prisma's `select` / `include`
|
|
6
|
+
* args, mutating `args` in place.
|
|
7
|
+
*
|
|
8
|
+
* Rules:
|
|
9
|
+
* - select=all + include=none/all/map → use `include` (or omit when no
|
|
10
|
+
* relations). Prisma defaults to all scalar fields when neither is set.
|
|
11
|
+
* - select≠all → must use `select`; relations from include are nested into
|
|
12
|
+
* `select` (Prisma forbids using both `select` and `include` at the same
|
|
13
|
+
* level).
|
|
14
|
+
* - Selected fields always include the primary key, so hydration works and
|
|
15
|
+
* later relation joins line up.
|
|
16
|
+
*/
|
|
17
|
+
export declare function applySelectAndInclude(args: PrismaFindManyArgs, query: ParsedQuery, schema: Schema): void;
|
|
18
|
+
//# sourceMappingURL=select.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/select.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,WAAW,EAEZ,MAAM,cAAc,CAAC;AACtB,OAAO,EAEL,KAAK,MAAM,EAGZ,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,kBAAkB,EAA+B,MAAM,SAAS,CAAC;AAE/E;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,kBAAkB,EACxB,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,MAAM,GACb,IAAI,CAYN"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applySelectAndInclude = applySelectAndInclude;
|
|
4
|
+
const schema_1 = require("../../schema");
|
|
5
|
+
/**
|
|
6
|
+
* Translate ParsedSelect + ParsedInclude into Prisma's `select` / `include`
|
|
7
|
+
* args, mutating `args` in place.
|
|
8
|
+
*
|
|
9
|
+
* Rules:
|
|
10
|
+
* - select=all + include=none/all/map → use `include` (or omit when no
|
|
11
|
+
* relations). Prisma defaults to all scalar fields when neither is set.
|
|
12
|
+
* - select≠all → must use `select`; relations from include are nested into
|
|
13
|
+
* `select` (Prisma forbids using both `select` and `include` at the same
|
|
14
|
+
* level).
|
|
15
|
+
* - Selected fields always include the primary key, so hydration works and
|
|
16
|
+
* later relation joins line up.
|
|
17
|
+
*/
|
|
18
|
+
function applySelectAndInclude(args, query, schema) {
|
|
19
|
+
const rootEntity = (0, schema_1.getEntity)(schema, query.rootEntity);
|
|
20
|
+
const select = query.select;
|
|
21
|
+
const include = query.include;
|
|
22
|
+
if (select.kind === "all") {
|
|
23
|
+
const inc = buildIncludeOnly(include, rootEntity, schema);
|
|
24
|
+
if (inc)
|
|
25
|
+
args.include = inc;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
args.select = buildSelectFromBoth(select, include, rootEntity, schema);
|
|
29
|
+
}
|
|
30
|
+
function buildIncludeOnly(include, rootEntity, schema) {
|
|
31
|
+
if (include.kind === "none")
|
|
32
|
+
return undefined;
|
|
33
|
+
if (include.kind === "all") {
|
|
34
|
+
const rels = rootEntity.relations ?? {};
|
|
35
|
+
const keys = Object.keys(rels);
|
|
36
|
+
if (keys.length === 0)
|
|
37
|
+
return undefined;
|
|
38
|
+
const out = {};
|
|
39
|
+
for (const k of keys)
|
|
40
|
+
out[k] = true;
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
// map
|
|
44
|
+
const out = {};
|
|
45
|
+
for (const [relName, spec] of Object.entries(include.relations)) {
|
|
46
|
+
const relDef = rootEntity.relations?.[relName];
|
|
47
|
+
if (!relDef)
|
|
48
|
+
continue;
|
|
49
|
+
out[relName] = relationToSpec(spec, relDef.target, schema);
|
|
50
|
+
}
|
|
51
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
52
|
+
}
|
|
53
|
+
function buildSelectFromBoth(select, include, rootEntity, schema) {
|
|
54
|
+
const pk = (0, schema_1.primaryKeyOf)(rootEntity);
|
|
55
|
+
const out = {};
|
|
56
|
+
if (select.kind === "none") {
|
|
57
|
+
out[pk] = true;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const set = new Set(select.fields);
|
|
61
|
+
set.add(pk);
|
|
62
|
+
for (const f of set)
|
|
63
|
+
out[f] = true;
|
|
64
|
+
}
|
|
65
|
+
// Nest relations into `select`.
|
|
66
|
+
if (include.kind === "all") {
|
|
67
|
+
for (const [relName, relDef] of Object.entries(rootEntity.relations ?? {})) {
|
|
68
|
+
void relDef;
|
|
69
|
+
out[relName] = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (include.kind === "map") {
|
|
73
|
+
for (const [relName, spec] of Object.entries(include.relations)) {
|
|
74
|
+
const relDef = rootEntity.relations?.[relName];
|
|
75
|
+
if (!relDef)
|
|
76
|
+
continue;
|
|
77
|
+
out[relName] = relationToSpec(spec, relDef.target, schema);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
function relationToSpec(spec, targetEntityName, schema) {
|
|
83
|
+
if (spec.kind === "all")
|
|
84
|
+
return true;
|
|
85
|
+
const target = (0, schema_1.getEntity)(schema, targetEntityName);
|
|
86
|
+
const pk = (0, schema_1.primaryKeyOf)(target);
|
|
87
|
+
const set = new Set(spec.fields);
|
|
88
|
+
set.add(pk);
|
|
89
|
+
const sel = {};
|
|
90
|
+
for (const f of set)
|
|
91
|
+
sel[f] = true;
|
|
92
|
+
return { select: sel };
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=select.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../../src/adapters/prisma/select.ts"],"names":[],"mappings":";;AA2BA,sDAgBC;AArCD,yCAKsB;AAGtB;;;;;;;;;;;;GAYG;AACH,SAAgB,qBAAqB,CACnC,IAAwB,EACxB,KAAkB,EAClB,MAAc;IAEd,MAAM,UAAU,GAAG,IAAA,kBAAS,EAAC,MAAM,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;IACvD,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;IAE9B,IAAI,MAAM,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;QAC1B,MAAM,GAAG,GAAG,gBAAgB,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;QAC1D,IAAI,GAAG;YAAE,IAAI,CAAC,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