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

wip: defineData

This commit is contained in:
bholmesdev 2024-02-02 15:58:39 -05:00
parent 7ea9be9736
commit a8e9682d29
3 changed files with 25 additions and 0 deletions

View file

@ -6,6 +6,7 @@ declare namespace Config {
declare module 'astro:db' {
export const db: import('./dist/runtime/index.js').SqliteDB;
export const dbUrl: string;
export const defineData: typeof import('./dist/runtime/types.js').defineData;
export {
sql,

View file

@ -2,6 +2,7 @@ import type { ColumnDataType, ColumnBaseConfig } from 'drizzle-orm';
import type {
SQLiteColumn,
SQLiteInsertValue,
SQLiteTable,
SQLiteTableWithColumns,
} from 'drizzle-orm/sqlite-core';
import type { DBField, FieldsConfig, MaybeArray, MaybePromise } from '../core/types.js';
@ -102,3 +103,16 @@ export type Table<
>;
};
}>;
export type DBDataContext = {
db: SqliteDB;
seed<TTable extends SQLiteTable>(
collection: TTable,
data: MaybeArray<SQLiteInsertValue<TTable>>
): Promise<any> /** TODO: type output */;
mode: 'dev' | 'build';
};
export function defineData(callback: (ctx: DBDataContext) => MaybePromise<void>) {
return callback;
}

View file

@ -0,0 +1,10 @@
/// <reference path="./src/env.d.ts" />
import { db, defineData, Ingredient, Recipe } from 'astro:db';
export default defineData(async ({ seed }) => {
await seed(Recipe, {
title: 'Pancakes',
description: 'A delicious breakfast',
});
await db.insert(Recipe).values({});
});