pothos-drizzle-generator 0.0.3 → 0.1.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/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # pothos-drizzle-generator
2
2
 
3
+ [![](https://img.shields.io/npm/l/pothos-drizzle-generator)](https://www.npmjs.com/package/pothos-drizzle-generator)
4
+ [![](https://img.shields.io/npm/v/pothos-drizzle-generator)](https://www.npmjs.com/package/pothos-drizzle-generator)
5
+ [![](https://img.shields.io/npm/dw/pothos-drizzle-generator)](https://www.npmjs.com/package/pothos-drizzle-generator)
6
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/node-libraries/pothos-drizzle-generator)
7
+
3
8
  A Pothos plugin that automatically generates GraphQL schemas based on Drizzle schema information.
4
9
 
5
10
  ![](./documents/image.png)
@@ -49,6 +54,8 @@ const schema = builder.toSchema();
49
54
 
50
55
  # Options
51
56
 
57
+ Settings defined in `all` are overridden by `models`.
58
+
52
59
  ```ts
53
60
  const builder = new SchemaBuilder<PothosTypes>({
54
61
  plugins: [
@@ -65,12 +72,32 @@ const builder = new SchemaBuilder<PothosTypes>({
65
72
  depthLimit: ({ ctx, modelName, operation }) => $limit$,
66
73
  // Specifying the model to use
67
74
  use: { include: [...$modelNames$], exclude: [...$modelNames$] },
75
+ // Applies to all models
76
+ all:{
77
+ // Specifying fields to use in queries
78
+ fields: ({ modelName }) => { include: [...$fields$], exclude: [...$fields$] },
79
+ // Specifying the method of operation for the model
80
+ operations: ({ modelName }) => { include: [...$operation$], exclude: [...$operation$] },
81
+ // Runtime Permission Check
82
+ executable: ({ ctx, modelName, operation }) => $permission$,
83
+ // Specify the maximum value for the query's limit
84
+ limit: ({ ctx, modelName, operation }) => $limit$,
85
+ // Override the query's orderBy
86
+ orderBy: ({ ctx, modelName, operation }) => $orderBy$,
87
+ // Add query conditions
88
+ where: ({ ctx, modelName, operation }) => $where$,
89
+ // Specifying input fields
90
+ inputFields: { include: [$fields$], exclude: [$fields$] },
91
+ // Overwriting input data
92
+ inputData: ({ ctx, modelName, operation }) => $inputData$,
93
+ },
94
+ // Apply to individual models
68
95
  models: {
69
96
  [$modelName$]: {
70
97
  // Specifying fields to use in queries
71
- fields: { include: [...$fields$], exclude: [...$fields$] },
98
+ fields: ({ modelName }) => { include: [...$fields$], exclude: [...$fields$] },
72
99
  // Specifying the method of operation for the model
73
- operations: { include: [...$operation$], exclude: [...$operation$] },
100
+ operations: ({ modelName }) => { include: [...$operation$], exclude: [...$operation$] },
74
101
  // Runtime Permission Check
75
102
  executable: ({ ctx, modelName, operation }) => $permission$,
76
103
  // Specify the maximum value for the query's limit
@@ -89,6 +116,68 @@ const builder = new SchemaBuilder<PothosTypes>({
89
116
  });
90
117
  ```
91
118
 
119
+ - example
120
+
121
+ ```ts
122
+ const builder = new SchemaBuilder<PothosTypes>({
123
+ plugins: [
124
+ DrizzlePlugin,
125
+ PothosDrizzleGeneratorPlugin, // Set plugin
126
+ ],
127
+ drizzle: {
128
+ client: () => db,
129
+ relations,
130
+ getTableConfig,
131
+ },
132
+ pothosDrizzleGenerator: {
133
+ // Tables not used
134
+ use: { exclude: ["postsToCategories"] },
135
+ all: {
136
+ // Maximum query depth
137
+ depthLimit: () => 5,
138
+ executable: ({ operation, ctx }) => {
139
+ // Prohibit write operations if the user is not authenticated
140
+ if (isOperation(OperationMutation, operation) && !ctx.get("user")) {
141
+ return false;
142
+ }
143
+ return true;
144
+ },
145
+ inputFields: () => {
146
+ // Exclude auto-generated fields
147
+ return { exclude: ["createdAt", "updatedAt"] };
148
+ },
149
+ },
150
+ models: {
151
+ posts: {
152
+ // Fields that cannot be overwritten
153
+ // inputFields: () => ({ exclude: ["createdAt", "updatedAt"] }), // Defined in "all", so commented out
154
+ // Set the current user's ID when writing data
155
+ inputData: ({ ctx }) => {
156
+ const user = ctx.get("user");
157
+ if (!user) throw new Error("No permission");
158
+ return { authorId: user.id };
159
+ },
160
+ where: ({ ctx, operation }) => {
161
+ // When querying, only return published data or the user's own data
162
+ if (isOperation(OperationQuery, operation)) {
163
+ return {
164
+ OR: [
165
+ { published: true },
166
+ { authorId: { eq: ctx.get("user")?.id } },
167
+ ],
168
+ };
169
+ }
170
+ // When writing, only allow operations on the user's own data
171
+ if (isOperation(OperationMutation, operation)) {
172
+ return { authorId: ctx.get("user")?.id };
173
+ }
174
+ },
175
+ },
176
+ },
177
+ },
178
+ });
179
+ ```
180
+
92
181
  # Current implementation status
93
182
 
94
183
  ## Operations
@@ -15,9 +15,8 @@ class PothosDrizzleGeneratorPlugin extends core_1.BasePlugin {
15
15
  beforeBuild() {
16
16
  const generator = this.generator;
17
17
  const builder = this.builder;
18
- const depthLimit = generator.getDepthLimit();
19
18
  const tables = generator.getTables();
20
- for (const [modelName, { table, tableInfo, relations, columns, operations, executable, limit, where, orderBy, inputData, },] of Object.entries(tables)) {
19
+ for (const [modelName, { table, tableInfo, relations, columns, operations, executable, limit, where, orderBy, inputData, depthLimit, },] of Object.entries(tables)) {
21
20
  const objectRef = builder.objectRef(modelName);
22
21
  objectRef.implement({
23
22
  fields: (t) => Object.fromEntries(columns.map((c) => {
@@ -9,31 +9,36 @@ type ModelData = {
9
9
  inputColumns: PgColumn<any, object>[];
10
10
  tableInfo: ReturnType<typeof getTableConfig>;
11
11
  relations: RelationsRecord;
12
- executable?: (params: {
12
+ executable?: ((params: {
13
13
  ctx: any;
14
14
  modelName: string;
15
15
  operation: (typeof OperationBasic)[number];
16
- }) => boolean | undefined;
17
- limit?: (params: {
16
+ }) => boolean | undefined) | undefined;
17
+ limit?: ((params: {
18
18
  ctx: any;
19
19
  modelName: string;
20
20
  operation: (typeof OperationBasic)[number];
21
- }) => number | undefined;
22
- orderBy?: (params: {
21
+ }) => number | undefined) | undefined;
22
+ orderBy?: ((params: {
23
+ ctx: any;
24
+ modelName: string;
25
+ operation: (typeof OperationBasic)[number];
26
+ }) => object | undefined) | undefined;
27
+ where?: ((params: {
23
28
  ctx: any;
24
29
  modelName: string;
25
30
  operation: (typeof OperationBasic)[number];
26
- }) => object | undefined;
27
- where?: (params: {
31
+ }) => object | undefined) | undefined;
32
+ inputData?: ((params: {
28
33
  ctx: any;
29
34
  modelName: string;
30
35
  operation: (typeof OperationBasic)[number];
31
- }) => object | undefined;
32
- inputData?: (params: {
36
+ }) => object | undefined) | undefined;
37
+ depthLimit?: ((params: {
33
38
  ctx: any;
34
39
  modelName: string;
35
40
  operation: (typeof OperationBasic)[number];
36
- }) => object | undefined;
41
+ }) => number | undefined) | undefined;
37
42
  };
38
43
  export declare class PothosDrizzleGenerator {
39
44
  enums: Record<string, PothosSchemaTypes.EnumRef<any, any>>;
@@ -46,11 +51,6 @@ export declare class PothosDrizzleGenerator {
46
51
  getClient(ctx: any): DrizzleClient;
47
52
  getRelations(): AnyRelations;
48
53
  getTables(): Record<string, ModelData>;
49
- getDepthLimit(): ((params: {
50
- ctx: any;
51
- modelName: string | number | symbol;
52
- operation: (typeof OperationBasic)[number];
53
- }) => number | undefined) | undefined;
54
54
  getInputType(modelName: string, type: string, options: PothosSchemaTypes.InputObjectTypeOptions<any, any>): PothosSchemaTypes.InputObjectRef<any, any>;
55
55
  getInputCreate(modelName: string): PothosSchemaTypes.InputObjectRef<any, any>;
56
56
  getInputUpdate(modelName: string): PothosSchemaTypes.InputObjectRef<any, any>;
@@ -23,26 +23,30 @@ class PothosDrizzleGenerator {
23
23
  const relations = this.getRelations();
24
24
  const tables = Object.values(relations)
25
25
  .filter((t) => (0, drizzle_orm_1.isTable)(t.table))
26
- .map(({ name, table, relations }) => {
26
+ .map(({ name: modelName, table, relations }) => {
27
27
  const tableInfo = getConfig(table);
28
- const modelOptions = options?.models?.[name];
28
+ const allOptions = options?.all;
29
+ const modelOptions = options?.models?.[modelName];
29
30
  const columns = tableInfo.columns;
30
31
  //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 ?? []);
32
+ const operationValue = (modelOptions?.operations ?? allOptions?.operations)?.({ modelName });
33
+ const operationIncludes = (0, operations_js_1.expandOperations)(operationValue?.include ?? operations_js_1.OperationBasic);
34
+ const operationExcludes = (0, operations_js_1.expandOperations)(operationValue?.exclude ?? []);
33
35
  const operations = operationIncludes.filter((v) => !operationExcludes.includes(v));
34
36
  // Columns filter
35
- const include = options?.models?.[name]?.fields?.include ??
36
- columns.map((c) => c.name);
37
- const exclude = options?.models?.[name]?.fields?.exclude ?? [];
37
+ const columnValue = (modelOptions?.fields ?? allOptions?.fields)?.({
38
+ modelName,
39
+ });
40
+ const include = columnValue?.include ?? columns.map((c) => c.name);
41
+ const exclude = columnValue?.exclude ?? [];
38
42
  const filterColumns = include.filter((name) => !exclude.includes(name));
39
43
  // 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 ?? [];
44
+ const inputFieldValue = (modelOptions?.inputFields ?? allOptions?.inputFields)?.({ modelName });
45
+ const includeInput = inputFieldValue?.include ?? columns.map((c) => c.name);
46
+ const excludeInput = inputFieldValue?.exclude ?? [];
43
47
  const filterInputColumns = includeInput.filter((name) => !excludeInput.includes(name));
44
48
  return [
45
- name,
49
+ modelName,
46
50
  {
47
51
  table,
48
52
  columns: columns.filter((c) => filterColumns.includes(c.name)),
@@ -50,11 +54,12 @@ class PothosDrizzleGenerator {
50
54
  inputColumns: columns.filter((c) => filterInputColumns.includes(c.name)),
51
55
  tableInfo,
52
56
  relations,
53
- executable: modelOptions?.executable,
54
- limit: modelOptions?.limit,
55
- orderBy: modelOptions?.orderBy,
56
- where: modelOptions?.where,
57
- inputData: modelOptions?.inputData,
57
+ executable: modelOptions?.executable ?? allOptions?.executable,
58
+ limit: modelOptions?.limit ?? allOptions?.limit,
59
+ orderBy: modelOptions?.orderBy ?? allOptions?.orderBy,
60
+ where: modelOptions?.where ?? allOptions?.where,
61
+ inputData: modelOptions?.inputData ?? allOptions?.inputData,
62
+ depthLimit: modelOptions?.depthLimit ?? allOptions?.depthLimit,
58
63
  },
59
64
  ];
60
65
  });
@@ -85,10 +90,6 @@ class PothosDrizzleGenerator {
85
90
  this.tables = tables;
86
91
  return tables;
87
92
  }
88
- getDepthLimit() {
89
- const options = this.builder.options.pothosDrizzleGenerator;
90
- return options?.depthLimit;
91
- }
92
93
  getInputType(modelName, type, options) {
93
94
  if (!this.inputType[modelName])
94
95
  this.inputType[modelName] = {};
@@ -1,6 +1,6 @@
1
- import { SchemaTypes } from "@pothos/core";
2
- import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin";
3
- import type { DBQueryConfigColumns, GetTableViewFieldSelection, RelationsFilter } from "drizzle-orm";
1
+ import type { SchemaTypes } from "@pothos/core";
2
+ import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin.js";
3
+ import type { DBQueryConfigColumns, GetTableViewFieldSelection, RelationsFilter, SchemaEntry } from "drizzle-orm";
4
4
  import type { Operation, OperationBasic } from "./libs/operations.js";
5
5
  import type { PgInsertValue, PgTable } from "drizzle-orm/pg-core";
6
6
  declare global {
@@ -9,15 +9,12 @@ declare global {
9
9
  pothosDrizzleGenerator: PothosDrizzleGeneratorPlugin<Types, T>;
10
10
  }
11
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"]>>;
12
+ type TableNames<Types extends SchemaTypes> = keyof Relations<Types>;
13
+ type Columns<Types extends SchemaTypes, U extends TableNames<Types>> = keyof DBQueryConfigColumns<GetTableViewFieldSelection<Relations<Types>[U]["table"]>>;
14
+ type AnyTable<Types extends SchemaTypes> = Relations<Types>[keyof Relations<Types>]["table"];
15
+ type AnyColumns<Types extends SchemaTypes> = AnyTable<Types> extends infer R ? R extends SchemaEntry ? keyof DBQueryConfigColumns<GetTableViewFieldSelection<R>> : never : never;
14
16
  interface SchemaBuilderOptions<Types extends SchemaTypes> {
15
17
  pothosDrizzleGenerator?: {
16
- depthLimit?: (params: {
17
- ctx: Types["Context"];
18
- modelName: Tables<Types>;
19
- operation: (typeof OperationBasic)[number];
20
- }) => number | undefined;
21
18
  use?: {
22
19
  include: (keyof Relations<Types>)[];
23
20
  exclude?: undefined;
@@ -25,24 +22,91 @@ declare global {
25
22
  exclude: (keyof Relations<Types>)[];
26
23
  include?: undefined;
27
24
  };
25
+ all?: {
26
+ depthLimit?: <U extends TableNames<Types>>(params: {
27
+ ctx: Types["Context"];
28
+ modelName: U;
29
+ operation: (typeof OperationBasic)[number];
30
+ }) => number | undefined;
31
+ fields?: <U extends TableNames<Types>>(params: {
32
+ modelName: U;
33
+ }) => {
34
+ include: AnyColumns<Types>[];
35
+ exclude?: undefined;
36
+ } | {
37
+ exclude: AnyColumns<Types>[];
38
+ include?: undefined;
39
+ } | undefined;
40
+ operations?: <U extends TableNames<Types>>(params: {
41
+ modelName: U;
42
+ }) => {
43
+ include?: Operation[];
44
+ exclude?: Operation[];
45
+ } | undefined;
46
+ executable?: <U extends TableNames<Types>>(params: {
47
+ ctx: Types["Context"];
48
+ modelName: U;
49
+ operation: (typeof OperationBasic)[number];
50
+ }) => boolean | undefined;
51
+ limit?: <U extends TableNames<Types>>(params: {
52
+ ctx: Types["Context"];
53
+ modelName: U;
54
+ operation: (typeof OperationBasic)[number];
55
+ }) => number | undefined;
56
+ orderBy?: <U extends TableNames<Types>>(params: {
57
+ ctx: Types["Context"];
58
+ modelName: U;
59
+ operation: (typeof OperationBasic)[number];
60
+ }) => {
61
+ [P in AnyColumns<Types>]?: "asc" | "desc";
62
+ } | undefined;
63
+ where?: <U extends TableNames<Types>>(params: {
64
+ ctx: Types["Context"];
65
+ modelName: U;
66
+ operation: (typeof OperationBasic)[number];
67
+ }) => RelationsFilter<Relations<Types>[U], Relations<Types>> | undefined;
68
+ inputFields?: <U extends TableNames<Types>>(params: {
69
+ modelName: U;
70
+ }) => {
71
+ include: AnyColumns<Types>[];
72
+ exclude?: undefined;
73
+ } | {
74
+ exclude: AnyColumns<Types>[];
75
+ include?: undefined;
76
+ } | undefined;
77
+ inputData?: <U extends TableNames<Types>>(params: {
78
+ ctx: Types["Context"];
79
+ modelName: U;
80
+ operation: (typeof OperationBasic)[number];
81
+ }) => PgInsertValue<Relations<Types>[U]["table"] extends PgTable ? Relations<Types>[U]["table"] : never, true> | undefined;
82
+ };
28
83
  models?: {
29
- [U in Tables<Types>]?: {
30
- fields?: {
84
+ [U in TableNames<Types>]?: {
85
+ depthLimit?: (params: {
86
+ ctx: Types["Context"];
87
+ modelName: U;
88
+ operation: (typeof OperationBasic)[number];
89
+ }) => number | undefined;
90
+ fields?: (params: {
91
+ modelName: U;
92
+ }) => {
31
93
  include: Columns<Types, U>[];
32
94
  exclude?: undefined;
33
95
  } | {
34
96
  exclude: Columns<Types, U>[];
35
97
  include?: undefined;
36
98
  };
37
- operations?: {
99
+ operations?: <U extends TableNames<Types>>(params: {
100
+ modelName: U;
101
+ }) => {
38
102
  include?: Operation[];
39
103
  exclude?: Operation[];
40
- };
104
+ } | undefined;
41
105
  executable?: (params: {
42
106
  ctx: Types["Context"];
43
107
  modelName: U;
44
108
  operation: (typeof OperationBasic)[number];
45
- }) => boolean;
109
+ }) => boolean | undefined;
46
110
  limit?: (params: {
47
111
  ctx: Types["Context"];
48
112
  modelName: U;
@@ -60,7 +124,9 @@ declare global {
60
124
  modelName: U;
61
125
  operation: (typeof OperationBasic)[number];
62
126
  }) => RelationsFilter<Relations<Types>[U], Relations<Types>> | undefined;
63
- inputFields?: {
127
+ inputFields?: (params: {
128
+ modelName: U;
129
+ }) => {
64
130
  include: Columns<Types, U>[];
65
131
  exclude?: undefined;
66
132
  } | {
@@ -1,5 +1,5 @@
1
1
  import * as p from "drizzle-orm";
2
- import { GraphQLResolveInfo } from "graphql";
2
+ import type { GraphQLResolveInfo } from "graphql";
3
3
  export declare function getQueryDepth(info: GraphQLResolveInfo): number;
4
4
  export declare const getQueryFields: (info: GraphQLResolveInfo) => {
5
5
  [k: string]: boolean;
@@ -46,9 +46,13 @@ function getDepthFromSelection(selection, currentDepth) {
46
46
  return currentDepth;
47
47
  }
48
48
  function getQueryDepth(info) {
49
+ if (!info.fieldNodes[0])
50
+ return 0;
49
51
  return getDepthFromSelection(info.fieldNodes[0], 0);
50
52
  }
51
53
  const getQueryFields = (info) => {
54
+ if (!info.fieldNodes[0])
55
+ return {};
52
56
  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
57
  };
54
58
  exports.getQueryFields = getQueryFields;
@@ -12,9 +12,8 @@ export class PothosDrizzleGeneratorPlugin extends BasePlugin {
12
12
  beforeBuild() {
13
13
  const generator = this.generator;
14
14
  const builder = this.builder;
15
- const depthLimit = generator.getDepthLimit();
16
15
  const tables = generator.getTables();
17
- for (const [modelName, { table, tableInfo, relations, columns, operations, executable, limit, where, orderBy, inputData, },] of Object.entries(tables)) {
16
+ for (const [modelName, { table, tableInfo, relations, columns, operations, executable, limit, where, orderBy, inputData, depthLimit, },] of Object.entries(tables)) {
18
17
  const objectRef = builder.objectRef(modelName);
19
18
  objectRef.implement({
20
19
  fields: (t) => Object.fromEntries(columns.map((c) => {
@@ -9,31 +9,36 @@ type ModelData = {
9
9
  inputColumns: PgColumn<any, object>[];
10
10
  tableInfo: ReturnType<typeof getTableConfig>;
11
11
  relations: RelationsRecord;
12
- executable?: (params: {
12
+ executable?: ((params: {
13
13
  ctx: any;
14
14
  modelName: string;
15
15
  operation: (typeof OperationBasic)[number];
16
- }) => boolean | undefined;
17
- limit?: (params: {
16
+ }) => boolean | undefined) | undefined;
17
+ limit?: ((params: {
18
18
  ctx: any;
19
19
  modelName: string;
20
20
  operation: (typeof OperationBasic)[number];
21
- }) => number | undefined;
22
- orderBy?: (params: {
21
+ }) => number | undefined) | undefined;
22
+ orderBy?: ((params: {
23
+ ctx: any;
24
+ modelName: string;
25
+ operation: (typeof OperationBasic)[number];
26
+ }) => object | undefined) | undefined;
27
+ where?: ((params: {
23
28
  ctx: any;
24
29
  modelName: string;
25
30
  operation: (typeof OperationBasic)[number];
26
- }) => object | undefined;
27
- where?: (params: {
31
+ }) => object | undefined) | undefined;
32
+ inputData?: ((params: {
28
33
  ctx: any;
29
34
  modelName: string;
30
35
  operation: (typeof OperationBasic)[number];
31
- }) => object | undefined;
32
- inputData?: (params: {
36
+ }) => object | undefined) | undefined;
37
+ depthLimit?: ((params: {
33
38
  ctx: any;
34
39
  modelName: string;
35
40
  operation: (typeof OperationBasic)[number];
36
- }) => object | undefined;
41
+ }) => number | undefined) | undefined;
37
42
  };
38
43
  export declare class PothosDrizzleGenerator {
39
44
  enums: Record<string, PothosSchemaTypes.EnumRef<any, any>>;
@@ -46,11 +51,6 @@ export declare class PothosDrizzleGenerator {
46
51
  getClient(ctx: any): DrizzleClient;
47
52
  getRelations(): AnyRelations;
48
53
  getTables(): Record<string, ModelData>;
49
- getDepthLimit(): ((params: {
50
- ctx: any;
51
- modelName: string | number | symbol;
52
- operation: (typeof OperationBasic)[number];
53
- }) => number | undefined) | undefined;
54
54
  getInputType(modelName: string, type: string, options: PothosSchemaTypes.InputObjectTypeOptions<any, any>): PothosSchemaTypes.InputObjectRef<any, any>;
55
55
  getInputCreate(modelName: string): PothosSchemaTypes.InputObjectRef<any, any>;
56
56
  getInputUpdate(modelName: string): PothosSchemaTypes.InputObjectRef<any, any>;
@@ -20,26 +20,30 @@ export class PothosDrizzleGenerator {
20
20
  const relations = this.getRelations();
21
21
  const tables = Object.values(relations)
22
22
  .filter((t) => isTable(t.table))
23
- .map(({ name, table, relations }) => {
23
+ .map(({ name: modelName, table, relations }) => {
24
24
  const tableInfo = getConfig(table);
25
- const modelOptions = options?.models?.[name];
25
+ const allOptions = options?.all;
26
+ const modelOptions = options?.models?.[modelName];
26
27
  const columns = tableInfo.columns;
27
28
  //Operations
28
- const operationIncludes = expandOperations(modelOptions?.operations?.include ?? OperationBasic);
29
- const operationExcludes = expandOperations(modelOptions?.operations?.exclude ?? []);
29
+ const operationValue = (modelOptions?.operations ?? allOptions?.operations)?.({ modelName });
30
+ const operationIncludes = expandOperations(operationValue?.include ?? OperationBasic);
31
+ const operationExcludes = expandOperations(operationValue?.exclude ?? []);
30
32
  const operations = operationIncludes.filter((v) => !operationExcludes.includes(v));
31
33
  // Columns filter
32
- const include = options?.models?.[name]?.fields?.include ??
33
- columns.map((c) => c.name);
34
- const exclude = options?.models?.[name]?.fields?.exclude ?? [];
34
+ const columnValue = (modelOptions?.fields ?? allOptions?.fields)?.({
35
+ modelName,
36
+ });
37
+ const include = columnValue?.include ?? columns.map((c) => c.name);
38
+ const exclude = columnValue?.exclude ?? [];
35
39
  const filterColumns = include.filter((name) => !exclude.includes(name));
36
40
  // Input columns filter
37
- const includeInput = options?.models?.[name]?.inputFields?.include ??
38
- columns.map((c) => c.name);
39
- const excludeInput = options?.models?.[name]?.inputFields?.exclude ?? [];
41
+ const inputFieldValue = (modelOptions?.inputFields ?? allOptions?.inputFields)?.({ modelName });
42
+ const includeInput = inputFieldValue?.include ?? columns.map((c) => c.name);
43
+ const excludeInput = inputFieldValue?.exclude ?? [];
40
44
  const filterInputColumns = includeInput.filter((name) => !excludeInput.includes(name));
41
45
  return [
42
- name,
46
+ modelName,
43
47
  {
44
48
  table,
45
49
  columns: columns.filter((c) => filterColumns.includes(c.name)),
@@ -47,11 +51,12 @@ export class PothosDrizzleGenerator {
47
51
  inputColumns: columns.filter((c) => filterInputColumns.includes(c.name)),
48
52
  tableInfo,
49
53
  relations,
50
- executable: modelOptions?.executable,
51
- limit: modelOptions?.limit,
52
- orderBy: modelOptions?.orderBy,
53
- where: modelOptions?.where,
54
- inputData: modelOptions?.inputData,
54
+ executable: modelOptions?.executable ?? allOptions?.executable,
55
+ limit: modelOptions?.limit ?? allOptions?.limit,
56
+ orderBy: modelOptions?.orderBy ?? allOptions?.orderBy,
57
+ where: modelOptions?.where ?? allOptions?.where,
58
+ inputData: modelOptions?.inputData ?? allOptions?.inputData,
59
+ depthLimit: modelOptions?.depthLimit ?? allOptions?.depthLimit,
55
60
  },
56
61
  ];
57
62
  });
@@ -82,10 +87,6 @@ export class PothosDrizzleGenerator {
82
87
  this.tables = tables;
83
88
  return tables;
84
89
  }
85
- getDepthLimit() {
86
- const options = this.builder.options.pothosDrizzleGenerator;
87
- return options?.depthLimit;
88
- }
89
90
  getInputType(modelName, type, options) {
90
91
  if (!this.inputType[modelName])
91
92
  this.inputType[modelName] = {};
@@ -1,6 +1,6 @@
1
- import { SchemaTypes } from "@pothos/core";
2
- import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin";
3
- import type { DBQueryConfigColumns, GetTableViewFieldSelection, RelationsFilter } from "drizzle-orm";
1
+ import type { SchemaTypes } from "@pothos/core";
2
+ import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin.js";
3
+ import type { DBQueryConfigColumns, GetTableViewFieldSelection, RelationsFilter, SchemaEntry } from "drizzle-orm";
4
4
  import type { Operation, OperationBasic } from "./libs/operations.js";
5
5
  import type { PgInsertValue, PgTable } from "drizzle-orm/pg-core";
6
6
  declare global {
@@ -9,15 +9,12 @@ declare global {
9
9
  pothosDrizzleGenerator: PothosDrizzleGeneratorPlugin<Types, T>;
10
10
  }
11
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"]>>;
12
+ type TableNames<Types extends SchemaTypes> = keyof Relations<Types>;
13
+ type Columns<Types extends SchemaTypes, U extends TableNames<Types>> = keyof DBQueryConfigColumns<GetTableViewFieldSelection<Relations<Types>[U]["table"]>>;
14
+ type AnyTable<Types extends SchemaTypes> = Relations<Types>[keyof Relations<Types>]["table"];
15
+ type AnyColumns<Types extends SchemaTypes> = AnyTable<Types> extends infer R ? R extends SchemaEntry ? keyof DBQueryConfigColumns<GetTableViewFieldSelection<R>> : never : never;
14
16
  interface SchemaBuilderOptions<Types extends SchemaTypes> {
15
17
  pothosDrizzleGenerator?: {
16
- depthLimit?: (params: {
17
- ctx: Types["Context"];
18
- modelName: Tables<Types>;
19
- operation: (typeof OperationBasic)[number];
20
- }) => number | undefined;
21
18
  use?: {
22
19
  include: (keyof Relations<Types>)[];
23
20
  exclude?: undefined;
@@ -25,24 +22,91 @@ declare global {
25
22
  exclude: (keyof Relations<Types>)[];
26
23
  include?: undefined;
27
24
  };
25
+ all?: {
26
+ depthLimit?: <U extends TableNames<Types>>(params: {
27
+ ctx: Types["Context"];
28
+ modelName: U;
29
+ operation: (typeof OperationBasic)[number];
30
+ }) => number | undefined;
31
+ fields?: <U extends TableNames<Types>>(params: {
32
+ modelName: U;
33
+ }) => {
34
+ include: AnyColumns<Types>[];
35
+ exclude?: undefined;
36
+ } | {
37
+ exclude: AnyColumns<Types>[];
38
+ include?: undefined;
39
+ } | undefined;
40
+ operations?: <U extends TableNames<Types>>(params: {
41
+ modelName: U;
42
+ }) => {
43
+ include?: Operation[];
44
+ exclude?: Operation[];
45
+ } | undefined;
46
+ executable?: <U extends TableNames<Types>>(params: {
47
+ ctx: Types["Context"];
48
+ modelName: U;
49
+ operation: (typeof OperationBasic)[number];
50
+ }) => boolean | undefined;
51
+ limit?: <U extends TableNames<Types>>(params: {
52
+ ctx: Types["Context"];
53
+ modelName: U;
54
+ operation: (typeof OperationBasic)[number];
55
+ }) => number | undefined;
56
+ orderBy?: <U extends TableNames<Types>>(params: {
57
+ ctx: Types["Context"];
58
+ modelName: U;
59
+ operation: (typeof OperationBasic)[number];
60
+ }) => {
61
+ [P in AnyColumns<Types>]?: "asc" | "desc";
62
+ } | undefined;
63
+ where?: <U extends TableNames<Types>>(params: {
64
+ ctx: Types["Context"];
65
+ modelName: U;
66
+ operation: (typeof OperationBasic)[number];
67
+ }) => RelationsFilter<Relations<Types>[U], Relations<Types>> | undefined;
68
+ inputFields?: <U extends TableNames<Types>>(params: {
69
+ modelName: U;
70
+ }) => {
71
+ include: AnyColumns<Types>[];
72
+ exclude?: undefined;
73
+ } | {
74
+ exclude: AnyColumns<Types>[];
75
+ include?: undefined;
76
+ } | undefined;
77
+ inputData?: <U extends TableNames<Types>>(params: {
78
+ ctx: Types["Context"];
79
+ modelName: U;
80
+ operation: (typeof OperationBasic)[number];
81
+ }) => PgInsertValue<Relations<Types>[U]["table"] extends PgTable ? Relations<Types>[U]["table"] : never, true> | undefined;
82
+ };
28
83
  models?: {
29
- [U in Tables<Types>]?: {
30
- fields?: {
84
+ [U in TableNames<Types>]?: {
85
+ depthLimit?: (params: {
86
+ ctx: Types["Context"];
87
+ modelName: U;
88
+ operation: (typeof OperationBasic)[number];
89
+ }) => number | undefined;
90
+ fields?: (params: {
91
+ modelName: U;
92
+ }) => {
31
93
  include: Columns<Types, U>[];
32
94
  exclude?: undefined;
33
95
  } | {
34
96
  exclude: Columns<Types, U>[];
35
97
  include?: undefined;
36
98
  };
37
- operations?: {
99
+ operations?: <U extends TableNames<Types>>(params: {
100
+ modelName: U;
101
+ }) => {
38
102
  include?: Operation[];
39
103
  exclude?: Operation[];
40
- };
104
+ } | undefined;
41
105
  executable?: (params: {
42
106
  ctx: Types["Context"];
43
107
  modelName: U;
44
108
  operation: (typeof OperationBasic)[number];
45
- }) => boolean;
109
+ }) => boolean | undefined;
46
110
  limit?: (params: {
47
111
  ctx: Types["Context"];
48
112
  modelName: U;
@@ -60,7 +124,9 @@ declare global {
60
124
  modelName: U;
61
125
  operation: (typeof OperationBasic)[number];
62
126
  }) => RelationsFilter<Relations<Types>[U], Relations<Types>> | undefined;
63
- inputFields?: {
127
+ inputFields?: (params: {
128
+ modelName: U;
129
+ }) => {
64
130
  include: Columns<Types, U>[];
65
131
  exclude?: undefined;
66
132
  } | {
@@ -1,5 +1,5 @@
1
1
  import * as p from "drizzle-orm";
2
- import { GraphQLResolveInfo } from "graphql";
2
+ import type { GraphQLResolveInfo } from "graphql";
3
3
  export declare function getQueryDepth(info: GraphQLResolveInfo): number;
4
4
  export declare const getQueryFields: (info: GraphQLResolveInfo) => {
5
5
  [k: string]: boolean;
@@ -9,9 +9,13 @@ function getDepthFromSelection(selection, currentDepth) {
9
9
  return currentDepth;
10
10
  }
11
11
  export function getQueryDepth(info) {
12
+ if (!info.fieldNodes[0])
13
+ return 0;
12
14
  return getDepthFromSelection(info.fieldNodes[0], 0);
13
15
  }
14
16
  export const getQueryFields = (info) => {
17
+ if (!info.fieldNodes[0])
18
+ return {};
15
19
  return Object.fromEntries(Array.from(collectFields(info.schema, info.fragments, info.variableValues, {}, info.fieldNodes[0].selectionSet).fields.keys()).map((v) => [v, true]));
16
20
  };
17
21
  const OperatorMap = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pothos-drizzle-generator",
3
- "version": "0.0.3",
3
+ "version": "0.1.1",
4
4
  "main": "./dist/cjs/index.js",
5
5
  "types": "./dist/cjs/index.d.ts",
6
6
  "exports": {
@@ -16,23 +16,23 @@
16
16
  "lint:fix": "eslint --fix ./src"
17
17
  },
18
18
  "dependencies": {
19
- "@graphql-tools/utils": "10.11.0",
20
- "@pothos/core": "4.10.0",
21
- "@pothos/plugin-drizzle": "0.16.0",
22
- "drizzle-orm": "1.0.0-beta.2-f9236e3",
23
- "graphql": "16.12.0",
24
- "graphql-scalars": "1.25.0"
19
+ "@graphql-tools/utils": "^10.11.0",
20
+ "@pothos/core": "^4.10.0",
21
+ "@pothos/plugin-drizzle": "^0.16.0",
22
+ "drizzle-orm": "1.0.0-beta.3-bd1ec83",
23
+ "graphql": "^16.12.0",
24
+ "graphql-scalars": "^1.25.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@types/graphql": "14.5.0",
28
- "@eslint/js": "9.39.2",
29
- "eslint": "9.39.2",
27
+ "@eslint/js": "^9.39.2",
28
+ "@types/graphql": "^14.5.0",
29
+ "cpy-cli": "^6.0.0",
30
+ "eslint": "^9.39.2",
30
31
  "eslint-config-prettier": "^10.1.8",
31
32
  "eslint-import-resolver-typescript": "^4.4.4",
32
33
  "eslint-plugin-import": "^2.32.0",
33
- "typescript": "5.9.3",
34
- "typescript-eslint": "^8.49.0",
35
- "cpy-cli": "^6.0.0"
34
+ "typescript": "^5.9.3",
35
+ "typescript-eslint": "^8.50.0"
36
36
  },
37
37
  "repository": "https://github.com/node-libraries/pothos-drizzle-generator",
38
38
  "author": "SoraKumo <info@croud.jp>",