Skip to content

Commit 3c5db86

Browse files
committed
feat(builder)!: better schema definition
1 parent e5f3d98 commit 3c5db86

File tree

4 files changed

+65
-65
lines changed

4 files changed

+65
-65
lines changed

packages/sqlite-builder/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ choose [NodeWasmDialect](../dialect-wasm/README.md#nodewasmdialect)
3333

3434
```ts
3535
import { SqliteBuilder } from 'kysely-sqlite-builder'
36-
import { defineLiteral, defineObject, defineTable, useSchema } from 'kysely-sqlite-builder/schema'
36+
import { Column, defineTable, useSchema } from 'kysely-sqlite-builder/schema'
3737
import type { InferDatabase } from 'kysely-sqlite-builder/schema'
3838

3939
// schemas for AutoSyncTables
4040
const testTable = defineTable({
41-
id: { type: 'increments' },
42-
person: { type: 'object', defaultTo: { name: 'test' } },
43-
gender: { type: 'boolean', notNull: true },
44-
str: defineLiteral<'str1' | 'str2'>('str1'),
45-
array: defineObject<string[]>().NotNull(),
46-
buffer: { type: 'blob' },
41+
id: Column.Increments(),
42+
person: Column.Object({ name: 'test' }),
43+
gender: Column.Boolean().NotNull(),
44+
array: Column.Object<string[]>(),
45+
literal: Column.String<'l1' | 'l2'>(),
46+
buffer: Column.Blob(),
4747
}, {
4848
primary: 'id',
4949
index: ['person', ['id', 'gender']],
Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RawBuilder } from 'kysely'
22
import type { IsNotNull } from '@subframe7536/type-utils'
33
import type {
4+
ColumnType,
45
Columns,
56
ColumnsWithErrorInfo,
67
Table,
@@ -11,18 +12,16 @@ import type {
1112
export const TGR = '__TIME_TRIGGER__'
1213

1314
/**
14-
* define table function
15+
* define table, use it with {@link Column}
1516
*
16-
* if you want to explicitly declare column type,
17-
* use {@link defineObject} or {@link defineLiteral}
1817
* @example
1918
* const testTable = defineTable({
20-
* id: { type: 'increments' },
21-
* person: { type: 'object', defaultTo: { name: 'test' } },
22-
* gender: { type: 'boolean', notNull: true },
23-
* array: defineObject<string[]>(),
24-
* literal: defineLiteral<'l1' | 'l2'>(),
25-
* buffer: { type: 'blob' },
19+
* id: Column.Increments(),
20+
* person: Column.Object({ name: 'test' }),
21+
* gender: Column.Boolean().NotNull(),
22+
* array: Column.Object<string[]>(),
23+
* literal: Column.String<'l1' | 'l2'>(),
24+
* buffer: Column.Blob(),
2625
* }, {
2726
* primary: 'id',
2827
* index: ['person', ['id', 'gender']],
@@ -61,20 +60,9 @@ export function defineTable<
6160
}
6261
}
6362

64-
/**
65-
* explicitly declare object column type
66-
*
67-
* @example
68-
* ```ts
69-
* const pet = defineTable({
70-
* // NotNull is optional
71-
* owner: defineColumn<{ name: string }>().NotNull(),
72-
* }
73-
* ```
74-
*/
75-
export function defineObject<T extends object>(defaultTo?: T | RawBuilder<unknown> | null) {
63+
function base<T>(type: ColumnType, defaultTo?: T | RawBuilder<unknown> | null) {
7664
const base = {
77-
type: 'object',
65+
type,
7866
defaultTo: defaultTo as IsNotNull<typeof defaultTo> extends true ? T : T | null,
7967
} as const
8068
return {
@@ -87,29 +75,41 @@ export function defineObject<T extends object>(defaultTo?: T | RawBuilder<unknow
8775
},
8876
}
8977
}
78+
9079
/**
91-
* explicitly declare string column type
92-
*
93-
* @example
94-
* ```ts
95-
* const typeTable = defineTable({
96-
* // NotNull is optional
97-
* type: defineColumn<'generic' | 'custom'>().NotNull(),
98-
* }
99-
* ```
80+
* define column
10081
*/
101-
export function defineLiteral<T extends string>(defaultTo?: T | RawBuilder<unknown> | null) {
102-
const base = {
103-
type: 'string',
104-
defaultTo: defaultTo as IsNotNull<typeof defaultTo> extends true ? T : T | null,
105-
} as const
106-
return {
107-
...base,
108-
NotNull() {
109-
return {
110-
...base,
111-
notNull: true,
112-
} as const
113-
},
114-
}
82+
export const Column = {
83+
/**
84+
* column type: text
85+
*/
86+
String: <T extends string>(defaultTo?: T | RawBuilder<unknown> | null) => base('string', defaultTo),
87+
/**
88+
* column type: integer
89+
*/
90+
Int: <T extends number>(defaultTo?: T | RawBuilder<unknown> | null) => base('int', defaultTo),
91+
/**
92+
* column type: real
93+
*/
94+
Float: <T extends number>(defaultTo?: T | RawBuilder<unknown> | null) => base('float', defaultTo),
95+
/**
96+
* column type: blob
97+
*/
98+
Blob: () => base<ArrayBufferLike>('blob'),
99+
/**
100+
* column type: interger auto increment
101+
*/
102+
Increments: () => ({ type: 'increments' } as const),
103+
/**
104+
* column type: text (parse with `JSON.parse`)
105+
*/
106+
Boolean: (defaultTo?: boolean | RawBuilder<unknown> | null) => base('boolean', defaultTo),
107+
/**
108+
* column type: text (parse with `JSON.parse`)
109+
*/
110+
Date: (defaultTo?: Date | RawBuilder<unknown> | null) => base('date', defaultTo),
111+
/**
112+
* column type: text (parse with `JSON.parse`)
113+
*/
114+
Object: <T extends object>(defaultTo?: T | RawBuilder<unknown> | null) => base('object', defaultTo),
115115
}

packages/sqlite-builder/src/schema/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { syncTables } from './core'
77

88
export * from './types'
99

10-
export { defineTable, defineLiteral, defineObject } from './define'
10+
export { defineTable, Column } from './define'
1111

1212
/**
1313
* auto sync table using schema, only sync table/index/trigger

test/builder.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import Database from 'better-sqlite3'
33
import { beforeEach, describe, expect, it } from 'vitest'
44
import { getOrSetDBVersion, optimizePragma } from '../packages/sqlite-utils/src'
55
import type { InferDatabase } from '../packages/sqlite-builder/src/schema'
6-
import { defineLiteral, defineObject, defineTable, useSchema } from '../packages/sqlite-builder/src/schema'
6+
import { Column, defineTable, useSchema } from '../packages/sqlite-builder/src/schema'
77
import { SqliteBuilder } from '../packages/sqlite-builder/src'
88

99
const testTable = defineTable({
10-
id: { type: 'increments' },
11-
person: { type: 'object', defaultTo: { name: 'test' } },
12-
gender: { type: 'boolean', notNull: true },
13-
array: defineObject<string[]>(),
14-
literal: defineLiteral<'l1' | 'l2'>(),
15-
buffer: { type: 'blob' },
10+
id: Column.Increments(),
11+
person: Column.Object({ name: 'test' }),
12+
gender: Column.Boolean().NotNull(),
13+
array: Column.Object<string[]>(),
14+
literal: Column.String<'l1' | 'l2'>(),
15+
buffer: Column.Blob(),
1616
}, {
1717
primary: 'id',
1818
index: ['person', ['id', 'gender']],
@@ -67,11 +67,11 @@ describe('test sync table', async () => {
6767
it('should update and diff same table with columns', async () => {
6868
const foo = defineTable(
6969
{
70-
id: { type: 'increments' },
71-
person: { type: 'int' },
72-
bool: { type: 'boolean', notNull: true },
73-
array: defineObject<string[]>(),
74-
buffer: { type: 'blob' },
70+
id: Column.Increments(),
71+
person: Column.Int(),
72+
bool: Column.Boolean().NotNull(),
73+
array: Column.Object<string[]>(),
74+
buffer: Column.Blob(),
7575
},
7676
{
7777
primary: 'id',

0 commit comments

Comments
 (0)