From f7a054956b8739a3bc305a49faee929ea0da1e15 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 17 Jun 2024 17:33:25 -0400 Subject: [PATCH] fix: add app_uid param to kv interface --- packages/backend/src/drivers/DBKVStore.js | 41 ++++++++++++++----- .../src/services/drivers/interfaces.js | 18 ++++++-- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/packages/backend/src/drivers/DBKVStore.js b/packages/backend/src/drivers/DBKVStore.js index d86928ec..2131e46a 100644 --- a/packages/backend/src/drivers/DBKVStore.js +++ b/packages/backend/src/drivers/DBKVStore.js @@ -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( diff --git a/packages/backend/src/services/drivers/interfaces.js b/packages/backend/src/services/drivers/interfaces.js index d60c64b1..10a95825 100644 --- a/packages/backend/src/services/drivers/interfaces.js +++ b/packages/backend/src/services/drivers/interfaces.js @@ -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 }, }, } */