mirror of
https://github.com/withastro/astro.git
synced 2025-01-13 22:11:20 -05:00
feat: types for writable dev only
This commit is contained in:
parent
463796c48b
commit
3fd771da36
5 changed files with 49 additions and 27 deletions
|
@ -46,10 +46,12 @@ 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)
|
||||
|
|
|
@ -259,27 +259,53 @@ export type DBSnapshot = {
|
|||
export type ReadableDBCollection = z.infer<typeof readableCollectionSchema>;
|
||||
export type WritableDBCollection = z.infer<typeof writableCollectionSchema>;
|
||||
|
||||
export type DBDataContext = {
|
||||
export type DBDataContext = DBDevDataContext | DBBuildDataContext;
|
||||
|
||||
type BaseDataContext = {
|
||||
db: SqliteDB;
|
||||
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>>>,
|
||||
>(
|
||||
collection: ResolvedCollectionConfig<TFields>,
|
||||
data: TData
|
||||
) => Promise<
|
||||
TData extends Array<SQLiteInsertValue<Table<string, TFields>>>
|
||||
? InferSelectModel<Table<string, TFields>>[]
|
||||
: InferSelectModel<Table<string, TFields>>
|
||||
>;
|
||||
mode: 'dev' | 'build';
|
||||
};
|
||||
|
||||
export function defineData(fn: (ctx: DBDataContext) => MaybePromise<void>) {
|
||||
type DBDevDataContext = BaseDataContext & {
|
||||
seedReturning: <
|
||||
TWritable extends boolean,
|
||||
TFields extends FieldsConfig,
|
||||
TTable extends Table<string, TFields>,
|
||||
TData extends MaybeArray<SQLiteInsertValue<TTable>>,
|
||||
>(
|
||||
collection: ResolvedCollectionConfig<TFields, TWritable>,
|
||||
data: TData
|
||||
) => Promise<
|
||||
TData extends Array<SQLiteInsertValue<TTable>>
|
||||
? InferSelectModel<TTable>[]
|
||||
: InferSelectModel<TTable>
|
||||
>;
|
||||
mode: 'dev';
|
||||
};
|
||||
|
||||
type DBBuildDataContext = BaseDataContext & {
|
||||
seedReturning: <
|
||||
TWritable extends boolean,
|
||||
TFields extends FieldsConfig,
|
||||
TTable extends Table<string, TFields>,
|
||||
TData extends MaybeArray<SQLiteInsertValue<TTable>>,
|
||||
>(
|
||||
collection: ResolvedCollectionConfig<TFields, TWritable>,
|
||||
data: TData
|
||||
) => Promise<
|
||||
TWritable extends false
|
||||
? TData extends Array<SQLiteInsertValue<TTable>>
|
||||
? InferSelectModel<TTable>[]
|
||||
: InferSelectModel<TTable>
|
||||
: undefined
|
||||
>;
|
||||
mode: 'build';
|
||||
};
|
||||
|
||||
export function defineData(fn: (ctx: DBDevDataContext | DBBuildDataContext) => MaybePromise<void>) {
|
||||
return fn;
|
||||
}
|
||||
|
||||
|
@ -293,7 +319,7 @@ export const dbConfigSchema = z.object({
|
|||
});
|
||||
|
||||
export type DBUserConfig = Omit<z.input<typeof dbConfigSchema>, 'data'> & {
|
||||
data(params: DBDataContext): MaybePromise<void>;
|
||||
data(params: DBDevDataContext | DBBuildDataContext): MaybePromise<void>;
|
||||
};
|
||||
|
||||
export const astroConfigWithDbSchema = z.object({
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export { defineCollection, defineWritableCollection, defineData, field } from './core/types.js';
|
||||
export type { ResolvedCollectionConfig, DBDataContext } from './core/types.js';
|
||||
export type { ResolvedCollectionConfig, DBDataContext as DBDataContext } from './core/types.js';
|
||||
export { cli } from './core/cli/index.js';
|
||||
export { integration as default } from './core/integration/index.js';
|
||||
export { sql, NOW, TRUE, FALSE } from './runtime/index.js';
|
||||
|
|
|
@ -87,7 +87,7 @@ export type Table<
|
|||
{
|
||||
tableName: TTableName;
|
||||
name: K;
|
||||
hasDefault: TFields[K] extends { default: NonNullable<unknown> }
|
||||
hasDefault: TFields[K]['schema'] extends { default: NonNullable<unknown> }
|
||||
? true
|
||||
: TFields[K]['schema'] extends { primaryKey: true }
|
||||
? true
|
||||
|
|
14
packages/db/test/fixtures/basics/astro.config.ts
vendored
14
packages/db/test/fixtures/basics/astro.config.ts
vendored
|
@ -11,10 +11,10 @@ const Themes = defineWritableCollection({
|
|||
fields: {
|
||||
name: field.text(),
|
||||
added: field.date({
|
||||
default: sql`CURRENT_TIMESTAMP`
|
||||
default: sql`CURRENT_TIMESTAMP`,
|
||||
}),
|
||||
updated: field.date({
|
||||
default: NOW
|
||||
default: NOW,
|
||||
}),
|
||||
isDark: field.boolean({ default: sql`TRUE` }),
|
||||
owner: field.text({ optional: true, default: sql`NULL` }),
|
||||
|
@ -27,7 +27,7 @@ export default defineConfig({
|
|||
db: {
|
||||
studio: true,
|
||||
collections: { Author, Themes },
|
||||
async data({ seed, mode }) {
|
||||
async data({ seed }) {
|
||||
await seed(Author, [
|
||||
{ name: 'Ben' },
|
||||
{ name: 'Nate' },
|
||||
|
@ -35,13 +35,7 @@ export default defineConfig({
|
|||
{ name: 'Bjorn' },
|
||||
{ name: 'Sarah' },
|
||||
]);
|
||||
// Seed writable collections in dev mode, only
|
||||
if (mode === 'dev') {
|
||||
await seed(Themes, [
|
||||
{ name: 'dracula' },
|
||||
{ name: 'monokai' },
|
||||
]);
|
||||
}
|
||||
await seed(Themes, [{ name: 'dracula' }, { name: 'monokai' }]);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue