Skip to content

Commit

Permalink
refactor: create a unified storage plugin (context + module)
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed May 23, 2023
1 parent 29db63f commit a9b9e1b
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 128 deletions.
50 changes: 26 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ import nodered from "node-red";
import { LocalSettings } from "@node-red/runtime";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { storageModule } from "./storage.js";
import { createStorage } from "unstorage";
import mongodbDriver from "unstorage/drivers/mongodb";
import { contextModule } from "./context.js";
import storage from "./storage/index.js";

dotenv.config();

const storage = createStorage({
//@ts-ignore
driver: mongodbDriver({
connectionString: process.env.MONGO_DB_URL,
databaseName: "nodeRed",
collectionName: "unstorage",
}),
const { contextStore, storageModule } = storage({
app: "app0",

storageOptions: {
//@ts-ignore
driver: mongodbDriver({
connectionString: process.env.MONGO_DB_URL,
databaseName: "nodeRed",
collectionName: "storage",
}),
},

contextOptions: {
//@ts-ignore
driver: mongodbDriver({
connectionString: process.env.MONGO_DB_URL,
databaseName: "nodeRed",
collectionName: "context",
}),
},
});

const cwd = dirname(fileURLToPath(import.meta.url));
Expand All @@ -36,9 +47,11 @@ const settings: LocalSettings = {
//@ts-ignore
storageModule: storageModule,

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

adminAuth: {
Expand All @@ -54,17 +67,6 @@ const settings: LocalSettings = {
},
],
},

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

const app = express();
Expand Down
98 changes: 0 additions & 98 deletions src/storage.ts

This file was deleted.

20 changes: 14 additions & 6 deletions src/context.ts → src/storage/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { prefixStorage, Storage } from "unstorage";
import { prefixStorage, Storage, createStorage } from "unstorage";
import type { CreateStorageOptions } from "unstorage";

var appStorage: Storage;

var ContextStore = function (storageSettings) {
appStorage = prefixStorage(storageSettings.storage, storageSettings.appName);
var ContextStore = function (args: {
app: string;
options: CreateStorageOptions;
}) {
let appStorage: Storage;

const storage = createStorage(args.options);

appStorage = prefixStorage(storage, args.app);
};

ContextStore.prototype.open = () => {};
Expand Down Expand Up @@ -49,6 +57,6 @@ ContextStore.prototype.delete = (scope: string) =>

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

export function contextModule(config) {
return new ContextStore(config);
}
export default (args: { app: string; options: CreateStorageOptions }) => {
return new ContextStore(args);
};
17 changes: 17 additions & 0 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { CreateStorageOptions } from "unstorage";
import contextStore from "./context.js";
import storageModule from "./module.js";

export default function (args: {
app: string;
storageOptions: CreateStorageOptions;
contextOptions: CreateStorageOptions;
}) {
const module = storageModule({ app: args.app, options: args.storageOptions });

return {
storageModule: module,
contextStore: () =>
contextStore({ app: args.app, options: args.contextOptions }),
};
}
99 changes: 99 additions & 0 deletions src/storage/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//@ts-nocheck
import { prefixStorage, Storage, createStorage } from "unstorage";
import type { CreateStorageOptions } from "unstorage";

var appStorage: Storage;
var libraryStorage: Storage;

export default (args: { app: string; options: CreateStorageOptions }) => {
return {
init: () => {
const storage = createStorage(args.options);

appStorage = prefixStorage(storage, args.app);

libraryStorage = prefixStorage(appStorage, "library");
},

getFlows: () => appStorage.getItem("flows").then((flows) => flows || []),

saveFlows: (flows) => appStorage.setItem("flows", flows),

getCredentials: () =>
appStorage.getItem("credentials").then((creds) => creds || {}),

saveCredentials: (credentials) =>
appStorage.setItem("credentials", credentials),

getSettings: () =>
appStorage.getItem("settings").then((settings) => settings || {}),

saveSettings: (settings) => appStorage.setItem("settings", settings),

getSessions: () =>
appStorage.getItem("sessions").then((sessions) => sessions || {}),

saveSessions: (sessions) => appStorage.setItem("sessions", sessions),

getLibraryEntry: async function (type, name) {
if (name == "") {
name = "/";
} else if (!name.startsWith("/")) {
name = "/" + name;
}

const library = await libraryStorage.getItem(name + ":" + type);

if (library) {
const body = JSON.parse(library.body);
return body;
}

let libraryKeys = await libraryStorage.getKeys(name);

if (!libraryKeys) {
return [];
}

libraryKeys = libraryKeys.filter((el) => el.endsWith(type));

var dirs = [];
var files = [];

for (var i = 0; i < libraryKeys.length; i++) {
const library = await libraryStorage.getItem(libraryKeys[i]);

var n = library.name;
n = n.replace(name, "");
if (n.indexOf("/") == -1) {
var f = library.meta;
f.fn = n;
files.push(f);
} else {
n = n.substr(0, n.indexOf("/"));
dirs.push(n);
}
}

dirs = dirs.concat(files);

return dirs;
},

saveLibraryEntry: function (type, name, meta, body) {
var p = name.split("/"); // strip multiple slash
p = p.filter(Boolean);
name = p.slice(0, p.length).join("/");
if (name != "" && !name.startsWith("/")) {
name = "/" + name;
}

return libraryStorage.setItem(name + ":" + type, {
name,
meta,
body,
type,
});
},
};
};

0 comments on commit a9b9e1b

Please sign in to comment.