0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-17 23:11:29 -05:00

feat: seed -> seedReturning

This commit is contained in:
bholmesdev 2024-02-13 11:37:29 -05:00
parent 998e2309c9
commit e3ba7de572
3 changed files with 22 additions and 17 deletions

View file

@ -12,7 +12,7 @@ import {
} from '../core/types.js';
import { bold } from 'kleur/colors';
import { type SQL, sql } from 'drizzle-orm';
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';
import { SQLiteAsyncDialect, type SQLiteInsert } from 'drizzle-orm/sqlite-core';
import type { AstroIntegrationLogger } from 'astro';
import type { DBUserConfig } from '../core/types.js';
import { hasPrimaryKey } from '../runtime/index.js';
@ -53,16 +53,17 @@ export async function seedData({
try {
await data({
seed: async ({ table }, values) => {
const result = Array.isArray(values)
? db.insert(table).values(values).returning()
: db
.insert(table)
.values(values as any)
.returning()
.get();
// Drizzle types don't *quite* line up, and it's tough to debug why.
// we're casting and calling this close enough :)
return result as any;
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;
},
db,
mode,

View file

@ -260,7 +260,11 @@ export type WritableDBCollection = z.infer<typeof writableCollectionSchema>;
export type DBDataContext = {
db: SqliteDB;
seed: <
seed: <TFields extends FieldsConfig>(
collection: ResolvedCollectionConfig<TFields>,
data: MaybeArray<SQLiteInsertValue<Table<string, TFields>>>
) => Promise<void>;
seedReturning: <
TFields extends FieldsConfig,
TData extends MaybeArray<SQLiteInsertValue<Table<string, TFields>>>,
>(

View file

@ -26,13 +26,13 @@ export default defineConfig({
integrations: [astroDb()],
db: {
collections: { Recipe, Ingredient },
async data({ seed }) {
const pancakes = await seed(Recipe, {
async data({ seed, seedReturning }) {
const pancakes = await seedReturning(Recipe, {
title: 'Pancakes',
description: 'A delicious breakfast',
});
seed(Ingredient, [
await seed(Ingredient, [
{
name: 'Flour',
quantity: 1,
@ -50,12 +50,12 @@ export default defineConfig({
},
]);
const pizza = await seed(Recipe, {
const pizza = await seedReturning(Recipe, {
title: 'Pizza',
description: 'A delicious dinner',
});
seed(Ingredient, [
await seed(Ingredient, [
{
name: 'Flour',
quantity: 1,