@pothos/plugin-drizzle 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.
Files changed (44) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dts/drizzle-field-builder.d.ts.map +1 -1
  3. package/dts/model-loader.d.ts +2 -0
  4. package/dts/model-loader.d.ts.map +1 -1
  5. package/dts/types.d.ts +12 -6
  6. package/dts/types.d.ts.map +1 -1
  7. package/dts/utils/config.d.ts +2 -0
  8. package/dts/utils/config.d.ts.map +1 -1
  9. package/esm/drizzle-field-builder.d.ts.map +1 -1
  10. package/esm/model-loader.d.ts +2 -0
  11. package/esm/model-loader.d.ts.map +1 -1
  12. package/esm/src/drizzle-field-builder.js +6 -5
  13. package/esm/src/drizzle-field-builder.js.map +1 -1
  14. package/esm/src/field-builder.js +2 -2
  15. package/esm/src/field-builder.js.map +1 -1
  16. package/esm/src/model-loader.js +4 -2
  17. package/esm/src/model-loader.js.map +1 -1
  18. package/esm/src/schema-builder.js +14 -13
  19. package/esm/src/schema-builder.js.map +1 -1
  20. package/esm/src/types.js.map +1 -1
  21. package/esm/src/utils/config.js +15 -2
  22. package/esm/src/utils/config.js.map +1 -1
  23. package/esm/types.d.ts +12 -6
  24. package/esm/types.d.ts.map +1 -1
  25. package/esm/utils/config.d.ts +2 -0
  26. package/esm/utils/config.d.ts.map +1 -1
  27. package/lib/src/drizzle-field-builder.js +6 -5
  28. package/lib/src/drizzle-field-builder.js.map +1 -1
  29. package/lib/src/field-builder.js +2 -2
  30. package/lib/src/field-builder.js.map +1 -1
  31. package/lib/src/model-loader.js +3 -1
  32. package/lib/src/model-loader.js.map +1 -1
  33. package/lib/src/schema-builder.js +14 -13
  34. package/lib/src/schema-builder.js.map +1 -1
  35. package/lib/src/types.js.map +1 -1
  36. package/lib/src/utils/config.js +26 -5
  37. package/lib/src/utils/config.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/drizzle-field-builder.ts +10 -6
  40. package/src/field-builder.ts +3 -4
  41. package/src/model-loader.ts +6 -2
  42. package/src/schema-builder.ts +8 -7
  43. package/src/types.ts +16 -3
  44. package/src/utils/config.ts +29 -3
@@ -2,14 +2,24 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- Object.defineProperty(exports, "getSchemaConfig", {
6
- enumerable: true,
7
- get: function() {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ getClient: function() {
13
+ return getClient;
14
+ },
15
+ getSchemaConfig: function() {
8
16
  return getSchemaConfig;
9
17
  }
10
18
  });
11
19
  const _core = require("@pothos/core");
12
- const cache = (0, _core.createContextCache)((config)=>{
20
+ const configCache = (0, _core.createContextCache)((builder)=>{
21
+ var _builder_options_drizzle_schema;
22
+ const config = (_builder_options_drizzle_schema = builder.options.drizzle.schema) !== null && _builder_options_drizzle_schema !== void 0 ? _builder_options_drizzle_schema : builder.options.drizzle.client._;
13
23
  const dbToSchema = Object.values(config.schema).reduce((acc, table)=>{
14
24
  acc[table.dbName] = table;
15
25
  return acc;
@@ -19,8 +29,19 @@ const cache = (0, _core.createContextCache)((config)=>{
19
29
  ...config
20
30
  };
21
31
  });
32
+ const clientCache = (0, _core.createContextCache)((builder)=>{
33
+ const clientConfig = builder.options.drizzle.client;
34
+ const getClient = typeof clientConfig === 'function' ? (0, _core.createContextCache)((ctx)=>clientConfig(ctx)) : (_ctx)=>clientConfig;
35
+ return (0, _core.createContextCache)((context)=>{
36
+ const client = getClient(context);
37
+ return client;
38
+ });
39
+ });
22
40
  function getSchemaConfig(builder) {
23
- return cache(builder.options.drizzle.client._);
41
+ return configCache(builder);
42
+ }
43
+ function getClient(builder, context) {
44
+ return clientCache(builder)(context);
24
45
  }
25
46
 
26
47
  //# sourceMappingURL=config.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/config.ts"],"names":["getSchemaConfig","cache","createContextCache","config","dbToSchema","Object","values","schema","reduce","acc","table","dbName","builder","options","drizzle","client","_"],"mappings":";;;;+BA2BgBA;;;eAAAA;;;sBA3BqC;AAUrD,MAAMC,QAAQC,IAAAA,wBAAkB,EAC9B,CAACC;IACC,MAAMC,aAAaC,OAAOC,MAAM,CAACH,OAAOI,MAAM,EAAEC,MAAM,CACpD,CAACC,KAAKC;QACJD,GAAG,CAACC,MAAMC,MAAM,CAAC,GAAGD;QACpB,OAAOD;IACT,GACA,CAAC;IAGH,OAAO;QACLL;QACA,GAAGD,MAAM;IACX;AACF;AAGK,SAASH,gBACdY,OAA+C;IAE/C,OAAOX,MAAMW,QAAQC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,CAAC;AAC/C"}
1
+ {"version":3,"sources":["../../../src/utils/config.ts"],"names":["getClient","getSchemaConfig","configCache","createContextCache","builder","config","options","drizzle","schema","client","_","dbToSchema","Object","values","reduce","acc","table","dbName","clientCache","clientConfig","ctx","_ctx","context"],"mappings":";;;;;;;;;;;IAoDgBA,SAAS;eAATA;;IANAC,eAAe;eAAfA;;;sBA9CqC;AAWrD,MAAMC,cAAcC,IAAAA,wBAAkB,EACpC,CAACC;QACiBA;IAAhB,MAAMC,SAAUD,CAAAA,kCAAAA,QAAQE,OAAO,CAACC,OAAO,CAACC,MAAM,cAA9BJ,6CAAAA,kCACd,AAACA,QAAQE,OAAO,CAACC,OAAO,CAACE,MAAM,CAC5BC,CAAC;IAEN,MAAMC,aAAaC,OAAOC,MAAM,CAACR,OAAOG,MAAM,EAAEM,MAAM,CACpD,CAACC,KAAKC;QACJD,GAAG,CAACC,MAAMC,MAAM,CAAC,GAAGD;QACpB,OAAOD;IACT,GACA,CAAC;IAGH,OAAO;QACLJ;QACA,GAAGN,MAAM;IACX;AACF;AAGF,MAAMa,cAAcf,IAAAA,wBAAkB,EAAC,CAACC;IACtC,MAAMe,eAAef,QAAQE,OAAO,CAACC,OAAO,CAACE,MAAM;IACnD,MAAMT,YACJ,OAAOmB,iBAAiB,aACpBhB,IAAAA,wBAAkB,EAAC,CAACiB,MAAQD,aAAaC,QACzC,CAACC,OAAiBF;IAExB,OAAOhB,IAAAA,wBAAkB,EAAC,CAACmB;QACzB,MAAMb,SAAST,UAAUsB;QAEzB,OAAOb;IACT;AACF;AAEO,SAASR,gBACdG,OAA+C;IAE/C,OAAOF,YAAYE;AACrB;AAEO,SAASJ,UACdI,OAA+C,EAC/CkB,OAAe;IAEf,OAAOJ,YAAYd,SAAkBkB;AACvC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pothos/plugin-drizzle",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "A Pothos plugin for drizzle",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./dts/index.d.ts",
@@ -46,8 +46,8 @@
46
46
  "graphql-scalars": "^1.23.0",
47
47
  "graphql-tag": "^2.12.6",
48
48
  "postgres": "^3.4.4",
49
- "@pothos/plugin-add-graphql": "4.1.0",
50
49
  "@pothos/core": "4.1.0",
50
+ "@pothos/plugin-add-graphql": "4.1.0",
51
51
  "@pothos/plugin-relay": "4.1.0",
52
52
  "@pothos/plugin-scope-auth": "4.1.0",
53
53
  "@pothos/plugin-with-input": "4.1.0",
@@ -32,6 +32,7 @@ import type {
32
32
  TypesForRelation,
33
33
  VariantFieldOptions,
34
34
  } from './types';
35
+ import { getSchemaConfig } from './utils/config';
35
36
  import {
36
37
  drizzleCursorConnectionQuery,
37
38
  getCursorFormatter,
@@ -195,10 +196,12 @@ export class DrizzleObjectFieldBuilder<
195
196
  connectionOptions = {},
196
197
  edgeOptions = {},
197
198
  ) {
198
- const relationField =
199
- this.builder.options.drizzle.client._.schema?.[this.table].relations[name as string];
200
- const relatedModel =
201
- this.builder.options.drizzle.client._.schema?.[relationField?.referencedTableName!]!;
199
+ const relationField = getSchemaConfig(this.builder).schema?.[this.table].relations[
200
+ name as string
201
+ ];
202
+ const relatedModel = getSchemaConfig(this.builder).schema?.[
203
+ relationField?.referencedTableName!
204
+ ]!;
202
205
 
203
206
  if (!relationField) {
204
207
  throw new PothosSchemaError(
@@ -381,8 +384,9 @@ export class DrizzleObjectFieldBuilder<
381
384
  >
382
385
  ): FieldRef<Types, TypesForRelation<Types, TableConfig['relations'][Field]>, 'Object'> {
383
386
  const [options = {} as never] = allArgs;
384
- const relationField =
385
- this.builder.options.drizzle.client._.schema?.[this.table].relations[name as string];
387
+ const relationField = getSchemaConfig(this.builder).schema?.[this.table].relations[
388
+ name as string
389
+ ];
386
390
 
387
391
  if (!relationField) {
388
392
  throw new PothosSchemaError(
@@ -125,10 +125,9 @@ fieldBuilderProto.drizzleConnection = function drizzleConnection<
125
125
  context: {},
126
126
  info: GraphQLResolveInfo,
127
127
  ) => {
128
- const drizzleModel =
129
- this.builder.options.drizzle.client._.schema?.[
130
- typeof type === 'string' ? type : (ref as DrizzleRef<SchemaTypes>).tableName
131
- ]!;
128
+ const drizzleModel = getSchemaConfig(this.builder).schema?.[
129
+ typeof type === 'string' ? type : (ref as DrizzleRef<SchemaTypes>).tableName
130
+ ]!;
132
131
 
133
132
  return resolveDrizzleCursorConnection(
134
133
  drizzleModel,
@@ -1,7 +1,8 @@
1
1
  import { type SchemaTypes, createContextCache } from '@pothos/core';
2
2
  import { type Column, type TableRelationalConfig, inArray, sql } from 'drizzle-orm';
3
3
  import type { GraphQLResolveInfo } from 'graphql';
4
- import { type PothosDrizzleSchemaConfig, getSchemaConfig } from './utils/config';
4
+ import type { DrizzleClient } from './types';
5
+ import { type PothosDrizzleSchemaConfig, getClient, getSchemaConfig } from './utils/config';
5
6
  import { cacheKey, setLoaderMappings } from './utils/loader-map';
6
7
  import { selectionStateFromInfo, stateFromInfo } from './utils/map-query';
7
8
  import {
@@ -35,6 +36,7 @@ export class ModelLoader {
35
36
  table: TableRelationalConfig;
36
37
  columns: Column[];
37
38
  selectSQL;
39
+ client: DrizzleClient;
38
40
 
39
41
  constructor(
40
42
  context: object,
@@ -52,6 +54,8 @@ export class ModelLoader {
52
54
  this.columns.length > 1
53
55
  ? sql`(${sql.join(this.columns, sql`, `)})`
54
56
  : this.columns[0].getSQL();
57
+
58
+ this.client = getClient(builder, context);
55
59
  }
56
60
 
57
61
  static forModel<Types extends SchemaTypes>(
@@ -166,7 +170,7 @@ export class ModelLoader {
166
170
  nextTick.promise
167
171
  .then(() => {
168
172
  const api = (
169
- this.builder.options.drizzle.client.query as Record<
173
+ this.client.query as Record<
170
174
  string,
171
175
  { findMany: (...args: unknown[]) => Promise<Record<string, unknown>[]> }
172
176
  >
@@ -16,6 +16,7 @@ import { ModelLoader } from './model-loader';
16
16
  import { DrizzleNodeRef } from './node-ref';
17
17
  import { DrizzleObjectRef } from './object-ref';
18
18
  import type { DrizzleGraphQLInputExtensions, DrizzleNodeOptions } from './types';
19
+ import { getSchemaConfig } from './utils/config';
19
20
  import { getColumnParser, getColumnSerializer } from './utils/cursors';
20
21
  import { getRefFromModel } from './utils/refs';
21
22
 
@@ -35,7 +36,7 @@ schemaBuilderProto.drizzleObject = function drizzleObject(table, { select, field
35
36
  extensions: {
36
37
  ...options.extensions,
37
38
  pothosDrizzleModel: table,
38
- pothosDrizzleTable: this.options.drizzle.client._.schema?.[table],
39
+ pothosDrizzleTable: getSchemaConfig(this).schema?.[table],
39
40
  pothosDrizzleSelect: select ?? {},
40
41
  pothosDrizzleLoader: ModelLoader.forModel(table, this),
41
42
  },
@@ -63,7 +64,7 @@ schemaBuilderProto.drizzleInterface = function drizzleInterface(
63
64
  extensions: {
64
65
  ...options.extensions,
65
66
  pothosDrizzleModel: table,
66
- pothosDrizzleTable: this.options.drizzle.client._.schema?.[table],
67
+ pothosDrizzleTable: getSchemaConfig(this).schema?.[table],
67
68
  pothosDrizzleSelect: select ?? {},
68
69
  pothosDrizzleLoader: ModelLoader.forModel(table, this),
69
70
  },
@@ -88,7 +89,7 @@ schemaBuilderProto.drizzleNode = function drizzleNode(
88
89
  ...options
89
90
  }: DrizzleNodeOptions<SchemaTypes, keyof SchemaTypes['DrizzleRelationSchema'], {}, {}, []>,
90
91
  ) {
91
- const tableConfig = this.options.drizzle.client._.schema![table];
92
+ const tableConfig = getSchemaConfig(this).schema![table];
92
93
  const idColumn = typeof column === 'function' ? column(tableConfig.columns) : column;
93
94
  const idColumns = Array.isArray(idColumn) ? idColumn : [idColumn];
94
95
  const interfaceRef = this.nodeInterfaceRef?.();
@@ -214,7 +215,7 @@ schemaBuilderProto.drizzleGraphQLOrderBy = function drizzleGraphQLOrderBy(
214
215
  drizzleGraphQL: {
215
216
  inputType: 'orderBy',
216
217
  table,
217
- tableConfig: this.options.drizzle.client._.schema?.[table]!,
218
+ tableConfig: getSchemaConfig(this).schema?.[table]!,
218
219
  } satisfies DrizzleGraphQLInputExtensions,
219
220
  },
220
221
  });
@@ -240,7 +241,7 @@ schemaBuilderProto.drizzleGraphQLFilters = function drizzleGraphQLFilters(
240
241
  drizzleGraphQL: {
241
242
  inputType: 'filters',
242
243
  table,
243
- tableConfig: this.options.drizzle.client._.schema?.[table]!,
244
+ tableConfig: getSchemaConfig(this).schema?.[table]!,
244
245
  } satisfies DrizzleGraphQLInputExtensions,
245
246
  },
246
247
  });
@@ -266,7 +267,7 @@ schemaBuilderProto.drizzleGraphQLInsert = function drizzleGraphQLInsert(
266
267
  drizzleGraphQL: {
267
268
  inputType: 'insert',
268
269
  table,
269
- tableConfig: this.options.drizzle.client._.schema?.[table]!,
270
+ tableConfig: getSchemaConfig(this).schema?.[table]!,
270
271
  } satisfies DrizzleGraphQLInputExtensions,
271
272
  },
272
273
  });
@@ -292,7 +293,7 @@ schemaBuilderProto.drizzleGraphQLUpdate = function drizzleGraphQLUpdate(
292
293
  drizzleGraphQL: {
293
294
  inputType: 'update',
294
295
  table,
295
- tableConfig: this.options.drizzle.client._.schema?.[table]!,
296
+ tableConfig: getSchemaConfig(this).schema?.[table]!,
296
297
  } satisfies DrizzleGraphQLInputExtensions,
297
298
  },
298
299
  });
package/src/types.ts CHANGED
@@ -41,11 +41,24 @@ import type { DrizzleRef } from './interface-ref';
41
41
  import type { IndirectInclude } from './utils/map-query';
42
42
  import type { SelectionMap } from './utils/selections';
43
43
 
44
- export interface DrizzlePluginOptions<_Types extends SchemaTypes> {
45
- client: { _: Partial<RelationalSchemaConfig<TablesRelationalConfig>>; query: {} };
44
+ export type DrizzleClient = {
45
+ _: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
46
+ query: {};
47
+ };
48
+
49
+ export type DrizzlePluginOptions<Types extends SchemaTypes> = {
46
50
  maxConnectionSize?: number;
47
51
  defaultConnectionSize?: number;
48
- }
52
+ } & (
53
+ | {
54
+ client: DrizzleClient;
55
+ schema?: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
56
+ }
57
+ | {
58
+ client: (ctx: Types['Context']) => DrizzleClient;
59
+ schema: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
60
+ }
61
+ );
49
62
 
50
63
  export const drizzleTableName = Symbol.for('Pothos.drizzleTableName');
51
64
 
@@ -4,12 +4,17 @@ import type {
4
4
  TableRelationalConfig,
5
5
  TablesRelationalConfig,
6
6
  } from 'drizzle-orm';
7
+ import type { DrizzleClient } from '../types';
7
8
 
8
9
  export interface PothosDrizzleSchemaConfig extends RelationalSchemaConfig<TablesRelationalConfig> {
9
10
  dbToSchema: Record<string, TableRelationalConfig>;
10
11
  }
11
- const cache = createContextCache(
12
- (config: RelationalSchemaConfig<TablesRelationalConfig>): PothosDrizzleSchemaConfig => {
12
+ const configCache = createContextCache(
13
+ (builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>): PothosDrizzleSchemaConfig => {
14
+ const config = (builder.options.drizzle.schema ??
15
+ (builder.options.drizzle.client as DrizzleClient)
16
+ ._) as RelationalSchemaConfig<TablesRelationalConfig>;
17
+
13
18
  const dbToSchema = Object.values(config.schema).reduce<Record<string, TableRelationalConfig>>(
14
19
  (acc, table) => {
15
20
  acc[table.dbName] = table;
@@ -25,8 +30,29 @@ const cache = createContextCache(
25
30
  },
26
31
  );
27
32
 
33
+ const clientCache = createContextCache((builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>) => {
34
+ const clientConfig = builder.options.drizzle.client;
35
+ const getClient =
36
+ typeof clientConfig === 'function'
37
+ ? createContextCache((ctx) => clientConfig(ctx))
38
+ : (_ctx: object) => clientConfig;
39
+
40
+ return createContextCache((context: object) => {
41
+ const client = getClient(context);
42
+
43
+ return client;
44
+ });
45
+ });
46
+
28
47
  export function getSchemaConfig<Types extends SchemaTypes>(
29
48
  builder: PothosSchemaTypes.SchemaBuilder<Types>,
30
49
  ) {
31
- return cache(builder.options.drizzle.client._ as RelationalSchemaConfig<TablesRelationalConfig>);
50
+ return configCache(builder as never);
51
+ }
52
+
53
+ export function getClient<Types extends SchemaTypes>(
54
+ builder: PothosSchemaTypes.SchemaBuilder<Types>,
55
+ context: object,
56
+ ) {
57
+ return clientCache(builder as never)(context);
32
58
  }