db-bridge 1.1.3 → 1.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.
Files changed (2) hide show
  1. package/README.md +315 -89
  2. package/package.json +6 -6
package/README.md CHANGED
@@ -1,129 +1,355 @@
1
- # DB Bridge
1
+ # db-bridge
2
2
 
3
- Unified database adapter for Node.js with MySQL, PostgreSQL, and Redis support.
3
+ <div align="center">
4
4
 
5
- ## Installation
5
+ [![npm version](https://img.shields.io/npm/v/db-bridge.svg)](https://www.npmjs.com/package/db-bridge)
6
+ [![npm downloads](https://img.shields.io/npm/dm/db-bridge.svg)](https://www.npmjs.com/package/db-bridge)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.3+-blue.svg)](https://www.typescriptlang.org/)
9
+ [![Node.js](https://img.shields.io/badge/Node.js-18+-green.svg)](https://nodejs.org/)
6
10
 
7
- ### Install everything (recommended for new projects):
11
+ **Unified database interface for Node.js with MySQL, PostgreSQL, Redis support and powerful migration system.**
8
12
 
9
- ```bash
10
- npm install db-bridge
11
- ```
13
+ [Getting Started](#quick-start) •
14
+ [Documentation](https://github.com/berkeerdo/db-bridge/tree/master/docs) •
15
+ [Migration CLI](#migration-cli) •
16
+ [Contributing](https://github.com/berkeerdo/db-bridge/blob/master/CONTRIBUTING.md)
12
17
 
13
- ### Or install only what you need:
18
+ </div>
14
19
 
15
- ```bash
16
- # Core + MySQL only
17
- npm install @db-bridge/core @db-bridge/mysql
20
+ ---
21
+
22
+ ## Table of Contents
18
23
 
19
- # Core + Redis only
20
- npm install @db-bridge/core @db-bridge/redis
24
+ - [Features](#features)
25
+ - [Installation](#installation)
26
+ - [Quick Start](#quick-start)
27
+ - [Query Builder](#query-builder)
28
+ - [Migration CLI](#migration-cli)
29
+ - [Transactions](#transactions)
30
+ - [Configuration](#configuration)
31
+ - [Documentation](#documentation)
32
+ - [Packages](#packages)
33
+ - [Contributing](#contributing)
34
+ - [License](#license)
21
35
 
22
- # Core + PostgreSQL only
23
- npm install @db-bridge/core @db-bridge/postgresql
36
+ ## Features
37
+
38
+ - **Unified API** - Same interface for MySQL, PostgreSQL, and Redis
39
+ - **Query Builder** - Chainable, type-safe query construction
40
+ - **Migration System** - Industry-grade CLI with batch tracking, rollback, checksums
41
+ - **Schema Builder** - Fluent API for table creation and modification
42
+ - **Transactions** - Full transaction support with savepoints
43
+ - **Connection Pooling** - Built-in connection pool management
44
+ - **Seeding** - Database seeding support for test data
45
+ - **TypeScript** - Full type safety with generics support
46
+ - **ESM** - Native ES modules support
47
+ - **Modular** - Install only the adapters you need
48
+
49
+ ## Installation
50
+
51
+ ```bash
52
+ # All-in-one (includes all adapters)
53
+ npm install db-bridge
54
+
55
+ # Or install only what you need
56
+ npm install @db-bridge/core # Core + CLI
57
+ npm install @db-bridge/mysql # MySQL/MariaDB
58
+ npm install @db-bridge/postgresql # PostgreSQL
59
+ npm install @db-bridge/redis # Redis (cache adapter)
24
60
  ```
25
61
 
26
62
  ## Quick Start
27
63
 
28
- ```javascript
29
- import { DBBridge } from 'db-bridge';
30
- // or
31
- const { DBBridge } = require('db-bridge');
64
+ ### Connection
32
65
 
33
- // MySQL
34
- const mysql = DBBridge.mysql({
66
+ ```typescript
67
+ import { DBBridge } from '@db-bridge/core';
68
+
69
+ const db = DBBridge.mysql({
35
70
  host: 'localhost',
36
71
  user: 'root',
37
- password: 'password',
38
- database: 'mydb',
72
+ password: '',
73
+ database: 'myapp',
39
74
  });
40
75
 
41
- // PostgreSQL
42
- const pg = DBBridge.postgresql({
43
- host: 'localhost',
44
- user: 'postgres',
45
- password: 'password',
46
- database: 'mydb',
47
- });
76
+ await db.connect();
77
+ ```
48
78
 
49
- // Redis
50
- const redis = DBBridge.redis({
51
- host: 'localhost',
52
- port: 6379,
79
+ ### Basic Queries
80
+
81
+ ```typescript
82
+ // SELECT with conditions
83
+ const users = await db
84
+ .table('users')
85
+ .select('id', 'name', 'email')
86
+ .where('status', 'active')
87
+ .orderBy('name', 'asc')
88
+ .limit(50)
89
+ .get();
90
+
91
+ // INSERT
92
+ await db.table('users').insert({
93
+ name: 'John Doe',
94
+ email: 'john@example.com',
53
95
  });
54
96
 
55
- // Connect and use
56
- await mysql.connect();
57
- const users = await mysql.query('SELECT * FROM users');
58
- await mysql.disconnect();
97
+ // UPDATE
98
+ await db.table('users').where('id', 1).update({ status: 'inactive' });
99
+
100
+ // DELETE
101
+ await db.table('sessions').where('expires_at', '<', new Date()).delete();
59
102
  ```
60
103
 
61
- ## Why DB Bridge?
104
+ ## Query Builder
62
105
 
63
- - 🚀 **Unified API** - Same interface for all databases
64
- - 📦 **Optional Dependencies** - Install only what you need
65
- - 🔄 **Auto Driver Installation** - Database drivers are included
66
- - 💪 **TypeScript Support** - Full type safety
67
- - 🏊 **Connection Pooling** - Built-in pool management
68
- - 🔒 **Transactions** - ACID compliance
69
- - 🔍 **Query Builder** - Type-safe query construction
70
- - ⚡ **Redis Integration** - Built-in caching support
106
+ Full-featured query builder with joins, aggregates, and more:
71
107
 
72
- ## Features
108
+ ```typescript
109
+ // JOIN
110
+ const orders = await db
111
+ .table('orders')
112
+ .join('users', 'orders.user_id = users.id')
113
+ .select('orders.*', 'users.name as customer')
114
+ .where('orders.status', 'pending')
115
+ .get();
73
116
 
74
- ### Consistent API
117
+ // Complex conditions
118
+ const users = await db
119
+ .table('users')
120
+ .whereIn('role', ['admin', 'editor'])
121
+ .whereBetween('age', [18, 65])
122
+ .whereNotNull('email_verified_at')
123
+ .get();
75
124
 
76
- ```javascript
77
- // Same methods work across all databases
78
- await db.connect();
79
- await db.query('SELECT * FROM users');
80
- await db.execute('INSERT INTO users VALUES (?, ?)', ['John', 'john@example.com']);
81
- await db.disconnect();
125
+ // Aggregates
126
+ const count = await db.table('users').where('active', true).count();
127
+ const total = await db.table('orders').sum('amount');
82
128
  ```
83
129
 
84
- ### Query Builder
130
+ > **[View full Query Builder documentation →](https://github.com/berkeerdo/db-bridge/blob/master/docs/guides/query-builder.md)**
131
+
132
+ ## Migration CLI
133
+
134
+ Industry-grade migration system inspired by Laravel, Knex, and Prisma.
135
+
136
+ ### Setup
137
+
138
+ Create `db-bridge.config.mjs` in your project root:
85
139
 
86
140
  ```javascript
87
- const users = await db
88
- .createQueryBuilder()
89
- .table('users') // or .from('users')
90
- .select('id', 'name', 'email')
91
- .where('active', '=', true)
92
- .orderBy('created_at', 'DESC')
93
- .limit(10)
94
- .execute();
141
+ export default {
142
+ connection: {
143
+ dialect: 'mysql', // or 'postgresql'
144
+ host: 'localhost',
145
+ port: 3306,
146
+ user: 'root',
147
+ password: 'secret',
148
+ database: 'myapp',
149
+ },
150
+ migrations: {
151
+ directory: './src/migrations',
152
+ },
153
+ seeds: {
154
+ directory: './src/seeds',
155
+ },
156
+ };
95
157
  ```
96
158
 
97
- ### Transactions
159
+ ### Commands
98
160
 
99
- ```javascript
100
- const trx = await db.beginTransaction();
101
- try {
102
- await trx.execute('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
103
- await trx.execute('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
104
- await trx.commit();
105
- } catch (error) {
106
- await trx.rollback();
107
- }
161
+ ```bash
162
+ # Create a new migration
163
+ npx db-bridge migrate:make create_users_table
164
+
165
+ # Run pending migrations
166
+ npx db-bridge migrate:latest
167
+
168
+ # Check migration status
169
+ npx db-bridge migrate:status
170
+
171
+ # Rollback last batch
172
+ npx db-bridge migrate:rollback
173
+
174
+ # Rollback multiple batches
175
+ npx db-bridge migrate:rollback --step=3
176
+
177
+ # Reset all migrations
178
+ npx db-bridge migrate:reset
179
+
180
+ # Fresh start (reset + migrate)
181
+ npx db-bridge migrate:fresh
182
+
183
+ # Validate migration checksums
184
+ npx db-bridge migrate:validate
185
+
186
+ # Create a seeder
187
+ npx db-bridge make:seeder users
188
+
189
+ # Run seeders
190
+ npx db-bridge db:seed
108
191
  ```
109
192
 
110
- ### Redis Caching
193
+ ### Migration Example
111
194
 
112
- ```javascript
113
- const cache = DBBridge.redis();
114
- await cache.connect();
115
-
116
- // Basic operations
117
- await cache.set('key', { data: 'value' }, 3600); // 1 hour TTL
118
- const data = await cache.get('key');
119
- await cache.del('key');
120
-
121
- // Redis commands
122
- const redis = cache.getAdapter();
123
- await redis.commands.lpush('queue', 'task1', 'task2');
124
- await redis.commands.hset('user:1', 'name', 'John');
195
+ ```typescript
196
+ // src/migrations/20260118120000_create_users_table.ts
197
+ import type { SchemaBuilder } from '@db-bridge/core';
198
+
199
+ export default {
200
+ name: '20260118120000_create_users_table',
201
+
202
+ async up(schema: SchemaBuilder): Promise<void> {
203
+ await schema.createTable('users', (table) => {
204
+ table.increments('id');
205
+ table.string('name', 100).notNull();
206
+ table.string('email', 255).unique().notNull();
207
+ table.string('password', 255).notNull();
208
+ table.boolean('active').default(true);
209
+ table.timestamps();
210
+ });
211
+ },
212
+
213
+ async down(schema: SchemaBuilder): Promise<void> {
214
+ await schema.dropTableIfExists('users');
215
+ },
216
+ };
217
+ ```
218
+
219
+ ### Schema Builder API
220
+
221
+ ```typescript
222
+ schema.createTable('posts', (table) => {
223
+ table.increments('id'); // INT AUTO_INCREMENT PRIMARY KEY
224
+ table.bigIncrements('id'); // BIGINT AUTO_INCREMENT
225
+ table.string('title', 255); // VARCHAR(255)
226
+ table.text('content'); // TEXT
227
+ table.integer('views'); // INT
228
+ table.boolean('published'); // TINYINT(1)
229
+ table.timestamp('published_at'); // TIMESTAMP
230
+ table.json('metadata'); // JSON
231
+ table.timestamps(); // created_at, updated_at
232
+
233
+ // Foreign keys
234
+ table.integer('user_id').unsigned();
235
+ table.foreign('user_id').references('id').on('users').onDelete('CASCADE');
236
+
237
+ // Indexes
238
+ table.index('title');
239
+ table.unique('slug');
240
+ });
125
241
  ```
126
242
 
243
+ > **[View full Migration documentation →](https://github.com/berkeerdo/db-bridge/blob/master/docs/migration.md)**
244
+
245
+ ## Transactions
246
+
247
+ ```typescript
248
+ await db.transaction(async (trx) => {
249
+ const orderId = await trx.table('orders').insert({
250
+ user_id: 1,
251
+ total: 99.99,
252
+ });
253
+
254
+ await trx.table('order_items').insert([
255
+ { order_id: orderId, product_id: 1, quantity: 2 },
256
+ { order_id: orderId, product_id: 3, quantity: 1 },
257
+ ]);
258
+
259
+ await trx.table('inventory').where('product_id', 1).decrement('quantity', 2);
260
+ });
261
+ ```
262
+
263
+ ## Configuration
264
+
265
+ ### MySQL
266
+
267
+ | Option | Type | Default | Description |
268
+ | ----------------- | ------ | ------------ | -------------------- |
269
+ | `host` | string | localhost | Database host |
270
+ | `port` | number | 3306 | Database port |
271
+ | `user` | string | root | Username |
272
+ | `password` | string | - | Password |
273
+ | `database` | string | **required** | Database name |
274
+ | `connectionLimit` | number | 10 | Max pool connections |
275
+
276
+ ### PostgreSQL
277
+
278
+ | Option | Type | Default | Description |
279
+ | ---------- | ------ | ------------ | -------------------- |
280
+ | `host` | string | localhost | Database host |
281
+ | `port` | number | 5432 | Database port |
282
+ | `user` | string | postgres | Username |
283
+ | `password` | string | - | Password |
284
+ | `database` | string | **required** | Database name |
285
+ | `max` | number | 10 | Max pool connections |
286
+
287
+ ### Redis
288
+
289
+ | Option | Type | Default | Description |
290
+ | ----------- | ------ | --------- | ----------------------------- |
291
+ | `host` | string | localhost | Redis host |
292
+ | `port` | number | 6379 | Redis port |
293
+ | `password` | string | - | Password |
294
+ | `db` | number | 0 | Database index |
295
+ | `keyPrefix` | string | - | Key prefix for all operations |
296
+
297
+ ## Documentation
298
+
299
+ | Guide | Description |
300
+ | ------------------------------------------------------------------------------------------------ | --------------------------- |
301
+ | [Getting Started](https://github.com/berkeerdo/db-bridge/blob/master/docs/getting-started.md) | Quick start guide |
302
+ | [Query Builder](https://github.com/berkeerdo/db-bridge/blob/master/docs/guides/query-builder.md) | Full query builder API |
303
+ | [Migrations](https://github.com/berkeerdo/db-bridge/blob/master/docs/migration.md) | Migration system guide |
304
+ | [Architecture](https://github.com/berkeerdo/db-bridge/blob/master/docs/architecture.md) | System architecture |
305
+ | [API Reference](https://github.com/berkeerdo/db-bridge/blob/master/docs/api.md) | Complete API reference |
306
+ | [Caching](https://github.com/berkeerdo/db-bridge/blob/master/docs/caching.md) | Redis caching guide |
307
+ | [Encryption](https://github.com/berkeerdo/db-bridge/blob/master/docs/encryption.md) | Field-level encryption |
308
+ | [Troubleshooting](https://github.com/berkeerdo/db-bridge/blob/master/docs/troubleshooting.md) | Common issues and solutions |
309
+
310
+ ## Packages
311
+
312
+ | Package | Description |
313
+ | ----------------------- | ----------------------------------- |
314
+ | `db-bridge` | All-in-one package |
315
+ | `@db-bridge/core` | Core interfaces, CLI, and utilities |
316
+ | `@db-bridge/mysql` | MySQL/MariaDB adapter |
317
+ | `@db-bridge/postgresql` | PostgreSQL adapter |
318
+ | `@db-bridge/redis` | Redis cache adapter |
319
+
320
+ ## Contributing
321
+
322
+ Contributions are welcome! Please read our [Contributing Guide](https://github.com/berkeerdo/db-bridge/blob/master/CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.
323
+
324
+ ```bash
325
+ # Clone the repository
326
+ git clone https://github.com/berkeerdo/db-bridge.git
327
+
328
+ # Install dependencies
329
+ npm install
330
+
331
+ # Run tests
332
+ npm test
333
+
334
+ # Build
335
+ npm run build
336
+ ```
337
+
338
+ ## Requirements
339
+
340
+ - Node.js >= 18.0.0
341
+ - TypeScript >= 5.3.0 (for TypeScript users)
342
+
127
343
  ## License
128
344
 
129
- MIT
345
+ MIT © [berkeerdo](https://github.com/berkeerdo)
346
+
347
+ ---
348
+
349
+ <div align="center">
350
+
351
+ **[⬆ back to top](#db-bridge)**
352
+
353
+ Made with ❤️ by [Berke Erdoğan](https://github.com/berkeerdo)
354
+
355
+ </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "db-bridge",
3
- "version": "1.1.3",
3
+ "version": "1.1.7",
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.3",
22
- "@db-bridge/mysql": "^1.1.3",
23
- "@db-bridge/postgresql": "^1.1.3",
24
- "@db-bridge/redis": "^1.1.3"
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"
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": "3f461bcfb11f2a4d978123425ab9a7068c9e0f30"
69
+ "gitHead": "654018624e7e4f5fbef87aa7d423dcf172d04da3"
70
70
  }