@@ -4,9 +4,11 @@ import type { Resolver } from '@nuxt/kit'
4
4
import cloudflareD1Connector from 'db0/connectors/cloudflare-d1'
5
5
import { isAbsolute , join , dirname } from 'pathe'
6
6
import { isWebContainer } from '@webcontainer/env'
7
- import type { CacheEntry , D1DatabaseConfig , LocalDevelopmentDatabase , SqliteDatabaseConfig } from '../types'
7
+ import { z } from 'zod'
8
+ import type { CacheEntry , D1DatabaseConfig , LocalDevelopmentDatabase , ResolvedCollection , SqliteDatabaseConfig } from '../types'
8
9
import type { ModuleOptions } from '../types/module'
9
10
import { logger } from './dev'
11
+ import { generateCollectionInsert , generateCollectionTableDefinition } from './collection'
10
12
11
13
export async function refineDatabaseConfig ( database : ModuleOptions [ 'database' ] , opts : { rootDir : string , updateSqliteFileName ?: boolean } ) {
12
14
if ( database . type === 'd1' ) {
@@ -63,8 +65,22 @@ export async function getLocalDatabase(database: SqliteDatabaseConfig | D1Databa
63
65
const databaseLocation = database . type === 'sqlite' ? database . filename : database . bindingName
64
66
const db = _localDatabase [ databaseLocation ] || connector || await getDatabase ( database , { nativeSqlite } )
65
67
68
+ const cacheCollection = {
69
+ tableName : '_development_cache' ,
70
+ extendedSchema : z . object ( {
71
+ id : z . string ( ) ,
72
+ checksum : z . string ( ) ,
73
+ parsedContent : z . string ( ) ,
74
+ } ) ,
75
+ fields : {
76
+ id : 'string' ,
77
+ checksum : 'string' ,
78
+ parsedContent : 'string' ,
79
+ } ,
80
+ } as unknown as ResolvedCollection
81
+
66
82
_localDatabase [ databaseLocation ] = db
67
- await db . exec ( 'CREATE TABLE IF NOT EXISTS _development_cache (id TEXT PRIMARY KEY, checksum TEXT, parsedContent TEXT)' )
83
+ await db . exec ( generateCollectionTableDefinition ( cacheCollection ) )
68
84
69
85
const fetchDevelopmentCache = async ( ) => {
70
86
const result = await db . prepare ( 'SELECT * FROM _development_cache' ) . all ( ) as CacheEntry [ ]
@@ -77,8 +93,10 @@ export async function getLocalDatabase(database: SqliteDatabaseConfig | D1Databa
77
93
78
94
const insertDevelopmentCache = async ( id : string , checksum : string , parsedContent : string ) => {
79
95
deleteDevelopmentCache ( id )
80
- db . prepare ( `INSERT INTO _development_cache (id, checksum, parsedContent) VALUES (?, ?, ?)` )
81
- . run ( id , checksum , parsedContent )
96
+ const insert = generateCollectionInsert ( cacheCollection , { id, checksum, parsedContent } )
97
+ for ( const query of insert . queries ) {
98
+ await db . exec ( query )
99
+ }
82
100
}
83
101
84
102
const deleteDevelopmentCache = async ( id : string ) => {
0 commit comments