Skip to content

Commit

Permalink
SQLite Key-value store to replace Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Nov 25, 2024
1 parent 4c25435 commit 3742dbf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/Uwave.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import acl from './plugins/acl.js';
import waitlist from './plugins/waitlist.js';
import passport from './plugins/passport.js';
import migrations from './plugins/migrations.js';
import { SqliteDateColumnsPlugin, connect as connectSqlite } from './utils/sqlite.js';
import { SqliteDateColumnsPlugin, connect as connectSqlite, jsonb } from './utils/sqlite.js';

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

class KeyValue {
#db;

/** @param {Kysely<import('./schema.js').Database>} db */
constructor(db) {
this.#db = db;
}

/** @param {string} key */
async get(key, db = this.#db) {
const row = await db.selectFrom('keyval')
.select('value')
.where('key', '=', key)
.executeTakeFirst();
return row != null ? JSON.parse(/** @type {string} */ (/** @type {unknown} */ (row.value))) : null;

Check failure on line 197 in src/Uwave.js

View workflow job for this annotation

GitHub Actions / Code style

This line has a length of 107. Maximum allowed is 100

Check failure on line 197 in src/Uwave.js

View workflow job for this annotation

GitHub Actions / Code style

This line has a length of 107. Maximum allowed is 100
}

/**
* @param {string} key
* @param {import('type-fest').JsonValue} value
*/
async set(key, value, db = this.#db) {
await db.insertInto('keyval')
.values({ key, value: jsonb(value) })
.onConflict((oc) => oc.column('key').doUpdateSet({ value: jsonb(value) }))
.execute();
}
}

this.keyv = new KeyValue(this.db);

boot.use(migrations);
boot.use(configStore);

Expand Down
5 changes: 5 additions & 0 deletions src/migrations/002-sql.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ async function up({ context: uw }) {
.addColumn('value', 'jsonb')
.execute();

await db.schema.createTable('keyval')
.addColumn('key', 'text', (col) => col.primaryKey())
.addColumn('value', 'jsonb')
.execute();

await db.schema.createTable('media')
.addColumn('id', 'uuid', (col) => col.primaryKey())
.addColumn('source_type', 'text', (col) => col.notNull())
Expand Down
8 changes: 7 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Kysely as KyselyBase, Generated } from 'kysely';
import type { JsonObject, Tagged } from 'type-fest'; // eslint-disable-line n/no-missing-import, n/no-unpublished-import
import type { JsonObject, JsonValue, Tagged } from 'type-fest'; // eslint-disable-line n/no-missing-import, n/no-unpublished-import

export type UserID = Tagged<string, 'UserID'>;
export type MediaID = Tagged<string, 'MediaID'>;
Expand Down Expand Up @@ -134,12 +134,18 @@ export interface ConfigurationTable {
value: JsonObject | null,
}

export interface KeyvalTable {
key: string,
value: JsonValue,
}

export interface MigrationTable {
name: string,
}

export interface Database {
configuration: ConfigurationTable,
keyval: KeyvalTable,
migrations: MigrationTable,
media: MediaTable,
users: UserTable,
Expand Down

0 comments on commit 3742dbf

Please sign in to comment.