bun-query-builder 0.1.2 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/meta.d.ts CHANGED
@@ -9,6 +9,13 @@ export declare interface SchemaMeta {
9
9
  hasMany?: Record<string, string>
10
10
  belongsTo?: Record<string, string>
11
11
  belongsToMany?: Record<string, string>
12
+ hasOneThrough?: Record<string, { through: string, target: string }>
13
+ hasManyThrough?: Record<string, { through: string, target: string }>
14
+ morphOne?: Record<string, string>
15
+ morphMany?: Record<string, string>
16
+ morphTo?: Record<string, unknown>
17
+ morphToMany?: Record<string, string>
18
+ morphedByMany?: Record<string, string>
12
19
  }>
13
20
  scopes?: Record<string, Record<string, (qb: any, value?: any) => any>>
14
21
  }
@@ -15,11 +15,14 @@ export declare function generateDiffSqlString(previous: MigrationPlan | undefine
15
15
  */
16
16
  export declare function hashMigrationPlan(plan: MigrationPlan): string;
17
17
  /**
18
- * Generate safe, additive-only SQL to migrate from a previous plan to a new plan.
18
+ * Generate comprehensive SQL to migrate from a previous plan to a new plan.
19
19
  * - Creates new tables
20
- * - Adds new columns (no drops or type/nullable/default changes)
20
+ * - Drops removed tables
21
+ * - Adds new columns
22
+ * - Drops removed columns
23
+ * - Adds new indexes
24
+ * - Drops removed indexes
21
25
  * - Adds new foreign keys for newly added columns
22
- * - Adds new indexes and unique indexes
23
26
  *
24
27
  * If there is no previous plan or the dialect changed, generates full SQL.
25
28
  */
@@ -33,6 +36,7 @@ export declare interface ColumnPlan {
33
36
  hasDefault: boolean
34
37
  defaultValue?: PrimitiveDefault
35
38
  references?: { table: string, column: string }
39
+ enumValues?: string[]
36
40
  }
37
41
  export declare interface IndexPlan {
38
42
  name: string
@@ -62,4 +66,5 @@ export type NormalizedColumnType = | 'string'
62
66
  | 'decimal'
63
67
  | 'date'
64
68
  | 'datetime'
65
- | 'json'
69
+ | 'json'
70
+ | 'enum'
package/dist/schema.d.ts CHANGED
@@ -108,10 +108,13 @@ export declare interface ModelOptions extends Base {
108
108
  hasMany?: HasMany<ModelNames> | ModelNames[]
109
109
  belongsTo?: BelongsTo<ModelNames> | ModelNames[]
110
110
  belongsToMany?: BelongsToMany<ModelNames> | ModelNames[]
111
- hasOneThrough?: HasOneThrough<ModelNames> | ModelNames[]
112
- morphOne?: MorphOne<ModelNames> | ModelNames
113
- morphMany?: MorphMany<ModelNames>[] | ModelNames[]
111
+ hasOneThrough?: HasOneThrough<ModelNames>
112
+ hasManyThrough?: HasManyThrough<ModelNames>
113
+ morphOne?: MorphOne<ModelNames>
114
+ morphMany?: MorphMany<ModelNames>
114
115
  morphTo?: MorphTo
116
+ morphToMany?: MorphToMany<ModelNames>
117
+ morphedByMany?: MorphedByMany<ModelNames>
115
118
  scopes?: {
116
119
  [key: string]: (value: any) => any
117
120
  }
@@ -146,10 +149,13 @@ export type HasOne<T extends string> = Record<string, T>
146
149
  export type HasMany<T extends string> = Record<string, T>
147
150
  export type BelongsTo<T extends string> = Record<string, T>
148
151
  export type BelongsToMany<T extends string> = Record<string, T>
149
- export type HasOneThrough<T extends string> = Record<string, T>
152
+ export type HasOneThrough<T extends string> = Record<string, { through: T, target: T }>
153
+ export type HasManyThrough<T extends string> = Record<string, { through: T, target: T }>
150
154
  export type MorphOne<T extends string> = Record<string, T>
151
155
  export type MorphMany<T extends string> = Record<string, T>
152
156
  export type MorphTo = Record<string, unknown>
157
+ export type MorphToMany<T extends string> = Record<string, T>
158
+ export type MorphedByMany<T extends string> = Record<string, T>
153
159
  export type ModelDefinition = Readonly<ModelOptions>
154
160
  /**
155
161
  * # `ModelRecord`
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Helper function to create a seeder
3
+ */
4
+ export declare function defineSeeder(seederClass: new () => Seeder): new () => Seeder;
5
+ /**
6
+ * Seeder configuration options
7
+ */
8
+ export declare interface SeederConfig {
9
+ seedersDir?: string
10
+ seeders?: string[]
11
+ verbose?: boolean
12
+ }
13
+ /**
14
+ * Options for running seeders
15
+ */
16
+ export declare interface RunSeederOptions {
17
+ class?: string
18
+ verbose?: boolean
19
+ }
20
+ /**
21
+ * Base seeder class that all seeders should extend.
22
+ * Provides access to query builder and faker for generating test data.
23
+ */
24
+ export declare abstract class Seeder {
25
+ abstract run(qb: any): Promise<void>;
26
+ }
package/dist/types.d.ts CHANGED
@@ -91,6 +91,9 @@ export declare interface AliasingConfig {
91
91
  export declare interface RelationsConfig {
92
92
  foreignKeyFormat: 'singularParent_id' | 'parentId'
93
93
  singularizeStrategy?: 'stripTrailingS' | 'none'
94
+ maxDepth?: number
95
+ maxEagerLoad?: number
96
+ detectCycles?: boolean
94
97
  }
95
98
  /**
96
99
  * # `SqlConfig`
@@ -123,6 +126,12 @@ export declare interface QueryHooks {
123
126
  onQueryEnd?: (event: { sql: string, params?: any[], durationMs: number, rowCount?: number, kind?: 'select' | 'insert' | 'update' | 'delete' | 'raw' }) => void
124
127
  onQueryError?: (event: { sql: string, params?: any[], error: any, durationMs: number, kind?: 'select' | 'insert' | 'update' | 'delete' | 'raw' }) => void
125
128
  startSpan?: (event: { sql: string, params?: any[], kind?: 'select' | 'insert' | 'update' | 'delete' | 'raw' }) => { end: (error?: any) => void }
129
+ beforeCreate?: (event: { table: string, data: any }) => void | Promise<void>
130
+ afterCreate?: (event: { table: string, data: any, result: any }) => void | Promise<void>
131
+ beforeUpdate?: (event: { table: string, data: any, where?: any }) => void | Promise<void>
132
+ afterUpdate?: (event: { table: string, data: any, where?: any, result: any }) => void | Promise<void>
133
+ beforeDelete?: (event: { table: string, where?: any }) => void | Promise<void>
134
+ afterDelete?: (event: { table: string, where?: any, result: any }) => void | Promise<void>
126
135
  }
127
136
  /**
128
137
  * # `FeatureToggles`
@@ -134,6 +143,14 @@ export declare interface QueryHooks {
134
143
  export declare interface FeatureToggles {
135
144
  distinctOn: boolean
136
145
  }
146
+ export declare interface DatabaseConfig {
147
+ database: string
148
+ username: string
149
+ password: string
150
+ host: string
151
+ url?: string
152
+ port: number
153
+ }
137
154
  /**
138
155
  * # `QueryBuilderConfig`
139
156
  *
@@ -153,6 +170,7 @@ export declare interface FeatureToggles {
153
170
  export declare interface QueryBuilderConfig {
154
171
  verbose: boolean
155
172
  dialect: SupportedDialect
173
+ database: DatabaseConfig
156
174
  timestamps: TimestampConfig
157
175
  pagination: PaginationConfig
158
176
  aliasing: AliasingConfig
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.5",
5
5
  "description": "A simple yet performant query builder for TypeScript. Built with Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -41,20 +41,20 @@
41
41
  ],
42
42
  "scripts": {
43
43
  "build": "bun --bun build.ts && bun run compile",
44
- "compile": "bun build ./bin/cli.ts --compile --minify --outfile bin/query-builder",
44
+ "compile": "bun build ./bin/cli.ts --compile --minify --outfile bin/qbx",
45
45
  "compile:all": "bun run compile:linux-x64 && bun run compile:linux-arm64 && bun run compile:windows-x64 && bun run compile:darwin-x64 && bun run compile:darwin-arm64",
46
- "compile:linux-x64": "bun build ./bin/cli.ts --compile --minify --target=bun-linux-x64 --outfile bin/query-builder-linux-x64",
47
- "compile:linux-arm64": "bun build ./bin/cli.ts --compile --minify --target=bun-linux-arm64 --outfile bin/query-builder-linux-arm64",
48
- "compile:windows-x64": "bun build ./bin/cli.ts --compile --minify --target=bun-windows-x64 --outfile bin/query-builder-windows-x64.exe",
49
- "compile:darwin-x64": "bun build ./bin/cli.ts --compile --minify --target=bun-darwin-x64 --outfile bin/query-builder-darwin-x64",
50
- "compile:darwin-arm64": "bun build ./bin/cli.ts --compile --minify --target=bun-darwin-arm64 --outfile bin/query-builder-darwin-arm64",
46
+ "compile:linux-x64": "bun build ./bin/cli.ts --compile --minify --target=bun-linux-x64 --outfile bin/qbx-linux-x64",
47
+ "compile:linux-arm64": "bun build ./bin/cli.ts --compile --minify --target=bun-linux-arm64 --outfile bin/qbx-linux-arm64",
48
+ "compile:windows-x64": "bun build ./bin/cli.ts --compile --minify --target=bun-windows-x64 --outfile bin/qbx-windows-x64.exe",
49
+ "compile:darwin-x64": "bun build ./bin/cli.ts --compile --minify --target=bun-darwin-x64 --outfile bin/qbx-darwin-x64",
50
+ "compile:darwin-arm64": "bun build ./bin/cli.ts --compile --minify --target=bun-darwin-arm64 --outfile bin/qbx-darwin-arm64",
51
51
  "zip": "bun run zip:all",
52
52
  "zip:all": "bun run zip:linux-x64 && bun run zip:linux-arm64 && bun run zip:windows-x64 && bun run zip:darwin-x64 && bun run zip:darwin-arm64",
53
- "zip:linux-x64": "zip -j bin/query-builder-linux-x64.zip bin/query-builder-linux-x64",
54
- "zip:linux-arm64": "zip -j bin/query-builder-linux-arm64.zip bin/query-builder-linux-arm64",
55
- "zip:windows-x64": "zip -j bin/query-builder-windows-x64.zip bin/query-builder-windows-x64.exe",
56
- "zip:darwin-x64": "zip -j bin/query-builder-darwin-x64.zip bin/query-builder-darwin-x64",
57
- "zip:darwin-arm64": "zip -j bin/query-builder-darwin-arm64.zip bin/query-builder-darwin-arm64",
53
+ "zip:linux-x64": "zip -j bin/qbx-linux-x64.zip bin/qbx-linux-x64",
54
+ "zip:linux-arm64": "zip -j bin/qbx-linux-arm64.zip bin/qbx-linux-arm64",
55
+ "zip:windows-x64": "zip -j bin/qbx-windows-x64.zip bin/qbx-windows-x64.exe",
56
+ "zip:darwin-x64": "zip -j bin/qbx-darwin-x64.zip bin/qbx-darwin-x64",
57
+ "zip:darwin-arm64": "zip -j bin/qbx-darwin-arm64.zip bin/qbx-darwin-arm64",
58
58
  "fresh": "bunx rimraf node_modules/ bun.lock && bun i",
59
59
  "prepublishOnly": "bun --bun run build && bun run compile:all && bun run zip",
60
60
  "test": "bun test",
@@ -63,27 +63,17 @@
63
63
  "changelog": "bunx logsmith --verbose",
64
64
  "changelog:generate": "bunx logsmith --output CHANGELOG.md",
65
65
  "release": "bun run changelog:generate && bunx bumpx prompt --recursive",
66
- "postinstall": "bunx git-hooks",
67
66
  "dev:docs": "bun --bun vitepress dev docs",
68
67
  "build:docs": "bun --bun vitepress build docs",
69
68
  "preview:docs": "bun --bun vitepress preview docs",
70
- "typecheck": "bun --bun tsc --noEmit"
69
+ "typecheck": "bun --bun tsc"
71
70
  },
72
71
  "dependencies": {
73
- "@stacksjs/launchpad": "^0.6.4"
72
+ "@stacksjs/ts-validation": "^0.4.7",
73
+ "ts-mocker": "^0.1.3"
74
74
  },
75
75
  "devDependencies": {
76
- "@stacksjs/bumpx": "^0.1.61",
77
- "@stacksjs/docs": "^0.70.23",
78
- "@stacksjs/eslint-config": "^4.14.0-beta.3",
79
- "@stacksjs/gitlint": "^0.1.5",
80
- "@stacksjs/logsmith": "^0.1.15",
81
- "@types/bun": "^1.2.21",
82
- "buddy-bot": "^0.8.10",
83
- "bun-git-hooks": "^0.2.19",
84
- "bun-plugin-dtsx": "0.9.5",
85
- "bunfig": "^0.15.0",
86
- "typescript": "^5.9.2"
76
+ "bunfig": "^0.15.0"
87
77
  },
88
78
  "overrides": {
89
79
  "unconfig": "0.3.10"
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- # MIT License
2
-
3
- Copyright (c) 2024 Open Web Foundation
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/README.md DELETED
@@ -1,150 +0,0 @@
1
- <p align="center"><img src=".github/art/cover.jpg" alt="Social Card of this repo"></p>
2
-
3
- [![npm version][npm-version-src]][npm-version-href]
4
- [![GitHub Actions][github-actions-src]][github-actions-href]
5
- [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
6
- <!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
7
- <!-- [![Codecov][codecov-src]][codecov-href] -->
8
-
9
- # bun-query-builder
10
-
11
- Fully-typed, model-driven Query Builder for Bun’s native `sql`.
12
-
13
- Define your data model once and get a type-safe query experience _(a la Kysely/Laravel)_, powered by Bun’s tagged templates for safety and performance.
14
-
15
- ## Features
16
-
17
- - **Typed from Models**: Infer tables/columns/PKs from your model files; `selectFrom('users')` and `where({ active: true })` are typed.
18
- - **Fluent Builder**: `select/insert/update/delete`, `where/andWhere/orWhere`, `join/leftJoin/rightJoin/crossJoin`, `groupBy/having`, `union/unionAll`.
19
- - **Relations**: `with(...)`, `withCount(...)`, `whereHas(...)`, `selectAllRelations()` with configurable aliasing.
20
- - **Utilities**: `distinct/distinctOn`, `orderByDesc/latest/oldest/inRandomOrder`, `whereColumn/whereRaw/groupByRaw/havingRaw`, JSON/date helpers.
21
- - **Pagination**: `paginate`, `simplePaginate`, `cursorPaginate`, plus `chunk/chunkById/eachById`.
22
- - **Transactions**: `transaction` with retries/backoff/isolation/onRetry/afterCommit; `savepoint`; distributed tx helpers.
23
- - **Configurable**: Dialect hints, timestamps, alias strategies, relation FK formats, JSON mode, random function, shared lock syntax.
24
- - **Bun API passthroughs**: `unsafe`, `file`, `simple`, pool `reserve/release`, `close`, `ping/waitForReady`.
25
- - **CLI**: Introspection, query printing, connectivity checks, file/unsafe execution, explain.
26
-
27
- > Note: LISTEN/NOTIFY and COPY helpers are scaffolded and will be wired as Bun exposes native APIs.
28
-
29
- ## Get Started
30
-
31
- ### Installation
32
-
33
- ```bash
34
- bun add bun-query-builder
35
- ```
36
-
37
- ### Usage
38
-
39
- ```ts
40
- import { buildDatabaseSchema, buildSchemaMeta, createQueryBuilder } from 'bun-query-builder'
41
-
42
- // Load or define your model files (see docs for model shape)
43
- const models = {
44
- User: { name: 'User', table: 'users', primaryKey: 'id', attributes: { id: { validation: { rule: {} } }, name: { validation: { rule: {} } }, active: { validation: { rule: {} } } } },
45
- } as const
46
-
47
- const schema = buildDatabaseSchema(models as any)
48
- const meta = buildSchemaMeta(models as any)
49
- const db = createQueryBuilder<typeof schema>({ schema, meta })
50
-
51
- // Fully-typed query
52
- const q = db
53
- .selectFrom('users')
54
- .where({ active: true })
55
- .orderBy('created_at', 'desc')
56
- .limit(10)
57
-
58
- const rows = await q.execute()
59
- ```
60
-
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
-
79
- ### CLI
80
-
81
- ```bash
82
- # Print inferred schema from model dir
83
- query-builder introspect ./app/Models --verbose
84
-
85
- # Print a sample SQL (text) for a table
86
- query-builder sql ./app/Models users --limit 5
87
-
88
- # Connectivity:
89
- query-builder ping
90
- query-builder wait-ready --attempts 30 --delay 250
91
-
92
- # Execute a file or unsafe string (be careful!)
93
- query-builder file ./migrations/seed.sql
94
- query-builder unsafe "SELECT * FROM users WHERE id = $1" --params "[1]"
95
-
96
- # Explain a query
97
- query-builder explain "SELECT * FROM users WHERE active = true"
98
- ```
99
-
100
- ## Testing
101
-
102
- ```bash
103
- bun test
104
- ```
105
-
106
- ## Changelog
107
-
108
- Please see our [releases](https://github.com/stackjs/bun-query-builder/releases) page for more information on what has changed recently.
109
-
110
- ## Contributing
111
-
112
- Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
113
-
114
- ## Community
115
-
116
- For help, discussion about best practices, or any other conversation that would benefit from being searchable:
117
-
118
- [Discussions on GitHub](https://github.com/stacksjs/ts-starter/discussions)
119
-
120
- For casual chit-chat with others using this package:
121
-
122
- [Join the Stacks Discord Server](https://discord.gg/stacksjs)
123
-
124
- ## Postcardware
125
-
126
- “Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.
127
-
128
- Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
129
-
130
- ## Sponsors
131
-
132
- We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
133
-
134
- - [JetBrains](https://www.jetbrains.com/)
135
- - [The Solana Foundation](https://solana.com/)
136
-
137
- ## License
138
-
139
- The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.
140
-
141
- Made with 💙
142
-
143
- <!-- Badges -->
144
- [npm-version-src]: https://img.shields.io/npm/v/bun-query-builder?style=flat-square
145
- [npm-version-href]: https://npmjs.com/package/bun-query-builder
146
- [github-actions-src]: https://img.shields.io/github/actions/workflow/status/stacksjs/ts-starter/ci.yml?style=flat-square&branch=main
147
- [github-actions-href]: https://github.com/stacksjs/ts-starter/actions?query=workflow%3Aci
148
-
149
- <!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/ts-starter/main?style=flat-square
150
- [codecov-href]: https://codecov.io/gh/stacksjs/ts-starter -->