Skip to content

Commit 3742dbf

Browse files
committed
SQLite Key-value store to replace Redis
1 parent 4c25435 commit 3742dbf

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/Uwave.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import acl from './plugins/acl.js';
2020
import waitlist from './plugins/waitlist.js';
2121
import passport from './plugins/passport.js';
2222
import migrations from './plugins/migrations.js';
23-
import { SqliteDateColumnsPlugin, connect as connectSqlite } from './utils/sqlite.js';
23+
import { SqliteDateColumnsPlugin, connect as connectSqlite, jsonb } from './utils/sqlite.js';
2424

2525
const DEFAULT_MONGO_URL = 'mongodb://localhost:27017/uwave';
2626
const DEFAULT_REDIS_URL = 'redis://localhost:6379';
@@ -180,6 +180,37 @@ class UwaveServer extends EventEmitter {
180180
this.redis.quit(),
181181
]));
182182

183+
class KeyValue {
184+
#db;
185+
186+
/** @param {Kysely<import('./schema.js').Database>} db */
187+
constructor(db) {
188+
this.#db = db;
189+
}
190+
191+
/** @param {string} key */
192+
async get(key, db = this.#db) {
193+
const row = await db.selectFrom('keyval')
194+
.select('value')
195+
.where('key', '=', key)
196+
.executeTakeFirst();
197+
return row != null ? JSON.parse(/** @type {string} */ (/** @type {unknown} */ (row.value))) : null;
198+
}
199+
200+
/**
201+
* @param {string} key
202+
* @param {import('type-fest').JsonValue} value
203+
*/
204+
async set(key, value, db = this.#db) {
205+
await db.insertInto('keyval')
206+
.values({ key, value: jsonb(value) })
207+
.onConflict((oc) => oc.column('key').doUpdateSet({ value: jsonb(value) }))
208+
.execute();
209+
}
210+
}
211+
212+
this.keyv = new KeyValue(this.db);
213+
183214
boot.use(migrations);
184215
boot.use(configStore);
185216

src/migrations/002-sql.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ async function up({ context: uw }) {
1616
.addColumn('value', 'jsonb')
1717
.execute();
1818

19+
await db.schema.createTable('keyval')
20+
.addColumn('key', 'text', (col) => col.primaryKey())
21+
.addColumn('value', 'jsonb')
22+
.execute();
23+
1924
await db.schema.createTable('media')
2025
.addColumn('id', 'uuid', (col) => col.primaryKey())
2126
.addColumn('source_type', 'text', (col) => col.notNull())

src/schema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Kysely as KyselyBase, Generated } from 'kysely';
2-
import type { JsonObject, Tagged } from 'type-fest'; // eslint-disable-line n/no-missing-import, n/no-unpublished-import
2+
import type { JsonObject, JsonValue, Tagged } from 'type-fest'; // eslint-disable-line n/no-missing-import, n/no-unpublished-import
33

44
export type UserID = Tagged<string, 'UserID'>;
55
export type MediaID = Tagged<string, 'MediaID'>;
@@ -134,12 +134,18 @@ export interface ConfigurationTable {
134134
value: JsonObject | null,
135135
}
136136

137+
export interface KeyvalTable {
138+
key: string,
139+
value: JsonValue,
140+
}
141+
137142
export interface MigrationTable {
138143
name: string,
139144
}
140145

141146
export interface Database {
142147
configuration: ConfigurationTable,
148+
keyval: KeyvalTable,
143149
migrations: MigrationTable,
144150
media: MediaTable,
145151
users: UserTable,

0 commit comments

Comments
 (0)