Skip to content

Commit

Permalink
feat: add context store plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed May 23, 2023
1 parent e035608 commit 29db63f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { prefixStorage, Storage } from "unstorage";

var appStorage: Storage;

var ContextStore = function (storageSettings) {
appStorage = prefixStorage(storageSettings.storage, storageSettings.appName);
};

ContextStore.prototype.open = () => {};

ContextStore.prototype.close = () => {};

ContextStore.prototype.get = (scope: string, key: string | string[]) => {
if (Array.isArray(key)) {
return Promise.all(key.map((el) => appStorage.getItem(scope + ":" + el)));
}

return appStorage.getItem(scope + ":" + key);
};

ContextStore.prototype.set = (
scope: string,
key: string | string[],
value: string | string[]
) => {
if (Array.isArray(key)) {
const values = [...value];
return Promise.all(
key.map((el, index) =>
appStorage.setItem(scope + ":" + el, values[index] || null)
)
);
}

return appStorage.setItem(scope + ":" + key, value);
};

ContextStore.prototype.keys = (scope: string) =>
appStorage
.getKeys(scope)
.then((result) => result.map((el) => el.split(":")[1]));

ContextStore.prototype.delete = (scope: string) =>
appStorage
.getKeys(scope)
.then((result) =>
Promise.all(result.map((key) => appStorage.removeItem(key)))
);

ContextStore.prototype.clean = (activeNodes) => {};

export function contextModule(config) {
return new ContextStore(config);
}
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fileURLToPath } from "url";
import { storageModule } from "./storage.js";
import { createStorage } from "unstorage";
import mongodbDriver from "unstorage/drivers/mongodb";
import { contextModule } from "./context.js";

dotenv.config();

Expand All @@ -31,12 +32,15 @@ const settings: LocalSettings = {
uiHost: "0.0.0.0",
uiPort: parseInt(process.env.PORT) || 8080,
credentialSecret: process.env.CREDENTIAL_SECRET || "secret",

//@ts-ignore
storageModule: storageModule,

storageSettings: {
storage: storage,
appName: process.env.APP_NAME || "default",
},

adminAuth: {
type: "credentials",
users: [
Expand All @@ -50,6 +54,17 @@ const settings: LocalSettings = {
},
],
},

contextStorage: {
unstorage: {
//@ts-ignore
module: contextModule,
config: {
storage: storage,
appName: process.env.APP_NAME || "default",
},
},
},
};

const app = express();
Expand Down

0 comments on commit 29db63f

Please sign in to comment.