0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

fix: just infer insert keys for now

This commit is contained in:
bholmesdev 2024-01-25 13:58:34 -05:00
parent efafea2a96
commit 285244e3d4
2 changed files with 20 additions and 6 deletions

View file

@ -8,7 +8,9 @@ import {
type collectionSchema,
collectionsSchema,
type MaybePromise,
type MaybeArray,
type Table,
type DBField,
} from './types.js';
import { z } from 'zod';
import { type SqliteDB } from './internal.js';
@ -61,17 +63,28 @@ type ResolvedCollectionConfig<
Writable extends boolean,
> = CollectionConfig<TFields, Writable> & {
writable: Writable;
set(data: SQLiteInsertValue<Table<string, TFields>>): Promise<any> /** TODO: type output */;
set(
data: MaybeArray<
SQLiteInsertValue<
Table<
string,
/** TODO: true type inference */ Record<Extract<keyof TFields, string>, DBField>
>
>
>
): Promise<any> /** TODO: type output */;
};
type SetData<TFields extends z.input<typeof collectionSchema>['fields']> = SQLiteInsertValue<
Table<string, TFields>
>;
export function defineCollection<TFields extends z.input<typeof collectionSchema>['fields']>(
userConfig: CollectionConfig<TFields, false>
): ResolvedCollectionConfig<TFields, false> {
let db: SqliteDB | undefined;
let table: Table<string, TFields> | undefined;
let table:
| Table<
string,
/** TODO: true type inference */ Record<Extract<keyof TFields, string>, DBField>
>
| undefined;
function _setEnv(env: { db: SqliteDB; table: Table<string, TFields> }) {
db = env.db;
table = env.table;
@ -81,7 +94,7 @@ export function defineCollection<TFields extends z.input<typeof collectionSchema
writable: false,
// @ts-expect-error keep private
_setEnv,
set: async (values: SetData<TFields>) => {
set: async (values) => {
if (!db || !table) {
throw new Error('Collection `.set()` can only be called during `data()` seeding.');
}

View file

@ -177,6 +177,7 @@ export type AstroId<T extends Pick<GeneratedConfig<'string'>, 'tableName'>> = SQ
>;
export type MaybePromise<T> = T | Promise<T>;
export type MaybeArray<T> = T | T[];
export type Column<T extends DBField['type'], S extends GeneratedConfig> = T extends 'boolean'
? AstroBoolean<S>