0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

feat: db types extend

This commit is contained in:
Florian Lefebvre 2024-04-29 18:35:57 +02:00
parent 6f3b1672d3
commit c89cc7bf7d
6 changed files with 31 additions and 29 deletions

View file

@ -9,10 +9,10 @@ import { INTEGRATION_TABLE_CONFLICT_ERROR } from './errors.js';
import { errorMap } from './integration/error-map.js';
import { getConfigVirtualModContents } from './integration/vite-plugin-db.js';
import { dbConfigSchema } from './schemas.js';
import { type AstroDbIntegration } from './types.js';
import { type AstroDbHooks } from './types.js';
import { getDbDirectoryUrl } from './utils.js';
const isDbIntegration = (integration: AstroIntegration): integration is AstroDbIntegration =>
const isDbIntegration = (integration: AstroIntegration): integration is AstroIntegration & { hooks: AstroDbHooks } =>
'astro:db:setup' in integration.hooks;
/**

View file

@ -88,13 +88,11 @@ interface LegacyIndexConfig<TColumns extends ColumnsConfig>
export type NumberColumnOpts = z.input<typeof numberColumnOptsSchema>;
export type TextColumnOpts = z.input<typeof textColumnOptsSchema>;
export type AstroDbIntegration = AstroIntegration & {
hooks: {
'astro:db:setup'?: (options: {
extendDb: (options: {
configEntrypoint?: URL | string;
seedEntrypoint?: URL | string;
}) => void;
}) => void | Promise<void>;
};
};
export interface AstroDbHooks {
'astro:db:setup'?: (options: {
extendDb: (options: {
configEntrypoint?: URL | string;
seedEntrypoint?: URL | string;
}) => void;
}) => void | Promise<void>;
}

View file

@ -1,6 +1,5 @@
import type { AstroConfig, AstroIntegration } from 'astro';
import type { AstroConfig } from 'astro';
import { loadEnv } from 'vite';
import type { AstroDbIntegration } from './types.js';
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
@ -23,10 +22,6 @@ export function getDbDirectoryUrl(root: URL | string) {
return new URL('db/', root);
}
export function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration {
return integration;
}
export type Result<T> = { success: true; data: T } | { success: false; data: unknown };
/**

View file

@ -1,4 +1,3 @@
export { defineDbIntegration } from './core/utils.js';
import { tableSchema } from './core/schemas.js';
import type { ColumnsConfig, TableConfig } from './core/types.js';
import { type Table, asDrizzleTable as internal_asDrizzleTable } from './runtime/index.js';

View file

@ -1,15 +1,20 @@
import { defineDbIntegration } from '@astrojs/db/utils';
import type { AstroIntegration } from "astro"
import type { AstroDbHooks } from "@astrojs/db/types"
export default function testIntegration() {
return defineDbIntegration({
declare module "astro" {
interface AstroIntegrationHooks extends AstroDbHooks {}
}
export default function testIntegration(): AstroIntegration {
return {
name: 'db-test-integration',
hooks: {
'astro:db:setup'({ extendDb }) {
'astro:db:setup': ({ extendDb }) => {
extendDb({
configEntrypoint: './integration/config.ts',
seedEntrypoint: './integration/seed.ts',
});
},
},
});
};
}

View file

@ -1,15 +1,20 @@
import { defineDbIntegration } from '@astrojs/db/utils';
import type { AstroIntegration } from "astro"
import type { AstroDbHooks } from "@astrojs/db/types"
export default function testIntegration() {
return defineDbIntegration({
declare module "astro" {
interface AstroIntegrationHooks extends AstroDbHooks {}
}
export default function testIntegration(): AstroIntegration {
return {
name: 'db-test-integration',
hooks: {
'astro:db:setup'({ extendDb }) {
'astro:db:setup': ({ extendDb }) => {
extendDb({
configEntrypoint: './integration/config.ts',
seedEntrypoint: './integration/seed.ts',
});
},
},
});
};
}