mirror of
https://github.com/withastro/astro.git
synced 2025-03-03 22:57:08 -05:00
refactor: clean up types
This commit is contained in:
parent
87230f6982
commit
49b82a0e7d
4 changed files with 44 additions and 67 deletions
|
@ -1,5 +1,6 @@
|
|||
import type { Table } from './internal.js';
|
||||
import type { collectionSchema } from './types.js';
|
||||
import type { SQLiteInsertValue } from 'drizzle-orm/sqlite-core';
|
||||
import type { SqliteDB, Table } from './internal.js';
|
||||
import type { MaybeArray, collectionSchema } from './types.js';
|
||||
import {
|
||||
type BooleanField,
|
||||
type DBFieldInput,
|
||||
|
@ -9,14 +10,33 @@ import {
|
|||
type TextField,
|
||||
collectionsSchema,
|
||||
type MaybePromise,
|
||||
type DbDataContext,
|
||||
} from './types.js';
|
||||
import { z } from 'zod';
|
||||
|
||||
export type DBFieldsConfig = z.input<typeof collectionSchema>['fields'];
|
||||
|
||||
export type DBDataContext = {
|
||||
db: SqliteDB;
|
||||
seed<TFields extends DBFieldsConfig>(
|
||||
collection: ResolvedCollectionConfig<TFields>,
|
||||
data: MaybeArray<
|
||||
SQLiteInsertValue<
|
||||
Table<
|
||||
string,
|
||||
/** TODO: true type inference */ Record<
|
||||
Extract<keyof TFields, string>,
|
||||
DBFieldsConfig[number]
|
||||
>
|
||||
>
|
||||
>
|
||||
>
|
||||
): Promise<any> /** TODO: type output */;
|
||||
mode: 'dev' | 'build';
|
||||
};
|
||||
|
||||
export const dbConfigSchema = z.object({
|
||||
studio: z.boolean().optional(),
|
||||
collections: collectionsSchema.optional(),
|
||||
// TODO: strict types
|
||||
data: z
|
||||
.function()
|
||||
.returns(z.union([z.void(), z.promise(z.void())]))
|
||||
|
@ -24,49 +44,34 @@ export const dbConfigSchema = z.object({
|
|||
});
|
||||
|
||||
export type DBUserConfig = Omit<z.input<typeof dbConfigSchema>, 'data'> & {
|
||||
data(params: DbDataContext): MaybePromise<void>;
|
||||
data(params: DBDataContext): MaybePromise<void>;
|
||||
};
|
||||
|
||||
export const astroConfigWithDbSchema = z.object({
|
||||
db: dbConfigSchema.optional(),
|
||||
});
|
||||
|
||||
type CollectionMeta<TFields extends z.input<typeof collectionSchema>['fields']> = {
|
||||
type CollectionMeta<TFields extends DBFieldsConfig> = {
|
||||
// Collection table is set later when running the data() function.
|
||||
// Collection config is assigned to an object key,
|
||||
// so the collection itself does not know the table name.
|
||||
table: Table<string, TFields>;
|
||||
};
|
||||
|
||||
type CollectionConfig<
|
||||
TFields extends z.input<typeof collectionSchema>['fields'],
|
||||
Writable extends boolean,
|
||||
> = Writable extends true
|
||||
? {
|
||||
fields: TFields;
|
||||
// TODO: type inference based on field type. Just `any` for now.
|
||||
seed?: Writable extends false
|
||||
? never
|
||||
: () => MaybePromise<Array<Record<keyof TFields, any> & { id?: string }>>;
|
||||
}
|
||||
: {
|
||||
fields: TFields;
|
||||
// TODO: type inference based on field type. Just `any` for now.
|
||||
data?: Writable extends true
|
||||
? never
|
||||
: () => MaybePromise<Array<Record<keyof TFields, any> & { id?: string }>>;
|
||||
};
|
||||
type CollectionConfig<TFields extends DBFieldsConfig> = {
|
||||
fields: TFields;
|
||||
};
|
||||
|
||||
export type ResolvedCollectionConfig<
|
||||
TFields extends z.input<typeof collectionSchema>['fields'],
|
||||
TFields extends DBFieldsConfig = DBFieldsConfig,
|
||||
Writable extends boolean = boolean,
|
||||
> = CollectionConfig<TFields, Writable> & {
|
||||
> = CollectionConfig<TFields> & {
|
||||
writable: Writable;
|
||||
table: Table<string, TFields>;
|
||||
};
|
||||
|
||||
export function defineCollection<TFields extends z.input<typeof collectionSchema>['fields']>(
|
||||
userConfig: CollectionConfig<TFields, false>
|
||||
export function defineCollection<TFields extends DBFieldsConfig>(
|
||||
userConfig: CollectionConfig<TFields>
|
||||
): ResolvedCollectionConfig<TFields, false> {
|
||||
const meta: CollectionMeta<TFields> = { table: null! };
|
||||
function _setMeta(values: CollectionMeta<TFields>) {
|
||||
|
@ -83,19 +88,19 @@ export function defineCollection<TFields extends z.input<typeof collectionSchema
|
|||
};
|
||||
}
|
||||
|
||||
export function defineWritableCollection<
|
||||
TFields extends z.input<typeof collectionSchema>['fields'],
|
||||
>(userConfig: CollectionConfig<TFields, true>): ResolvedCollectionConfig<TFields, true> {
|
||||
const meta: CollectionMeta<TFields> = {
|
||||
table: null!,
|
||||
};
|
||||
export function defineWritableCollection<TFields extends DBFieldsConfig>(
|
||||
userConfig: CollectionConfig<TFields>
|
||||
): ResolvedCollectionConfig<TFields, true> {
|
||||
const meta: CollectionMeta<TFields> = { table: null! };
|
||||
function _setMeta(values: CollectionMeta<TFields>) {
|
||||
Object.assign(meta, values);
|
||||
}
|
||||
return {
|
||||
...userConfig,
|
||||
writable: true,
|
||||
table: meta.table,
|
||||
get table() {
|
||||
return meta.table;
|
||||
},
|
||||
// @ts-expect-error private field
|
||||
_setMeta,
|
||||
};
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
export { defineCollection, defineWritableCollection, field } from './config.js';
|
||||
|
||||
export { type DbDataContext } from './types.js';
|
||||
export { type ResolvedCollectionConfig } from './config.js';
|
||||
export { type ResolvedCollectionConfig, type DBDataContext } from './config.js';
|
||||
export { cli } from './cli/index.js';
|
||||
export { integration as default } from './integration.js';
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
import type { ColumnDataType, ColumnBaseConfig } from 'drizzle-orm';
|
||||
import type {
|
||||
SQLiteColumn,
|
||||
SQLiteInsertValue,
|
||||
SQLiteTableWithColumns,
|
||||
} from 'drizzle-orm/sqlite-core';
|
||||
import type { SQLiteColumn, SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
|
||||
import { z } from 'zod';
|
||||
import type { SqliteDB } from './internal.js';
|
||||
import type { ResolvedCollectionConfig } from './config.js';
|
||||
|
||||
const baseFieldSchema = z.object({
|
||||
label: z.string().optional(),
|
||||
|
@ -113,27 +107,6 @@ type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<
|
|||
'name' | 'tableName' | 'notNull' | 'hasDefault'
|
||||
>;
|
||||
|
||||
type DbFieldsConfig = z.input<typeof collectionSchema>['fields'];
|
||||
|
||||
export type DbDataContext = {
|
||||
db: SqliteDB;
|
||||
seed<TFields extends DbFieldsConfig>(
|
||||
collection: ResolvedCollectionConfig<TFields>,
|
||||
data: MaybeArray<
|
||||
SQLiteInsertValue<
|
||||
Table<
|
||||
string,
|
||||
/** TODO: true type inference */ Record<
|
||||
Extract<keyof TFields, string>,
|
||||
DbFieldsConfig[number]
|
||||
>
|
||||
>
|
||||
>
|
||||
>
|
||||
): Promise<any> /** TODO: type output */;
|
||||
mode: 'dev' | 'build';
|
||||
};
|
||||
|
||||
export type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<
|
||||
T & {
|
||||
data: string;
|
||||
|
|
4
packages/db/test/fixtures/glob/utils.ts
vendored
4
packages/db/test/fixtures/glob/utils.ts
vendored
|
@ -2,9 +2,9 @@ import fastGlob from 'fast-glob';
|
|||
import { readFile } from 'fs/promises';
|
||||
import chokidar from 'chokidar';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { type ResolvedCollectionConfig, type DbDataContext } from '@astrojs/db';
|
||||
import { type ResolvedCollectionConfig, type DBDataContext } from '@astrojs/db';
|
||||
|
||||
export function createGlob({ db, mode }: Pick<DbDataContext, 'db' | 'mode'>) {
|
||||
export function createGlob({ db, mode }: Pick<DBDataContext, 'db' | 'mode'>) {
|
||||
return async function glob(
|
||||
pattern: string,
|
||||
opts: {
|
||||
|
|
Loading…
Add table
Reference in a new issue