mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
fix: avoid seed on writable
This commit is contained in:
parent
3fd771da36
commit
6f225b38cd
2 changed files with 45 additions and 23 deletions
|
@ -14,7 +14,7 @@ import { bold } from 'kleur/colors';
|
|||
import { type SQL, sql } from 'drizzle-orm';
|
||||
import { SQLiteAsyncDialect, type SQLiteInsert } from 'drizzle-orm/sqlite-core';
|
||||
import type { AstroIntegrationLogger } from 'astro';
|
||||
import type { DBUserConfig } from '../core/types.js';
|
||||
import type { DBBuildDataContext, DBDevDataContext, DBUserConfig } from '../core/types.js';
|
||||
import { hasPrimaryKey } from '../runtime/index.js';
|
||||
import { isSerializedSQL } from '../runtime/types.js';
|
||||
|
||||
|
@ -26,7 +26,6 @@ export async function setupDbTables({
|
|||
collections,
|
||||
logger,
|
||||
mode,
|
||||
// TODO: Remove once Turso has foreign key PRAGMA support
|
||||
}: {
|
||||
db: SqliteRemoteDatabase;
|
||||
data?: DBUserConfig['data'];
|
||||
|
@ -46,25 +45,48 @@ export async function setupDbTables({
|
|||
}
|
||||
if (data) {
|
||||
try {
|
||||
//@ts-ignore
|
||||
await data({
|
||||
seed: async ({ table }, values) => {
|
||||
await db.insert(table).values(values as any);
|
||||
},
|
||||
// @ts-ignore
|
||||
seedReturning: async ({ table }, values) => {
|
||||
let result: SQLiteInsert<any, any, any, any> = db
|
||||
.insert(table)
|
||||
.values(values as any)
|
||||
.returning();
|
||||
if (!Array.isArray(values)) {
|
||||
result = result.get();
|
||||
}
|
||||
return result;
|
||||
},
|
||||
db,
|
||||
mode,
|
||||
});
|
||||
// Split to separate conditional blocks for `seedReturning` type
|
||||
if (mode === 'dev') {
|
||||
const context: DBDevDataContext = {
|
||||
db,
|
||||
mode,
|
||||
seed: async ({ table }, values) => {
|
||||
await db.insert(table).values(values as any);
|
||||
},
|
||||
seedReturning: async ({ table }, values) => {
|
||||
let result: SQLiteInsert<any, any, any, any> = db
|
||||
.insert(table)
|
||||
.values(values as any)
|
||||
.returning();
|
||||
if (!Array.isArray(values)) {
|
||||
result = result.get();
|
||||
}
|
||||
return result;
|
||||
},
|
||||
};
|
||||
await data(context);
|
||||
} else {
|
||||
const context: DBBuildDataContext = {
|
||||
db,
|
||||
mode,
|
||||
seed: async (collection, values) => {
|
||||
if (collection.writable) return;
|
||||
await db.insert(collection.table).values(values as any);
|
||||
},
|
||||
seedReturning: async (collection, values) => {
|
||||
if (collection.writable) return undefined;
|
||||
let result: SQLiteInsert<any, any, any, any> = db
|
||||
.insert(collection.table)
|
||||
.values(values as any)
|
||||
.returning();
|
||||
if (!Array.isArray(values)) {
|
||||
return result.get();
|
||||
}
|
||||
return result;
|
||||
},
|
||||
};
|
||||
await data(context);
|
||||
}
|
||||
} catch (error) {
|
||||
(logger ?? console).error(
|
||||
`Failed to seed data. Did you update to match recent schema changes?`
|
||||
|
|
|
@ -269,7 +269,7 @@ type BaseDataContext = {
|
|||
) => Promise<void>;
|
||||
};
|
||||
|
||||
type DBDevDataContext = BaseDataContext & {
|
||||
export type DBDevDataContext = BaseDataContext & {
|
||||
seedReturning: <
|
||||
TWritable extends boolean,
|
||||
TFields extends FieldsConfig,
|
||||
|
@ -286,7 +286,7 @@ type DBDevDataContext = BaseDataContext & {
|
|||
mode: 'dev';
|
||||
};
|
||||
|
||||
type DBBuildDataContext = BaseDataContext & {
|
||||
export type DBBuildDataContext = BaseDataContext & {
|
||||
seedReturning: <
|
||||
TWritable extends boolean,
|
||||
TFields extends FieldsConfig,
|
||||
|
|
Loading…
Reference in a new issue