Skip to content

Commit

Permalink
fix: add app_uid param to kv interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jun 17, 2024
1 parent 21c537f commit f7a0549
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
41 changes: 31 additions & 10 deletions packages/backend/src/drivers/DBKVStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

Expand All @@ -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(
Expand All @@ -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');

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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');
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 15 additions & 3 deletions packages/backend/src/services/drivers/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -128,6 +135,7 @@ module.exports = {
as: {
type: 'string',
},
app_uid: { type: 'string', optional: true },
},
result: { type: 'array' },
},
Expand All @@ -141,6 +149,7 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
amount: { type: 'number' },
app_uid: { type: 'string', optional: true },
},
result: { type: 'number' },
},
Expand All @@ -149,6 +158,7 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
amount: { type: 'number' },
app_uid: { type: 'string', optional: true },
},
result: { type: 'number' },
},
Expand All @@ -158,13 +168,15 @@ module.exports = {
parameters: {
key: { type: 'string', required: true, },
timestamp: { type: 'number', required: true, },
app_uid: { type: 'string', optional: true },
},
},
expire: {
description: 'Set a key\'s time-to-live.',
parameters: {
key: { type: 'string', required: true, },
ttl: { type: 'number', required: true, },
app_uid: { type: 'string', optional: true },
},
}
*/
Expand Down

0 comments on commit f7a0549

Please sign in to comment.