Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/reference/api/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ import { db, eq, and, or, desc, like, inArray, sql } from 'void/db';

**Full list:** `sql`, `eq`, `ne`, `gt`, `gte`, `lt`, `lte`, `and`, `or`, `not`, `desc`, `asc`, `like`, `ilike`, `notLike`, `inArray`, `notInArray`, `isNull`, `isNotNull`, `between`, `notBetween`, `exists`, `notExists`, `count`, `sum`, `avg`, `min`, `max`.

It also exports the Drizzle types `SQL`, `SQLWrapper`, `AnyColumn`, and `BatchItem` for helpers that take part of a query:

```ts
import { posts } from '@schema';
import { db, sql, type BatchItem, type SQL, type SQLWrapper } from 'void/db';

function contains(column: SQLWrapper, text: string): SQL {
return sql`instr(${column}, ${text}) > 0`;
}

// D1 only: `db.batch()` takes at least one query.
const writes: [BatchItem<'sqlite'>, ...Array<BatchItem<'sqlite'>>] = [
db.update(posts).set({ published: true }).where(contains(posts.title, 'void')),
];
await db.batch(writes);
```

## Seeding {#seeding}

Imported from `"void/seed"`.
Expand Down
3 changes: 3 additions & 0 deletions packages/void/src/codegen/db-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { writeFileIfChanged } from './write-if-changed.ts';
export const DRIZZLE_OPERATORS =
'sql, eq, ne, gt, gte, lt, lte, and, or, not, desc, asc, like, ilike, notLike, inArray, notInArray, isNull, isNotNull, between, notBetween, exists, notExists, count, sum, avg, min, max';

export const DRIZZLE_TYPES = 'AnyColumn, SQL, SQLWrapper, BatchItem';

const DIALECT_DB_TYPES = {
sqlite: {
type: 'DrizzleD1Database',
Expand Down Expand Up @@ -41,6 +43,7 @@ export function generateDbDts(
`export declare const db: ${type}<${schemaType}>;`,
`export declare function createDb(${arg}): ${type}<${schemaType}>;`,
`export { ${DRIZZLE_OPERATORS} } from "void/_db";`,
`export type { ${DRIZZLE_TYPES} } from "void/_db";`,
`export type { ${type} } from "${from}";`,
'',
];
Expand Down
2 changes: 2 additions & 0 deletions packages/void/src/runtime/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export {
min,
max,
} from 'drizzle-orm';
export type { AnyColumn, SQL, SQLWrapper } from 'drizzle-orm';
export type { BatchItem } from 'drizzle-orm/batch';
export type { DrizzleD1Database } from 'drizzle-orm/d1';

/**
Expand Down
44 changes: 44 additions & 0 deletions packages/void/test/unit/db-types.typecheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { expectTypeOf } from 'vite-plus/test';
import {
and,
asc,
createDb,
eq,
sql,
type AnyColumn,
type BatchItem,
type SQL,
type SQLWrapper,
} from '../../src/runtime/db.ts';

const posts = sqliteTable('posts', {
id: integer('id').primaryKey(),
title: text('title').notNull(),
});

declare const binding: D1Database;
const db = createDb(binding);

function contains(column: SQLWrapper, value: string): SQL {
return sql`instr(${column}, ${value}) > 0`;
}

function countIf(condition: SQL) {
return sql<number>`sum(case when ${condition} then 1 else 0 end)`;
}

function orderBy(column: AnyColumn) {
return asc(column);
}

const condition = and(eq(posts.id, 1), contains(posts.title, 'void'));
expectTypeOf(condition).toEqualTypeOf<SQL | undefined>();
countIf(eq(posts.id, 1));
orderBy(posts.title);

const writes: [BatchItem<'sqlite'>, ...Array<BatchItem<'sqlite'>>] = [
db.update(posts).set({ title: 'Void' }).where(eq(posts.id, 1)),
];
writes.push(db.insert(posts).values({ id: 2, title: 'Void' }));
void db.batch(writes);
14 changes: 14 additions & 0 deletions packages/void/test/unit/plugin-drizzle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@ describe('drizzlePlugin db typing generation', () => {
);
});

it.each(['sqlite', 'postgresql', 'mysql'] as const)(
'exports Drizzle query types from the %s database types',
(dialect) => {
const root = createProjectRoot();

drizzlePlugin(root, false, dialect);

const dts = readFileSync(join(root, '.void', 'db.d.ts'), 'utf8');
expect(dts).toContain(
'export type { AnyColumn, SQL, SQLWrapper, BatchItem } from "void/_db";',
);
},
);

it('generates MySQL database types from the dedicated runtime entry', () => {
const root = createProjectRoot();

Expand Down