lamix 4.2.16 → 4.2.18

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/lib/index.d.ts CHANGED
@@ -294,15 +294,8 @@ export class QueryBuilder {
294
294
  _pushWhere(w: any): this;
295
295
  then(resolve: any, reject: any): Promise<Collection>;
296
296
  where(columnOrObject: any, operator: any, value: any, ...args: any[]): this;
297
+ andWhere(...args: any[]): this;
297
298
  orWhere(columnOrObject: any, operatorOrValue: any, value: any, ...args: any[]): this;
298
- toSQL(): string;
299
- /**************************************************************************
300
- * TO SQL
301
- **************************************************************************/
302
- toSQL(): {
303
- sql: string;
304
- bindings: any[];
305
- };
306
299
  whereRaw(sql: any, bindings?: any[]): this;
307
300
  orWhereRaw(sql: any, bindings?: any[]): this;
308
301
  whereColumn(a: any, op: any, b: any, ...args: any[]): this;
@@ -433,6 +426,13 @@ export class QueryBuilder {
433
426
  }[];
434
427
  fromRaw: any;
435
428
  };
429
+ /**************************************************************************
430
+ * TO SQL
431
+ **************************************************************************/
432
+ toSQL(): {
433
+ sql: string;
434
+ bindings: any[];
435
+ };
436
436
  toSQLJSON(): {
437
437
  sql: string;
438
438
  bindings: any[];
@@ -494,7 +494,7 @@ export class LamixSessionStore {
494
494
  cleanupInterval: any;
495
495
  logger: any;
496
496
  compress: any;
497
- cache: LRU<any, any>;
497
+ cache: any;
498
498
  redisEnabled: boolean;
499
499
  redisRetryAt: number;
500
500
  redisCooldown: any;
@@ -547,11 +547,25 @@ export class BaseModel extends Model {
547
547
  serialize(): {};
548
548
  }
549
549
  declare class SimpleCache {
550
- map: Map<any, any>;
551
- get(k: any): any;
552
- set(k: any, v: any, ttl?: number): void;
553
- del(k: any): void;
550
+ constructor(options?: {});
551
+ cache: any;
552
+ get(key: any): any;
553
+ set(key: any, value: any, ttl?: any): void;
554
+ has(key: any): any;
555
+ delete(key: any): any;
554
556
  clear(): void;
557
+ /**
558
+ * Get value or compute + store it
559
+ * Supports async functions
560
+ */
561
+ getOrSet(key: any, fetchFn: any, ttl?: any): Promise<any>;
562
+ /**
563
+ * Basic cache stats
564
+ */
565
+ stats(): {
566
+ size: any;
567
+ calculatedSize: any;
568
+ };
555
569
  }
556
570
  import { AsyncLocalStorage } from "node:async_hooks";
557
571
  declare class HasManyThrough extends Relation {
@@ -611,5 +625,4 @@ declare class Relation {
611
625
  deleteBehavior: any;
612
626
  onDelete(behavior: any): this;
613
627
  }
614
- import LRU = require("lru-cache");
615
628
  export {};
package/lib/index.js CHANGED
@@ -4,6 +4,10 @@ const util = require('util');
4
4
  const { performance } = require('perf_hooks');
5
5
  const { AsyncLocalStorage } = require('async_hooks');
6
6
  require('dotenv').config();
7
+ const session = require('express-session');
8
+ const Redis = require('ioredis');
9
+ const LRUCache = require('lru-cache');
10
+ const zlib = require('zlib');
7
11
 
8
12
  /* ---------------- Utilities ---------------- */
9
13
 
@@ -25,25 +29,76 @@ class DBError extends Error {
25
29
  }
26
30
 
27
31
  /* ---------------- Cache ---------------- */
28
-
29
32
  class SimpleCache {
30
- constructor() { this.map = new Map(); }
31
- get(k) {
32
- const e = this.map.get(k);
33
- if (!e) return null;
34
- if (e.ttl && Date.now() > e.ts + e.ttl) {
35
- this.map.delete(k);
36
- return null;
33
+ constructor(options = {}) {
34
+ const {
35
+ max = 1000, // max number of items
36
+ ttl = 0, // default TTL (ms)
37
+ maxSize, // optional size-based eviction
38
+ sizeCalculation, // function(value, key)
39
+ } = options;
40
+
41
+ this.cache = new LRUCache({
42
+ max,
43
+ ttl,
44
+ ttlAutopurge: true,
45
+ maxSize,
46
+ sizeCalculation,
47
+ allowStale: false,
48
+ updateAgeOnGet: false,
49
+ });
50
+ }
51
+
52
+ get(key) {
53
+ const value = this.cache.get(key);
54
+ return value === undefined ? null : value;
55
+ }
56
+
57
+ set(key, value, ttl = null) {
58
+ if (ttl !== null) {
59
+ this.cache.set(key, value, { ttl });
60
+ } else {
61
+ this.cache.set(key, value);
37
62
  }
38
- return e.v;
39
63
  }
40
- set(k, v, ttl = 0) {
41
- this.map.set(k, { v, ts: Date.now(), ttl });
64
+
65
+ has(key) {
66
+ return this.cache.has(key);
67
+ }
68
+
69
+ delete(key) {
70
+ return this.cache.delete(key);
71
+ }
72
+
73
+ clear() {
74
+ this.cache.clear();
75
+ }
76
+
77
+ /**
78
+ * Get value or compute + store it
79
+ * Supports async functions
80
+ */
81
+ async getOrSet(key, fetchFn, ttl = null) {
82
+ const existing = this.get(key);
83
+ if (existing !== null) return existing;
84
+
85
+ const value = await fetchFn();
86
+ this.set(key, value, ttl);
87
+ return value;
88
+ }
89
+
90
+ /**
91
+ * Basic cache stats
92
+ */
93
+ stats() {
94
+ return {
95
+ size: this.cache.size,
96
+ calculatedSize: this.cache.calculatedSize,
97
+ };
42
98
  }
43
- del(k) { this.map.delete(k); }
44
- clear() { this.map.clear(); }
45
99
  }
46
100
 
101
+
47
102
  /* ---------------- Grammar ---------------- */
48
103
 
49
104
  class Grammar {
@@ -69,7 +124,7 @@ class DB {
69
124
  static config = null;
70
125
  static pool = null;
71
126
 
72
- static cache = new SimpleCache();
127
+ static cache = new SimpleCache({ max: 1000, ttl: 60_000 });
73
128
  static retryAttempts = 1;
74
129
 
75
130
  static eventHandlers = { query: [], error: [], reconnect: [] };
@@ -1292,6 +1347,10 @@ class QueryBuilder {
1292
1347
  });
1293
1348
  }
1294
1349
 
1350
+ andWhere(...args) {
1351
+ return this.where(...args);
1352
+ }
1353
+
1295
1354
  orWhere(columnOrObject, operatorOrValue, value) {
1296
1355
  if (typeof columnOrObject === 'object' && columnOrObject !== null) {
1297
1356
  let query = this;
@@ -1320,15 +1379,14 @@ class QueryBuilder {
1320
1379
  }
1321
1380
 
1322
1381
  // Example method to generate SQL
1323
- toSQL() {
1324
- if (!this.wheres.length) return '';
1325
- const sql = this.wheres.map((w, i) => {
1326
- const prefix = i === 0 ? '' : ` ${w.boolean} `;
1327
- return `${prefix}\`${w.column}\` ${w.operator} ?`;
1328
- }).join('');
1329
- return 'WHERE ' + sql;
1330
- }
1331
-
1382
+ // toSQL() {
1383
+ // if (!this.wheres.length) return '';
1384
+ // const sql = this.wheres.map((w, i) => {
1385
+ // const prefix = i === 0 ? '' : ` ${w.boolean} `;
1386
+ // return `${prefix}\`${w.column}\` ${w.operator} ?`;
1387
+ // }).join('');
1388
+ // return 'WHERE ' + sql;
1389
+ // }
1332
1390
 
1333
1391
  whereRaw(sql, bindings = []) {
1334
1392
  return this._pushWhere({
@@ -3435,7 +3493,9 @@ class Model {
3435
3493
  // INSERT – validation first
3436
3494
  // ──────────────────────────────
3437
3495
  async saveNew(attrs) {
3438
- if (!this._exists && !Object.keys(this._attributes).length) {
3496
+ this._attributes = { ...attrs };
3497
+
3498
+ if (!Object.keys(this._attributes).length) {
3439
3499
  throw new DBError('Cannot save empty model', {
3440
3500
  model: this.constructor.name
3441
3501
  });
@@ -3891,11 +3951,6 @@ class Model {
3891
3951
  }
3892
3952
  }
3893
3953
 
3894
- const session = require('express-session');
3895
- const Redis = require('ioredis');
3896
- const LRU = require('lru-cache');
3897
- const zlib = require('zlib');
3898
-
3899
3954
  /* -------------------- DB Model -------------------- */
3900
3955
  class Session extends Model {
3901
3956
  static table = 'sessions';
@@ -3927,7 +3982,7 @@ class LamixSessionStore extends session.Store {
3927
3982
  this.compress = options.compress ?? true;
3928
3983
 
3929
3984
  /* -------- In-memory cache -------- */
3930
- this.cache = new LRU({
3985
+ this.cache = new LRUCache({
3931
3986
  max: options.cacheSize || 1000,
3932
3987
  ttl: options.cacheTTL || 60000,
3933
3988
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lamix",
3
- "version": "4.2.16",
3
+ "version": "4.2.18",
4
4
  "description": "lamix - ORM for Node-express js",
5
5
  "main": "./lib",
6
6
  "type": "commonjs",
@@ -12,8 +12,7 @@
12
12
  },
13
13
  "files": [
14
14
  "lib",
15
- "bin",
16
- "README.md"
15
+ "bin"
17
16
  ],
18
17
  "repository": {
19
18
  "type": "git",
@@ -32,10 +31,10 @@
32
31
  "dependencies": {
33
32
  "bcrypt": "^6.0.0",
34
33
  "express-session": "^1.19.0",
35
- "lru-cache": "^7.18.3",
34
+ "lru-cache": "^11.2.5",
36
35
  "ioredis": "^5.9.2",
37
36
  "chalk": "^4.1.2",
38
- "dotenv": "^17.2.2"
37
+ "dotenv": "^17.2.4 "
39
38
  },
40
39
  "keywords": [
41
40
  "lamix",