@pothos/plugin-drizzle 0.8.1 → 0.10.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 (122) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/TODO.md +25 -4
  3. package/dts/connection-helpers.d.ts +34 -0
  4. package/dts/connection-helpers.d.ts.map +1 -0
  5. package/dts/drizzle-field-builder.d.ts +18 -53
  6. package/dts/drizzle-field-builder.d.ts.map +1 -1
  7. package/dts/global-types.d.ts +22 -41
  8. package/dts/global-types.d.ts.map +1 -1
  9. package/dts/index.d.ts +5 -2
  10. package/dts/index.d.ts.map +1 -1
  11. package/dts/interface-ref.d.ts +3 -2
  12. package/dts/interface-ref.d.ts.map +1 -1
  13. package/dts/model-loader.d.ts +3 -2
  14. package/dts/model-loader.d.ts.map +1 -1
  15. package/dts/node-ref.d.ts +1 -1
  16. package/dts/node-ref.d.ts.map +1 -1
  17. package/dts/object-ref.d.ts +2 -2
  18. package/dts/object-ref.d.ts.map +1 -1
  19. package/dts/types.d.ts +61 -41
  20. package/dts/types.d.ts.map +1 -1
  21. package/dts/utils/config.d.ts +4 -3
  22. package/dts/utils/config.d.ts.map +1 -1
  23. package/dts/utils/cursors.d.ts +13 -7
  24. package/dts/utils/cursors.d.ts.map +1 -1
  25. package/dts/utils/drizzle-graphql.d.ts +0 -17
  26. package/dts/utils/drizzle-graphql.d.ts.map +1 -1
  27. package/dts/utils/map-query.d.ts.map +1 -1
  28. package/dts/utils/selections.d.ts +5 -5
  29. package/dts/utils/selections.d.ts.map +1 -1
  30. package/esm/connection-helpers.d.ts +36 -0
  31. package/esm/connection-helpers.d.ts.map +1 -0
  32. package/esm/connection-helpers.js +82 -0
  33. package/esm/connection-helpers.js.map +1 -0
  34. package/esm/drizzle-field-builder.d.ts +18 -73
  35. package/esm/drizzle-field-builder.d.ts.map +1 -1
  36. package/esm/drizzle-field-builder.js +25 -14
  37. package/esm/drizzle-field-builder.js.map +1 -1
  38. package/esm/field-builder.js +2 -3
  39. package/esm/field-builder.js.map +1 -1
  40. package/esm/global-types.d.ts +35 -60
  41. package/esm/global-types.d.ts.map +1 -1
  42. package/esm/global-types.js.map +1 -1
  43. package/esm/index.d.ts +5 -2
  44. package/esm/index.d.ts.map +1 -1
  45. package/esm/index.js +71 -61
  46. package/esm/index.js.map +1 -1
  47. package/esm/interface-ref.d.ts +3 -2
  48. package/esm/interface-ref.d.ts.map +1 -1
  49. package/esm/interface-ref.js.map +1 -1
  50. package/esm/model-loader.d.ts +3 -2
  51. package/esm/model-loader.d.ts.map +1 -1
  52. package/esm/model-loader.js +17 -10
  53. package/esm/model-loader.js.map +1 -1
  54. package/esm/node-ref.d.ts +1 -1
  55. package/esm/node-ref.d.ts.map +1 -1
  56. package/esm/object-ref.d.ts +2 -2
  57. package/esm/object-ref.d.ts.map +1 -1
  58. package/esm/schema-builder.js +113 -67
  59. package/esm/schema-builder.js.map +1 -1
  60. package/esm/types.d.ts +61 -41
  61. package/esm/types.d.ts.map +1 -1
  62. package/esm/types.js.map +1 -1
  63. package/esm/utils/config.d.ts +4 -3
  64. package/esm/utils/config.d.ts.map +1 -1
  65. package/esm/utils/config.js +32 -15
  66. package/esm/utils/config.js.map +1 -1
  67. package/esm/utils/cursors.d.ts +13 -7
  68. package/esm/utils/cursors.d.ts.map +1 -1
  69. package/esm/utils/cursors.js +137 -63
  70. package/esm/utils/cursors.js.map +1 -1
  71. package/esm/utils/drizzle-graphql.d.ts +0 -17
  72. package/esm/utils/drizzle-graphql.d.ts.map +1 -1
  73. package/esm/utils/drizzle-graphql.js +230 -198
  74. package/esm/utils/drizzle-graphql.js.map +1 -1
  75. package/esm/utils/map-query.d.ts.map +1 -1
  76. package/esm/utils/map-query.js +10 -6
  77. package/esm/utils/map-query.js.map +1 -1
  78. package/esm/utils/selections.d.ts +5 -5
  79. package/esm/utils/selections.d.ts.map +1 -1
  80. package/esm/utils/selections.js +19 -18
  81. package/esm/utils/selections.js.map +1 -1
  82. package/lib/connection-helpers.js +93 -0
  83. package/lib/connection-helpers.js.map +1 -0
  84. package/lib/drizzle-field-builder.js +24 -13
  85. package/lib/drizzle-field-builder.js.map +1 -1
  86. package/lib/field-builder.js +2 -3
  87. package/lib/field-builder.js.map +1 -1
  88. package/lib/index.js +79 -60
  89. package/lib/index.js.map +1 -1
  90. package/lib/interface-ref.js.map +1 -1
  91. package/lib/model-loader.js +17 -10
  92. package/lib/model-loader.js.map +1 -1
  93. package/lib/schema-builder.js +113 -67
  94. package/lib/schema-builder.js.map +1 -1
  95. package/lib/types.js.map +1 -1
  96. package/lib/utils/config.js +31 -14
  97. package/lib/utils/config.js.map +1 -1
  98. package/lib/utils/cursors.js +135 -63
  99. package/lib/utils/cursors.js.map +1 -1
  100. package/lib/utils/drizzle-graphql.js +230 -221
  101. package/lib/utils/drizzle-graphql.js.map +1 -1
  102. package/lib/utils/map-query.js +10 -6
  103. package/lib/utils/map-query.js.map +1 -1
  104. package/lib/utils/selections.js +18 -17
  105. package/lib/utils/selections.js.map +1 -1
  106. package/package.json +14 -14
  107. package/src/connection-helpers.ts +206 -0
  108. package/src/drizzle-field-builder.ts +30 -26
  109. package/src/field-builder.ts +4 -7
  110. package/src/global-types.ts +112 -119
  111. package/src/index.ts +91 -70
  112. package/src/interface-ref.ts +7 -3
  113. package/src/model-loader.ts +32 -17
  114. package/src/node-ref.ts +1 -1
  115. package/src/object-ref.ts +2 -2
  116. package/src/schema-builder.ts +116 -113
  117. package/src/types.ts +95 -62
  118. package/src/utils/config.ts +39 -30
  119. package/src/utils/cursors.ts +134 -101
  120. package/src/utils/drizzle-graphql.ts +273 -274
  121. package/src/utils/map-query.ts +20 -8
  122. package/src/utils/selections.ts +34 -28
@@ -1,9 +1,11 @@
1
1
  import { PothosValidationError } from '@pothos/core';
2
2
  import {
3
3
  type DBQueryConfig,
4
- type SQL,
4
+ type SQLOperator,
5
+ type SQLWrapper,
6
+ type Table,
5
7
  type TableRelationalConfig,
6
- getOperators,
8
+ getTableUniqueName,
7
9
  } from 'drizzle-orm';
8
10
  import type { PothosDrizzleSchemaConfig } from './config';
9
11
  import { deepEqual } from './deep-equal';
@@ -15,14 +17,14 @@ export interface SelectionState {
15
17
  allColumns: boolean;
16
18
  columns: Set<string>;
17
19
  with: Map<string, SelectionState>;
18
- extras: Map<string, SQL.Aliased>;
20
+ extras: Map<string, NonNullable<SelectionMap['extras']>[string]>;
19
21
  mappings: LoaderMappings;
20
22
  parent?: SelectionState;
21
23
  depth: number;
22
24
  skipDeferredFragments: boolean;
23
25
  }
24
26
 
25
- export type SelectionMap = DBQueryConfig<'one', false>;
27
+ export type SelectionMap = DBQueryConfig<'one'>;
26
28
 
27
29
  export function selectionCompatible(
28
30
  state: SelectionState,
@@ -48,15 +50,11 @@ export function selectionCompatible(
48
50
  return false;
49
51
  }
50
52
 
51
- const resolvedExtras =
52
- typeof extras === 'function' ? extras(state.table.columns, getOperators()) : extras;
53
-
54
53
  if (
55
- resolvedExtras &&
56
- Object.entries(resolvedExtras).some(([key, value]) => {
57
- const sql = state.extras.get(key);
58
-
59
- return sql && (sql.sql !== value.sql || sql.fieldAlias !== value.fieldAlias);
54
+ extras &&
55
+ Object.entries(extras).some(([key, value]) => {
56
+ const existing = state.extras.get(key);
57
+ return existing && existing !== value;
60
58
  })
61
59
  ) {
62
60
  return false;
@@ -123,8 +121,14 @@ export function createState(
123
121
  export function mergeSelection(
124
122
  config: PothosDrizzleSchemaConfig,
125
123
  state: SelectionState,
126
- { with: withSelection, extras, columns, ...query }: SelectionMap,
124
+ selection: SelectionMap | boolean,
127
125
  ) {
126
+ const {
127
+ with: withSelection,
128
+ extras,
129
+ columns,
130
+ ...query
131
+ } = typeof selection === 'boolean' ? {} : selection;
128
132
  if (withSelection) {
129
133
  for (const [key, value] of Object.entries(withSelection)) {
130
134
  const relation = state.table.relations[key];
@@ -133,7 +137,9 @@ export function mergeSelection(
133
137
  throw new PothosValidationError(`Relation ${key} does not exist on ${state.table.dbName}`);
134
138
  }
135
139
 
136
- merge(config.dbToSchema[relation.referencedTableName], key, value as SelectionMap | boolean);
140
+ const tableUniqueName = getTableUniqueName(relation.targetTable as Table);
141
+ const tableName = config.relations.tableNamesMap[tableUniqueName];
142
+ merge(config.relations.tablesConfig[tableName], key, value as SelectionMap | boolean);
137
143
  }
138
144
  }
139
145
 
@@ -141,11 +147,8 @@ export function mergeSelection(
141
147
  state.query = query;
142
148
  }
143
149
 
144
- const resolvedExtras =
145
- typeof extras === 'function' ? extras(state.table.columns, getOperators()) : extras;
146
-
147
- if (resolvedExtras) {
148
- for (const [key, value] of Object.entries(resolvedExtras)) {
150
+ if (extras) {
151
+ for (const [key, value] of Object.entries(extras)) {
149
152
  state.extras.set(key, value);
150
153
  }
151
154
  }
@@ -158,7 +161,7 @@ export function mergeSelection(
158
161
  for (const key of Object.keys(columns)) {
159
162
  state.columns.add(key);
160
163
  }
161
- } else {
164
+ } else if (selection === true) {
162
165
  state.allColumns = true;
163
166
  }
164
167
 
@@ -167,19 +170,20 @@ export function mergeSelection(
167
170
  return;
168
171
  }
169
172
 
170
- const selection = value === true ? {} : value;
171
-
172
173
  if (state.with.has(key)) {
173
- mergeSelection(config, state.with.get(key)!, selection);
174
+ mergeSelection(config, state.with.get(key)!, value);
174
175
  } else {
175
176
  const relatedState = createState(table, state.skipDeferredFragments, state);
176
- mergeSelection(config, relatedState, selection);
177
+ mergeSelection(config, relatedState, value);
177
178
  state.with.set(key, relatedState);
178
179
  }
179
180
  }
180
181
  }
181
182
 
182
- export function selectionToQuery(state: SelectionState): SelectionMap {
183
+ export function selectionToQuery(
184
+ config: PothosDrizzleSchemaConfig,
185
+ state: SelectionState,
186
+ ): SelectionMap {
183
187
  const query: SelectionMap & { extras: Record<string, unknown> } = {
184
188
  ...state.query,
185
189
  with: {},
@@ -195,8 +199,10 @@ export function selectionToQuery(state: SelectionState): SelectionMap {
195
199
  for (const key of state.columns) {
196
200
  query.columns![key] = true;
197
201
  }
198
- for (const { name } of state.table.primaryKey) {
199
- query.columns![name] = true;
202
+
203
+ for (const column of config.getPrimaryKey(state.table.tsName)) {
204
+ const tsName = config.columnToTsName(column);
205
+ query.columns![tsName] = true;
200
206
  }
201
207
  }
202
208
 
@@ -205,7 +211,7 @@ export function selectionToQuery(state: SelectionState): SelectionMap {
205
211
  }
206
212
 
207
213
  state.with.forEach((sel, relation) => {
208
- query.with![relation] = selectionToQuery(sel);
214
+ query.with![relation] = selectionToQuery(config, sel);
209
215
  });
210
216
 
211
217
  return query;