lamix 4.2.16 → 4.2.17
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 +11 -5
- package/lib/index.d.ts +5 -4
- package/lib/index.js +28 -17
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -117,9 +117,15 @@ const User = require('./models/User');
|
|
|
117
117
|
const user = await User.create({ name: 'Alice', email: 'alice@example.com', password: 'secret' });
|
|
118
118
|
|
|
119
119
|
# Fill & save (update)
|
|
120
|
-
|
|
120
|
+
name = 'Alice Smith';
|
|
121
|
+
user.fill({ name })
|
|
121
122
|
await user.save();
|
|
122
123
|
|
|
124
|
+
# Method 2 (update)
|
|
125
|
+
const user = await User.findOrFail(param.id);
|
|
126
|
+
const payload = req.body;
|
|
127
|
+
await user.update({ payload });
|
|
128
|
+
|
|
123
129
|
# Find by primary key
|
|
124
130
|
const user1 = await User.find(1);
|
|
125
131
|
const user2 = await User.findOrFail(param.id);
|
|
@@ -164,7 +170,7 @@ const qb = User.query();
|
|
|
164
170
|
const names = await User.query().select('id', 'name').get();
|
|
165
171
|
|
|
166
172
|
# Aggregates
|
|
167
|
-
const cnt = await User.query().count();
|
|
173
|
+
const cnt = await User.query().count(); # count(*)
|
|
168
174
|
const sumId = await User.query().sum('id');
|
|
169
175
|
const avgId = await User.query().avg('id');
|
|
170
176
|
const minId = await User.query().min('id');
|
|
@@ -181,12 +187,12 @@ const Pluckemails = await User.query().pluck('email');
|
|
|
181
187
|
|
|
182
188
|
# Pagination
|
|
183
189
|
const page1 = await User.query().paginate(1, 10);
|
|
184
|
-
|
|
190
|
+
#page1 = { total, perPage, page, lastPage, data: [users...] }
|
|
185
191
|
|
|
186
192
|
# Where In
|
|
187
193
|
const usersIn = await User.query().whereIn('id', [1, 2, 3]).get();
|
|
188
194
|
|
|
189
|
-
|
|
195
|
+
# Where Null / Not Null
|
|
190
196
|
const withNull = await User.query().whereNull('deleted_at').get();
|
|
191
197
|
const notNull = await User.query().whereNotNull('deleted_at').get();
|
|
192
198
|
|
|
@@ -219,7 +225,7 @@ const cached = await DB.cached('SELECT * FROM users WHERE id = ?', [5], 60000);
|
|
|
219
225
|
#You can use relations:
|
|
220
226
|
|
|
221
227
|
const user = await User.find(1);
|
|
222
|
-
const posts = await user.posts().get();
|
|
228
|
+
const posts = await user.posts().get(); # all posts for the user
|
|
223
229
|
|
|
224
230
|
# Eager load in a query with Relations:
|
|
225
231
|
const usersWithPosts = await User.query().with('posts').get();
|
package/lib/index.d.ts
CHANGED
|
@@ -494,7 +494,7 @@ export class LamixSessionStore {
|
|
|
494
494
|
cleanupInterval: any;
|
|
495
495
|
logger: any;
|
|
496
496
|
compress: any;
|
|
497
|
-
cache:
|
|
497
|
+
cache: LRUCache<{}, {}, unknown>;
|
|
498
498
|
redisEnabled: boolean;
|
|
499
499
|
redisRetryAt: number;
|
|
500
500
|
redisCooldown: any;
|
|
@@ -547,8 +547,9 @@ export class BaseModel extends Model {
|
|
|
547
547
|
serialize(): {};
|
|
548
548
|
}
|
|
549
549
|
declare class SimpleCache {
|
|
550
|
-
|
|
551
|
-
|
|
550
|
+
constructor(options?: {});
|
|
551
|
+
cache: LRUCache<{}, {}, unknown>;
|
|
552
|
+
get(k: any): {};
|
|
552
553
|
set(k: any, v: any, ttl?: number): void;
|
|
553
554
|
del(k: any): void;
|
|
554
555
|
clear(): void;
|
|
@@ -611,5 +612,5 @@ declare class Relation {
|
|
|
611
612
|
deleteBehavior: any;
|
|
612
613
|
onDelete(behavior: any): this;
|
|
613
614
|
}
|
|
614
|
-
import
|
|
615
|
+
import { LRUCache } from "lru-cache";
|
|
615
616
|
export {};
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
const util = require('util');
|
|
4
4
|
const { performance } = require('perf_hooks');
|
|
5
5
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
6
|
+
const session = require('express-session');
|
|
7
|
+
const Redis = require('ioredis');
|
|
8
|
+
const LRUCache = require('lru-cache');
|
|
9
|
+
const zlib = require('zlib');
|
|
6
10
|
require('dotenv').config();
|
|
7
11
|
|
|
8
12
|
/* ---------------- Utilities ---------------- */
|
|
@@ -27,21 +31,33 @@ class DBError extends Error {
|
|
|
27
31
|
/* ---------------- Cache ---------------- */
|
|
28
32
|
|
|
29
33
|
class SimpleCache {
|
|
30
|
-
constructor(
|
|
34
|
+
constructor(options = {}) {
|
|
35
|
+
this.cache = new LRUCache({
|
|
36
|
+
max: options.max || 1000, // max number of items
|
|
37
|
+
ttlAutopurge: true, // auto-remove expired entries
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
get(k) {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
if (e.ttl && Date.now() > e.ts + e.ttl) {
|
|
35
|
-
this.map.delete(k);
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
return e.v;
|
|
42
|
+
const v = this.cache.get(k);
|
|
43
|
+
return v === undefined ? null : v;
|
|
39
44
|
}
|
|
45
|
+
|
|
40
46
|
set(k, v, ttl = 0) {
|
|
41
|
-
|
|
47
|
+
if (ttl > 0) {
|
|
48
|
+
this.cache.set(k, v, { ttl });
|
|
49
|
+
} else {
|
|
50
|
+
this.cache.set(k, v);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
del(k) {
|
|
55
|
+
this.cache.delete(k);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
clear() {
|
|
59
|
+
this.cache.clear();
|
|
42
60
|
}
|
|
43
|
-
del(k) { this.map.delete(k); }
|
|
44
|
-
clear() { this.map.clear(); }
|
|
45
61
|
}
|
|
46
62
|
|
|
47
63
|
/* ---------------- Grammar ---------------- */
|
|
@@ -3891,11 +3907,6 @@ class Model {
|
|
|
3891
3907
|
}
|
|
3892
3908
|
}
|
|
3893
3909
|
|
|
3894
|
-
const session = require('express-session');
|
|
3895
|
-
const Redis = require('ioredis');
|
|
3896
|
-
const LRU = require('lru-cache');
|
|
3897
|
-
const zlib = require('zlib');
|
|
3898
|
-
|
|
3899
3910
|
/* -------------------- DB Model -------------------- */
|
|
3900
3911
|
class Session extends Model {
|
|
3901
3912
|
static table = 'sessions';
|
|
@@ -3927,7 +3938,7 @@ class LamixSessionStore extends session.Store {
|
|
|
3927
3938
|
this.compress = options.compress ?? true;
|
|
3928
3939
|
|
|
3929
3940
|
/* -------- In-memory cache -------- */
|
|
3930
|
-
this.cache = new
|
|
3941
|
+
this.cache = new LRUCache({
|
|
3931
3942
|
max: options.cacheSize || 1000,
|
|
3932
3943
|
ttl: options.cacheTTL || 60000,
|
|
3933
3944
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lamix",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.17",
|
|
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": "^
|
|
34
|
+
"lru-cache": "^11.2.5",
|
|
36
35
|
"ioredis": "^5.9.2",
|
|
37
36
|
"chalk": "^4.1.2",
|
|
38
|
-
"dotenv": "^17.2.
|
|
37
|
+
"dotenv": "^17.2.4"
|
|
39
38
|
},
|
|
40
39
|
"keywords": [
|
|
41
40
|
"lamix",
|