mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -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 { type SQL, sql } from 'drizzle-orm';
|
||||||
import { SQLiteAsyncDialect, type SQLiteInsert } from 'drizzle-orm/sqlite-core';
|
import { SQLiteAsyncDialect, type SQLiteInsert } from 'drizzle-orm/sqlite-core';
|
||||||
import type { AstroIntegrationLogger } from 'astro';
|
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 { hasPrimaryKey } from '../runtime/index.js';
|
||||||
import { isSerializedSQL } from '../runtime/types.js';
|
import { isSerializedSQL } from '../runtime/types.js';
|
||||||
|
|
||||||
|
@ -26,7 +26,6 @@ export async function setupDbTables({
|
||||||
collections,
|
collections,
|
||||||
logger,
|
logger,
|
||||||
mode,
|
mode,
|
||||||
// TODO: Remove once Turso has foreign key PRAGMA support
|
|
||||||
}: {
|
}: {
|
||||||
db: SqliteRemoteDatabase;
|
db: SqliteRemoteDatabase;
|
||||||
data?: DBUserConfig['data'];
|
data?: DBUserConfig['data'];
|
||||||
|
@ -46,25 +45,48 @@ export async function setupDbTables({
|
||||||
}
|
}
|
||||||
if (data) {
|
if (data) {
|
||||||
try {
|
try {
|
||||||
//@ts-ignore
|
// Split to separate conditional blocks for `seedReturning` type
|
||||||
await data({
|
if (mode === 'dev') {
|
||||||
seed: async ({ table }, values) => {
|
const context: DBDevDataContext = {
|
||||||
await db.insert(table).values(values as any);
|
db,
|
||||||
},
|
mode,
|
||||||
// @ts-ignore
|
seed: async ({ table }, values) => {
|
||||||
seedReturning: async ({ table }, values) => {
|
await db.insert(table).values(values as any);
|
||||||
let result: SQLiteInsert<any, any, any, any> = db
|
},
|
||||||
.insert(table)
|
seedReturning: async ({ table }, values) => {
|
||||||
.values(values as any)
|
let result: SQLiteInsert<any, any, any, any> = db
|
||||||
.returning();
|
.insert(table)
|
||||||
if (!Array.isArray(values)) {
|
.values(values as any)
|
||||||
result = result.get();
|
.returning();
|
||||||
}
|
if (!Array.isArray(values)) {
|
||||||
return result;
|
result = result.get();
|
||||||
},
|
}
|
||||||
db,
|
return result;
|
||||||
mode,
|
},
|
||||||
});
|
};
|
||||||
|
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) {
|
} catch (error) {
|
||||||
(logger ?? console).error(
|
(logger ?? console).error(
|
||||||
`Failed to seed data. Did you update to match recent schema changes?`
|
`Failed to seed data. Did you update to match recent schema changes?`
|
||||||
|
|
|
@ -269,7 +269,7 @@ type BaseDataContext = {
|
||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type DBDevDataContext = BaseDataContext & {
|
export type DBDevDataContext = BaseDataContext & {
|
||||||
seedReturning: <
|
seedReturning: <
|
||||||
TWritable extends boolean,
|
TWritable extends boolean,
|
||||||
TFields extends FieldsConfig,
|
TFields extends FieldsConfig,
|
||||||
|
@ -286,7 +286,7 @@ type DBDevDataContext = BaseDataContext & {
|
||||||
mode: 'dev';
|
mode: 'dev';
|
||||||
};
|
};
|
||||||
|
|
||||||
type DBBuildDataContext = BaseDataContext & {
|
export type DBBuildDataContext = BaseDataContext & {
|
||||||
seedReturning: <
|
seedReturning: <
|
||||||
TWritable extends boolean,
|
TWritable extends boolean,
|
||||||
TFields extends FieldsConfig,
|
TFields extends FieldsConfig,
|
||||||
|
|
Loading…
Add table
Reference in a new issue