fix: add app_uid param to kv interface

This commit is contained in:
KernelDeimos 2024-06-17 17:33:25 -04:00
parent 21c537fe22
commit f7a054956b
2 changed files with 46 additions and 13 deletions

View File

@ -20,6 +20,7 @@ const config = require("../config");
const APIError = require("../api/APIError");
const { DB_READ, DB_WRITE } = require("../services/database/consts");
const { Driver } = require("../definitions/Driver");
const { get_app } = require("../helpers");
class DBKVStore extends Driver {
static ID = 'public-db-kvstore';
@ -29,7 +30,7 @@ class DBKVStore extends Driver {
murmurhash: require('murmurhash'),
}
static METHODS = {
get: async function ({ key }) {
get: async function ({ app_uid, key }) {
console.log('THIS WAS CALLED', { key });
const actor = this.context.get('actor');
@ -39,11 +40,15 @@ class DBKVStore extends Driver {
// that are scoped to the app, so this should eventually be
// changed to get the app ID from the same interface that would
// be used to obtain per-app user-specified implementation params.
const app = actor.type?.app ?? undefined;
let app = actor.type?.app ?? undefined;
const user = actor.type?.user ?? undefined;
if ( ! user ) throw new Error('User not found');
if ( ! app && app_uid ) {
app = await get_app({ uid: app_uid });
}
const db = this.services.get('database').get(DB_READ, 'kvstore');
const key_hash = this.modules.murmurhash.v3(key);
const kv = app ? await db.read(
@ -56,7 +61,7 @@ class DBKVStore extends Driver {
return kv[0]?.value ?? null;
},
set: async function ({ key, value }) {
set: async function ({ app_uid, key, value }) {
console.log('THIS WAS CALLED (SET)', { key, value })
const actor = this.context.get('actor');
@ -77,9 +82,13 @@ class DBKVStore extends Driver {
throw new Error(`value is too large. Max size is ${config.kv_max_value_size}.`);
}
const app = actor.type?.app ?? undefined;
let app = actor.type?.app ?? undefined;
const user = actor.type?.user ?? undefined;
if ( ! user ) throw new Error('User not found');
if ( ! app && app_uid ) {
app = await get_app({ uid: app_uid });
}
const db = this.services.get('database').get(DB_WRITE, 'kvstore');
const key_hash = this.modules.murmurhash.v3(key);
@ -111,13 +120,17 @@ class DBKVStore extends Driver {
return true;
},
del: async function ({ key }) {
del: async function ({ app_uid, key }) {
const actor = this.context.get('actor');
const app = actor.type?.app ?? undefined;
let app = actor.type?.app ?? undefined;
const user = actor.type?.user ?? undefined;
if ( ! user ) throw new Error('User not found');
if ( ! app && app_uid ) {
app = await get_app({ uid: app_uid });
}
const db = this.services.get('database').get(DB_WRITE, 'kvstore');
const key_hash = this.modules.murmurhash.v3(key);
@ -128,12 +141,16 @@ class DBKVStore extends Driver {
return true;
},
list: async function ({ as }) {
list: async function ({ app_uid, as }) {
const actor = this.context.get('actor');
const app = actor.type?.app ?? undefined;
let app = actor.type?.app ?? undefined;
const user = actor.type?.user ?? undefined;
if ( ! app && app_uid ) {
app = await get_app({ uid: app_uid });
}
if ( ! user ) throw new Error('User not found');
const db = this.services.get('database').get(DB_READ, 'kvstore');
@ -164,13 +181,17 @@ class DBKVStore extends Driver {
return rows;
},
flush: async function () {
flush: async function ({ app_uid }) {
const actor = this.context.get('actor');
const app = actor.type?.app ?? undefined;
let app = actor.type?.app ?? undefined;
const user = actor.type?.user ?? undefined;
if ( ! user ) throw new Error('User not found');
if ( ! app && app_uid ) {
app = await get_app({ uid: app_uid });
}
const db = this.services.get('database').get(DB_WRITE, 'kvstore');
await db.write(

View File

@ -106,20 +106,27 @@ module.exports = {
methods: {
get: {
description: 'Get a value by key.',
parameters: { key: { type: 'string', required: true } },
parameters: {
key: { type: 'string', required: true },
app_uid: { type: 'string', optional: true },
},
result: { type: 'json' },
},
set: {
description: 'Set a value by key.',
parameters: {
key: { type: 'string', required: true, },
value: { type: 'json' }
value: { type: 'json' },
app_uid: { type: 'string', optional: true },
},
result: { type: 'void' },
},
del: {
description: 'Delete a value by key.',
parameters: { key: { type: 'string' } },
parameters: {
key: { type: 'string' },
app_uid: { type: 'string', optional: true },
},
result: { type: 'void' },
},
list: {
@ -128,6 +135,7 @@ module.exports = {
as: {
type: 'string',
},
app_uid: { type: 'string', optional: true },
},
result: { type: 'array' },
},
@ -141,6 +149,7 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
amount: { type: 'number' },
app_uid: { type: 'string', optional: true },
},
result: { type: 'number' },
},
@ -149,6 +158,7 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
amount: { type: 'number' },
app_uid: { type: 'string', optional: true },
},
result: { type: 'number' },
},
@ -158,6 +168,7 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
timestamp: { type: 'number', required: true, },
app_uid: { type: 'string', optional: true },
},
},
expire: {
@ -165,6 +176,7 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
ttl: { type: 'number', required: true, },
app_uid: { type: 'string', optional: true },
},
}
*/