From 52cbc5ac4e0b2a4f81d70050ba2e83ad8541db0d Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:21:39 +0000 Subject: saves stuff in data.json --- .gitignore | 1 + package-lock.json | 6 ++++++ package.json | 1 + src/app.js | 3 ++- src/db.js | 30 ++++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3c3629e..9726dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +data.json \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 564139f..8dd377b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "dependencies": { "body-parser": "^1.20.2", "express": "^4.18.3", + "fs": "^0.0.1-security", "twilio": "^4.23.0" }, "devDependencies": { @@ -543,6 +544,11 @@ "node": ">= 0.6" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/package.json b/package.json index dbbdaae..5df1265 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "dependencies": { "body-parser": "^1.20.2", "express": "^4.18.3", + "fs": "^0.0.1-security", "twilio": "^4.23.0" }, "devDependencies": { diff --git a/src/app.js b/src/app.js index 9b47728..a374995 100644 --- a/src/app.js +++ b/src/app.js @@ -1,9 +1,10 @@ const bodyParser = require('body-parser'); const express = require('express'); -const { isSubscribed, getSubscriptions, getSubscriptionFromNumber, subscribe } = require('./db'); +const { isSubscribed, getSubscriptions, getSubscriptionFromNumber, subscribe, load: loadData } = require('./db'); const { sendTextMessage } = require('./sendTextMessage'); const { MessagingResponse } = require('twilio').twiml; +loadData(); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); diff --git a/src/db.js b/src/db.js index ba37a30..c277f7c 100644 --- a/src/db.js +++ b/src/db.js @@ -1,6 +1,8 @@ // Ideally, we should write to a file and read it at app start in case of crashes. // An actual SQL database would probably be overkill. +const { readFileSync, writeFile } = require("fs"); + /** @type Array<{ name: string, number: string }> */ const subscriptions = []; @@ -11,6 +13,7 @@ const subscriptions = []; function subscribe(number, name) { const subscription = { name, number }; subscriptions.push(subscription); + void save(); return subscription; } @@ -37,9 +40,36 @@ function getSubscriptionFromNumber(number) { return subscriptions.find(sub => sub.number === number); } +async function save() { + try { + await writeFile('../data.json', JSON.stringify({ subscriptions })); + } catch (err) { + if (typeof err === 'string') { + console.error(`Error writing data: ${err}`); + } else { + console.error(`Error writing data: ${JSON.stringify(err)}`); + } + } +} + +function load() { + try { + const data = JSON.parse(readFileSync('../data.json')); + while (subscriptions.pop()) {} + subscriptions.push(...data['subscriptions']); + } catch (err){ + if (typeof err === 'string') { + console.error(`Error reading data: ${err}`); + } else { + console.error(`Error reading data: ${JSON.stringify(err)}`); + } + } +} + module.exports = { subscribe, isSubscribed, getSubscriptions, getSubscriptionFromNumber, + load, }; \ No newline at end of file -- cgit v1.2.3