Skip to content

Commit

Permalink
feat: split config types to separate build (#10401)
Browse files Browse the repository at this point in the history
* feat: split config types to separate build

* chore: changeset

* fix: drizzle property exports
  • Loading branch information
bholmesdev authored Mar 12, 2024
1 parent 24ee74a commit a084d8c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-gorillas-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Fix astro:db configuration types returning `any`
3 changes: 2 additions & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
3 changes: 1 addition & 2 deletions packages/db/src/core/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 1 addition & 2 deletions packages/db/src/core/integration/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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 }) {
const content = `// This file is generated by \`studio sync\`
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))
Expand Down
12 changes: 12 additions & 0 deletions packages/db/tsconfig.config-types.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
38 changes: 31 additions & 7 deletions packages/db/virtual.d.ts
Original file line number Diff line number Diff line change
@@ -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'];
}

0 comments on commit a084d8c

Please sign in to comment.