pothos-drizzle-generator 0.0.1
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/LICENSE +21 -0
- package/README.md +127 -0
- package/dist/cjs/PothosDrizzleGeneratorPlugin.d.ts +7 -0
- package/dist/cjs/PothosDrizzleGeneratorPlugin.js +399 -0
- package/dist/cjs/generator.d.ts +63 -0
- package/dist/cjs/generator.js +231 -0
- package/dist/cjs/global-types.d.ts +80 -0
- package/dist/cjs/global-types.js +2 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/libs/operations.d.ts +11 -0
- package/dist/cjs/libs/operations.js +45 -0
- package/dist/cjs/libs/utils.d.ts +58 -0
- package/dist/cjs/libs/utils.js +149 -0
- package/dist/esm/PothosDrizzleGeneratorPlugin.d.ts +7 -0
- package/dist/esm/PothosDrizzleGeneratorPlugin.js +395 -0
- package/dist/esm/generator.d.ts +63 -0
- package/dist/esm/generator.js +227 -0
- package/dist/esm/global-types.d.ts +80 -0
- package/dist/esm/global-types.js +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/libs/operations.d.ts +11 -0
- package/dist/esm/libs/operations.js +40 -0
- package/dist/esm/libs/utils.d.ts +58 -0
- package/dist/esm/libs/utils.js +108 -0
- package/dist/esm/package.json +3 -0
- package/eslint.config.mjs +43 -0
- package/package.json +40 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PothosDrizzleGenerator = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
6
|
+
const graphql_scalars_1 = require("graphql-scalars");
|
|
7
|
+
const operations_js_1 = require("./libs/operations.js");
|
|
8
|
+
const utils_js_1 = require("./libs/utils.js");
|
|
9
|
+
class PothosDrizzleGenerator {
|
|
10
|
+
enums = {};
|
|
11
|
+
inputOperators = {};
|
|
12
|
+
inputType = {};
|
|
13
|
+
tables;
|
|
14
|
+
builder;
|
|
15
|
+
constructor(builder) {
|
|
16
|
+
this.builder = builder;
|
|
17
|
+
this.createInputType();
|
|
18
|
+
}
|
|
19
|
+
createTableInfo() {
|
|
20
|
+
const options = this.builder.options.pothosDrizzleGenerator;
|
|
21
|
+
const drizzleOption = this.builder.options.drizzle;
|
|
22
|
+
const getConfig = drizzleOption.getTableConfig;
|
|
23
|
+
const relations = this.getRelations();
|
|
24
|
+
const tables = Object.values(relations)
|
|
25
|
+
.filter((t) => (0, drizzle_orm_1.isTable)(t.table))
|
|
26
|
+
.map(({ name, table, relations }) => {
|
|
27
|
+
const tableInfo = getConfig(table);
|
|
28
|
+
const modelOptions = options?.models?.[name];
|
|
29
|
+
const columns = tableInfo.columns;
|
|
30
|
+
//Operations
|
|
31
|
+
const operationIncludes = (0, operations_js_1.expandOperations)(modelOptions?.operations?.include ?? operations_js_1.OperationBasic);
|
|
32
|
+
const operationExcludes = (0, operations_js_1.expandOperations)(modelOptions?.operations?.exclude ?? []);
|
|
33
|
+
const operations = operationIncludes.filter((v) => !operationExcludes.includes(v));
|
|
34
|
+
// Columns filter
|
|
35
|
+
const include = options?.models?.[name]?.fields?.include ??
|
|
36
|
+
columns.map((c) => c.name);
|
|
37
|
+
const exclude = options?.models?.[name]?.fields?.exclude ?? [];
|
|
38
|
+
const filterColumns = include.filter((name) => !exclude.includes(name));
|
|
39
|
+
// Input columns filter
|
|
40
|
+
const includeInput = options?.models?.[name]?.inputFields?.include ??
|
|
41
|
+
columns.map((c) => c.name);
|
|
42
|
+
const excludeInput = options?.models?.[name]?.inputFields?.exclude ?? [];
|
|
43
|
+
const filterInputColumns = includeInput.filter((name) => !excludeInput.includes(name));
|
|
44
|
+
return [
|
|
45
|
+
name,
|
|
46
|
+
{
|
|
47
|
+
table,
|
|
48
|
+
columns: columns.filter((c) => filterColumns.includes(c.name)),
|
|
49
|
+
operations,
|
|
50
|
+
inputColumns: columns.filter((c) => filterInputColumns.includes(c.name)),
|
|
51
|
+
tableInfo,
|
|
52
|
+
relations,
|
|
53
|
+
executable: modelOptions?.executable,
|
|
54
|
+
limit: modelOptions?.limit,
|
|
55
|
+
orderBy: modelOptions?.orderBy,
|
|
56
|
+
where: modelOptions?.where,
|
|
57
|
+
inputData: modelOptions?.inputData,
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
});
|
|
61
|
+
const modelNames = tables.map(([name]) => name);
|
|
62
|
+
// Model filter
|
|
63
|
+
const include = options?.use?.include ?? modelNames;
|
|
64
|
+
const exclude = options?.use?.exclude ?? [];
|
|
65
|
+
const filterTables = include.filter((name) => !exclude.includes(name));
|
|
66
|
+
return Object.fromEntries(tables.filter(([name]) => filterTables.includes(name)));
|
|
67
|
+
}
|
|
68
|
+
getClient(ctx) {
|
|
69
|
+
const options = this.builder.options;
|
|
70
|
+
const drizzleOption = options.drizzle;
|
|
71
|
+
const client = drizzleOption.client instanceof Function
|
|
72
|
+
? drizzleOption.client(ctx)
|
|
73
|
+
: drizzleOption.client;
|
|
74
|
+
return client;
|
|
75
|
+
}
|
|
76
|
+
getRelations() {
|
|
77
|
+
const drizzleOption = this.builder.options.drizzle;
|
|
78
|
+
const client = drizzleOption.client;
|
|
79
|
+
return drizzleOption.relations ?? client._.relations;
|
|
80
|
+
}
|
|
81
|
+
getTables() {
|
|
82
|
+
if (this.tables)
|
|
83
|
+
return this.tables;
|
|
84
|
+
const tables = this.createTableInfo();
|
|
85
|
+
this.tables = tables;
|
|
86
|
+
return tables;
|
|
87
|
+
}
|
|
88
|
+
getDepthLimit() {
|
|
89
|
+
const options = this.builder.options.pothosDrizzleGenerator;
|
|
90
|
+
return options?.depthLimit;
|
|
91
|
+
}
|
|
92
|
+
getInputType(modelName, type, options) {
|
|
93
|
+
if (!this.inputType[modelName])
|
|
94
|
+
this.inputType[modelName] = {};
|
|
95
|
+
if (this.inputType[modelName][type])
|
|
96
|
+
return this.inputType[modelName][type];
|
|
97
|
+
const { tableInfo } = this.getTables()[modelName];
|
|
98
|
+
const input = this.builder.inputType(`${tableInfo.name}${type}`, options);
|
|
99
|
+
this.inputType[modelName][type] = input;
|
|
100
|
+
return input;
|
|
101
|
+
}
|
|
102
|
+
getInputCreate(modelName) {
|
|
103
|
+
const { inputColumns } = this.getTables()[modelName];
|
|
104
|
+
return this.getInputType(modelName, "Create", {
|
|
105
|
+
fields: (t) => Object.fromEntries(inputColumns.map((c) => [
|
|
106
|
+
c.name,
|
|
107
|
+
t.field({
|
|
108
|
+
type: this.getDataType(c),
|
|
109
|
+
required: c.notNull && !c.default,
|
|
110
|
+
}),
|
|
111
|
+
])),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
getInputUpdate(modelName) {
|
|
115
|
+
const { inputColumns } = this.getTables()[modelName];
|
|
116
|
+
return this.getInputType(modelName, "Input", {
|
|
117
|
+
fields: (t) => {
|
|
118
|
+
return Object.fromEntries(inputColumns.map((c) => [
|
|
119
|
+
c.name,
|
|
120
|
+
t.field({
|
|
121
|
+
type: this.getDataType(c),
|
|
122
|
+
}),
|
|
123
|
+
]));
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
getInputWhere(modelName) {
|
|
128
|
+
const { tableInfo } = this.getTables()[modelName];
|
|
129
|
+
const inputWhere = this.getInputType(modelName, "Where", {
|
|
130
|
+
fields: (t) => {
|
|
131
|
+
return Object.fromEntries([
|
|
132
|
+
["AND", t.field({ type: [inputWhere] })],
|
|
133
|
+
["OR", t.field({ type: [inputWhere] })],
|
|
134
|
+
["NOT", t.field({ type: inputWhere })],
|
|
135
|
+
...tableInfo.columns.map((c) => {
|
|
136
|
+
return [
|
|
137
|
+
c.name,
|
|
138
|
+
t.field({
|
|
139
|
+
type: this.getInputOperator(this.getDataType(c)),
|
|
140
|
+
}),
|
|
141
|
+
];
|
|
142
|
+
}),
|
|
143
|
+
]);
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
return inputWhere;
|
|
147
|
+
}
|
|
148
|
+
getInputOrderBy(modelName) {
|
|
149
|
+
const { tableInfo } = this.getTables()[modelName];
|
|
150
|
+
const inputWhere = this.getInputType(modelName, "OrderBy", {
|
|
151
|
+
fields: (t) => {
|
|
152
|
+
return Object.fromEntries(tableInfo.columns.map((c) => {
|
|
153
|
+
return [
|
|
154
|
+
c.name,
|
|
155
|
+
t.field({
|
|
156
|
+
type: this.enums["OrderBy"],
|
|
157
|
+
}),
|
|
158
|
+
];
|
|
159
|
+
}));
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
return inputWhere;
|
|
163
|
+
}
|
|
164
|
+
getInputOperator(type) {
|
|
165
|
+
const typeName = Array.isArray(type) ? `Array${type[0]}` : type;
|
|
166
|
+
const input = this.inputOperators[typeName] ?? (0, utils_js_1.createInputOperator)(this.builder, type);
|
|
167
|
+
this.inputOperators[typeName] = input;
|
|
168
|
+
return input;
|
|
169
|
+
}
|
|
170
|
+
createInputType() {
|
|
171
|
+
const builder = this.builder;
|
|
172
|
+
const scalars = [
|
|
173
|
+
["BigInt", graphql_scalars_1.BigIntResolver],
|
|
174
|
+
["Date", graphql_scalars_1.DateResolver],
|
|
175
|
+
["Bytes", graphql_scalars_1.ByteResolver],
|
|
176
|
+
["DateTime", graphql_scalars_1.DateTimeResolver],
|
|
177
|
+
["Json", graphql_scalars_1.JSONResolver],
|
|
178
|
+
["Decimal", graphql_scalars_1.HexadecimalResolver],
|
|
179
|
+
];
|
|
180
|
+
for (const [scalarName, scalarResolver] of scalars) {
|
|
181
|
+
if (!builder.configStore.hasConfig(scalarName)) {
|
|
182
|
+
builder.addScalarType(scalarName, scalarResolver, {});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
this.enums["OrderBy"] = builder.enumType("OrderBy", {
|
|
186
|
+
values: {
|
|
187
|
+
Asc: { value: "asc" },
|
|
188
|
+
Desc: { value: "desc" },
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
getDataType(column) {
|
|
193
|
+
const isArray = column.dataType.split(" ")[0] === "array";
|
|
194
|
+
const c = isArray ? column.baseColumn : column;
|
|
195
|
+
const types = c.dataType.split(" ");
|
|
196
|
+
switch (types[1] ?? types[0]) {
|
|
197
|
+
case "enum": {
|
|
198
|
+
const sqlType = c.getSQLType();
|
|
199
|
+
const e = this.enums[sqlType];
|
|
200
|
+
if (!e) {
|
|
201
|
+
this.enums[sqlType] = this.builder.enumType(sqlType, {
|
|
202
|
+
values: c.enumValues ?? [],
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return isArray ? [sqlType] : sqlType;
|
|
206
|
+
}
|
|
207
|
+
case "json":
|
|
208
|
+
return isArray ? ["Json"] : "Json";
|
|
209
|
+
case "date":
|
|
210
|
+
return isArray ? ["DateTime"] : "DateTime";
|
|
211
|
+
case "datetime":
|
|
212
|
+
return isArray ? ["DateTime"] : "DateTime";
|
|
213
|
+
case "boolean":
|
|
214
|
+
return isArray ? ["Boolean"] : "Boolean";
|
|
215
|
+
case "double":
|
|
216
|
+
case "float":
|
|
217
|
+
case "udouble":
|
|
218
|
+
case "ufloat":
|
|
219
|
+
return isArray ? ["Float"] : "Float";
|
|
220
|
+
}
|
|
221
|
+
const type = isArray ? types[1] : types[0];
|
|
222
|
+
const scalerMap = {
|
|
223
|
+
bigint: "BigInt",
|
|
224
|
+
number: "Float",
|
|
225
|
+
string: "String",
|
|
226
|
+
};
|
|
227
|
+
const result = scalerMap[type] ?? "String";
|
|
228
|
+
return isArray ? [result] : result;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.PothosDrizzleGenerator = PothosDrizzleGenerator;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { SchemaTypes } from "@pothos/core";
|
|
2
|
+
import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin";
|
|
3
|
+
import type { DBQueryConfigColumns, GetTableViewFieldSelection, RelationsFilter } from "drizzle-orm";
|
|
4
|
+
import type { Operation, OperationBasic } from "./libs/operations.js";
|
|
5
|
+
import type { PgInsertValue, PgTable } from "drizzle-orm/pg-core";
|
|
6
|
+
declare global {
|
|
7
|
+
export namespace PothosSchemaTypes {
|
|
8
|
+
interface Plugins<Types extends SchemaTypes, T extends object = object> {
|
|
9
|
+
pothosDrizzleGenerator: PothosDrizzleGeneratorPlugin<Types, T>;
|
|
10
|
+
}
|
|
11
|
+
type Relations<Types extends SchemaTypes> = Types["DrizzleRelations"];
|
|
12
|
+
type Tables<Types extends SchemaTypes> = keyof Relations<Types>;
|
|
13
|
+
type Columns<Types extends SchemaTypes, U extends Tables<Types>> = keyof DBQueryConfigColumns<GetTableViewFieldSelection<Relations<Types>[U]["table"]>>;
|
|
14
|
+
interface SchemaBuilderOptions<Types extends SchemaTypes> {
|
|
15
|
+
pothosDrizzleGenerator?: {
|
|
16
|
+
depthLimit?: (params: {
|
|
17
|
+
ctx: Types["Context"];
|
|
18
|
+
modelName: Tables<Types>;
|
|
19
|
+
operation: (typeof OperationBasic)[number];
|
|
20
|
+
}) => number | undefined;
|
|
21
|
+
use?: {
|
|
22
|
+
include: (keyof Relations<Types>)[];
|
|
23
|
+
exclude?: undefined;
|
|
24
|
+
} | {
|
|
25
|
+
exclude: (keyof Relations<Types>)[];
|
|
26
|
+
include?: undefined;
|
|
27
|
+
};
|
|
28
|
+
models?: {
|
|
29
|
+
[U in Tables<Types>]?: {
|
|
30
|
+
fields?: {
|
|
31
|
+
include: Columns<Types, U>[];
|
|
32
|
+
exclude?: undefined;
|
|
33
|
+
} | {
|
|
34
|
+
exclude: Columns<Types, U>[];
|
|
35
|
+
include?: undefined;
|
|
36
|
+
};
|
|
37
|
+
operations?: {
|
|
38
|
+
include?: Operation[];
|
|
39
|
+
exclude?: Operation[];
|
|
40
|
+
};
|
|
41
|
+
executable?: (params: {
|
|
42
|
+
ctx: Types["Context"];
|
|
43
|
+
modelName: U;
|
|
44
|
+
operation: (typeof OperationBasic)[number];
|
|
45
|
+
}) => boolean;
|
|
46
|
+
limit?: (params: {
|
|
47
|
+
ctx: Types["Context"];
|
|
48
|
+
modelName: U;
|
|
49
|
+
operation: (typeof OperationBasic)[number];
|
|
50
|
+
}) => number | undefined;
|
|
51
|
+
orderBy?: (params: {
|
|
52
|
+
ctx: Types["Context"];
|
|
53
|
+
modelName: U;
|
|
54
|
+
operation: (typeof OperationBasic)[number];
|
|
55
|
+
}) => {
|
|
56
|
+
[P in Columns<Types, U>]?: "asc" | "desc";
|
|
57
|
+
} | undefined;
|
|
58
|
+
where?: (params: {
|
|
59
|
+
ctx: Types["Context"];
|
|
60
|
+
modelName: U;
|
|
61
|
+
operation: (typeof OperationBasic)[number];
|
|
62
|
+
}) => RelationsFilter<Relations<Types>[U], Relations<Types>> | undefined;
|
|
63
|
+
inputFields?: {
|
|
64
|
+
include: Columns<Types, U>[];
|
|
65
|
+
exclude?: undefined;
|
|
66
|
+
} | {
|
|
67
|
+
exclude: Columns<Types, U>[];
|
|
68
|
+
include?: undefined;
|
|
69
|
+
};
|
|
70
|
+
inputData?: (params: {
|
|
71
|
+
ctx: Types["Context"];
|
|
72
|
+
modelName: U;
|
|
73
|
+
operation: (typeof OperationBasic)[number];
|
|
74
|
+
}) => PgInsertValue<Relations<Types>[U]["table"] extends PgTable ? Relations<Types>[U]["table"] : never, true> | undefined;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
const core_1 = __importDefault(require("@pothos/core"));
|
|
21
|
+
const PothosDrizzleGeneratorPlugin_js_1 = require("./PothosDrizzleGeneratorPlugin.js");
|
|
22
|
+
__exportStar(require("./global-types.js"), exports);
|
|
23
|
+
const pluginName = "pothosDrizzleGenerator";
|
|
24
|
+
const allowPluginReRegistration = core_1.default.allowPluginReRegistration;
|
|
25
|
+
core_1.default.allowPluginReRegistration = true;
|
|
26
|
+
core_1.default.registerPlugin(pluginName, PothosDrizzleGeneratorPlugin_js_1.PothosDrizzleGeneratorPlugin);
|
|
27
|
+
core_1.default.allowPluginReRegistration = allowPluginReRegistration;
|
|
28
|
+
exports.default = pluginName;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const OperationFind: readonly ["findFirst", "findMany"];
|
|
2
|
+
export declare const OperationQuery: readonly ["findFirst", "findMany", "count"];
|
|
3
|
+
export declare const OperationCreate: readonly ["createOne", "createMany"];
|
|
4
|
+
export declare const OperationUpdate: readonly ["update"];
|
|
5
|
+
export declare const OperationDelete: readonly ["delete"];
|
|
6
|
+
export declare const OperationMutation: readonly ["createOne", "createMany", "update", "delete"];
|
|
7
|
+
export declare const OperationBasic: readonly ["findFirst", "findMany", "count", "createOne", "createMany", "update", "delete"];
|
|
8
|
+
export declare const OperationAll: readonly ["find", "update", "delete", "query", "mutation", "findFirst", "findMany", "count", "createOne", "createMany", "update", "delete"];
|
|
9
|
+
export type Operation = (typeof OperationAll)[number] | "all";
|
|
10
|
+
export declare const expandOperations: (operations: readonly Operation[]) => ("findFirst" | "findMany" | "count" | "createOne" | "createMany" | "update" | "delete")[];
|
|
11
|
+
export declare const isOperation: (operations: readonly string[], operation: (typeof OperationBasic)[number]) => boolean;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isOperation = exports.expandOperations = exports.OperationAll = exports.OperationBasic = exports.OperationMutation = exports.OperationDelete = exports.OperationUpdate = exports.OperationCreate = exports.OperationQuery = exports.OperationFind = void 0;
|
|
4
|
+
exports.OperationFind = ["findFirst", "findMany"];
|
|
5
|
+
exports.OperationQuery = [...exports.OperationFind, "count"];
|
|
6
|
+
exports.OperationCreate = ["createOne", "createMany"];
|
|
7
|
+
exports.OperationUpdate = ["update"];
|
|
8
|
+
exports.OperationDelete = ["delete"];
|
|
9
|
+
exports.OperationMutation = [
|
|
10
|
+
...exports.OperationCreate,
|
|
11
|
+
...exports.OperationUpdate,
|
|
12
|
+
...exports.OperationDelete,
|
|
13
|
+
];
|
|
14
|
+
exports.OperationBasic = [
|
|
15
|
+
...exports.OperationQuery,
|
|
16
|
+
...exports.OperationMutation,
|
|
17
|
+
];
|
|
18
|
+
exports.OperationAll = [
|
|
19
|
+
"find",
|
|
20
|
+
"update",
|
|
21
|
+
"delete",
|
|
22
|
+
"query",
|
|
23
|
+
"mutation",
|
|
24
|
+
...exports.OperationBasic,
|
|
25
|
+
];
|
|
26
|
+
const expandOperations = (operations) => {
|
|
27
|
+
return operations.flatMap((v) => v === "all"
|
|
28
|
+
? exports.OperationBasic
|
|
29
|
+
: v === "find"
|
|
30
|
+
? exports.OperationFind
|
|
31
|
+
: v === "update"
|
|
32
|
+
? exports.OperationUpdate
|
|
33
|
+
: v === "delete"
|
|
34
|
+
? exports.OperationDelete
|
|
35
|
+
: v === "query"
|
|
36
|
+
? exports.OperationQuery
|
|
37
|
+
: v === "mutation"
|
|
38
|
+
? exports.OperationMutation
|
|
39
|
+
: [v]);
|
|
40
|
+
};
|
|
41
|
+
exports.expandOperations = expandOperations;
|
|
42
|
+
const isOperation = (operations, operation) => {
|
|
43
|
+
return operations.includes(operation);
|
|
44
|
+
};
|
|
45
|
+
exports.isOperation = isOperation;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as p from "drizzle-orm";
|
|
2
|
+
import { GraphQLResolveInfo } from "graphql";
|
|
3
|
+
export declare function getQueryDepth(info: GraphQLResolveInfo): number;
|
|
4
|
+
export declare const getQueryFields: (info: GraphQLResolveInfo) => {
|
|
5
|
+
[k: string]: boolean;
|
|
6
|
+
};
|
|
7
|
+
declare const OperatorMap: {
|
|
8
|
+
eq: p.BinaryOperator;
|
|
9
|
+
ne: p.BinaryOperator;
|
|
10
|
+
gt: p.BinaryOperator;
|
|
11
|
+
gte: p.BinaryOperator;
|
|
12
|
+
lt: p.BinaryOperator;
|
|
13
|
+
lte: p.BinaryOperator;
|
|
14
|
+
like: typeof p.like;
|
|
15
|
+
notLike: typeof p.notLike;
|
|
16
|
+
ilike: typeof p.ilike;
|
|
17
|
+
notIlike: typeof p.notIlike;
|
|
18
|
+
isNull: typeof p.isNull;
|
|
19
|
+
isNotNull: typeof p.isNotNull;
|
|
20
|
+
in: typeof p.inArray;
|
|
21
|
+
notIn: typeof p.notInArray;
|
|
22
|
+
arrayContained: typeof p.arrayContained;
|
|
23
|
+
arrayOverlaps: typeof p.arrayOverlaps;
|
|
24
|
+
arrayContains: typeof p.arrayContains;
|
|
25
|
+
};
|
|
26
|
+
type OperatorType = Record<string, Record<keyof typeof OperatorMap, unknown>>;
|
|
27
|
+
type OperatorTree = Record<"AND" | "OR", OperatorType[]> | Record<"NOT", OperatorType> | OperatorType;
|
|
28
|
+
export declare const createWhereQuery: (table: p.SchemaEntry, tree?: OperatorTree) => p.SQL | undefined;
|
|
29
|
+
export declare const createInputOperator: (builder: PothosSchemaTypes.SchemaBuilder<any>, type: string | [string]) => PothosSchemaTypes.InputObjectRef<any, {
|
|
30
|
+
eq?: any;
|
|
31
|
+
ne?: any;
|
|
32
|
+
gt?: any;
|
|
33
|
+
gte?: any;
|
|
34
|
+
lt?: any;
|
|
35
|
+
lte?: any;
|
|
36
|
+
like?: any;
|
|
37
|
+
notLike?: any;
|
|
38
|
+
ilike?: any;
|
|
39
|
+
notIlike?: any;
|
|
40
|
+
isNull?: any;
|
|
41
|
+
isNotNull?: any;
|
|
42
|
+
in: never;
|
|
43
|
+
notIn: never;
|
|
44
|
+
arrayContained: never;
|
|
45
|
+
arrayOverlaps: never;
|
|
46
|
+
arrayContains: never;
|
|
47
|
+
}>;
|
|
48
|
+
type AggregationQueryType = {
|
|
49
|
+
aggregation?: boolean;
|
|
50
|
+
columns?: object;
|
|
51
|
+
with: Record<string, AggregationQueryType>;
|
|
52
|
+
};
|
|
53
|
+
export declare const convertAggregationQuery: (query: AggregationQueryType) => {
|
|
54
|
+
with: Record<string, AggregationQueryType>;
|
|
55
|
+
aggregation?: boolean;
|
|
56
|
+
columns?: object;
|
|
57
|
+
};
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.convertAggregationQuery = exports.createInputOperator = exports.createWhereQuery = exports.getQueryFields = void 0;
|
|
37
|
+
exports.getQueryDepth = getQueryDepth;
|
|
38
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
39
|
+
const p = __importStar(require("drizzle-orm"));
|
|
40
|
+
function getDepthFromSelection(selection, currentDepth) {
|
|
41
|
+
if (selection.kind === "Field" && selection.selectionSet) {
|
|
42
|
+
// 子フィールドがある場合はさらに深さを加算
|
|
43
|
+
const childDepths = selection.selectionSet.selections.map((sel) => getDepthFromSelection(sel, currentDepth + 1));
|
|
44
|
+
return Math.max(...childDepths);
|
|
45
|
+
}
|
|
46
|
+
return currentDepth;
|
|
47
|
+
}
|
|
48
|
+
function getQueryDepth(info) {
|
|
49
|
+
return getDepthFromSelection(info.fieldNodes[0], 0);
|
|
50
|
+
}
|
|
51
|
+
const getQueryFields = (info) => {
|
|
52
|
+
return Object.fromEntries(Array.from((0, utils_1.collectFields)(info.schema, info.fragments, info.variableValues, {}, info.fieldNodes[0].selectionSet).fields.keys()).map((v) => [v, true]));
|
|
53
|
+
};
|
|
54
|
+
exports.getQueryFields = getQueryFields;
|
|
55
|
+
const OperatorMap = {
|
|
56
|
+
eq: p.eq,
|
|
57
|
+
ne: p.ne,
|
|
58
|
+
gt: p.gt,
|
|
59
|
+
gte: p.gte,
|
|
60
|
+
lt: p.lt,
|
|
61
|
+
lte: p.lte,
|
|
62
|
+
like: p.like,
|
|
63
|
+
notLike: p.notLike,
|
|
64
|
+
ilike: p.ilike,
|
|
65
|
+
notIlike: p.notIlike,
|
|
66
|
+
isNull: p.isNull,
|
|
67
|
+
isNotNull: p.isNotNull,
|
|
68
|
+
in: p.inArray,
|
|
69
|
+
notIn: p.notInArray,
|
|
70
|
+
arrayContained: p.arrayContained,
|
|
71
|
+
arrayOverlaps: p.arrayOverlaps,
|
|
72
|
+
arrayContains: p.arrayContains,
|
|
73
|
+
};
|
|
74
|
+
const createWhereQuery = (table, tree) => {
|
|
75
|
+
if (!tree)
|
|
76
|
+
return p.and();
|
|
77
|
+
const result = Object.entries(tree)
|
|
78
|
+
.map(([key, value]) => {
|
|
79
|
+
switch (key) {
|
|
80
|
+
case "AND":
|
|
81
|
+
return value.length
|
|
82
|
+
? p.and(...value.map((v) => (0, exports.createWhereQuery)(table, v)))
|
|
83
|
+
: undefined;
|
|
84
|
+
case "OR":
|
|
85
|
+
return value.length
|
|
86
|
+
? p.or(...value.map((v) => (0, exports.createWhereQuery)(table, v)))
|
|
87
|
+
: undefined;
|
|
88
|
+
case "NOT": {
|
|
89
|
+
const v = (0, exports.createWhereQuery)(table, value);
|
|
90
|
+
return v ? p.not(v) : undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const result = Object.entries(value).map(([k, v]) => {
|
|
94
|
+
const m = OperatorMap[k];
|
|
95
|
+
return m(p.getColumns(table)[key], v);
|
|
96
|
+
});
|
|
97
|
+
if (result.length === 1) {
|
|
98
|
+
return result[0];
|
|
99
|
+
}
|
|
100
|
+
return p.and(...result);
|
|
101
|
+
})
|
|
102
|
+
.flatMap((v) => (v ? [v] : []));
|
|
103
|
+
if (result.length === 1)
|
|
104
|
+
return result[0];
|
|
105
|
+
return p.and(...result);
|
|
106
|
+
};
|
|
107
|
+
exports.createWhereQuery = createWhereQuery;
|
|
108
|
+
const createInputOperator = (
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
110
|
+
builder, type) => {
|
|
111
|
+
const typeName = Array.isArray(type) ? `Array${type[0]}` : type;
|
|
112
|
+
const name = `${typeName}InputOperator`;
|
|
113
|
+
const inputType = builder.inputType(name, {
|
|
114
|
+
fields: (t) => ({
|
|
115
|
+
eq: t.field({ type }),
|
|
116
|
+
ne: t.field({ type }),
|
|
117
|
+
gt: t.field({ type }),
|
|
118
|
+
gte: t.field({ type }),
|
|
119
|
+
lt: t.field({ type }),
|
|
120
|
+
lte: t.field({ type }),
|
|
121
|
+
like: t.field({ type }),
|
|
122
|
+
notLike: t.field({ type }),
|
|
123
|
+
ilike: t.field({ type }),
|
|
124
|
+
notIlike: t.field({ type }),
|
|
125
|
+
isNull: t.boolean(),
|
|
126
|
+
isNotNull: t.boolean(),
|
|
127
|
+
in: t.field({ type: [type] }),
|
|
128
|
+
notIn: t.field({ type: [type] }),
|
|
129
|
+
arrayContained: t.field({ type: [type] }),
|
|
130
|
+
arrayOverlaps: t.field({ type: [type] }),
|
|
131
|
+
arrayContains: t.field({ type: [type] }),
|
|
132
|
+
}),
|
|
133
|
+
});
|
|
134
|
+
return inputType;
|
|
135
|
+
};
|
|
136
|
+
exports.createInputOperator = createInputOperator;
|
|
137
|
+
const convertAggregationQuery = (query) => {
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
139
|
+
const { aggregation, columns, ...q } = query;
|
|
140
|
+
const newQuery = aggregation ? { ...q, columns: {} } : query;
|
|
141
|
+
const newWith = query.with
|
|
142
|
+
? Object.fromEntries(Object.entries(query.with).map(([key, value]) => [
|
|
143
|
+
key,
|
|
144
|
+
(0, exports.convertAggregationQuery)(value),
|
|
145
|
+
]))
|
|
146
|
+
: query.with;
|
|
147
|
+
return { ...newQuery, with: newWith };
|
|
148
|
+
};
|
|
149
|
+
exports.convertAggregationQuery = convertAggregationQuery;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BasePlugin, type BuildCache, type SchemaTypes } from "@pothos/core";
|
|
2
|
+
import { PothosDrizzleGenerator } from "./generator.js";
|
|
3
|
+
export declare class PothosDrizzleGeneratorPlugin<Types extends SchemaTypes, T extends object = object> extends BasePlugin<Types, T> {
|
|
4
|
+
generator: PothosDrizzleGenerator;
|
|
5
|
+
constructor(buildCache: BuildCache<Types>, name: keyof PothosSchemaTypes.Plugins<Types>);
|
|
6
|
+
beforeBuild(): void;
|
|
7
|
+
}
|