@onebun/drizzle 0.1.0 → 0.1.3

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/README.md CHANGED
@@ -26,10 +26,11 @@ Choose the function that matches your database type configured in step 2.
26
26
  **For PostgreSQL:**
27
27
  ```typescript
28
28
  // src/schema/users.ts
29
- import { pgTable, serial, text, integer, timestamp } from 'drizzle-orm/pg-core';
29
+ import { pgTable, text, integer, timestamp } from 'drizzle-orm/pg-core';
30
30
 
31
31
  export const users = pgTable('users', {
32
- id: serial('id').primaryKey(),
32
+ // Use generatedAlwaysAsIdentity() for auto-increment primary key
33
+ id: integer('id').primaryKey().generatedAlwaysAsIdentity(),
33
34
  name: text('name').notNull(),
34
35
  email: text('email').notNull().unique(),
35
36
  age: integer('age'),
@@ -281,3 +282,7 @@ DB_MIGRATIONS_FOLDER=./drizzle
281
282
  DB_AUTO_MIGRATE=true
282
283
  DB_LOG_QUERIES=false
283
284
  ```
285
+
286
+ ## License
287
+
288
+ [LGPL-3.0](../../LICENSE)
package/package.json CHANGED
@@ -1,9 +1,19 @@
1
1
  {
2
2
  "name": "@onebun/drizzle",
3
- "version": "0.1.0",
4
- "description": "Drizzle ORM module for OneBun framework with SQLite and PostgreSQL support",
3
+ "version": "0.1.3",
4
+ "description": "Drizzle ORM module for OneBun framework - SQLite and PostgreSQL support",
5
5
  "license": "LGPL-3.0",
6
6
  "author": "RemRyahirev",
7
+ "keywords": [
8
+ "onebun",
9
+ "drizzle",
10
+ "orm",
11
+ "database",
12
+ "sqlite",
13
+ "postgresql",
14
+ "bun",
15
+ "typescript"
16
+ ],
7
17
  "repository": {
8
18
  "type": "git",
9
19
  "url": "git+https://github.com/RemRyahirev/onebun.git",
@@ -28,9 +38,9 @@
28
38
  "dev": "bun run --watch src/index.ts"
29
39
  },
30
40
  "dependencies": {
31
- "@onebun/core": "workspace:*",
32
- "@onebun/envs": "workspace:*",
33
- "@onebun/logger": "workspace:*",
41
+ "@onebun/core": "workspace:^",
42
+ "@onebun/envs": "workspace:^",
43
+ "@onebun/logger": "workspace:^",
34
44
  "drizzle-orm": "^0.44.7",
35
45
  "drizzle-kit": "^0.31.6",
36
46
  "drizzle-arktype": "^0.1.3",
@@ -185,7 +185,41 @@ export class DrizzleService<TDbType extends DatabaseTypeLiteral = DatabaseTypeLi
185
185
 
186
186
  constructor(...args: unknown[]) {
187
187
  super(...args);
188
- this.initPromise = this.autoInitialize();
188
+ // Only start auto-initialization if there's configuration to use
189
+ // Check synchronously to avoid unnecessary async work
190
+ if (this.shouldAutoInitialize()) {
191
+ this.initPromise = this.autoInitialize();
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Check synchronously if auto-initialization should be attempted
197
+ * This avoids starting async work when there's no configuration
198
+ * Note: Only checks env vars here to avoid slow require() in constructor.
199
+ * Module options are checked in autoInitialize() which is async.
200
+ */
201
+ private shouldAutoInitialize(): boolean {
202
+ // Check if DB_URL env var is set
203
+ // Module options will be checked in autoInitialize() if this returns false
204
+ const dbUrl = process.env.DB_URL;
205
+ if (dbUrl && dbUrl.trim() !== '') {
206
+ return true;
207
+ }
208
+
209
+ // Also check if DrizzleModule has options set (static check without require)
210
+ // This is done by checking if the module was already loaded
211
+ try {
212
+ // Only check if module is already in cache (don't trigger new require)
213
+ const modulePath = require.resolve('./drizzle.module');
214
+ const cachedModule = require.cache[modulePath];
215
+ if (cachedModule?.exports?.DrizzleModule?.getOptions?.()?.connection) {
216
+ return true;
217
+ }
218
+ } catch {
219
+ // Module not resolved or not in cache
220
+ }
221
+
222
+ return false;
189
223
  }
190
224
 
191
225
  /**
@@ -448,7 +482,13 @@ export class DrizzleService<TDbType extends DatabaseTypeLiteral = DatabaseTypeLi
448
482
  * Close database connection
449
483
  */
450
484
  async close(): Promise<void> {
451
- await this.waitForInit();
485
+ // Only wait for init if there are actual database clients to close
486
+ // This prevents hanging when close() is called on an uninitialized service
487
+ // Note: we check clients directly, not `initialized` flag, because autoInitialize()
488
+ // may still be running and `initialized` could be false while initPromise is pending
489
+ if (this.postgresClient || this.sqliteClient) {
490
+ await this.waitForInit();
491
+ }
452
492
 
453
493
  if (this.postgresClient) {
454
494
  // Bun.SQL uses close() instead of end()
package/src/migrations.ts CHANGED
@@ -7,10 +7,10 @@ import { spawnSync } from 'bun';
7
7
  * using pgTable() or sqliteTable(). The schemas should be exported from schema files
8
8
  * specified in schemaPath.
9
9
  *
10
- * @param options - Migration generation options
11
- * @param options.schemaPath - Path or paths to schema files containing pgTable/sqliteTable definitions
12
- * @param options.migrationsFolder - Output folder for migration files
13
- * @param options.dialect - Database dialect (sqlite or postgresql)
10
+ * @param options - Migration generation options:
11
+ * - schemaPath: Path or paths to schema files containing pgTable/sqliteTable definitions
12
+ * - migrationsFolder: Output folder for migration files
13
+ * - dialect: Database dialect (sqlite or postgresql)
14
14
  *
15
15
  * @example
16
16
  * ```typescript
@@ -73,10 +73,10 @@ export default {
73
73
  * This function applies schema changes directly to the database without creating
74
74
  * migration files. Useful for development, but not recommended for production.
75
75
  *
76
- * @param options - Schema push options
77
- * @param options.schemaPath - Path(s) to schema files containing pgTable/sqliteTable definitions
78
- * @param options.dialect - Database dialect ('sqlite' or 'postgresql')
79
- * @param options.connectionString - Database connection string
76
+ * @param options - Schema push options:
77
+ * - schemaPath: Path(s) to schema files containing pgTable/sqliteTable definitions
78
+ * - dialect: Database dialect ('sqlite' or 'postgresql')
79
+ * - connectionString: Database connection string
80
80
  *
81
81
  * @example
82
82
  * ```typescript