db-bridge 1.1.7 → 1.2.0

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.
Files changed (3) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +168 -0
  3. package/package.json +6 -6
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Berke Erdoğan
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
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Berke Erdoğan
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
21
  SOFTWARE.
package/README.md CHANGED
@@ -149,13 +149,36 @@ export default {
149
149
  },
150
150
  migrations: {
151
151
  directory: './src/migrations',
152
+ tableName: 'db_migrations', // Optional: custom table name
153
+ prefix: 'auth', // Optional: filename prefix (e.g., auth_20250119_xxx.ts)
152
154
  },
153
155
  seeds: {
154
156
  directory: './src/seeds',
157
+ prefix: 'auth', // Optional: filename prefix (e.g., auth_users_seeder.ts)
155
158
  },
156
159
  };
157
160
  ```
158
161
 
162
+ #### Migration Prefix (Multi-Service Support)
163
+
164
+ The `prefix` option is useful for **microservice architectures** where multiple services share the same database. Each service can have its own migration prefix to avoid naming conflicts:
165
+
166
+ ```javascript
167
+ // auth-service/dbbridge.config.ts
168
+ migrations: {
169
+ directory: './src/migrations',
170
+ tableName: 'db_migrations_auth', // Separate migration tracking table
171
+ prefix: 'auth', // Creates: auth_20250119120000_create_users.ts
172
+ }
173
+
174
+ // order-service/dbbridge.config.ts
175
+ migrations: {
176
+ directory: './src/migrations',
177
+ tableName: 'db_migrations_order',
178
+ prefix: 'order', // Creates: order_20250119120000_create_orders.ts
179
+ }
180
+ ```
181
+
159
182
  ### Commands
160
183
 
161
184
  ```bash
@@ -183,13 +206,158 @@ npx db-bridge migrate:fresh
183
206
  # Validate migration checksums
184
207
  npx db-bridge migrate:validate
185
208
 
209
+ # Preview migrations without executing (dry-run)
210
+ npx db-bridge migrate:latest --dry-run
211
+
186
212
  # Create a seeder
187
213
  npx db-bridge make:seeder users
188
214
 
189
215
  # Run seeders
190
216
  npx db-bridge db:seed
217
+
218
+ # Generate TypeScript types from database schema
219
+ npx db-bridge generate:types
220
+
221
+ # Generate types with options
222
+ npx db-bridge generate:types --output=./src/types/db.ts --camel-case
223
+ ```
224
+
225
+ ### Type Generation
226
+
227
+ Generate TypeScript interfaces directly from your database schema:
228
+
229
+ ```bash
230
+ npx db-bridge generate:types
231
+ ```
232
+
233
+ This generates a file like:
234
+
235
+ ```typescript
236
+ // src/types/database.ts (auto-generated)
237
+
238
+ /**
239
+ * Users - users table
240
+ */
241
+ export interface Users {
242
+ id: number;
243
+ name: string;
244
+ email: string;
245
+ password: string;
246
+ created_at: Date;
247
+ updated_at: Date | null;
248
+ }
249
+
250
+ /**
251
+ * Orders - orders table
252
+ */
253
+ export interface Orders {
254
+ id: number;
255
+ user_id: number;
256
+ total: number;
257
+ status: string;
258
+ created_at: Date;
259
+ }
260
+ ```
261
+
262
+ #### Type Generation Options
263
+
264
+ | Option | Description |
265
+ | ------------------ | ----------------------------------------------------- |
266
+ | `--output=<path>` | Output file path (default: `./src/types/database.ts`) |
267
+ | `--tables=<list>` | Comma-separated list of tables to include |
268
+ | `--exclude=<list>` | Comma-separated list of tables to exclude |
269
+ | `--camel-case` | Use camelCase for property names |
270
+ | `--comments` | Include JSDoc comments (default: true) |
271
+
272
+ You can also configure type generation in your config file:
273
+
274
+ ```javascript
275
+ // db-bridge.config.mjs
276
+ export default {
277
+ connection: {
278
+ /* ... */
279
+ },
280
+ types: {
281
+ output: './src/types/database.ts',
282
+ exclude: ['logs', 'sessions'],
283
+ camelCase: true,
284
+ },
285
+ };
191
286
  ```
192
287
 
288
+ ### Seeder Ordering
289
+
290
+ Seeders support priority and dependency-based ordering to ensure data is seeded in the correct order:
291
+
292
+ ```typescript
293
+ // src/seeds/users_seeder.ts
294
+ import type { Seeder, DatabaseAdapter } from '@db-bridge/core';
295
+
296
+ export default {
297
+ // Lower priority runs first (default: 100)
298
+ priority: 10,
299
+
300
+ async run(adapter: DatabaseAdapter): Promise<void> {
301
+ await adapter.execute(`
302
+ INSERT INTO users (name, email) VALUES
303
+ ('Admin', 'admin@example.com')
304
+ `);
305
+ },
306
+ } satisfies Seeder;
307
+ ```
308
+
309
+ ```typescript
310
+ // src/seeds/orders_seeder.ts
311
+ import type { Seeder, DatabaseAdapter } from '@db-bridge/core';
312
+
313
+ export default {
314
+ // This seeder depends on users being seeded first
315
+ depends: ['users'],
316
+
317
+ async run(adapter: DatabaseAdapter): Promise<void> {
318
+ await adapter.execute(`
319
+ INSERT INTO orders (user_id, total) VALUES
320
+ (1, 99.99)
321
+ `);
322
+ },
323
+ } satisfies Seeder;
324
+ ```
325
+
326
+ **Ordering Rules:**
327
+
328
+ 1. Seeders with dependencies run after their dependencies
329
+ 2. Within the same dependency level, lower priority runs first
330
+ 3. Default priority is 100
331
+
332
+ ### Migration Dry-Run
333
+
334
+ Preview SQL statements that would be executed without actually running them:
335
+
336
+ ```bash
337
+ npx db-bridge migrate:latest --dry-run
338
+ ```
339
+
340
+ Output:
341
+
342
+ ```
343
+ ℹ Running: 20260118120000_create_users_table
344
+ ℹ DRY RUN: UP 20260118120000_create_users_table
345
+ ℹ SQL statements that would be executed:
346
+ ℹ CREATE TABLE `users` (
347
+ `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
348
+ `name` VARCHAR(100) NOT NULL,
349
+ `email` VARCHAR(255) NOT NULL UNIQUE,
350
+ `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
351
+ `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
352
+ )
353
+ ```
354
+
355
+ This is useful for:
356
+
357
+ - Reviewing SQL before executing on production
358
+ - Debugging migration issues
359
+ - Generating SQL scripts for manual execution
360
+
193
361
  ### Migration Example
194
362
 
195
363
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "db-bridge",
3
- "version": "1.1.7",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "description": "Unified database adapter for Node.js - All-in-one package with MySQL, PostgreSQL, and Redis support",
6
6
  "types": "./dist/index.d.ts",
@@ -18,10 +18,10 @@
18
18
  "prepublishOnly": "npm run clean && npm run build"
19
19
  },
20
20
  "dependencies": {
21
- "@db-bridge/core": "^1.1.7",
22
- "@db-bridge/mysql": "^1.1.7",
23
- "@db-bridge/postgresql": "^1.1.7",
24
- "@db-bridge/redis": "^1.1.7"
21
+ "@db-bridge/core": "^1.2.0",
22
+ "@db-bridge/mysql": "^1.2.0",
23
+ "@db-bridge/postgresql": "^1.2.0",
24
+ "@db-bridge/redis": "^1.2.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^20.10.5",
@@ -66,5 +66,5 @@
66
66
  "publishConfig": {
67
67
  "access": "public"
68
68
  },
69
- "gitHead": "654018624e7e4f5fbef87aa7d423dcf172d04da3"
69
+ "gitHead": "bddce510748d76b76c57b1f1085ddf06ef167b82"
70
70
  }