@igojs/db 6.0.0-beta.1

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 ADDED
@@ -0,0 +1,153 @@
1
+ # @igojs/db
2
+
3
+ Database abstraction layer for MySQL and PostgreSQL with Active Record-style ORM.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @igojs/db
9
+ ```
10
+
11
+ **Note:** Database drivers are optional dependencies. Install the one you need:
12
+ ```sh
13
+ npm install mysql2 # For MySQL
14
+ npm install pg # For PostgreSQL
15
+ ```
16
+
17
+ ## Features
18
+
19
+ - **Active Record ORM** - Ruby on Rails-inspired Model API
20
+ - **Query Builder** - Chainable query API with scopes
21
+ - **Associations** - has_many, belongs_to relationships
22
+ - **Caching** - Redis-based query result caching
23
+ - **Migrations** - SQL file-based migrations
24
+ - **Optimized Queries** - PaginatedOptimizedQuery for large tables
25
+
26
+ ## Quick Start
27
+
28
+ ```javascript
29
+ const { Model } = require('@igojs/db');
30
+
31
+ const schema = {
32
+ table: 'users',
33
+ columns: ['id', 'email', 'name', 'created_at']
34
+ };
35
+
36
+ class User extends Model(schema) {
37
+ fullName() {
38
+ return `${this.first_name} ${this.last_name}`;
39
+ }
40
+ }
41
+
42
+ // CRUD operations
43
+ const user = await User.create({ email: 'john@example.com', name: 'John' });
44
+ const users = await User.where({ name: 'John' }).list();
45
+ await user.update({ name: 'Jane' });
46
+ await user.delete();
47
+ ```
48
+
49
+ ## API
50
+
51
+ ### Exports
52
+
53
+ | Export | Description |
54
+ |--------|-------------|
55
+ | `Model` | Base model class factory |
56
+ | `Query` | Query builder class |
57
+ | `CachedQuery` | Query with Redis caching |
58
+ | `Schema` | Schema definition utilities |
59
+ | `Sql` | SQL generation utilities |
60
+ | `Db` | Database connection class |
61
+ | `dbs` | Database connections manager |
62
+ | `migrations` | Migration runner |
63
+ | `DataTypes` | Column type definitions |
64
+ | `CacheStats` | Cache statistics |
65
+ | `PaginatedOptimizedQuery` | Optimized pagination for large tables |
66
+
67
+ ### Model Definition
68
+
69
+ ```javascript
70
+ const schema = {
71
+ table: 'users',
72
+ columns: [
73
+ 'id',
74
+ 'email',
75
+ { name: 'is_active', type: 'boolean' },
76
+ { name: 'settings_json', type: 'json', attr: 'settings' },
77
+ ],
78
+ associations: [
79
+ ['has_many', 'posts', Post, 'id', 'user_id'],
80
+ ['belongs_to', 'country', Country, 'country_id'],
81
+ ],
82
+ scopes: {
83
+ default: (q) => q.order('created_at DESC'),
84
+ active: (q) => q.where({ is_active: true }),
85
+ },
86
+ cache: true, // Enable Redis caching
87
+ };
88
+ ```
89
+
90
+ ### Query API
91
+
92
+ ```javascript
93
+ // Find
94
+ const user = await User.find(1);
95
+
96
+ // Where
97
+ const users = await User.where({ status: 'active' }).list();
98
+ const users = await User.where('age > ?', [18]).list();
99
+
100
+ // Chainable
101
+ const users = await User
102
+ .where({ status: 'active' })
103
+ .includes('posts')
104
+ .order('name ASC')
105
+ .limit(10)
106
+ .list();
107
+
108
+ // Scopes
109
+ const users = await User.scope('active').list();
110
+
111
+ // Pagination
112
+ const result = await User.page(1, 20);
113
+ // => { pagination: {...}, rows: [...] }
114
+
115
+ // Count
116
+ const count = await User.where({ status: 'active' }).count();
117
+ ```
118
+
119
+ ### Optimized Pagination
120
+
121
+ For large tables with many joins (100K+ rows), use `PaginatedOptimizedQuery`:
122
+
123
+ ```javascript
124
+ const result = await User.paginatedOptimized()
125
+ .where({
126
+ status: 'active',
127
+ 'company.country.code': 'FR', // Nested filter
128
+ })
129
+ .join(['company.country'])
130
+ .order('created_at DESC')
131
+ .page(1, 50)
132
+ .execute();
133
+ ```
134
+
135
+ ## Initialization
136
+
137
+ When used standalone (without @igojs/server), initialize with dependencies:
138
+
139
+ ```javascript
140
+ const db = require('@igojs/db');
141
+
142
+ db.init({
143
+ config: { mysql: { host: 'localhost', database: 'myapp' } },
144
+ cache: myRedisCache,
145
+ logger: myLogger,
146
+ utils: { toJSON, fromJSON },
147
+ errorhandler: myErrorHandler,
148
+ });
149
+ ```
150
+
151
+ ## Documentation
152
+
153
+ See the [full documentation](https://igocreate.github.io/igo/#/db/models).