bun-query-builder 0.1.2 → 0.1.6
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/actions/benchmark.d.ts +12 -0
- package/dist/actions/cache.d.ts +6 -0
- package/dist/actions/console.d.ts +2 -0
- package/dist/actions/data.d.ts +15 -0
- package/dist/actions/db-info.d.ts +17 -0
- package/dist/actions/db-optimize.d.ts +11 -0
- package/dist/actions/db-wipe.d.ts +10 -0
- package/dist/actions/file.d.ts +2 -1
- package/dist/actions/index.d.ts +25 -8
- package/dist/actions/inspect.d.ts +24 -0
- package/dist/actions/introspect.d.ts +2 -1
- package/dist/actions/make-model.d.ts +7 -0
- package/dist/actions/migrate-generate.d.ts +5 -0
- package/dist/actions/migrate-rollback.d.ts +6 -0
- package/dist/actions/migrate-status.d.ts +9 -0
- package/dist/actions/migrate.d.ts +13 -3
- package/dist/actions/model-show.d.ts +20 -0
- package/dist/actions/query-explain-all.d.ts +14 -0
- package/dist/actions/relation-diagram.d.ts +11 -0
- package/dist/actions/seed.d.ts +8 -0
- package/dist/actions/sql.d.ts +2 -1
- package/dist/actions/unsafe.d.ts +2 -1
- package/dist/actions/validate.d.ts +16 -0
- package/dist/actions/wait-ready.d.ts +2 -1
- package/dist/client.d.ts +230 -158
- package/dist/config.d.ts +2 -1
- package/dist/db.d.ts +10 -0
- package/dist/drivers/index.d.ts +12 -0
- package/dist/drivers/mysql.d.ts +187 -0
- package/dist/drivers/postgres.d.ts +173 -0
- package/dist/drivers/sqlite.d.ts +175 -0
- package/dist/factory.d.ts +3 -6
- package/dist/index.d.ts +11 -9
- package/dist/index.js +5444 -869
- package/dist/loader.d.ts +3 -2
- package/dist/meta.d.ts +10 -2
- package/dist/migrations.d.ts +43 -43
- package/dist/schema.d.ts +36 -177
- package/dist/seeder.d.ts +21 -0
- package/dist/types.d.ts +60 -143
- package/package.json +18 -27
- package/LICENSE.md +0 -21
- package/README.md +0 -150
package/dist/loader.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ModelRecord } from './schema';
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
export declare interface LoadModelsOptions {
|
|
4
4
|
cwd?: string
|
|
5
5
|
modelsDir: string
|
|
6
|
-
}
|
|
6
|
+
}
|
|
7
|
+
export declare function loadModels(options: LoadModelsOptions): Promise<ModelRecord>;
|
package/dist/meta.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ModelRecord } from './schema';
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
export declare interface SchemaMeta {
|
|
4
4
|
modelToTable: Record<string, string>
|
|
5
5
|
tableToModel: Record<string, string>
|
|
@@ -9,6 +9,14 @@ 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
|
+
}
|
|
22
|
+
export declare function buildSchemaMeta(models: ModelRecord): SchemaMeta;
|
package/dist/migrations.d.ts
CHANGED
|
@@ -1,30 +1,26 @@
|
|
|
1
1
|
import type { ModelRecord } from './schema';
|
|
2
2
|
import type { SupportedDialect } from './types';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* If there is no previous plan or the dialect changed, generates full SQL.
|
|
25
|
-
*/
|
|
26
|
-
export declare function generateDiffSql(previous: MigrationPlan | undefined, next: MigrationPlan): string[];
|
|
27
|
-
export declare interface ColumnPlan {
|
|
3
|
+
|
|
4
|
+
declare function findWorkspaceRoot(startPath: string): string;
|
|
5
|
+
declare function ensureSqlDirectory(): string;
|
|
6
|
+
declare function createMigrationFile(statement: string, fileName: string): boolean;
|
|
7
|
+
export declare type PrimitiveDefault = string | number | boolean | bigint | Date
|
|
8
|
+
|
|
9
|
+
export type NormalizedColumnType =
|
|
10
|
+
| 'string'
|
|
11
|
+
| 'text'
|
|
12
|
+
| 'boolean'
|
|
13
|
+
| 'integer'
|
|
14
|
+
| 'bigint'
|
|
15
|
+
| 'float'
|
|
16
|
+
| 'double'
|
|
17
|
+
| 'decimal'
|
|
18
|
+
| 'date'
|
|
19
|
+
| 'datetime'
|
|
20
|
+
| 'json'
|
|
21
|
+
| 'enum'
|
|
22
|
+
|
|
23
|
+
export interface ColumnPlan {
|
|
28
24
|
name: string
|
|
29
25
|
type: NormalizedColumnType
|
|
30
26
|
isPrimaryKey: boolean
|
|
@@ -33,33 +29,37 @@ export declare interface ColumnPlan {
|
|
|
33
29
|
hasDefault: boolean
|
|
34
30
|
defaultValue?: PrimitiveDefault
|
|
35
31
|
references?: { table: string, column: string }
|
|
32
|
+
enumValues?: string[]
|
|
36
33
|
}
|
|
37
|
-
|
|
34
|
+
|
|
35
|
+
export interface IndexPlan {
|
|
38
36
|
name: string
|
|
39
37
|
columns: string[]
|
|
40
38
|
type: 'index' | 'unique'
|
|
41
39
|
}
|
|
42
|
-
|
|
40
|
+
|
|
41
|
+
export interface TablePlan {
|
|
43
42
|
table: string
|
|
44
43
|
columns: ColumnPlan[]
|
|
45
44
|
indexes: IndexPlan[]
|
|
46
45
|
}
|
|
47
|
-
|
|
46
|
+
|
|
47
|
+
export interface MigrationPlan {
|
|
48
48
|
dialect: SupportedDialect
|
|
49
49
|
tables: TablePlan[]
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
export
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
51
|
+
declare function guessTypeFromName(columnName: string): NormalizedColumnType | undefined;
|
|
52
|
+
declare function normalizeDefaultValue(value: unknown): PrimitiveDefault | undefined;
|
|
53
|
+
declare function detectEnumFromValidationRule(rule: unknown): string[] | undefined;
|
|
54
|
+
declare function detectTypeFromValidationRule(rule: unknown): NormalizedColumnType | undefined;
|
|
55
|
+
export declare function buildMigrationPlan(models: ModelRecord, options: InferenceOptions): MigrationPlan;
|
|
56
|
+
export declare function generateSql(plan: MigrationPlan): string[];
|
|
57
|
+
export declare function generateSqlString(plan: MigrationPlan): string;
|
|
58
|
+
export declare function generateDiffSqlString(previous: MigrationPlan | undefined, next: MigrationPlan): string;
|
|
59
|
+
export declare function hashMigrationPlan(plan: MigrationPlan): string;
|
|
60
|
+
declare function canonicalize(value: any): any;
|
|
61
|
+
declare function mapTablesByName(tables: TablePlan[]): Record<string, TablePlan>;
|
|
62
|
+
declare function mapColumnsByName(columns: ColumnPlan[]): Record<string, ColumnPlan>;
|
|
63
|
+
declare function columnsAreDifferent(col1: ColumnPlan, col2: ColumnPlan): boolean;
|
|
64
|
+
declare function mapIndexesByKey(indexes: IndexPlan[]): Record<string, IndexPlan>;
|
|
65
|
+
export declare function generateDiffSql(previous: MigrationPlan | undefined, next: MigrationPlan): string[];
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,49 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* const Post = defineModel({
|
|
10
|
-
* name: 'Post',
|
|
11
|
-
* attributes: {
|
|
12
|
-
* title: { validation: { rule: {} // isLength({ min: 1 }) } },
|
|
13
|
-
* },
|
|
14
|
-
* })
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export declare function defineModel<const T extends ModelDefinition>(model: T): T;
|
|
18
|
-
/**
|
|
19
|
-
* # `defineModels(models)`
|
|
20
|
-
*
|
|
21
|
-
* Freezes and returns a record of model definitions, preserving literal keys so
|
|
22
|
-
* downstream types (like `DatabaseSchema`) can map model names to table names.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* const models = defineModels({ User, Post })
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export declare function defineModels<const T extends ModelRecord>(models: T): T;
|
|
30
|
-
/**
|
|
31
|
-
* # `Attribute`
|
|
32
|
-
*
|
|
33
|
-
* Describes a model column and its validation/meta options.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* const User = defineModel({
|
|
38
|
-
* name: 'User',
|
|
39
|
-
* attributes: {
|
|
40
|
-
* email: { validation: { rule: {} // isEmail() }, unique: true },
|
|
41
|
-
* age: { validation: { rule: {} // isInt({ min: 0 }) }, default: 0 },
|
|
42
|
-
* }
|
|
43
|
-
* })
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export declare interface Attribute {
|
|
1
|
+
export declare type ValidatorMessage = Record<string, string>
|
|
2
|
+
|
|
3
|
+
export type ValidationType = unknown
|
|
4
|
+
|
|
5
|
+
export interface Attribute {
|
|
47
6
|
default?: string | number | boolean | Date
|
|
48
7
|
unique?: boolean
|
|
49
8
|
order?: number
|
|
@@ -56,46 +15,33 @@ export declare interface Attribute {
|
|
|
56
15
|
message?: ValidatorMessage
|
|
57
16
|
}
|
|
58
17
|
}
|
|
59
|
-
export declare interface AttributesElements {
|
|
60
18
|
|
|
19
|
+
export interface AttributesElements {
|
|
20
|
+
[key: string]: Attribute
|
|
61
21
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
*
|
|
65
|
-
* Describes a named multi-column index.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* { name: 'user_email_unique', columns: ['email'] }
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
export declare interface CompositeIndex {
|
|
22
|
+
|
|
23
|
+
export interface CompositeIndex {
|
|
73
24
|
name: string
|
|
74
25
|
columns: string[]
|
|
75
26
|
}
|
|
76
|
-
export declare interface Base {
|
|
77
27
|
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
* })
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export declare interface ModelOptions extends Base {
|
|
28
|
+
export interface Base {}
|
|
29
|
+
|
|
30
|
+
export type ModelNames = string
|
|
31
|
+
|
|
32
|
+
export type HasOne<T extends string> = Record<string, T>
|
|
33
|
+
export type HasMany<T extends string> = Record<string, T>
|
|
34
|
+
export type BelongsTo<T extends string> = Record<string, T>
|
|
35
|
+
export type BelongsToMany<T extends string> = Record<string, T>
|
|
36
|
+
export type HasOneThrough<T extends string> = Record<string, { through: T, target: T }>
|
|
37
|
+
export type HasManyThrough<T extends string> = Record<string, { through: T, target: T }>
|
|
38
|
+
export type MorphOne<T extends string> = Record<string, T>
|
|
39
|
+
export type MorphMany<T extends string> = Record<string, T>
|
|
40
|
+
export type MorphTo = Record<string, unknown>
|
|
41
|
+
export type MorphToMany<T extends string> = Record<string, T>
|
|
42
|
+
export type MorphedByMany<T extends string> = Record<string, T>
|
|
43
|
+
|
|
44
|
+
export interface ModelOptions extends Base {
|
|
99
45
|
name: string
|
|
100
46
|
description?: string
|
|
101
47
|
table?: string
|
|
@@ -108,10 +54,13 @@ export declare interface ModelOptions extends Base {
|
|
|
108
54
|
hasMany?: HasMany<ModelNames> | ModelNames[]
|
|
109
55
|
belongsTo?: BelongsTo<ModelNames> | ModelNames[]
|
|
110
56
|
belongsToMany?: BelongsToMany<ModelNames> | ModelNames[]
|
|
111
|
-
hasOneThrough?: HasOneThrough<ModelNames>
|
|
112
|
-
|
|
113
|
-
|
|
57
|
+
hasOneThrough?: HasOneThrough<ModelNames>
|
|
58
|
+
hasManyThrough?: HasManyThrough<ModelNames>
|
|
59
|
+
morphOne?: MorphOne<ModelNames>
|
|
60
|
+
morphMany?: MorphMany<ModelNames>
|
|
114
61
|
morphTo?: MorphTo
|
|
62
|
+
morphToMany?: MorphToMany<ModelNames>
|
|
63
|
+
morphedByMany?: MorphedByMany<ModelNames>
|
|
115
64
|
scopes?: {
|
|
116
65
|
[key: string]: (value: any) => any
|
|
117
66
|
}
|
|
@@ -122,99 +71,9 @@ export declare interface ModelOptions extends Base {
|
|
|
122
71
|
[key: string]: (value: any) => any
|
|
123
72
|
}
|
|
124
73
|
}
|
|
125
|
-
|
|
126
|
-
* # `ValidatorMessage`
|
|
127
|
-
*
|
|
128
|
-
* Map of field identifiers to custom error messages returned by validators.
|
|
129
|
-
*/
|
|
130
|
-
export type ValidatorMessage = Record<string, string>
|
|
131
|
-
/**
|
|
132
|
-
* # `ValidationType`
|
|
133
|
-
*
|
|
134
|
-
* External validator rule type (compatible with ts-validation). Kept broad to
|
|
135
|
-
* avoid a hard dependency while still enabling type inference via rule shape.
|
|
136
|
-
*/
|
|
137
|
-
export type ValidationType = unknown
|
|
138
|
-
export type ModelNames = string
|
|
139
|
-
/**
|
|
140
|
-
* # Relationship helpers
|
|
141
|
-
*
|
|
142
|
-
* Lightweight relationship declarations for model definitions. Each helper is a
|
|
143
|
-
* record keyed by relation name with the related model name as value.
|
|
144
|
-
*/
|
|
145
|
-
export type HasOne<T extends string> = Record<string, T>
|
|
146
|
-
export type HasMany<T extends string> = Record<string, T>
|
|
147
|
-
export type BelongsTo<T extends string> = Record<string, T>
|
|
148
|
-
export type BelongsToMany<T extends string> = Record<string, T>
|
|
149
|
-
export type HasOneThrough<T extends string> = Record<string, T>
|
|
150
|
-
export type MorphOne<T extends string> = Record<string, T>
|
|
151
|
-
export type MorphMany<T extends string> = Record<string, T>
|
|
152
|
-
export type MorphTo = Record<string, unknown>
|
|
74
|
+
|
|
153
75
|
export type ModelDefinition = Readonly<ModelOptions>
|
|
154
|
-
|
|
155
|
-
* # `ModelRecord`
|
|
156
|
-
*
|
|
157
|
-
* Collection of models keyed by model name. Kept flexible to preserve literal
|
|
158
|
-
* attribute keys and value types.
|
|
159
|
-
*/
|
|
76
|
+
|
|
160
77
|
export type ModelRecord = Record<string, any>
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
*
|
|
164
|
-
* Given a `ModelDefinition`, produces a record of attribute names to their
|
|
165
|
-
* inferred input type based on the validator rule shape.
|
|
166
|
-
*/
|
|
167
|
-
declare type ExtractRuleInput<R> = R extends { validate: (value: infer T) => any }
|
|
168
|
-
? T
|
|
169
|
-
: R extends { test: (value: infer T) => any }
|
|
170
|
-
? T
|
|
171
|
-
: R extends { getRules: () => Array<{ test: (value: infer T) => any }> }
|
|
172
|
-
? T
|
|
173
|
-
: unknown
|
|
174
|
-
export type InferAttributes<M extends ModelDefinition> = M extends {
|
|
175
|
-
attributes: infer A extends Record<string, { validation: { rule: any } }>
|
|
176
|
-
}
|
|
177
|
-
? { [K in keyof A & string]: ExtractRuleInput<A[K]['validation']['rule']> }
|
|
178
|
-
: Record<string, unknown>
|
|
179
|
-
/**
|
|
180
|
-
* # `InferPrimaryKey<M>`
|
|
181
|
-
*
|
|
182
|
-
* Extracts a model's primary key field name, defaulting to `'id'`.
|
|
183
|
-
*/
|
|
184
|
-
export type InferPrimaryKey<M extends ModelDefinition> = M extends {
|
|
185
|
-
primaryKey: infer K extends string
|
|
186
|
-
}
|
|
187
|
-
? K
|
|
188
|
-
: 'id'
|
|
189
|
-
/**
|
|
190
|
-
* # `InferTableName<M>`
|
|
191
|
-
*
|
|
192
|
-
* Resolves the table name from a model: uses `table` when provided, otherwise
|
|
193
|
-
* falls back to a simple pluralized form of the model name.
|
|
194
|
-
*/
|
|
195
|
-
export type InferTableName<M extends ModelDefinition> = M extends {
|
|
196
|
-
table: infer T extends string
|
|
197
|
-
}
|
|
198
|
-
? T
|
|
199
|
-
: M extends { name: infer N extends string }
|
|
200
|
-
? `${Lowercase<N>}s`
|
|
201
|
-
: string
|
|
202
|
-
/**
|
|
203
|
-
* # `DatabaseSchema<Models>`
|
|
204
|
-
*
|
|
205
|
-
* Maps model definitions to a concrete database schema shape containing the
|
|
206
|
-
* table columns and primary key. This is the primary input for the query
|
|
207
|
-
* builder's type-safety.
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
|
-
* ```ts
|
|
211
|
-
* const models = defineModels({ User, Post })
|
|
212
|
-
* type Schema = DatabaseSchema<typeof models>
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
export type DatabaseSchema<MRecord extends ModelRecord> = {
|
|
216
|
-
[MName in keyof MRecord & string as InferTableName<MRecord[MName]>]: {
|
|
217
|
-
columns: InferAttributes<MRecord[MName]>
|
|
218
|
-
primaryKey: InferPrimaryKey<MRecord[MName]>
|
|
219
|
-
};
|
|
220
|
-
}
|
|
78
|
+
export declare function defineModel<const T extends ModelDefinition>(model: T): T;
|
|
79
|
+
export declare function defineModels<const T extends ModelRecord>(models: T): T;
|
package/dist/seeder.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare abstract class Seeder {
|
|
2
|
+
abstract run(qb: any): Promise<void>
|
|
3
|
+
|
|
4
|
+
get order(): number {
|
|
5
|
+
return 100
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get description(): string | undefined {
|
|
9
|
+
return undefined
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare interface SeederConfig {
|
|
13
|
+
seedersDir?: string
|
|
14
|
+
seeders?: string[]
|
|
15
|
+
verbose?: boolean
|
|
16
|
+
}
|
|
17
|
+
export declare interface RunSeederOptions {
|
|
18
|
+
class?: string
|
|
19
|
+
verbose?: boolean
|
|
20
|
+
}
|
|
21
|
+
export declare function defineSeeder(seederClass?: new ()): new () => Seeder;
|