@tmlmobilidade/utils 20250827.1127.52 → 20250827.1228.57
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/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './src/batching/index.js';
|
|
2
2
|
export * from './src/convert-object.js';
|
|
3
3
|
export * from './src/css/index.js';
|
|
4
|
+
export * from './src/databases/index.js';
|
|
4
5
|
export * from './src/dates/index.js';
|
|
5
6
|
export * from './src/files/files.js';
|
|
6
7
|
export * from './src/generic/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './src/batching/index.js';
|
|
2
2
|
export * from './src/convert-object.js';
|
|
3
3
|
export * from './src/css/index.js';
|
|
4
|
+
export * from './src/databases/index.js';
|
|
4
5
|
export * from './src/dates/index.js';
|
|
5
6
|
export * from './src/files/files.js';
|
|
6
7
|
export * from './src/generic/index.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './sqlite-map.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './sqlite-map.js';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A map-like structure backed by SQLite for persistent key-value storage.
|
|
3
|
+
* Use it to store and retrieve data across sessions without memory constraints.
|
|
4
|
+
* The API is similar to the native javascript Map object.
|
|
5
|
+
*/
|
|
6
|
+
export declare class SQLiteMap<K extends string, V> {
|
|
7
|
+
/**
|
|
8
|
+
* Returns the number of entries in the map.
|
|
9
|
+
*/
|
|
10
|
+
get size(): number;
|
|
11
|
+
private databaseInstance;
|
|
12
|
+
constructor();
|
|
13
|
+
/**
|
|
14
|
+
* Clears all entries from the map.
|
|
15
|
+
*/
|
|
16
|
+
clear(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Deletes a key-value pair from the map.
|
|
19
|
+
* @param key The key of the entry to delete.
|
|
20
|
+
* @returns True if the entry was deleted, false if it didn't exist.
|
|
21
|
+
*/
|
|
22
|
+
delete(key: K): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Returns an iterator over the entries in the map.
|
|
25
|
+
*/
|
|
26
|
+
entries(): IterableIterator<[K, V]>;
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves the value associated with the given key.
|
|
29
|
+
* @param key The key of the entry to retrieve.
|
|
30
|
+
* @returns The value associated with the key, or undefined if it doesn't exist.
|
|
31
|
+
*/
|
|
32
|
+
get(key: K): undefined | V;
|
|
33
|
+
/**
|
|
34
|
+
* Checks if the map contains a value for the given key.
|
|
35
|
+
* @param key The key to check.
|
|
36
|
+
* @returns True if the key exists, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
has(key: K): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Returns an iterator over the keys in the map.
|
|
41
|
+
*/
|
|
42
|
+
keys(): IterableIterator<K>;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the value for the given key.
|
|
45
|
+
* @param key The key of the entry to set.
|
|
46
|
+
* @param value The value to associate with the key.
|
|
47
|
+
*/
|
|
48
|
+
set(key: K, value: V): void;
|
|
49
|
+
/**
|
|
50
|
+
* Returns an iterator over the entries in the map.
|
|
51
|
+
*/
|
|
52
|
+
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
53
|
+
/**
|
|
54
|
+
* Returns an iterator over the values in the map.
|
|
55
|
+
*/
|
|
56
|
+
values(): IterableIterator<V>;
|
|
57
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { generateRandomString } from '../random/generate-random-string.js';
|
|
3
|
+
import BSQLite3 from 'better-sqlite3';
|
|
4
|
+
/* * */
|
|
5
|
+
/**
|
|
6
|
+
* A map-like structure backed by SQLite for persistent key-value storage.
|
|
7
|
+
* Use it to store and retrieve data across sessions without memory constraints.
|
|
8
|
+
* The API is similar to the native javascript Map object.
|
|
9
|
+
*/
|
|
10
|
+
export class SQLiteMap {
|
|
11
|
+
//
|
|
12
|
+
/**
|
|
13
|
+
* Returns the number of entries in the map.
|
|
14
|
+
*/
|
|
15
|
+
get size() {
|
|
16
|
+
const row = this.databaseInstance
|
|
17
|
+
.prepare(`SELECT COUNT(*) as count FROM map`)
|
|
18
|
+
.get();
|
|
19
|
+
return row.count;
|
|
20
|
+
}
|
|
21
|
+
databaseInstance;
|
|
22
|
+
constructor() {
|
|
23
|
+
//
|
|
24
|
+
//
|
|
25
|
+
// Set up a new database instance
|
|
26
|
+
const databaseFileName = generateRandomString();
|
|
27
|
+
this.databaseInstance = new BSQLite3(`/tmp/${databaseFileName}.db`);
|
|
28
|
+
//
|
|
29
|
+
// Create a new table if it doesn't exist
|
|
30
|
+
this.databaseInstance.pragma('journal_mode = WAL');
|
|
31
|
+
this.databaseInstance.pragma('synchronous = NORMAL');
|
|
32
|
+
this.databaseInstance
|
|
33
|
+
.prepare(`CREATE TABLE IF NOT EXISTS map (key TEXT PRIMARY KEY, value TEXT NOT NULL)`)
|
|
34
|
+
.run();
|
|
35
|
+
//
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Clears all entries from the map.
|
|
39
|
+
*/
|
|
40
|
+
clear() {
|
|
41
|
+
this.databaseInstance
|
|
42
|
+
.prepare(`DELETE FROM map`)
|
|
43
|
+
.run();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Deletes a key-value pair from the map.
|
|
47
|
+
* @param key The key of the entry to delete.
|
|
48
|
+
* @returns True if the entry was deleted, false if it didn't exist.
|
|
49
|
+
*/
|
|
50
|
+
delete(key) {
|
|
51
|
+
const result = this.databaseInstance
|
|
52
|
+
.prepare(`DELETE FROM map WHERE key = ?`)
|
|
53
|
+
.run(key);
|
|
54
|
+
return result.changes > 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns an iterator over the entries in the map.
|
|
58
|
+
*/
|
|
59
|
+
*entries() {
|
|
60
|
+
const statement = this.databaseInstance.prepare(`SELECT key, value FROM map`);
|
|
61
|
+
for (const row of statement.iterate()) {
|
|
62
|
+
yield [JSON.parse(row.key), JSON.parse(row.value)];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves the value associated with the given key.
|
|
67
|
+
* @param key The key of the entry to retrieve.
|
|
68
|
+
* @returns The value associated with the key, or undefined if it doesn't exist.
|
|
69
|
+
*/
|
|
70
|
+
get(key) {
|
|
71
|
+
const row = this.databaseInstance
|
|
72
|
+
.prepare(`SELECT value FROM map WHERE key = ?`)
|
|
73
|
+
.get(key);
|
|
74
|
+
return row ? JSON.parse(row.value) : undefined;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Checks if the map contains a value for the given key.
|
|
78
|
+
* @param key The key to check.
|
|
79
|
+
* @returns True if the key exists, false otherwise.
|
|
80
|
+
*/
|
|
81
|
+
has(key) {
|
|
82
|
+
const row = this.databaseInstance
|
|
83
|
+
.prepare(`SELECT 1 FROM map WHERE key = ?`)
|
|
84
|
+
.get(key);
|
|
85
|
+
return !!row;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Returns an iterator over the keys in the map.
|
|
89
|
+
*/
|
|
90
|
+
*keys() {
|
|
91
|
+
const statement = this.databaseInstance.prepare(`SELECT key FROM map`);
|
|
92
|
+
for (const row of statement.iterate()) {
|
|
93
|
+
yield JSON.parse(row.key);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Sets the value for the given key.
|
|
98
|
+
* @param key The key of the entry to set.
|
|
99
|
+
* @param value The value to associate with the key.
|
|
100
|
+
*/
|
|
101
|
+
set(key, value) {
|
|
102
|
+
this.databaseInstance
|
|
103
|
+
.prepare(`INSERT OR REPLACE INTO map (key, value) VALUES (?, ?)`)
|
|
104
|
+
.run(key, JSON.stringify(value));
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns an iterator over the entries in the map.
|
|
108
|
+
*/
|
|
109
|
+
[Symbol.iterator]() {
|
|
110
|
+
return this.entries();
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns an iterator over the values in the map.
|
|
114
|
+
*/
|
|
115
|
+
*values() {
|
|
116
|
+
const statement = this.databaseInstance.prepare(`SELECT value FROM map`);
|
|
117
|
+
for (const row of statement.iterate()) {
|
|
118
|
+
yield JSON.parse(row.value);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmlmobilidade/utils",
|
|
3
|
-
"version": "20250827.
|
|
3
|
+
"version": "20250827.1228.57",
|
|
4
4
|
"author": "João de Vasconcelos & Jusi Monteiro",
|
|
5
5
|
"license": "AGPL-3.0-or-later",
|
|
6
6
|
"homepage": "https://github.com/tmlmobilidade/services#readme",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@tmlmobilidade/lib": "*",
|
|
43
43
|
"@turf/turf": "7.2.0",
|
|
44
|
+
"better-sqlite3": "12.2.0",
|
|
44
45
|
"extract-zip": "2.0.1",
|
|
45
46
|
"geojson": "0.5.0",
|
|
46
47
|
"jszip": "3.10.1",
|