From a084d8cec66e4fb1952bd0dfe293712401f2f463 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Tue, 12 Mar 2024 09:44:15 -0400 Subject: [PATCH] feat: split config types to separate build (#10401) * feat: split config types to separate build * chore: changeset * fix: drizzle property exports --- .changeset/beige-gorillas-warn.md | 5 +++ packages/db/package.json | 3 +- packages/db/src/core/consts.ts | 3 +- packages/db/src/core/integration/typegen.ts | 3 +- packages/db/tsconfig.config-types.json | 12 +++++++ packages/db/virtual.d.ts | 38 +++++++++++++++++---- 6 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 .changeset/beige-gorillas-warn.md create mode 100644 packages/db/tsconfig.config-types.json diff --git a/.changeset/beige-gorillas-warn.md b/.changeset/beige-gorillas-warn.md new file mode 100644 index 0000000000..6da46ac80a --- /dev/null +++ b/.changeset/beige-gorillas-warn.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Fix astro:db configuration types returning `any` diff --git a/packages/db/package.json b/packages/db/package.json index e8bc7d272f..fd8a6976f4 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -55,7 +55,8 @@ "astro-integration" ], "scripts": { - "build": "astro-scripts build \"src/**/*.ts\" && tsc", + "types:config": "tsc -p ./tsconfig.config-types.json", + "build": "astro-scripts build \"src/**/*.ts\" && tsc && pnpm types:config", "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", "test": "mocha --exit --timeout 20000 \"test/*.js\" \"test/unit/**/*.js\"", diff --git a/packages/db/src/core/consts.ts b/packages/db/src/core/consts.ts index 43279f6aa7..3ba7b43d74 100644 --- a/packages/db/src/core/consts.ts +++ b/packages/db/src/core/consts.ts @@ -5,8 +5,7 @@ export const PACKAGE_NAME = JSON.parse( ).name; export const RUNTIME_IMPORT = JSON.stringify(`${PACKAGE_NAME}/runtime`); -// Exposed without type definitions -// to avoid duplicate suggestions in Intellisense + export const RUNTIME_CONFIG_IMPORT = JSON.stringify(`${PACKAGE_NAME}/dist/runtime/config.js`); export const DB_TYPES_FILE = 'db-types.d.ts'; diff --git a/packages/db/src/core/integration/typegen.ts b/packages/db/src/core/integration/typegen.ts index 7c046dbfd3..9133c5dd40 100644 --- a/packages/db/src/core/integration/typegen.ts +++ b/packages/db/src/core/integration/typegen.ts @@ -1,6 +1,6 @@ import { existsSync } from 'node:fs'; import { mkdir, writeFile } from 'node:fs/promises'; -import { DB_TYPES_FILE, RUNTIME_CONFIG_IMPORT, RUNTIME_IMPORT } from '../consts.js'; +import { DB_TYPES_FILE, RUNTIME_IMPORT } from '../consts.js'; import type { DBTable, DBTables } from '../types.js'; export async function typegen({ tables, root }: { tables: DBTables; root: URL }) { @@ -8,7 +8,6 @@ export async function typegen({ tables, root }: { tables: DBTables; root: URL }) declare module 'astro:db' { export const db: import(${RUNTIME_IMPORT}).SqliteDB; export const dbUrl: string; - export * from ${RUNTIME_CONFIG_IMPORT}; ${Object.entries(tables) .map(([name, collection]) => generateTableType(name, collection)) diff --git a/packages/db/tsconfig.config-types.json b/packages/db/tsconfig.config-types.json new file mode 100644 index 0000000000..f21f8445d5 --- /dev/null +++ b/packages/db/tsconfig.config-types.json @@ -0,0 +1,12 @@ +{ + // We want to avoid defineTable() and defineDb() import hints + // from the runtime config export instead of astro:db. + // We exclude runtime/config from the base types, + // and generate to a separate _internal/ directory + // for our virtual module (virtual.d.ts) to reference. + "extends": "../../tsconfig.base.json", + "files": ["./src/runtime/config.ts"], + "compilerOptions": { + "outDir": "./dist/_internal" + } +} diff --git a/packages/db/virtual.d.ts b/packages/db/virtual.d.ts index 8538812e20..b97d2a19f2 100644 --- a/packages/db/virtual.d.ts +++ b/packages/db/virtual.d.ts @@ -1,9 +1,33 @@ declare module 'astro:db' { - export const sql: typeof import('./dist/runtime/config.js').sql; - export const NOW: typeof import('./dist/runtime/config.js').NOW; - export const TRUE: typeof import('./dist/runtime/config.js').TRUE; - export const FALSE: typeof import('./dist/runtime/config.js').FALSE; - export const column: typeof import('./dist/runtime/config.js').column; - export const defineDb: typeof import('./dist/runtime/config.js').defineDb; - export const defineTable: typeof import('./dist/runtime/config.js').defineTable; + type RuntimeConfig = typeof import('./dist/_internal/runtime/config.js'); + + export const sql: RuntimeConfig['sql']; + export const NOW: RuntimeConfig['NOW']; + export const TRUE: RuntimeConfig['TRUE']; + export const FALSE: RuntimeConfig['FALSE']; + export const column: RuntimeConfig['column']; + export const defineDb: RuntimeConfig['defineDb']; + export const defineTable: RuntimeConfig['defineTable']; + + export const eq: RuntimeConfig['eq']; + export const gt: RuntimeConfig['gt']; + export const gte: RuntimeConfig['gte']; + export const lt: RuntimeConfig['lt']; + export const lte: RuntimeConfig['lte']; + export const ne: RuntimeConfig['ne']; + export const isNull: RuntimeConfig['isNull']; + export const isNotNull: RuntimeConfig['isNotNull']; + export const inArray: RuntimeConfig['inArray']; + export const notInArray: RuntimeConfig['notInArray']; + export const exists: RuntimeConfig['exists']; + export const notExists: RuntimeConfig['notExists']; + export const between: RuntimeConfig['between']; + export const notBetween: RuntimeConfig['notBetween']; + export const like: RuntimeConfig['like']; + export const notIlike: RuntimeConfig['notIlike']; + export const not: RuntimeConfig['not']; + export const asc: RuntimeConfig['asc']; + export const desc: RuntimeConfig['desc']; + export const and: RuntimeConfig['and']; + export const or: RuntimeConfig['or']; }