@onebun/drizzle 0.1.5 → 0.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onebun/drizzle",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Drizzle ORM module for OneBun framework - SQLite and PostgreSQL support",
5
5
  "license": "LGPL-3.0",
6
6
  "author": "RemRyahirev",
@@ -47,9 +47,9 @@
47
47
  "dev": "bun run --watch src/index.ts"
48
48
  },
49
49
  "dependencies": {
50
- "@onebun/core": "^0.1.7",
51
- "@onebun/envs": "^0.1.2",
52
- "@onebun/logger": "^0.1.3",
50
+ "@onebun/core": "^0.1.11",
51
+ "@onebun/envs": "^0.1.4",
52
+ "@onebun/logger": "^0.1.5",
53
53
  "drizzle-orm": "^0.44.7",
54
54
  "drizzle-kit": "^0.31.6",
55
55
  "drizzle-arktype": "^0.1.3",
@@ -1,6 +1,10 @@
1
1
  import type { DrizzleModuleOptions } from './types';
2
2
 
3
- import { Module } from '@onebun/core';
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
 
@@ -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(...args: unknown[]) {
187
- super(...args);
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/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
  /**