Skip to content

Commit

Permalink
refactor: add credentials & flows & settings storage handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed May 17, 2023
1 parent f4b0de6 commit 7481ffb
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 35 deletions.
158 changes: 158 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"typescript": "^5.0.4"
},
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongodb": "^5.5.0",
"node-red": "^3.0.2",
"node-red-contrib-sendgrid": "^0.2.1",
"unstorage": "^1.6.0"
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as dotenv from "dotenv";
dotenv.config();

import express from "express";
import { createServer } from "http";
import nodered from "node-red";
Expand Down
31 changes: 27 additions & 4 deletions src/models/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { appStorage } from "../storage.js";

interface ICredentials {
appname: string;
credentials: string;
Expand All @@ -6,12 +8,33 @@ interface ICredentials {
export default {
findOne: (
where: { appname: string },
cb: (err: Error, credentials: ICredentials) => void
) => {},
cb: (err: Error | null, credentials?: ICredentials) => void
) => {
appStorage
.getItem("credentials")
.then((value) => {
const entity: ICredentials = {
appname: where.appname,
credentials: value && value.toString(),
};
cb(null, entity);
})
.catch(cb);
},

findOneAndUpdate: (
where: { appname: string },
data: { credentials: any },
cb: (err: Error, credentials: ICredentials) => {}
) => {},
cb: (err: Error | null, credentials?: ICredentials) => {}
) => {
const entity: ICredentials = {
appname: where.appname,
credentials: data.credentials,
};

appStorage
.setItem("credentials", entity)
.then(() => cb(null, entity))
.catch(cb);
},
};
31 changes: 27 additions & 4 deletions src/models/flows.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { appStorage } from "../storage.js";

interface IFlows {
appname: string;
flow: any;
Expand All @@ -6,12 +8,33 @@ interface IFlows {
export default {
findOne: (
where: { appname: string },
cb: (err: Error, flows: IFlows) => void
) => {},
cb: (err: Error | null, flows?: IFlows) => void
) => {
appStorage
.getItem("flows")
.then((value) => {
const entity: IFlows = {
appname: where.appname,
flow: value,
};
cb(null, entity);
})
.catch(cb);
},

findOneAndUpdate: (
where: { appname: string },
data: { flow: any },
cb: (err: Error, flows: IFlows) => {}
) => {},
cb: (err: Error | null, flows?: IFlows) => {}
) => {
const entity: IFlows = {
appname: where.appname,
flow: data.flow,
};

appStorage
.setItem("flows", entity)
.then(() => cb(null, entity))
.catch(cb);
},
};
32 changes: 28 additions & 4 deletions src/models/library.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { appStorage } from "../storage.js";

interface ILibrary {
appname: string;
type: string;
Expand All @@ -9,12 +11,34 @@ interface ILibrary {
export default {
findOne: (
where: { appname: string; type: string; name: string },
cb: (err: Error, library: ILibrary) => void
) => {},
cb: (err: Error | null, library?: ILibrary) => void
) => {
appStorage
.getItem("library")
.then((value) => {
const parsed = JSON.parse(value.toString()) as ILibrary;

cb(null, parsed);
})
.catch(cb);
},

findOneAndUpdate: (
where: { appname: string; name: string },
data: { name: string; meta: any; body: any; type: string },
cb: (err: Error, library: ILibrary) => {}
) => {},
cb: (err: Error | null, library?: ILibrary) => {}
) => {
const entity: ILibrary = {
appname: where.appname,
name: where.name,
body: data.body,
meta: data.meta,
type: data.type,
};

appStorage
.setItem("library", entity)
.then(() => cb(null, entity))
.catch(cb);
},
};
Loading

0 comments on commit 7481ffb

Please sign in to comment.