pothos-drizzle-generator 0.1.6 → 0.1.7

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.
@@ -0,0 +1,7 @@
1
+ import { BasePlugin, type BuildCache, type SchemaTypes } from "@pothos/core";
2
+ import { DrizzleGenerator } from "./generator.js";
3
+ export declare class PothosDrizzleGenerator<Types extends SchemaTypes, T extends object = object> extends BasePlugin<Types, T> {
4
+ generator: DrizzleGenerator;
5
+ constructor(buildCache: BuildCache<Types>, name: keyof PothosSchemaTypes.Plugins<Types>);
6
+ beforeBuild(): void;
7
+ }
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PothosDrizzleGeneratorPlugin = void 0;
3
+ exports.PothosDrizzleGenerator = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
5
  const core_1 = require("@pothos/core");
6
6
  const drizzle_orm_1 = require("drizzle-orm");
7
7
  const generator_js_1 = require("./generator.js");
8
8
  const utils_js_1 = require("./libs/utils.js");
9
- class PothosDrizzleGeneratorPlugin extends core_1.BasePlugin {
9
+ class PothosDrizzleGenerator extends core_1.BasePlugin {
10
10
  generator;
11
11
  constructor(buildCache, name) {
12
12
  super(buildCache, name);
13
- this.generator = new generator_js_1.PothosDrizzleGenerator(this.builder);
13
+ this.generator = new generator_js_1.DrizzleGenerator(this.builder);
14
14
  }
15
15
  beforeBuild() {
16
16
  const generator = this.generator;
@@ -87,27 +87,75 @@ class PothosDrizzleGeneratorPlugin extends core_1.BasePlugin {
87
87
  if (!operations.includes(operation))
88
88
  return [];
89
89
  const inputWhere = generator.getInputWhere(modelName);
90
- return [
91
- `${relayName}Count`,
92
- t.relatedCount(relayName, {
93
- args: { where: t.arg({ type: inputWhere }) },
94
- where: (args, ctx) => {
95
- if (executable?.({
96
- modelName,
97
- ctx,
98
- operation,
99
- }) === false) {
100
- throw new Error("No permission");
101
- }
102
- const p = {
103
- where: where?.({ modelName, ctx, operation }),
104
- };
105
- return (0, utils_js_1.createWhereQuery)(relay.targetTable, {
106
- AND: [structuredClone(args.where), p.where].filter((v) => v),
107
- });
108
- },
109
- }),
110
- ];
90
+ if (relay.throughTable) {
91
+ return [
92
+ `${relayName}Count`,
93
+ t.field({
94
+ type: "Int",
95
+ nullable: false,
96
+ args: { where: t.arg({ type: inputWhere }) },
97
+ extensions: {
98
+ pothosDrizzleSelect: (args, ctx) => {
99
+ if (executable?.({
100
+ modelName,
101
+ ctx,
102
+ operation,
103
+ }) === false) {
104
+ throw new Error("No permission");
105
+ }
106
+ const p = {
107
+ where: where?.({
108
+ modelName,
109
+ ctx,
110
+ operation,
111
+ }),
112
+ };
113
+ return {
114
+ columns: {},
115
+ extras: {
116
+ [`${relayName}Count`]: (table) => {
117
+ const client = generator.getClient(ctx);
118
+ return client
119
+ .select({ count: (0, drizzle_orm_1.sql) `count(*)` })
120
+ .from(relay.targetTable)
121
+ .leftJoin(relay.throughTable, (0, drizzle_orm_1.and)(...relay.targetColumns.map((v, index) => (0, drizzle_orm_1.eq)(relay.through.target[index]._.column, v))))
122
+ .where((0, drizzle_orm_1.and)(...relay.sourceColumns.map((v, index) => (0, drizzle_orm_1.eq)(relay.through.source[index]._.column, table[v.name])), (0, utils_js_1.createWhereQuery)(relay.targetTable, {
123
+ AND: [
124
+ structuredClone(args.where),
125
+ p.where,
126
+ ].filter((v) => v),
127
+ })));
128
+ },
129
+ },
130
+ };
131
+ },
132
+ },
133
+ }),
134
+ ];
135
+ }
136
+ else {
137
+ return [
138
+ `${relayName}Count`,
139
+ t.relatedCount(relayName, {
140
+ args: { where: t.arg({ type: inputWhere }) },
141
+ where: (args, ctx) => {
142
+ if (executable?.({
143
+ modelName,
144
+ ctx,
145
+ operation,
146
+ }) === false) {
147
+ throw new Error("No permission");
148
+ }
149
+ const p = {
150
+ where: where?.({ modelName, ctx, operation }),
151
+ };
152
+ return (0, utils_js_1.createWhereQuery)(relay.targetTable, {
153
+ AND: [structuredClone(args.where), p.where].filter((v) => v),
154
+ });
155
+ },
156
+ }),
157
+ ];
158
+ }
111
159
  });
112
160
  return Object.fromEntries([
113
161
  ...relayCount,
@@ -403,4 +451,4 @@ class PothosDrizzleGeneratorPlugin extends core_1.BasePlugin {
403
451
  }
404
452
  }
405
453
  }
406
- exports.PothosDrizzleGeneratorPlugin = PothosDrizzleGeneratorPlugin;
454
+ exports.PothosDrizzleGenerator = PothosDrizzleGenerator;
@@ -40,13 +40,14 @@ type ModelData = {
40
40
  operation: (typeof OperationBasic)[number];
41
41
  }) => number | undefined) | undefined;
42
42
  };
43
- export declare class PothosDrizzleGenerator {
43
+ export declare class DrizzleGenerator {
44
44
  enums: Record<string, PothosSchemaTypes.EnumRef<any, any>>;
45
45
  inputOperators: Record<string, PothosSchemaTypes.InputObjectRef<any, any>>;
46
46
  inputType: Record<string, Record<string, PothosSchemaTypes.InputObjectRef<any, any>>>;
47
47
  tables?: Record<string, ModelData>;
48
48
  builder: PothosSchemaTypes.SchemaBuilder<any>;
49
49
  constructor(builder: PothosSchemaTypes.SchemaBuilder<any>);
50
+ getTableConfig(): (param: Parameters<typeof getTableConfig>[0] | SchemaEntry) => ReturnType<typeof getTableConfig>;
50
51
  createTableInfo(): Record<string, ModelData>;
51
52
  getClient(ctx: any): DrizzleClient;
52
53
  getRelations(): AnyRelations;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PothosDrizzleGenerator = void 0;
3
+ exports.DrizzleGenerator = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
5
  const drizzle_orm_1 = require("drizzle-orm");
6
6
  const graphql_scalars_1 = require("graphql-scalars");
7
7
  const operations_js_1 = require("./libs/operations.js");
8
8
  const utils_js_1 = require("./libs/utils.js");
9
- class PothosDrizzleGenerator {
9
+ class DrizzleGenerator {
10
10
  enums = {};
11
11
  inputOperators = {};
12
12
  inputType = {};
@@ -16,10 +16,13 @@ class PothosDrizzleGenerator {
16
16
  this.builder = builder;
17
17
  this.createInputType();
18
18
  }
19
+ getTableConfig() {
20
+ const drizzleOption = this.builder.options.drizzle;
21
+ return drizzleOption.getTableConfig;
22
+ }
19
23
  createTableInfo() {
20
24
  const options = this.builder.options.pothosDrizzleGenerator;
21
- const drizzleOption = this.builder.options.drizzle;
22
- const getConfig = drizzleOption.getTableConfig;
25
+ const getConfig = this.getTableConfig();
23
26
  const relations = this.getRelations();
24
27
  const tables = Object.values(relations)
25
28
  .filter((t) => (0, drizzle_orm_1.isTable)(t.table))
@@ -229,4 +232,4 @@ class PothosDrizzleGenerator {
229
232
  return isArray ? [result] : result;
230
233
  }
231
234
  }
232
- exports.PothosDrizzleGenerator = PothosDrizzleGenerator;
235
+ exports.DrizzleGenerator = DrizzleGenerator;
@@ -1,12 +1,12 @@
1
1
  import type { SchemaTypes } from "@pothos/core";
2
- import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin.js";
2
+ import type { PothosDrizzleGenerator } from "./PothosDrizzleGenerator.js";
3
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 {
7
7
  export namespace PothosSchemaTypes {
8
8
  interface Plugins<Types extends SchemaTypes, T extends object = object> {
9
- pothosDrizzleGenerator: PothosDrizzleGeneratorPlugin<Types, T>;
9
+ pothosDrizzleGenerator: PothosDrizzleGenerator<Types, T>;
10
10
  }
11
11
  type Relations<Types extends SchemaTypes> = Types["DrizzleRelations"];
12
12
  type TableNames<Types extends SchemaTypes> = keyof Relations<Types>;
package/dist/cjs/index.js CHANGED
@@ -18,12 +18,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  const core_1 = __importDefault(require("@pothos/core"));
21
- const PothosDrizzleGeneratorPlugin_js_1 = require("./PothosDrizzleGeneratorPlugin.js");
21
+ const PothosDrizzleGenerator_js_1 = require("./PothosDrizzleGenerator.js");
22
22
  __exportStar(require("./global-types.js"), exports);
23
23
  __exportStar(require("./libs/operations.js"), exports);
24
24
  const pluginName = "pothosDrizzleGenerator";
25
25
  const allowPluginReRegistration = core_1.default.allowPluginReRegistration;
26
26
  core_1.default.allowPluginReRegistration = true;
27
- core_1.default.registerPlugin(pluginName, PothosDrizzleGeneratorPlugin_js_1.PothosDrizzleGeneratorPlugin);
27
+ core_1.default.registerPlugin(pluginName, PothosDrizzleGenerator_js_1.PothosDrizzleGenerator);
28
28
  core_1.default.allowPluginReRegistration = allowPluginReRegistration;
29
29
  exports.default = pluginName;
@@ -0,0 +1,7 @@
1
+ import { BasePlugin, type BuildCache, type SchemaTypes } from "@pothos/core";
2
+ import { DrizzleGenerator } from "./generator.js";
3
+ export declare class PothosDrizzleGenerator<Types extends SchemaTypes, T extends object = object> extends BasePlugin<Types, T> {
4
+ generator: DrizzleGenerator;
5
+ constructor(buildCache: BuildCache<Types>, name: keyof PothosSchemaTypes.Plugins<Types>);
6
+ beforeBuild(): void;
7
+ }
@@ -1,13 +1,13 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { BasePlugin } from "@pothos/core";
3
- import { sql } from "drizzle-orm";
4
- import { PothosDrizzleGenerator } from "./generator.js";
3
+ import { and, eq, sql } from "drizzle-orm";
4
+ import { DrizzleGenerator } from "./generator.js";
5
5
  import { createWhereQuery, getQueryDepth } from "./libs/utils.js";
6
- export class PothosDrizzleGeneratorPlugin extends BasePlugin {
6
+ export class PothosDrizzleGenerator extends BasePlugin {
7
7
  generator;
8
8
  constructor(buildCache, name) {
9
9
  super(buildCache, name);
10
- this.generator = new PothosDrizzleGenerator(this.builder);
10
+ this.generator = new DrizzleGenerator(this.builder);
11
11
  }
12
12
  beforeBuild() {
13
13
  const generator = this.generator;
@@ -84,27 +84,75 @@ export class PothosDrizzleGeneratorPlugin extends BasePlugin {
84
84
  if (!operations.includes(operation))
85
85
  return [];
86
86
  const inputWhere = generator.getInputWhere(modelName);
87
- return [
88
- `${relayName}Count`,
89
- t.relatedCount(relayName, {
90
- args: { where: t.arg({ type: inputWhere }) },
91
- where: (args, ctx) => {
92
- if (executable?.({
93
- modelName,
94
- ctx,
95
- operation,
96
- }) === false) {
97
- throw new Error("No permission");
98
- }
99
- const p = {
100
- where: where?.({ modelName, ctx, operation }),
101
- };
102
- return createWhereQuery(relay.targetTable, {
103
- AND: [structuredClone(args.where), p.where].filter((v) => v),
104
- });
105
- },
106
- }),
107
- ];
87
+ if (relay.throughTable) {
88
+ return [
89
+ `${relayName}Count`,
90
+ t.field({
91
+ type: "Int",
92
+ nullable: false,
93
+ args: { where: t.arg({ type: inputWhere }) },
94
+ extensions: {
95
+ pothosDrizzleSelect: (args, ctx) => {
96
+ if (executable?.({
97
+ modelName,
98
+ ctx,
99
+ operation,
100
+ }) === false) {
101
+ throw new Error("No permission");
102
+ }
103
+ const p = {
104
+ where: where?.({
105
+ modelName,
106
+ ctx,
107
+ operation,
108
+ }),
109
+ };
110
+ return {
111
+ columns: {},
112
+ extras: {
113
+ [`${relayName}Count`]: (table) => {
114
+ const client = generator.getClient(ctx);
115
+ return client
116
+ .select({ count: sql `count(*)` })
117
+ .from(relay.targetTable)
118
+ .leftJoin(relay.throughTable, and(...relay.targetColumns.map((v, index) => eq(relay.through.target[index]._.column, v))))
119
+ .where(and(...relay.sourceColumns.map((v, index) => eq(relay.through.source[index]._.column, table[v.name])), createWhereQuery(relay.targetTable, {
120
+ AND: [
121
+ structuredClone(args.where),
122
+ p.where,
123
+ ].filter((v) => v),
124
+ })));
125
+ },
126
+ },
127
+ };
128
+ },
129
+ },
130
+ }),
131
+ ];
132
+ }
133
+ else {
134
+ return [
135
+ `${relayName}Count`,
136
+ t.relatedCount(relayName, {
137
+ args: { where: t.arg({ type: inputWhere }) },
138
+ where: (args, ctx) => {
139
+ if (executable?.({
140
+ modelName,
141
+ ctx,
142
+ operation,
143
+ }) === false) {
144
+ throw new Error("No permission");
145
+ }
146
+ const p = {
147
+ where: where?.({ modelName, ctx, operation }),
148
+ };
149
+ return createWhereQuery(relay.targetTable, {
150
+ AND: [structuredClone(args.where), p.where].filter((v) => v),
151
+ });
152
+ },
153
+ }),
154
+ ];
155
+ }
108
156
  });
109
157
  return Object.fromEntries([
110
158
  ...relayCount,
@@ -40,13 +40,14 @@ type ModelData = {
40
40
  operation: (typeof OperationBasic)[number];
41
41
  }) => number | undefined) | undefined;
42
42
  };
43
- export declare class PothosDrizzleGenerator {
43
+ export declare class DrizzleGenerator {
44
44
  enums: Record<string, PothosSchemaTypes.EnumRef<any, any>>;
45
45
  inputOperators: Record<string, PothosSchemaTypes.InputObjectRef<any, any>>;
46
46
  inputType: Record<string, Record<string, PothosSchemaTypes.InputObjectRef<any, any>>>;
47
47
  tables?: Record<string, ModelData>;
48
48
  builder: PothosSchemaTypes.SchemaBuilder<any>;
49
49
  constructor(builder: PothosSchemaTypes.SchemaBuilder<any>);
50
+ getTableConfig(): (param: Parameters<typeof getTableConfig>[0] | SchemaEntry) => ReturnType<typeof getTableConfig>;
50
51
  createTableInfo(): Record<string, ModelData>;
51
52
  getClient(ctx: any): DrizzleClient;
52
53
  getRelations(): AnyRelations;
@@ -3,7 +3,7 @@ import { isTable, } from "drizzle-orm";
3
3
  import { BigIntResolver, ByteResolver, DateResolver, DateTimeResolver, HexadecimalResolver, JSONResolver, } from "graphql-scalars";
4
4
  import { expandOperations, OperationBasic } from "./libs/operations.js";
5
5
  import { createInputOperator } from "./libs/utils.js";
6
- export class PothosDrizzleGenerator {
6
+ export class DrizzleGenerator {
7
7
  enums = {};
8
8
  inputOperators = {};
9
9
  inputType = {};
@@ -13,10 +13,13 @@ export class PothosDrizzleGenerator {
13
13
  this.builder = builder;
14
14
  this.createInputType();
15
15
  }
16
+ getTableConfig() {
17
+ const drizzleOption = this.builder.options.drizzle;
18
+ return drizzleOption.getTableConfig;
19
+ }
16
20
  createTableInfo() {
17
21
  const options = this.builder.options.pothosDrizzleGenerator;
18
- const drizzleOption = this.builder.options.drizzle;
19
- const getConfig = drizzleOption.getTableConfig;
22
+ const getConfig = this.getTableConfig();
20
23
  const relations = this.getRelations();
21
24
  const tables = Object.values(relations)
22
25
  .filter((t) => isTable(t.table))
@@ -1,12 +1,12 @@
1
1
  import type { SchemaTypes } from "@pothos/core";
2
- import type { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin.js";
2
+ import type { PothosDrizzleGenerator } from "./PothosDrizzleGenerator.js";
3
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 {
7
7
  export namespace PothosSchemaTypes {
8
8
  interface Plugins<Types extends SchemaTypes, T extends object = object> {
9
- pothosDrizzleGenerator: PothosDrizzleGeneratorPlugin<Types, T>;
9
+ pothosDrizzleGenerator: PothosDrizzleGenerator<Types, T>;
10
10
  }
11
11
  type Relations<Types extends SchemaTypes> = Types["DrizzleRelations"];
12
12
  type TableNames<Types extends SchemaTypes> = keyof Relations<Types>;
package/dist/esm/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import SchemaBuilder from "@pothos/core";
2
- import { PothosDrizzleGeneratorPlugin } from "./PothosDrizzleGeneratorPlugin.js";
2
+ import { PothosDrizzleGenerator } from "./PothosDrizzleGenerator.js";
3
3
  export * from "./global-types.js";
4
4
  export * from "./libs/operations.js";
5
5
  const pluginName = "pothosDrizzleGenerator";
6
6
  const allowPluginReRegistration = SchemaBuilder.allowPluginReRegistration;
7
7
  SchemaBuilder.allowPluginReRegistration = true;
8
- SchemaBuilder.registerPlugin(pluginName, PothosDrizzleGeneratorPlugin);
8
+ SchemaBuilder.registerPlugin(pluginName, PothosDrizzleGenerator);
9
9
  SchemaBuilder.allowPluginReRegistration = allowPluginReRegistration;
10
10
  export default pluginName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pothos-drizzle-generator",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "main": "./dist/cjs/index.js",
5
5
  "types": "./dist/cjs/index.d.ts",
6
6
  "exports": {
@@ -1,7 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
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
- }