diff --git a/src/utils/collection.ts b/src/utils/collection.ts index 2d0fe5f5b..b48bab673 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -103,7 +103,11 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R const value = (collection.extendedSchema).shape[key] const underlyingType = getUnderlyingType(value as ZodType) - const defaultValue = value._def.defaultValue ? value._def.defaultValue() : 'NULL' + let defaultValue = value._def.defaultValue ? value._def.defaultValue() : 'NULL' + + if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') { + defaultValue = JSON.stringify(defaultValue) + } const valueToInsert = typeof data[key] !== 'undefined' ? data[key] : defaultValue fields.push(key) @@ -163,9 +167,13 @@ export function generateCollectionTableDefinition(collection: ResolvedCollection // Handle default values if (type._def.defaultValue !== undefined) { - const defaultValue = typeof type._def.defaultValue() === 'string' + let defaultValue = typeof type._def.defaultValue() === 'string' ? `'${type._def.defaultValue()}'` : type._def.defaultValue() + + if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') { + defaultValue = `'${JSON.stringify(defaultValue)}'` + } constraints.push(`DEFAULT ${defaultValue}`) } diff --git a/src/utils/config.ts b/src/utils/config.ts index d587db04c..79459e537 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -44,6 +44,13 @@ export async function loadLayersConfig(nuxt: Nuxt) { } } + if (Object.keys(collectionMap).length === 0) { + const collections = resolveCollections(defaultConfig.collections) + for (const collection of collections) { + collectionMap[collection.name] = collection + } + } + return { collections: Object.values(collectionMap), } diff --git a/test/unit/generateCollectionTableDefinition.test.ts b/test/unit/generateCollectionTableDefinition.test.ts index c0fb660b1..8b7563cf3 100644 --- a/test/unit/generateCollectionTableDefinition.test.ts +++ b/test/unit/generateCollectionTableDefinition.test.ts @@ -20,7 +20,7 @@ describe('generateCollectionTableDefinition', () => { ' "meta" TEXT,', ' "navigation" TEXT DEFAULT true,', ' "path" VARCHAR,', - ' "seo" TEXT,', + ' "seo" TEXT DEFAULT \'{}\',', ' "stem" VARCHAR,', ' "title" VARCHAR', ');', @@ -47,7 +47,7 @@ describe('generateCollectionTableDefinition', () => { ' "meta" TEXT,', ' "navigation" TEXT DEFAULT true,', ' "path" VARCHAR,', - ' "seo" TEXT,', + ' "seo" TEXT DEFAULT \'{}\',', ' "stem" VARCHAR,', ' "title" VARCHAR', ');',