lamix 4.2.10 → 4.2.12

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
@@ -106,6 +106,185 @@ npm i lamix
106
106
 
107
107
  npm install mysql2 # or pg or sqlite3
108
108
 
109
- ```bash
110
109
  # TO See All the Available CLI Commands Run in Terminal
111
110
  npx lamix
111
+
112
+ #Basic CRUD & Query
113
+
114
+ const User = require('./models/User');
115
+
116
+ # Create new user
117
+ const user = await User.create({ name: 'Alice', email: 'alice@example.com', password: 'secret' });
118
+
119
+ # Fill & save (update)
120
+ user.name = 'Alice Smith'; // you can also use user.fill({ name: 'Alice Smith' })
121
+ await user.save();
122
+
123
+ # Find by primary key
124
+ const user1 = await User.find(1);
125
+ const user2 = await User.findOrFail(param.id);
126
+
127
+ # Query with where
128
+ const someUsers = await User.where('email', 'alice@example.com').get();
129
+
130
+ # First match
131
+ const firstUser = await User.where('name', 'Alice').first();
132
+ const firstOrFail = await User.where('name', 'Alice').firstOrFail();
133
+
134
+ const user = await User.findBy('email', 'jane@example.com');
135
+ # by default Field password is Auto encrypted when creating
136
+ const isValid = await user.checkPassword('user_input_password');
137
+ # Delete (soft delete if enabled)
138
+ await user.delete();
139
+
140
+ # destroy (delete Multiple ids)
141
+ await user.destroy();
142
+
143
+ # update (by default)
144
+ await user.update({ body });
145
+
146
+ # or update
147
+ await user.fill({ body });
148
+ await user.save();
149
+
150
+ # Restore (if softDeletes enabled)
151
+ await user.restore();
152
+
153
+ # List all (excluding trashed, by default)
154
+ const allUsers = await User.all();
155
+
156
+ # Query including soft-deleted (trashed) records
157
+ const withTrashed = await User.withTrashed().where('id', 1).first();
158
+
159
+ #QueryBuilder Advanced Features
160
+
161
+ const qb = User.query();
162
+
163
+ # Select specific columns
164
+ const names = await User.query().select('id', 'name').get();
165
+
166
+ # Aggregates
167
+ const cnt = await User.query().count(); // count(*)
168
+ const sumId = await User.query().sum('id');
169
+ const avgId = await User.query().avg('id');
170
+ const minId = await User.query().min('id');
171
+ const maxId = await User.query().max('id');
172
+
173
+ # status a column (returns status of boolean values)
174
+ const usersAll = await User.all();
175
+ const users = await User.query().where('status', true).get();
176
+ const usersorderbyid = await User.query().where('status', true).orderBy('id', 'desc').get();
177
+ const usersorderbycreateAt = await User.query().where('status', true).orderBy('created_at', 'desc').get();
178
+
179
+ # Pluck a column (returns array of values)
180
+ const Pluckemails = await User.query().pluck('email');
181
+
182
+ # Pagination
183
+ const page1 = await User.query().paginate(1, 10);
184
+ // page1 = { total, perPage, page, lastPage, data: [users...] }
185
+
186
+ # Where In
187
+ const usersIn = await User.query().whereIn('id', [1, 2, 3]).get();
188
+
189
+ // Where Null / Not Null
190
+ const withNull = await User.query().whereNull('deleted_at').get();
191
+ const notNull = await User.query().whereNotNull('deleted_at').get();
192
+
193
+ # Where Between
194
+ const inRange = await User.query().whereBetween('id', [10, 20]).get();
195
+
196
+ # Ordering, grouping, having
197
+ const grouped = await User.query()
198
+ .select('user_id', 'COUNT(*) as cnt')
199
+ .groupBy('user_id')
200
+ .having('cnt', '>', 1)
201
+ .get();
202
+
203
+ # Joins
204
+ const withPosts = await User.query()
205
+ .select('users.*', 'posts.title as post_title')
206
+ .join('INNER', 'posts', 'users.id', '=', 'posts.user_id')
207
+ .get();
208
+
209
+ # Using “forUpdate” (locking)
210
+ await User.query().where('id', 5).forUpdate().get();
211
+
212
+ # Raw queries
213
+ const rows = await User.raw('SELECT * FROM users WHERE id = ?', [5]);
214
+
215
+ # Caching
216
+ const cachedRows = await User.raw('SELECT * FROM users', []).then(rows => rows);
217
+ const cached = await DB.cached('SELECT * FROM users WHERE id = ?', [5], 60000);
218
+
219
+ #You can use relations:
220
+
221
+ const user = await User.find(1);
222
+ const posts = await user.posts().get(); // all posts for the user
223
+
224
+ # Eager load in a query with Relations:
225
+ const usersWithPosts = await User.query().with('posts').get();
226
+ # Each user will have a `posts` field with array of post models.
227
+
228
+ # Many-to-many example
229
+ # Suppose you have an intermediate pivot table user_roles (user_id, role_id)
230
+ # and models User and Role, with pivot user_roles.
231
+ #
232
+
233
+ # Then:
234
+ const user = await User.find(2);
235
+ const roles = await user.roles().get();
236
+
237
+ # Eager load with Relations:
238
+ const usersWithRoles = await User.query().with('roles').get();
239
+
240
+ # Eager load with Many to Many Relations:
241
+ const usersWith_Roles_posts_comments = await User.query().with(['roles', 'posts', 'comments' ]).get();
242
+ #OR with Many to Many Relations:
243
+ const users_With_Roles_posts_comments = await User.with(['roles', 'posts', 'comments' ]).get();
244
+
245
+ # Find by field
246
+ const user = await User.findBy('email', 'jane@example.com');
247
+
248
+ # Check hashed password
249
+ const isValid = await user.checkPassword('password');
250
+
251
+ # models/User.js(Example)
252
+ const { BaseModel} = require('lamix');
253
+
254
+ class User extends BaseModel {
255
+ static table = 'users';
256
+ static primaryKey = 'id';
257
+ static timestamps = true;
258
+ static softDeletes = false; # Optional if you don't need it
259
+ static fillable = ['name', 'email', 'password']; # password is Auto encrypted when creating.
260
+ static rules = { # Optional if you don't need it
261
+ name: 'required|string',
262
+ email: 'required|email|unique:users,email',
263
+ password: 'required|string|min:6',
264
+ phone: 'nullable|phone',
265
+ status: 'boolean'
266
+ };
267
+
268
+ profile() {
269
+ const Profile = require('./Profile');
270
+ return this.hasOne(Profile).onDelete('restrict');
271
+ }
272
+
273
+
274
+ // Many-to-many: User ↔ Role via pivot user_roles (user_id, role_id)
275
+ roles() {
276
+ const Role = require('./Role');
277
+ return this.belongsToMany(
278
+ Role,
279
+ 'user_roles',
280
+ 'user_id',
281
+ 'role_id'
282
+ ).onDelete('detach');
283
+ }
284
+
285
+ // One-to-many: User -> Post
286
+ posts() {
287
+ const Post = require('./Post');
288
+ return this.hasMany(Post', 'user_id', 'id').onDelete('cascade');
289
+ }
290
+ }
@@ -7,7 +7,10 @@ export class DB {
7
7
  static eventHandlers: {
8
8
  query: any[];
9
9
  error: any[];
10
+ reconnect: any[];
10
11
  };
12
+ static _grammar: any;
13
+ static _als: AsyncLocalStorage<any>;
11
14
  static initFromEnv({ driver, config, retryAttempts }?: {
12
15
  driver?: string;
13
16
  config?: any;
@@ -17,17 +20,22 @@ export class DB {
17
20
  static _emit(event: any, payload: any): void;
18
21
  static _ensureModule(): any;
19
22
  static connect(): Promise<any>;
23
+ static getConnection(): Promise<any>;
24
+ static reconnect(): Promise<any>;
20
25
  static end(): Promise<void>;
21
- static _pgConvert(sql: any, params: any): {
22
- text: any;
23
- values: any;
24
- };
25
- static _extractTable(sql: any): string;
26
- static _tableExists(table: any): Promise<boolean>;
27
- static raw(sql: any, params?: any[], options?: {}): Promise<any>;
26
+ static run(sql: any, bindings: any, executor: any, { isWrite }?: {
27
+ isWrite?: boolean;
28
+ }): Promise<any>;
29
+ static raw(sql: any, bindings?: any[]): Promise<any>;
30
+ static affectingStatement(sql: any, bindings?: any[]): Promise<any>;
31
+ static select(sql: any, bindings?: any[]): Promise<any>;
32
+ static statement(sql: any, bindings?: any[]): Promise<any>;
33
+ static insert(sql: any, bindings?: any[]): Promise<any>;
34
+ static update(sql: any, bindings?: any[]): Promise<any>;
35
+ static delete(sql: any, bindings?: any[]): Promise<any>;
28
36
  static transaction(fn: any): Promise<any>;
29
37
  static escapeId(id: any): any;
30
- static cached(sql: any, params?: any[], ttl?: number): Promise<any>;
38
+ static cached(sql: any, bindings?: any[], ttl?: number): Promise<any>;
31
39
  }
32
40
  export class Model {
33
41
  static table: any;
@@ -520,6 +528,7 @@ declare class SimpleCache {
520
528
  del(k: any): void;
521
529
  clear(): void;
522
530
  }
531
+ import { AsyncLocalStorage } from "node:async_hooks";
523
532
  declare class HasManyThrough extends Relation {
524
533
  constructor(parent: any, relatedClass: any, throughClass: any, firstKey: any, secondKey: any, localKey: any, secondLocalKey: any);
525
534
  throughClass: any;