@@ -8,8 +8,8 @@ import { logger } from './dev'
88
99const JSON_FIELDS_TYPES = [ 'ZodObject' , 'ZodArray' , 'ZodRecord' , 'ZodIntersection' , 'ZodUnion' , 'ZodAny' ]
1010
11- function getTableName ( name : string ) {
12- return `content_ ${ name } `
11+ export function getTableName ( name : string ) {
12+ return `_content_ ${ name } `
1313}
1414
1515export function defineCollection < T extends ZodRawShape > ( collection : Collection < T > ) : DefinedCollection {
@@ -45,17 +45,24 @@ export function resolveCollection(name: string, collection: DefinedCollection):
4545 name,
4646 type : collection . type || 'page' ,
4747 tableName : getTableName ( name ) ,
48- private : name === '_info ' ,
48+ private : name === 'info ' ,
4949 }
5050}
5151
5252export function resolveCollections ( collections : Record < string , DefinedCollection > ) : ResolvedCollection [ ] {
53- collections . _info = defineCollection ( {
53+ collections . info = {
5454 type : 'data' ,
55+ source : undefined ,
5556 schema : z . object ( {
56- version : z . string ( ) ,
57+ id : z . string ( ) ,
58+ value : z . string ( ) ,
5759 } ) ,
58- } )
60+ extendedSchema : z . object ( {
61+ id : z . string ( ) ,
62+ value : z . string ( ) ,
63+ } ) ,
64+ jsonFields : [ ] ,
65+ }
5966
6067 return Object . entries ( collections )
6168 . map ( ( [ name , collection ] ) => resolveCollection ( name , collection ) )
@@ -89,7 +96,7 @@ function resolveSource(source: string | CollectionSource | undefined): ResolvedC
8996export function generateCollectionInsert ( collection : ResolvedCollection , data : Record < string , unknown > ) {
9097 const fields : string [ ] = [ ]
9198 const values : Array < string | number | boolean > = [ ]
92- const sortedKeys = Object . keys ( ( collection . extendedSchema ) . shape ) . sort ( )
99+ const sortedKeys = getOrderedColumns ( ( collection . extendedSchema ) . shape )
93100
94101 sortedKeys . forEach ( ( key ) => {
95102 const value = ( collection . extendedSchema ) . shape [ key ]
@@ -128,12 +135,12 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R
128135
129136// Convert a collection with Zod schema to SQL table definition
130137export function generateCollectionTableDefinition ( collection : ResolvedCollection , opts : { drop ?: boolean } = { } ) {
131- const sortedKeys = Object . keys ( ( collection . extendedSchema ) . shape ) . sort ( )
138+ const sortedKeys = getOrderedColumns ( ( collection . extendedSchema ) . shape )
132139 const sqlFields = sortedKeys . map ( ( key ) => {
133140 const type = ( collection . extendedSchema ) . shape [ key ] !
134141 const underlyingType = getUnderlyingType ( type )
135142
136- if ( key === '_id ' ) return `${ key } TEXT PRIMARY KEY`
143+ if ( key === 'id ' ) return `${ key } TEXT PRIMARY KEY`
137144
138145 let sqlType : string = ZodToSqlFieldTypes [ underlyingType . constructor . name as ZodFieldType ]
139146
@@ -180,3 +187,13 @@ export function generateCollectionTableDefinition(collection: ResolvedCollection
180187
181188 return definition
182189}
190+
191+ function getOrderedColumns ( shape : ZodRawShape ) {
192+ const keys = new Set ( [
193+ shape . id ? 'id' : undefined ,
194+ shape . title ? 'title' : undefined ,
195+ ...Object . keys ( shape ) . sort ( ) ,
196+ ] . filter ( Boolean ) )
197+
198+ return Array . from ( keys ) as string [ ]
199+ }
0 commit comments