mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
feat: data() fn with basic type safety
This commit is contained in:
parent
8d3ab37ac4
commit
9e8fb5c388
3 changed files with 34 additions and 9 deletions
|
@ -20,9 +20,15 @@ export const astroConfigWithDBValidator = z.object({
|
|||
db: adjustedConfigSchema.optional(),
|
||||
});
|
||||
|
||||
export function defineCollection(
|
||||
userConfig: z.input<typeof collectionSchema>
|
||||
): z.input<typeof collectionSchema> {
|
||||
type CollectionConfig<TFields extends z.input<typeof collectionSchema>['fields']> = {
|
||||
fields: TFields;
|
||||
// TODO: type inference based on field type. Just `any` for now.
|
||||
data?: () => Array<Record<keyof TFields, any> & { id?: string }>;
|
||||
};
|
||||
|
||||
export function defineCollection<TFields extends z.input<typeof collectionSchema>['fields']>(
|
||||
userConfig: CollectionConfig<TFields>
|
||||
): CollectionConfig<TFields> {
|
||||
return userConfig;
|
||||
}
|
||||
|
||||
|
|
|
@ -83,6 +83,22 @@ export function getCreateTableQuery(collectionName: string, collection: DBCollec
|
|||
return query;
|
||||
}
|
||||
|
||||
export async function executeCollectionDataFn({
|
||||
db,
|
||||
collectionName,
|
||||
collection,
|
||||
}: {
|
||||
db: LibSQLDatabase;
|
||||
collectionName: string;
|
||||
collection: DBCollection;
|
||||
}) {
|
||||
const { data } = collection;
|
||||
if (!data) return;
|
||||
|
||||
const table = collectionToTable(collectionName, collection);
|
||||
await db.insert(table).values(await data());
|
||||
}
|
||||
|
||||
function schemaTypeToSqlType(type: FieldType): 'text' | 'integer' {
|
||||
switch (type) {
|
||||
case 'date':
|
||||
|
@ -124,10 +140,6 @@ function hasDefault(field: DBField): field is DBFieldWithDefault {
|
|||
return field.default !== undefined;
|
||||
}
|
||||
|
||||
function hasRuntimeDefault(field: DBField): field is DBFieldWithDefault {
|
||||
return field.type === 'date' && field.default === 'now';
|
||||
}
|
||||
|
||||
function getDefaultValueSql(columnName: string, column: DBFieldWithDefault): string {
|
||||
switch (column.type) {
|
||||
case 'boolean':
|
||||
|
|
|
@ -52,6 +52,7 @@ const fieldsSchema = z.record(fieldSchema);
|
|||
|
||||
export const collectionSchema = z.object({
|
||||
fields: fieldsSchema,
|
||||
data: z.function().returns(fieldsSchema).optional(),
|
||||
});
|
||||
|
||||
export const collectionsSchema = z.record(collectionSchema);
|
||||
|
@ -74,8 +75,12 @@ export type FieldType =
|
|||
export type DBField = z.infer<typeof fieldSchema>;
|
||||
export type DBFieldInput = DateFieldInput | BooleanField | NumberField | TextField | JsonField;
|
||||
export type DBFields = z.infer<typeof fieldsSchema>;
|
||||
export type DBCollection = z.infer<typeof collectionSchema>;
|
||||
export type DBCollections = z.infer<typeof collectionsSchema>;
|
||||
export type DBCollection<TFields extends DBFields = DBFields> = {
|
||||
fields: TFields;
|
||||
// TODO: better `insert` types
|
||||
data?: () => MaybePromise<Array<Record<keyof TFields, any>>>;
|
||||
};
|
||||
export type DBCollections = Record<string, DBCollection>;
|
||||
|
||||
export type AstroTable<T extends Pick<TableConfig, 'name' | 'columns'>> = SQLiteTableWithColumns<
|
||||
T & {
|
||||
|
@ -157,3 +162,5 @@ export type AstroId<T extends Pick<GeneratedConfig<'string'>, 'tableName'>> = SQ
|
|||
baseColumn: never;
|
||||
}
|
||||
>;
|
||||
|
||||
export type MaybePromise<T> = T | Promise<T>;
|
||||
|
|
Loading…
Add table
Reference in a new issue