@lakutata/cache 3.0.0-beta.0
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/LICENSE +23 -0
- package/README.md +175 -0
- package/dist/cjs/cache/Cacher.d.ts +446 -0
- package/dist/cjs/cache/Cacher.js +523 -0
- package/dist/cjs/cache/exceptions/CacheDriverNotFoundException.d.ts +10 -0
- package/dist/cjs/cache/exceptions/CacheDriverNotFoundException.js +17 -0
- package/dist/cjs/cache/interfaces/CacherOptions.d.ts +42 -0
- package/dist/cjs/cache/interfaces/CacherOptions.js +2 -0
- package/dist/cjs/cache/lib/LikePrefix.d.ts +5 -0
- package/dist/cjs/cache/lib/LikePrefix.js +10 -0
- package/dist/cjs/cache/lib/LoadDriver.d.ts +6 -0
- package/dist/cjs/cache/lib/LoadDriver.js +66 -0
- package/dist/cjs/cache/lib/Serializer.d.ts +18 -0
- package/dist/cjs/cache/lib/Serializer.js +46 -0
- package/dist/cjs/cache/lib/WithTimeout.d.ts +7 -0
- package/dist/cjs/cache/lib/WithTimeout.js +23 -0
- package/dist/cjs/cache/options/FileCacheOptions.d.ts +54 -0
- package/dist/cjs/cache/options/FileCacheOptions.js +62 -0
- package/dist/cjs/cache/options/MemcacheCacheOptions.d.ts +57 -0
- package/dist/cjs/cache/options/MemcacheCacheOptions.js +66 -0
- package/dist/cjs/cache/options/MemoryCacheOptions.d.ts +39 -0
- package/dist/cjs/cache/options/MemoryCacheOptions.js +52 -0
- package/dist/cjs/cache/options/MongoCacheOptions.d.ts +72 -0
- package/dist/cjs/cache/options/MongoCacheOptions.js +81 -0
- package/dist/cjs/cache/options/MysqlCacheOptions.d.ts +73 -0
- package/dist/cjs/cache/options/MysqlCacheOptions.js +82 -0
- package/dist/cjs/cache/options/PostgresCacheOptions.d.ts +87 -0
- package/dist/cjs/cache/options/PostgresCacheOptions.js +92 -0
- package/dist/cjs/cache/options/RedisCacheOptions.d.ts +120 -0
- package/dist/cjs/cache/options/RedisCacheOptions.js +113 -0
- package/dist/cjs/cache/options/SqliteCacheOptions.d.ts +53 -0
- package/dist/cjs/cache/options/SqliteCacheOptions.js +61 -0
- package/dist/cjs/cache/stores/CacheStore.d.ts +44 -0
- package/dist/cjs/cache/stores/CacheStore.js +42 -0
- package/dist/cjs/cache/stores/CreateCacheStore.d.ts +7 -0
- package/dist/cjs/cache/stores/CreateCacheStore.js +37 -0
- package/dist/cjs/cache/stores/FileStore.d.ts +51 -0
- package/dist/cjs/cache/stores/FileStore.js +143 -0
- package/dist/cjs/cache/stores/MemcacheStore.d.ts +42 -0
- package/dist/cjs/cache/stores/MemcacheStore.js +69 -0
- package/dist/cjs/cache/stores/MemoryStore.d.ts +34 -0
- package/dist/cjs/cache/stores/MemoryStore.js +75 -0
- package/dist/cjs/cache/stores/MongoStore.d.ts +55 -0
- package/dist/cjs/cache/stores/MongoStore.js +72 -0
- package/dist/cjs/cache/stores/MysqlStore.d.ts +27 -0
- package/dist/cjs/cache/stores/MysqlStore.js +71 -0
- package/dist/cjs/cache/stores/PostgresStore.d.ts +30 -0
- package/dist/cjs/cache/stores/PostgresStore.js +85 -0
- package/dist/cjs/cache/stores/RedisStore.d.ts +58 -0
- package/dist/cjs/cache/stores/RedisStore.js +131 -0
- package/dist/cjs/cache/stores/SerializedCacheStore.d.ts +48 -0
- package/dist/cjs/cache/stores/SerializedCacheStore.js +80 -0
- package/dist/cjs/cache/stores/SqliteStore.d.ts +44 -0
- package/dist/cjs/cache/stores/SqliteStore.js +91 -0
- package/dist/cjs/cache/types/CacheStoreOptions.d.ts +15 -0
- package/dist/cjs/cache/types/CacheStoreOptions.js +2 -0
- package/dist/cjs/exports/Cache.d.ts +12 -0
- package/dist/cjs/exports/Cache.js +31 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/cache/Cacher.js +518 -0
- package/dist/esm/cache/exceptions/CacheDriverNotFoundException.js +13 -0
- package/dist/esm/cache/interfaces/CacherOptions.js +1 -0
- package/dist/esm/cache/lib/LikePrefix.js +7 -0
- package/dist/esm/cache/lib/LoadDriver.js +30 -0
- package/dist/esm/cache/lib/Serializer.js +42 -0
- package/dist/esm/cache/lib/WithTimeout.js +20 -0
- package/dist/esm/cache/options/FileCacheOptions.js +58 -0
- package/dist/esm/cache/options/MemcacheCacheOptions.js +62 -0
- package/dist/esm/cache/options/MemoryCacheOptions.js +48 -0
- package/dist/esm/cache/options/MongoCacheOptions.js +77 -0
- package/dist/esm/cache/options/MysqlCacheOptions.js +78 -0
- package/dist/esm/cache/options/PostgresCacheOptions.js +88 -0
- package/dist/esm/cache/options/RedisCacheOptions.js +109 -0
- package/dist/esm/cache/options/SqliteCacheOptions.js +57 -0
- package/dist/esm/cache/stores/CacheStore.js +36 -0
- package/dist/esm/cache/stores/CreateCacheStore.js +34 -0
- package/dist/esm/cache/stores/FileStore.js +136 -0
- package/dist/esm/cache/stores/MemcacheStore.js +65 -0
- package/dist/esm/cache/stores/MemoryStore.js +71 -0
- package/dist/esm/cache/stores/MongoStore.js +68 -0
- package/dist/esm/cache/stores/MysqlStore.js +67 -0
- package/dist/esm/cache/stores/PostgresStore.js +81 -0
- package/dist/esm/cache/stores/RedisStore.js +127 -0
- package/dist/esm/cache/stores/SerializedCacheStore.js +76 -0
- package/dist/esm/cache/stores/SqliteStore.js +87 -0
- package/dist/esm/cache/types/CacheStoreOptions.js +1 -0
- package/dist/esm/exports/Cache.js +12 -0
- package/dist/types/cache/Cacher.d.ts +446 -0
- package/dist/types/cache/exceptions/CacheDriverNotFoundException.d.ts +10 -0
- package/dist/types/cache/interfaces/CacherOptions.d.ts +42 -0
- package/dist/types/cache/lib/LikePrefix.d.ts +5 -0
- package/dist/types/cache/lib/LoadDriver.d.ts +6 -0
- package/dist/types/cache/lib/Serializer.d.ts +18 -0
- package/dist/types/cache/lib/WithTimeout.d.ts +7 -0
- package/dist/types/cache/options/FileCacheOptions.d.ts +54 -0
- package/dist/types/cache/options/MemcacheCacheOptions.d.ts +57 -0
- package/dist/types/cache/options/MemoryCacheOptions.d.ts +39 -0
- package/dist/types/cache/options/MongoCacheOptions.d.ts +72 -0
- package/dist/types/cache/options/MysqlCacheOptions.d.ts +73 -0
- package/dist/types/cache/options/PostgresCacheOptions.d.ts +87 -0
- package/dist/types/cache/options/RedisCacheOptions.d.ts +120 -0
- package/dist/types/cache/options/SqliteCacheOptions.d.ts +53 -0
- package/dist/types/cache/stores/CacheStore.d.ts +44 -0
- package/dist/types/cache/stores/CreateCacheStore.d.ts +7 -0
- package/dist/types/cache/stores/FileStore.d.ts +51 -0
- package/dist/types/cache/stores/MemcacheStore.d.ts +42 -0
- package/dist/types/cache/stores/MemoryStore.d.ts +34 -0
- package/dist/types/cache/stores/MongoStore.d.ts +55 -0
- package/dist/types/cache/stores/MysqlStore.d.ts +27 -0
- package/dist/types/cache/stores/PostgresStore.d.ts +30 -0
- package/dist/types/cache/stores/RedisStore.d.ts +58 -0
- package/dist/types/cache/stores/SerializedCacheStore.d.ts +48 -0
- package/dist/types/cache/stores/SqliteStore.d.ts +44 -0
- package/dist/types/cache/types/CacheStoreOptions.d.ts +15 -0
- package/dist/types/exports/Cache.d.ts +12 -0
- package/package.json +72 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteStore = void 0;
|
|
4
|
+
const SerializedCacheStore_js_1 = require("./SerializedCacheStore.js");
|
|
5
|
+
const LoadDriver_js_1 = require("../lib/LoadDriver.js");
|
|
6
|
+
/**
|
|
7
|
+
* The table name of 2.x: the alphanumeric characters, starting with a letter
|
|
8
|
+
* @param name
|
|
9
|
+
*/
|
|
10
|
+
function tableName(name) {
|
|
11
|
+
const sanitized = name.replace(/[^a-zA-Z0-9_]/g, '');
|
|
12
|
+
if (!sanitized)
|
|
13
|
+
throw new Error('Invalid table name: must contain alphanumeric characters');
|
|
14
|
+
return /^[a-zA-Z]/.test(sanitized) ? sanitized : `_${sanitized}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The SQLite store
|
|
18
|
+
* Table format: (key VARCHAR(255) PRIMARY KEY, value TEXT), with "{namespace}:{key}" keys
|
|
19
|
+
*/
|
|
20
|
+
class SqliteStore extends SerializedCacheStore_js_1.SerializedCacheStore {
|
|
21
|
+
constructor(options) {
|
|
22
|
+
super(options.namespace);
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.table = tableName(options.table);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Query the rows
|
|
28
|
+
* @param sql
|
|
29
|
+
* @param params
|
|
30
|
+
* @protected
|
|
31
|
+
*/
|
|
32
|
+
all(sql, params) {
|
|
33
|
+
return new Promise((resolve, reject) => this.database.all(sql, params, (error, rows) => error ? reject(error) : resolve(rows)));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Run a statement, resolve the number of changed rows
|
|
37
|
+
* @param sql
|
|
38
|
+
* @param params
|
|
39
|
+
* @protected
|
|
40
|
+
*/
|
|
41
|
+
run(sql, params = []) {
|
|
42
|
+
return new Promise((resolve, reject) => this.database.run(sql, params, function (error) {
|
|
43
|
+
error ? reject(error) : resolve(this.changes);
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
async connect() {
|
|
47
|
+
const driver = await (0, LoadDriver_js_1.loadDriver)('Database', 'sqlite3');
|
|
48
|
+
await new Promise((resolve, reject) => {
|
|
49
|
+
this.database = new driver.Database(this.options.database, (error) => error ? reject(error) : resolve());
|
|
50
|
+
});
|
|
51
|
+
this.database.configure('busyTimeout', this.options.busyTimeout ?? 10000);
|
|
52
|
+
await this.run(`CREATE TABLE IF NOT EXISTS ${this.table}(key VARCHAR(255) PRIMARY KEY, value TEXT)`);
|
|
53
|
+
}
|
|
54
|
+
async disconnect() {
|
|
55
|
+
if (!this.database)
|
|
56
|
+
return;
|
|
57
|
+
await new Promise((resolve, reject) => this.database.close((error) => error ? reject(error) : resolve()));
|
|
58
|
+
}
|
|
59
|
+
async read(key) {
|
|
60
|
+
const rows = await this.all(`SELECT value FROM ${this.table} WHERE key = ?`, [key]);
|
|
61
|
+
return rows[0]?.value;
|
|
62
|
+
}
|
|
63
|
+
async readMany(keys) {
|
|
64
|
+
const rows = await this.all(`SELECT key, value FROM ${this.table} WHERE key IN (SELECT value FROM json_each(?))`, [JSON.stringify(keys)]);
|
|
65
|
+
const values = new Map(rows.map((row) => [row.key, row.value]));
|
|
66
|
+
return keys.map((key) => values.get(key));
|
|
67
|
+
}
|
|
68
|
+
async write(entry) {
|
|
69
|
+
await this.run(`INSERT INTO ${this.table} (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value`, [entry.key, entry.data]);
|
|
70
|
+
}
|
|
71
|
+
async writeMany(entries) {
|
|
72
|
+
await this.run(`INSERT INTO ${this.table} (key, value) SELECT json_extract(value, '$[0]'), json_extract(value, '$[1]') FROM json_each(?) WHERE 1 ON CONFLICT (key) DO UPDATE SET value = excluded.value`, [JSON.stringify(entries.map((entry) => [entry.key, entry.data]))]);
|
|
73
|
+
}
|
|
74
|
+
async remove(key) {
|
|
75
|
+
return (await this.run(`DELETE FROM ${this.table} WHERE key = ?`, [key])) > 0;
|
|
76
|
+
}
|
|
77
|
+
async removeMany(keys) {
|
|
78
|
+
return (await this.run(`DELETE FROM ${this.table} WHERE key IN (SELECT value FROM json_each(?))`, [JSON.stringify(keys)])) > 0;
|
|
79
|
+
}
|
|
80
|
+
async clear() {
|
|
81
|
+
if (this.namespace) {
|
|
82
|
+
//LIKE ignores the case in SQLite
|
|
83
|
+
const prefix = `${this.namespace}:`;
|
|
84
|
+
await this.run(`DELETE FROM ${this.table} WHERE substr(key, 1, length(?1)) = ?1`, [prefix]);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
await this.run(`DELETE FROM ${this.table}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.SqliteStore = SqliteStore;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FileCacheOptions } from '../options/FileCacheOptions.js';
|
|
2
|
+
import { MemoryCacheOptions } from '../options/MemoryCacheOptions.js';
|
|
3
|
+
import { RedisCacheOptions } from '../options/RedisCacheOptions.js';
|
|
4
|
+
import { SqliteCacheOptions } from '../options/SqliteCacheOptions.js';
|
|
5
|
+
import { PostgresCacheOptions } from '../options/PostgresCacheOptions.js';
|
|
6
|
+
import { MysqlCacheOptions } from '../options/MysqlCacheOptions.js';
|
|
7
|
+
import { MongoCacheOptions } from '../options/MongoCacheOptions.js';
|
|
8
|
+
import { MemcacheCacheOptions } from '../options/MemcacheCacheOptions.js';
|
|
9
|
+
/**
|
|
10
|
+
* The options of one store of a `Cacher`, told apart by their `type`: `'memory'` ({@link MemoryCacheOptions}, the
|
|
11
|
+
* store of a cache without stores), `'file'` ({@link FileCacheOptions}), `'redis'` ({@link RedisCacheOptions}),
|
|
12
|
+
* `'memcache'` ({@link MemcacheCacheOptions}), `'mongo'` ({@link MongoCacheOptions}), `'sqlite'`
|
|
13
|
+
* ({@link SqliteCacheOptions}), `'postgres'` ({@link PostgresCacheOptions}) or `'mysql'` ({@link MysqlCacheOptions}).
|
|
14
|
+
*/
|
|
15
|
+
export type CacheStoreOptions = MemoryCacheOptions | FileCacheOptions | RedisCacheOptions | MemcacheCacheOptions | MongoCacheOptions | SqliteCacheOptions | PostgresCacheOptions | MysqlCacheOptions;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from '../cache/Cacher.js';
|
|
2
|
+
export * from '../cache/interfaces/CacherOptions.js';
|
|
3
|
+
export * from '../cache/options/FileCacheOptions.js';
|
|
4
|
+
export * from '../cache/options/MemoryCacheOptions.js';
|
|
5
|
+
export * from '../cache/options/MemcacheCacheOptions.js';
|
|
6
|
+
export * from '../cache/options/MongoCacheOptions.js';
|
|
7
|
+
export * from '../cache/options/MysqlCacheOptions.js';
|
|
8
|
+
export * from '../cache/options/PostgresCacheOptions.js';
|
|
9
|
+
export * from '../cache/options/RedisCacheOptions.js';
|
|
10
|
+
export * from '../cache/options/SqliteCacheOptions.js';
|
|
11
|
+
export * from '../cache/types/CacheStoreOptions.js';
|
|
12
|
+
export * from '../cache/exceptions/CacheDriverNotFoundException.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../cache/Cacher.js"), exports);
|
|
18
|
+
__exportStar(require("../cache/interfaces/CacherOptions.js"), exports);
|
|
19
|
+
__exportStar(require("../cache/options/FileCacheOptions.js"), exports);
|
|
20
|
+
__exportStar(require("../cache/options/MemoryCacheOptions.js"), exports);
|
|
21
|
+
__exportStar(require("../cache/options/MemcacheCacheOptions.js"), exports);
|
|
22
|
+
__exportStar(require("../cache/options/MongoCacheOptions.js"), exports);
|
|
23
|
+
__exportStar(require("../cache/options/MysqlCacheOptions.js"), exports);
|
|
24
|
+
__exportStar(require("../cache/options/PostgresCacheOptions.js"), exports);
|
|
25
|
+
__exportStar(require("../cache/options/RedisCacheOptions.js"), exports);
|
|
26
|
+
__exportStar(require("../cache/options/SqliteCacheOptions.js"), exports);
|
|
27
|
+
__exportStar(require("../cache/types/CacheStoreOptions.js"), exports);
|
|
28
|
+
__exportStar(require("../cache/exceptions/CacheDriverNotFoundException.js"), exports);
|
|
29
|
+
|
|
30
|
+
//The default of the entry: the entry itself (scripts/package-build/cjs-package.mjs)
|
|
31
|
+
if (!Object.prototype.hasOwnProperty.call(exports, 'default')) Object.defineProperty(exports, 'default', {value: exports})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|