mirror of
https://github.com/withastro/astro.git
synced 2025-03-17 23:11:29 -05:00
wip: defineData
This commit is contained in:
parent
7ea9be9736
commit
a8e9682d29
3 changed files with 25 additions and 0 deletions
1
packages/db/config-augment.d.ts
vendored
1
packages/db/config-augment.d.ts
vendored
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
10
packages/db/test/fixtures/recipes/astro.data.ts
vendored
Normal file
10
packages/db/test/fixtures/recipes/astro.data.ts
vendored
Normal 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({});
|
||||
});
|
Loading…
Add table
Reference in a new issue