Skip to content

Commit f866596

Browse files
chore: wip
1 parent 9cccc67 commit f866596

File tree

12 files changed

+83
-52
lines changed

12 files changed

+83
-52
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ Define your data model once and get a type-safe query experience _(a la Kysely/L
2828
2929
## Get Started
3030

31-
Install and use in code:
31+
### Installation
32+
33+
```bash
34+
bun add bun-query-builder
35+
```
36+
37+
### Usage
3238

3339
```ts
3440
import { buildDatabaseSchema, buildSchemaMeta, createQueryBuilder } from 'bun-query-builder'
@@ -52,6 +58,24 @@ const q = db
5258
const rows = await q.execute()
5359
```
5460

61+
## Migrations
62+
63+
Generate and execute migrations from your models:
64+
65+
```ts
66+
import { generateMigration, executeMigration } from 'bun-query-builder'
67+
68+
// Generate migration from models directory
69+
const migration = await generateMigration('./models', {
70+
dialect: 'postgres',
71+
apply: true,
72+
full: true
73+
})
74+
75+
// Execute the migration
76+
await executeMigration(migration)
77+
```
78+
5579
### CLI
5680

5781
```bash

bin/cli.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
import type { CliOption, FileOptions, MigrateOptions, SqlOptions, UnsafeOptions } from '../src/types'
12
import { CAC } from 'cac'
23
import { version } from '../package.json'
34
import { explain, file, introspect, ping, sql, unsafe, waitReady } from '../src/actions'
45
import { generateMigration } from '../src/actions/migrate'
56

67
const cli = new CAC('query-builder')
78

8-
interface CliOption {
9-
verbose: boolean
10-
}
11-
129
cli
1310
.command('introspect <dir>', 'Load models and print inferred schema')
1411
.option('--verbose', 'Enable verbose logging')
@@ -21,7 +18,7 @@ cli
2118
.command('sql <dir> <table>', 'Build a sample query for a table')
2219
.option('--limit <n>', 'Limit rows', { default: 10 })
2320
.example('query-builder sql ./app/Models users --limit 5')
24-
.action(async (dir: string, table: string, opts: any) => {
21+
.action(async (dir: string, table: string, opts: SqlOptions) => {
2522
await sql(dir, table, opts)
2623
})
2724

@@ -51,7 +48,7 @@ cli
5148
.command('unsafe <sql>', 'Execute an unsafe SQL string (one statement with params)')
5249
.option('--params <json>', 'JSON array of parameters')
5350
.example('query-builder unsafe "SELECT * FROM users WHERE id = $1" --params "[1]"')
54-
.action(async (sql: string, opts: any) => {
51+
.action(async (sql: string, opts: UnsafeOptions) => {
5552
await unsafe(sql, opts)
5653
})
5754

@@ -60,7 +57,7 @@ cli
6057
.option('--params <json>', 'JSON array of parameters')
6158
.example('query-builder file ./migrations/seed.sql')
6259
.example('query-builder file ./reports/top.sql --params "[30]"')
63-
.action(async (path: string, opts: any) => {
60+
.action(async (path: string, opts: FileOptions) => {
6461
await file(path, opts)
6562
})
6663

@@ -71,7 +68,7 @@ cli
7168
.option('--apply', 'Execute the generated SQL against the database')
7269
.option('--full', 'Force full migration SQL instead of incremental diff')
7370
.example('query-builder migrate ./app/Models --dialect postgres')
74-
.action(async (dir: string, opts: any) => {
71+
.action(async (dir: string, opts: MigrateOptions) => {
7572
try {
7673
await generateMigration(dir, {
7774
dialect: opts.dialect,

examples/test-queries.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ async function basicSelectQuery() {
3030
async function simpleMigration() {
3131
const migration = await generateMigration('./models', { dialect: 'postgres', apply: true, full: true })
3232

33-
const sqlScript = migration.sqlStatements
34-
35-
console.log(sqlScript)
36-
37-
await executeMigration(sqlScript)
33+
await executeMigration(migration)
3834
}
3935

4036
// Export for use in other files

src/actions/file.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
import type { FileOptions } from '../types'
12
import { createQueryBuilder } from '../index'
23

3-
export interface FileOptions {
4-
params?: string
5-
}
6-
74
export async function file(path: string, opts: FileOptions = {}) {
85
const qb = createQueryBuilder()
96
const params = opts.params ? JSON.parse(opts.params) : undefined

src/actions/introspect.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { buildDatabaseSchema, loadModels } from '../index'
1+
import type { IntrospectOptions } from '../types'
22

3-
export interface IntrospectOptions {
4-
verbose?: boolean
5-
}
3+
import { buildDatabaseSchema, loadModels } from '../index'
64

75
export async function introspect(dir: string, _opts: IntrospectOptions = {}) {
86
const models = await loadModels({ modelsDir: dir })

src/actions/migrate.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
import type { SupportedDialect } from '../types'
1+
import type { GenerateMigrationResult, MigrateOptions, SupportedDialect } from '../types'
22
import { sql as bunSql } from 'bun'
33
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
44
import { tmpdir } from 'node:os'
55
import { join } from 'node:path'
66
import { buildMigrationPlan, createQueryBuilder, generateDiffSql, generateSql, hashMigrationPlan, loadModels } from '../'
77

8-
export interface MigrateOptions {
9-
dialect?: SupportedDialect
10-
state?: string
11-
apply?: boolean
12-
full?: boolean
13-
}
14-
15-
export interface GenerateMigrationResult {
16-
sql: string
17-
sqlStatements: string[]
18-
hasChanges: boolean
19-
plan: any
20-
}
21-
228
export async function generateMigration(dir: string, opts: MigrateOptions = {}): Promise<GenerateMigrationResult> {
239
const dialect = String(opts.dialect || 'postgres') as SupportedDialect
2410
const models = await loadModels({ modelsDir: dir })
@@ -69,7 +55,9 @@ export async function generateMigration(dir: string, opts: MigrateOptions = {}):
6955
return { sql, sqlStatements, hasChanges, plan }
7056
}
7157

72-
export async function executeMigration(sqlStatements: string[]): Promise<boolean> {
58+
export async function executeMigration(migration: GenerateMigrationResult): Promise<boolean> {
59+
const { sqlStatements } = migration
60+
7361
try {
7462
for (const sql of sqlStatements) {
7563
// Use raw SQL execution instead of template literal to avoid parameter binding issues

src/actions/sql.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
import type { SqlOptions } from '../types'
12
import { config } from '../config'
23
import { buildDatabaseSchema, createQueryBuilder, loadModels } from '../index'
34

4-
export interface SqlOptions {
5-
limit?: number
6-
}
7-
85
export function sql(dir: string, table: string, opts: SqlOptions = {}) {
96
const models = loadModels({ modelsDir: dir })
107
const dbSchema = buildDatabaseSchema(models)

src/actions/unsafe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
import type { UnsafeOptions } from '../types'
12
import { createQueryBuilder } from '../index'
23

3-
export interface UnsafeOptions {
4-
params?: string
5-
}
6-
74
export async function unsafe(sql: string, opts: UnsafeOptions = {}) {
85
const qb = createQueryBuilder()
96
const params = opts.params ? JSON.parse(opts.params) : undefined

src/actions/wait-ready.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1+
import type { WaitReadyOptions } from '../types'
12
import { createQueryBuilder } from '../index'
23

3-
export interface WaitReadyOptions {
4-
attempts?: number
5-
delay?: number
6-
}
7-
84
export async function waitReady(opts: WaitReadyOptions = {}) {
95
const attempts = Number(opts.attempts || 10)
106
const delayMs = Number(opts.delay || 100)

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './actions'
12
export * from './client'
23
export * from './config'
34
export * from './factory'

0 commit comments

Comments
 (0)