Skip to content

Commit ef50925

Browse files
committed
fix(cache): split large queries
1 parent 96a9c4a commit ef50925

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/utils/database.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import type { Resolver } from '@nuxt/kit'
44
import cloudflareD1Connector from 'db0/connectors/cloudflare-d1'
55
import { isAbsolute, join, dirname } from 'pathe'
66
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'
89
import type { ModuleOptions } from '../types/module'
910
import { logger } from './dev'
11+
import { generateCollectionInsert, generateCollectionTableDefinition } from './collection'
1012

1113
export async function refineDatabaseConfig(database: ModuleOptions['database'], opts: { rootDir: string, updateSqliteFileName?: boolean }) {
1214
if (database.type === 'd1') {
@@ -63,8 +65,22 @@ export async function getLocalDatabase(database: SqliteDatabaseConfig | D1Databa
6365
const databaseLocation = database.type === 'sqlite' ? database.filename : database.bindingName
6466
const db = _localDatabase[databaseLocation] || connector || await getDatabase(database, { nativeSqlite })
6567

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+
6682
_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))
6884

6985
const fetchDevelopmentCache = async () => {
7086
const result = await db.prepare('SELECT * FROM _development_cache').all() as CacheEntry[]
@@ -77,8 +93,10 @@ export async function getLocalDatabase(database: SqliteDatabaseConfig | D1Databa
7793

7894
const insertDevelopmentCache = async (id: string, checksum: string, parsedContent: string) => {
7995
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+
}
82100
}
83101

84102
const deleteDevelopmentCache = async (id: string) => {

0 commit comments

Comments
 (0)