@@ -56,20 +56,22 @@ type TypeOverride<TCustomType> = {
5656 * @template TTable The Drizzle table type
5757 */
5858export type ColumnsConfig < TTable extends Table > =
59- | false
60- | Flatten < {
61- /**
62- * The columns to include in the Zero schema.
63- * Set to true to use default mapping, or provide a TypeOverride for custom mapping.
64- */
65- readonly [ KColumn in ColumnNames < TTable > ] :
66- | boolean
67- | ColumnBuilder <
68- TypeOverride <
69- ZeroTypeToTypescriptType [ DrizzleDataTypeToZeroType [ Columns < TTable > [ KColumn ] [ "dataType" ] ] ]
70- >
71- > ;
72- } > ;
59+ | boolean
60+ | Partial <
61+ Flatten < {
62+ /**
63+ * The columns to include in the Zero schema.
64+ * Set to true to use default mapping, or provide a TypeOverride for custom mapping.
65+ */
66+ readonly [ KColumn in ColumnNames < TTable > ] :
67+ | boolean
68+ | ColumnBuilder <
69+ TypeOverride <
70+ ZeroTypeToTypescriptType [ DrizzleDataTypeToZeroType [ Columns < TTable > [ KColumn ] [ "dataType" ] ] ]
71+ >
72+ > ;
73+ } >
74+ > ;
7375
7476/**
7577 * Maps a Drizzle column type to its corresponding Zero type.
@@ -262,20 +264,39 @@ const createZeroTableBuilder = <
262264 const tableColumns = getTableColumns ( table ) ;
263265 const tableConfig = getTableConfigForDatabase ( table ) ;
264266
265- const primaryKeysFromColumns : string [ ] = [ ] ;
266-
267- const columnsMapped = typedEntries ( tableColumns ) . reduce (
268- ( acc , [ key , column ] ) => {
269- const columnConfig = columns ?. [ key as keyof TColumnConfig ] ;
267+ const columnNameToStableKey = new Map < string , string > (
268+ typedEntries ( tableColumns ) . map ( ( [ key , column ] ) => [
269+ column . name ,
270+ String ( key ) ,
271+ ] ) ,
272+ ) ;
270273
271- if ( columnConfig === false ) {
272- debugLog (
273- debug ,
274- `Skipping column ${ String ( key ) } because columnConfig is false` ,
275- ) ;
274+ const primaryKeys = new Set < string > ( ) ;
275+ for ( const [ key , column ] of typedEntries ( tableColumns ) ) {
276+ if ( column . primary ) {
277+ primaryKeys . add ( String ( key ) ) ;
278+ }
279+ }
276280
277- return acc ;
281+ for ( const pk of tableConfig . primaryKeys ) {
282+ for ( const pkColumn of pk . columns ) {
283+ const key = columnNameToStableKey . get ( pkColumn . name ) ;
284+ if ( key ) {
285+ primaryKeys . add ( String ( key ) ) ;
278286 }
287+ }
288+ }
289+
290+ const isColumnBuilder = ( value : unknown ) : value is ColumnBuilder < any > =>
291+ typeof value === "object" && value !== null && "schema" in value ;
292+
293+ const columnsMapped = typedEntries ( tableColumns ) . reduce (
294+ ( acc , [ key , column ] ) => {
295+ const columnConfig =
296+ typeof columns === "object" && columns !== null
297+ ? columns [ key as keyof TColumnConfig ]
298+ : undefined ;
299+ const isColumnConfigOverride = isColumnBuilder ( columnConfig ) ;
279300
280301 // From https://github.com/drizzle-team/drizzle-orm/blob/e5c63db0df0eaff5cae8321d97a77e5b47c5800d/drizzle-kit/src/serializer/utils.ts#L5
281302 const resolvedColumnName =
@@ -285,21 +306,30 @@ const createZeroTableBuilder = <
285306 ? toCamelCase ( column . name )
286307 : toSnakeCase ( column . name ) ;
287308
288- if (
289- typeof columnConfig !== "boolean" &&
290- typeof columnConfig !== "object" &&
291- typeof columnConfig !== "undefined"
292- ) {
293- throw new Error (
294- `drizzle-zero: Invalid column config for column ${ resolvedColumnName } - expected boolean or ColumnBuilder but was ${ typeof columnConfig } ` ,
295- ) ;
309+ if ( typeof columns === "object" && columns !== null ) {
310+ if (
311+ columnConfig !== undefined &&
312+ typeof columnConfig !== "boolean" &&
313+ ! isColumnConfigOverride
314+ ) {
315+ throw new Error (
316+ `drizzle-zero: Invalid column config for column ${ resolvedColumnName } - expected boolean or ColumnBuilder but was ${ typeof columnConfig } ` ,
317+ ) ;
318+ }
319+
320+ if (
321+ columnConfig !== true &&
322+ ! isColumnConfigOverride &&
323+ ! primaryKeys . has ( String ( key ) )
324+ ) {
325+ debugLog (
326+ debug ,
327+ `Skipping non-primary column ${ resolvedColumnName } because it was not explicitly included in the config.` ,
328+ ) ;
329+ return acc ;
330+ }
296331 }
297332
298- const isColumnBuilder = ( value : unknown ) : value is ColumnBuilder < any > =>
299- typeof value === "object" && value !== null && "schema" in value ;
300-
301- const isColumnConfigOverride = isColumnBuilder ( columnConfig ) ;
302-
303333 const type =
304334 drizzleColumnTypeToZeroType [
305335 column . columnType as keyof DrizzleColumnTypeToZeroType
@@ -326,10 +356,6 @@ const createZeroTableBuilder = <
326356 ? columnConfig . schema . optional
327357 : false ;
328358
329- if ( column . primary ) {
330- primaryKeysFromColumns . push ( String ( key ) ) ;
331- }
332-
333359 if ( columnConfig && typeof columnConfig !== "boolean" ) {
334360 return {
335361 ...acc ,
@@ -362,19 +388,7 @@ const createZeroTableBuilder = <
362388 { } as Record < string , any > ,
363389 ) ;
364390
365- const primaryKeys = [
366- ...primaryKeysFromColumns ,
367- ...tableConfig . primaryKeys . flatMap ( ( k ) =>
368- k . columns . map ( ( c ) =>
369- getDrizzleColumnKeyFromColumnName ( {
370- columnName : c . name ,
371- table : c . table ,
372- } ) ,
373- ) ,
374- ) ,
375- ] ;
376-
377- if ( ! primaryKeys . length ) {
391+ if ( primaryKeys . size === 0 ) {
378392 throw new Error (
379393 `drizzle-zero: No primary keys found in table - ${ actualTableName } . Did you forget to define a primary key?` ,
380394 ) ;
0 commit comments