Skip to content

Commit

Permalink
feat: JSON support for kv driver
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jul 8, 2024
1 parent 1f659f7 commit 3ed7916
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/backend/src/drivers/DBKVStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class DBKVStore extends Driver {
`SELECT * FROM kv WHERE user_id=? AND (app IS NULL OR app = 'global') AND kkey_hash=? LIMIT 1`,
[ user.id, key_hash ]
);

if ( kv[0] ) kv[0].value = db.case({
mysql: () => kv[0].value,
otherwise: () => JSON.parse(kv[0].value ?? 'null'),
})();

return kv[0]?.value ?? null;
},
Expand All @@ -74,10 +79,11 @@ class DBKVStore extends Driver {
}

// Validate the value
value = value === undefined ? null : String(value);
value = value === undefined ? null : value;
if (
value !== null &&
Buffer.byteLength(value, 'utf8') > config.kv_max_value_size
Buffer.byteLength(JSON.stringify(value), 'utf8') >
config.kv_max_value_size
) {
throw new Error(`value is too large. Max size is ${config.kv_max_value_size}.`);
}
Expand All @@ -102,7 +108,8 @@ class DBKVStore extends Driver {
sqlite: 'ON CONFLICT(user_id, app, kkey_hash) DO UPDATE SET value = excluded.value',
}),
[
user.id, app?.uid ?? 'global', key_hash, key, value,
user.id, app?.uid ?? 'global', key_hash, key,
JSON.stringify(value),
...db.case({ mysql: [value], otherwise: [] }),
]
);
Expand Down Expand Up @@ -164,7 +171,10 @@ class DBKVStore extends Driver {

rows = rows.map(row => ({
key: row.kkey,
value: row.value,
value: db.case({
mysql: () => row.value,
otherwise: () => JSON.parse(row.value ?? 'null')
})(),
}));

as = as || 'entries';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
this.db = new Database(this.config.path);

// Database upgrade logic
const TARGET_VERSION = 20;
const TARGET_VERSION = 21;

if ( do_setup ) {
this.log.noticeme(`SETUP: creating database at ${this.config.path}`);
Expand All @@ -69,6 +69,7 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
'0020_dev-center.sql',
'0021_app-owner-id.sql',
'0022_dev-center-max.sql',
'0023_fix-kv.sql',
].map(p => path_.join(__dirname, 'sqlite_setup', p));
const fs = require('fs');
for ( const filename of sql_files ) {
Expand Down Expand Up @@ -165,6 +166,10 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
upgrade_files.push('0022_dev-center-max.sql');
}

if ( user_version <= 20 ) {
upgrade_files.push('0023_fix-kv.sql');
}

if ( upgrade_files.length > 0 ) {
this.log.noticeme(`Database out of date: ${this.config.path}`);
this.log.noticeme(`UPGRADING DATABASE: ${user_version} -> ${TARGET_VERSION}`);
Expand Down
31 changes: 31 additions & 0 deletions src/backend/src/services/database/sqlite_setup/0023_fix-kv.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE TABLE `new_kv` (
`id` INTEGER PRIMARY KEY,
`app` char(40) DEFAULT NULL,
`user_id` int(10) NOT NULL,
`kkey_hash` bigint(20) NOT NULL,
`kkey` text NOT NULL,
`value` JSON,
`migrated` tinyint(1) DEFAULT '0',
UNIQUE (user_id, app, kkey_hash)
);

INSERT INTO `new_kv`
(
`app`,
`user_id`,
`kkey_hash`,
`kkey`,
`value`
)
SELECT
`app`,
`user_id`,
`kkey_hash`,
`kkey`,
json_quote(value)
FROM `kv`;

DROP TABLE `kv`;

ALTER TABLE `new_kv`
RENAME TO `kv`;

0 comments on commit 3ed7916

Please sign in to comment.