@onebun/drizzle 0.1.4 → 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/bin/drizzle-kit.ts +34 -0
- package/package.json +13 -4
- package/src/drizzle.module.ts +94 -6
- package/src/drizzle.service.ts +2 -2
- package/src/index.ts +74 -0
- package/src/pg.ts +67 -0
- package/src/sqlite.ts +41 -0
- package/src/types.ts +8 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
/**
|
|
3
|
+
* CLI wrapper for drizzle-kit
|
|
4
|
+
*
|
|
5
|
+
* This wrapper ensures that the correct version of drizzle-kit is used
|
|
6
|
+
* (the one installed with \@onebun/drizzle) instead of a potentially
|
|
7
|
+
* different version that bunx might download from npm.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* bunx onebun-drizzle generate
|
|
11
|
+
* bunx onebun-drizzle push
|
|
12
|
+
* bunx onebun-drizzle studio
|
|
13
|
+
*/
|
|
14
|
+
import { dirname, join } from 'path';
|
|
15
|
+
import { fileURLToPath } from 'url';
|
|
16
|
+
|
|
17
|
+
import { spawn } from 'bun';
|
|
18
|
+
|
|
19
|
+
// Find drizzle-kit binary - resolve the package path without executing it
|
|
20
|
+
const drizzleKitModulePath = import.meta.resolve('drizzle-kit');
|
|
21
|
+
const drizzleKitDir = dirname(fileURLToPath(drizzleKitModulePath));
|
|
22
|
+
const drizzleKitBin = join(drizzleKitDir, 'bin.cjs');
|
|
23
|
+
|
|
24
|
+
// Pass all arguments to drizzle-kit
|
|
25
|
+
const args = Bun.argv.slice(2);
|
|
26
|
+
|
|
27
|
+
const proc = spawn(['bun', drizzleKitBin, ...args], {
|
|
28
|
+
stdout: 'inherit',
|
|
29
|
+
stderr: 'inherit',
|
|
30
|
+
stdin: 'inherit',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const exitCode = await proc.exited;
|
|
34
|
+
process.exit(exitCode);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onebun/drizzle",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Drizzle ORM module for OneBun framework - SQLite and PostgreSQL support",
|
|
5
5
|
"license": "LGPL-3.0",
|
|
6
6
|
"author": "RemRyahirev",
|
|
@@ -27,20 +27,29 @@
|
|
|
27
27
|
"access": "public",
|
|
28
28
|
"registry": "https://registry.npmjs.org/"
|
|
29
29
|
},
|
|
30
|
+
"bin": {
|
|
31
|
+
"onebun-drizzle": "./bin/drizzle-kit.ts"
|
|
32
|
+
},
|
|
30
33
|
"files": [
|
|
31
34
|
"src",
|
|
35
|
+
"bin",
|
|
32
36
|
"README.md"
|
|
33
37
|
],
|
|
34
38
|
"main": "src/index.ts",
|
|
35
39
|
"module": "src/index.ts",
|
|
36
40
|
"types": "src/index.ts",
|
|
41
|
+
"exports": {
|
|
42
|
+
".": "./src/index.ts",
|
|
43
|
+
"./pg": "./src/pg.ts",
|
|
44
|
+
"./sqlite": "./src/sqlite.ts"
|
|
45
|
+
},
|
|
37
46
|
"scripts": {
|
|
38
47
|
"dev": "bun run --watch src/index.ts"
|
|
39
48
|
},
|
|
40
49
|
"dependencies": {
|
|
41
|
-
"@onebun/core": "^0.1.
|
|
42
|
-
"@onebun/envs": "^0.1.
|
|
43
|
-
"@onebun/logger": "^0.1.
|
|
50
|
+
"@onebun/core": "^0.1.9",
|
|
51
|
+
"@onebun/envs": "^0.1.3",
|
|
52
|
+
"@onebun/logger": "^0.1.4",
|
|
44
53
|
"drizzle-orm": "^0.44.7",
|
|
45
54
|
"drizzle-kit": "^0.31.6",
|
|
46
55
|
"drizzle-arktype": "^0.1.3",
|
package/src/drizzle.module.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { DrizzleModuleOptions } from './types';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
Global,
|
|
5
|
+
Module,
|
|
6
|
+
removeFromGlobalModules,
|
|
7
|
+
} from '@onebun/core';
|
|
4
8
|
|
|
5
9
|
|
|
6
10
|
import { DrizzleService } from './drizzle.service';
|
|
@@ -15,6 +19,9 @@ const DRIZZLE_MODULE_OPTIONS = Symbol('DRIZZLE_MODULE_OPTIONS');
|
|
|
15
19
|
* Uses Bun.SQL for PostgreSQL (built-in Bun adapter) and bun:sqlite for SQLite.
|
|
16
20
|
* No external database drivers required - uses native Bun capabilities.
|
|
17
21
|
*
|
|
22
|
+
* By default, DrizzleModule is global - DrizzleService is available in all modules
|
|
23
|
+
* without explicit import. Use `isGlobal: false` to disable this behavior.
|
|
24
|
+
*
|
|
18
25
|
* Configuration is loaded from environment variables by default,
|
|
19
26
|
* but can be overridden with module options.
|
|
20
27
|
*
|
|
@@ -26,7 +33,7 @@ const DRIZZLE_MODULE_OPTIONS = Symbol('DRIZZLE_MODULE_OPTIONS');
|
|
|
26
33
|
* - DB_AUTO_MIGRATE: Whether to run migrations on startup (default: false)
|
|
27
34
|
* - DB_LOG_QUERIES: Whether to log SQL queries (default: false)
|
|
28
35
|
*
|
|
29
|
-
* @example Basic usage with environment variables
|
|
36
|
+
* @example Basic usage with environment variables (global by default)
|
|
30
37
|
* ```typescript
|
|
31
38
|
* import { Module } from '@onebun/core';
|
|
32
39
|
* import { DrizzleModule } from '@onebun/drizzle';
|
|
@@ -36,6 +43,13 @@ const DRIZZLE_MODULE_OPTIONS = Symbol('DRIZZLE_MODULE_OPTIONS');
|
|
|
36
43
|
* controllers: [MyController],
|
|
37
44
|
* })
|
|
38
45
|
* export class AppModule {}
|
|
46
|
+
*
|
|
47
|
+
* // DrizzleService is automatically available in all submodules
|
|
48
|
+
* @Module({
|
|
49
|
+
* controllers: [UserController],
|
|
50
|
+
* providers: [UserService], // UserService can inject DrizzleService
|
|
51
|
+
* })
|
|
52
|
+
* export class UserModule {}
|
|
39
53
|
* ```
|
|
40
54
|
*
|
|
41
55
|
* @example With module options (overrides environment variables)
|
|
@@ -61,6 +75,22 @@ const DRIZZLE_MODULE_OPTIONS = Symbol('DRIZZLE_MODULE_OPTIONS');
|
|
|
61
75
|
* export class AppModule {}
|
|
62
76
|
* ```
|
|
63
77
|
*
|
|
78
|
+
* @example Non-global mode (for multi-database scenarios)
|
|
79
|
+
* ```typescript
|
|
80
|
+
* // Each import creates a new DrizzleService instance
|
|
81
|
+
* DrizzleModule.forRoot({
|
|
82
|
+
* connection: { ... },
|
|
83
|
+
* isGlobal: false,
|
|
84
|
+
* })
|
|
85
|
+
*
|
|
86
|
+
* // Submodules must explicitly import DrizzleModule.forFeature()
|
|
87
|
+
* @Module({
|
|
88
|
+
* imports: [DrizzleModule.forFeature()],
|
|
89
|
+
* providers: [UserService],
|
|
90
|
+
* })
|
|
91
|
+
* export class UserModule {}
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
64
94
|
* Then inject DrizzleService in your controller:
|
|
65
95
|
* ```typescript
|
|
66
96
|
* import { Controller, Get } from '@onebun/core';
|
|
@@ -79,6 +109,7 @@ const DRIZZLE_MODULE_OPTIONS = Symbol('DRIZZLE_MODULE_OPTIONS');
|
|
|
79
109
|
* }
|
|
80
110
|
* ```
|
|
81
111
|
*/
|
|
112
|
+
@Global()
|
|
82
113
|
@Module({
|
|
83
114
|
providers: [DrizzleService],
|
|
84
115
|
exports: [DrizzleService],
|
|
@@ -88,27 +119,84 @@ export class DrizzleModule {
|
|
|
88
119
|
* Configure drizzle module with custom options
|
|
89
120
|
* Options will override environment variables
|
|
90
121
|
*
|
|
122
|
+
* By default, isGlobal is true - DrizzleService is available in all modules.
|
|
123
|
+
* Set isGlobal: false for multi-database scenarios where each import
|
|
124
|
+
* should create a new DrizzleService instance.
|
|
125
|
+
*
|
|
91
126
|
* @param options - Drizzle module configuration options
|
|
92
127
|
* @returns Module class with configuration
|
|
93
128
|
*
|
|
94
|
-
* @example
|
|
129
|
+
* @example Global mode (default)
|
|
95
130
|
* ```typescript
|
|
96
131
|
* DrizzleModule.forRoot({
|
|
97
132
|
* connection: {
|
|
98
133
|
* type: DatabaseType.SQLITE,
|
|
99
|
-
* options: {
|
|
100
|
-
* url: './data.db',
|
|
101
|
-
* },
|
|
134
|
+
* options: { url: './data.db' },
|
|
102
135
|
* },
|
|
103
136
|
* autoMigrate: true,
|
|
104
137
|
* })
|
|
105
138
|
* ```
|
|
139
|
+
*
|
|
140
|
+
* @example Non-global mode (for multiple databases)
|
|
141
|
+
* ```typescript
|
|
142
|
+
* DrizzleModule.forRoot({
|
|
143
|
+
* connection: {
|
|
144
|
+
* type: DatabaseType.POSTGRESQL,
|
|
145
|
+
* options: { ... },
|
|
146
|
+
* },
|
|
147
|
+
* isGlobal: false, // Each import creates new instance
|
|
148
|
+
* })
|
|
149
|
+
* ```
|
|
106
150
|
*/
|
|
107
151
|
static forRoot(options: DrizzleModuleOptions): typeof DrizzleModule {
|
|
108
152
|
// Store options in a static property that DrizzleService can access
|
|
109
153
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
110
154
|
(DrizzleModule as any)[DRIZZLE_MODULE_OPTIONS] = options;
|
|
111
155
|
|
|
156
|
+
// If isGlobal is explicitly set to false, remove from global modules registry
|
|
157
|
+
// This allows creating separate DrizzleService instances for multi-DB scenarios
|
|
158
|
+
if (options.isGlobal === false) {
|
|
159
|
+
removeFromGlobalModules(DrizzleModule);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return DrizzleModule;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Import DrizzleModule into a feature module
|
|
167
|
+
*
|
|
168
|
+
* Use this method when DrizzleModule is not global (isGlobal: false)
|
|
169
|
+
* and you need to explicitly import DrizzleService in a submodule.
|
|
170
|
+
*
|
|
171
|
+
* When DrizzleModule is global (default), you don't need to use forFeature() -
|
|
172
|
+
* DrizzleService is automatically available in all modules.
|
|
173
|
+
*
|
|
174
|
+
* @returns Module class that exports DrizzleService
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // In root module: non-global DrizzleModule
|
|
179
|
+
* @Module({
|
|
180
|
+
* imports: [
|
|
181
|
+
* DrizzleModule.forRoot({
|
|
182
|
+
* connection: { ... },
|
|
183
|
+
* isGlobal: false,
|
|
184
|
+
* }),
|
|
185
|
+
* ],
|
|
186
|
+
* })
|
|
187
|
+
* export class AppModule {}
|
|
188
|
+
*
|
|
189
|
+
* // In feature module: explicitly import DrizzleService
|
|
190
|
+
* @Module({
|
|
191
|
+
* imports: [DrizzleModule.forFeature()],
|
|
192
|
+
* providers: [UserService],
|
|
193
|
+
* })
|
|
194
|
+
* export class UserModule {}
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
static forFeature(): typeof DrizzleModule {
|
|
198
|
+
// Simply return the module class - it already exports DrizzleService
|
|
199
|
+
// The module system will handle service instance resolution
|
|
112
200
|
return DrizzleModule;
|
|
113
201
|
}
|
|
114
202
|
|
package/src/drizzle.service.ts
CHANGED
|
@@ -183,8 +183,8 @@ export class DrizzleService<TDbType extends DatabaseTypeLiteral = DatabaseTypeLi
|
|
|
183
183
|
private sqliteClient: Database | null = null;
|
|
184
184
|
private postgresClient: SQL | null = null;
|
|
185
185
|
|
|
186
|
-
constructor(
|
|
187
|
-
super(
|
|
186
|
+
constructor() {
|
|
187
|
+
super();
|
|
188
188
|
// Only start auto-initialization if there's configuration to use
|
|
189
189
|
// Check synchronously to avoid unnecessary async work
|
|
190
190
|
if (this.shouldAutoInitialize()) {
|
package/src/index.ts
CHANGED
|
@@ -49,3 +49,77 @@ export { generateMigrations, pushSchema } from './migrations';
|
|
|
49
49
|
|
|
50
50
|
// Validation utilities
|
|
51
51
|
export * from './validation';
|
|
52
|
+
|
|
53
|
+
// ============================================================================
|
|
54
|
+
// Re-exports from drizzle-orm (common utilities for all dialects)
|
|
55
|
+
// ============================================================================
|
|
56
|
+
|
|
57
|
+
// Query operators - comparison
|
|
58
|
+
export {
|
|
59
|
+
eq,
|
|
60
|
+
ne,
|
|
61
|
+
gt,
|
|
62
|
+
gte,
|
|
63
|
+
lt,
|
|
64
|
+
lte,
|
|
65
|
+
between,
|
|
66
|
+
notBetween,
|
|
67
|
+
} from 'drizzle-orm';
|
|
68
|
+
|
|
69
|
+
// Query operators - logical
|
|
70
|
+
export {
|
|
71
|
+
and,
|
|
72
|
+
or,
|
|
73
|
+
not,
|
|
74
|
+
} from 'drizzle-orm';
|
|
75
|
+
|
|
76
|
+
// Query operators - pattern matching
|
|
77
|
+
export {
|
|
78
|
+
like,
|
|
79
|
+
ilike,
|
|
80
|
+
notLike,
|
|
81
|
+
notIlike,
|
|
82
|
+
} from 'drizzle-orm';
|
|
83
|
+
|
|
84
|
+
// Query operators - array
|
|
85
|
+
export {
|
|
86
|
+
inArray,
|
|
87
|
+
notInArray,
|
|
88
|
+
} from 'drizzle-orm';
|
|
89
|
+
|
|
90
|
+
// Query operators - null
|
|
91
|
+
export {
|
|
92
|
+
isNull,
|
|
93
|
+
isNotNull,
|
|
94
|
+
} from 'drizzle-orm';
|
|
95
|
+
|
|
96
|
+
// SQL template and raw
|
|
97
|
+
export {
|
|
98
|
+
sql,
|
|
99
|
+
} from 'drizzle-orm';
|
|
100
|
+
|
|
101
|
+
// Aggregates
|
|
102
|
+
export {
|
|
103
|
+
count,
|
|
104
|
+
sum,
|
|
105
|
+
avg,
|
|
106
|
+
min,
|
|
107
|
+
max,
|
|
108
|
+
} from 'drizzle-orm';
|
|
109
|
+
|
|
110
|
+
// Ordering
|
|
111
|
+
export {
|
|
112
|
+
asc,
|
|
113
|
+
desc,
|
|
114
|
+
} from 'drizzle-orm';
|
|
115
|
+
|
|
116
|
+
// Relations
|
|
117
|
+
export {
|
|
118
|
+
relations,
|
|
119
|
+
} from 'drizzle-orm';
|
|
120
|
+
|
|
121
|
+
// ============================================================================
|
|
122
|
+
// Re-exports from drizzle-kit (config helper)
|
|
123
|
+
// ============================================================================
|
|
124
|
+
|
|
125
|
+
export { defineConfig } from 'drizzle-kit';
|
package/src/pg.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostgreSQL schema builders re-exported from drizzle-orm/pg-core
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { pgTable, text, integer, timestamp, uuid } from '@onebun/drizzle/pg';
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Table and schema builders
|
|
11
|
+
export {
|
|
12
|
+
pgTable,
|
|
13
|
+
pgSchema,
|
|
14
|
+
pgEnum,
|
|
15
|
+
pgView,
|
|
16
|
+
pgMaterializedView,
|
|
17
|
+
pgSequence,
|
|
18
|
+
} from 'drizzle-orm/pg-core';
|
|
19
|
+
|
|
20
|
+
// Column types
|
|
21
|
+
export {
|
|
22
|
+
bigint,
|
|
23
|
+
bigserial,
|
|
24
|
+
boolean,
|
|
25
|
+
char,
|
|
26
|
+
cidr,
|
|
27
|
+
customType,
|
|
28
|
+
date,
|
|
29
|
+
doublePrecision,
|
|
30
|
+
inet,
|
|
31
|
+
integer,
|
|
32
|
+
interval,
|
|
33
|
+
json,
|
|
34
|
+
jsonb,
|
|
35
|
+
line,
|
|
36
|
+
macaddr,
|
|
37
|
+
macaddr8,
|
|
38
|
+
numeric,
|
|
39
|
+
point,
|
|
40
|
+
real,
|
|
41
|
+
serial,
|
|
42
|
+
smallint,
|
|
43
|
+
smallserial,
|
|
44
|
+
text,
|
|
45
|
+
time,
|
|
46
|
+
timestamp,
|
|
47
|
+
uuid,
|
|
48
|
+
varchar,
|
|
49
|
+
} from 'drizzle-orm/pg-core';
|
|
50
|
+
|
|
51
|
+
// Constraints and indexes
|
|
52
|
+
export {
|
|
53
|
+
check,
|
|
54
|
+
foreignKey,
|
|
55
|
+
index,
|
|
56
|
+
primaryKey,
|
|
57
|
+
unique,
|
|
58
|
+
uniqueIndex,
|
|
59
|
+
} from 'drizzle-orm/pg-core';
|
|
60
|
+
|
|
61
|
+
// Types for type inference
|
|
62
|
+
export type {
|
|
63
|
+
PgTable,
|
|
64
|
+
PgColumn,
|
|
65
|
+
PgEnum,
|
|
66
|
+
PgTableWithColumns,
|
|
67
|
+
} from 'drizzle-orm/pg-core';
|
package/src/sqlite.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite schema builders re-exported from drizzle-orm/sqlite-core
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { sqliteTable, text, integer, real, blob } from '@onebun/drizzle/sqlite';
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Table and view builders
|
|
11
|
+
export {
|
|
12
|
+
sqliteTable,
|
|
13
|
+
sqliteView,
|
|
14
|
+
} from 'drizzle-orm/sqlite-core';
|
|
15
|
+
|
|
16
|
+
// Column types
|
|
17
|
+
export {
|
|
18
|
+
blob,
|
|
19
|
+
customType,
|
|
20
|
+
integer,
|
|
21
|
+
numeric,
|
|
22
|
+
real,
|
|
23
|
+
text,
|
|
24
|
+
} from 'drizzle-orm/sqlite-core';
|
|
25
|
+
|
|
26
|
+
// Constraints and indexes
|
|
27
|
+
export {
|
|
28
|
+
check,
|
|
29
|
+
foreignKey,
|
|
30
|
+
index,
|
|
31
|
+
primaryKey,
|
|
32
|
+
unique,
|
|
33
|
+
uniqueIndex,
|
|
34
|
+
} from 'drizzle-orm/sqlite-core';
|
|
35
|
+
|
|
36
|
+
// Types for type inference
|
|
37
|
+
export type {
|
|
38
|
+
SQLiteTable,
|
|
39
|
+
SQLiteColumn,
|
|
40
|
+
SQLiteTableWithColumns,
|
|
41
|
+
} from 'drizzle-orm/sqlite-core';
|
package/src/types.ts
CHANGED
|
@@ -134,6 +134,14 @@ export interface DrizzleModuleOptions {
|
|
|
134
134
|
* Default: false
|
|
135
135
|
*/
|
|
136
136
|
logQueries?: boolean;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether to register module as global
|
|
140
|
+
* When true, DrizzleService is available in all modules without explicit import.
|
|
141
|
+
* When false, each import creates a new instance (useful for multi-database scenarios).
|
|
142
|
+
* Default: true
|
|
143
|
+
*/
|
|
144
|
+
isGlobal?: boolean;
|
|
137
145
|
}
|
|
138
146
|
|
|
139
147
|
/**
|