I want to use an in memory no sql db in a node (express) application within the Quix platform.
The no sql db uses a file as it’s backing store.
I’d like to push the file to local storage.
Can I access local storage from nodeJS or should I devise some mechanism to publish the db updates to a topic and then somehow read them from the topic on start?
It looks possible to run a python script from within node/express
Using the code below when i access the root (/) it prints Wrote to storage the first time and DB Exists 12.51 the second time.
So maybe I’ll try integrating this into my solution.
Express code (main.js)
var express = require('express');
const {spawn} = require('child_process');
const port = 80;
var app = express();
module.exports = app;
app.get('/', (req, res) => {
var dataToSend;
// spawn new child process to call the python script
const python = spawn('python', ['quix_storage.py']);
// collect data from script
python.stdout.on('data', function (data) {
console.log('Pipe data from python script ...');
dataToSend = data.toString();
});
// in close event we are sure that stream from child process is closed
python.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
//send data to browser
res.send(dataToSend)
});
})
app.listen(port, () => console.log(`Server running at http://127.0.0.1:${port}`))
quix_storage.py
from quixstreams import LocalFileStorage
storage = LocalFileStorage()
if storage.contains_key("db"):
print("DB Exists")
print(storage.get("db"))
else:
print("Wrote to storage")
storage.set("db", 12.51)
dockerfile
FROM node
RUN apt-get update || : && apt-get install python -y
RUN apt-get install python3-pip -y
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir quixstreams==0.5.3
COPY --from=git /project /app
WORKDIR /app
RUN npm install
ENTRYPOINT node main.js
LocalFileStorage is a thin wrapper on top of reading and writing from and to disk. There are some nuances (such as MD5 hash check, python pickle for complex objects) that make it a bit more than straight reading the disk from file however.
Depending on your use case, you can do what you wrote above - assuming low load -, but I would only use it if you HAD TO, such as trying to expose a specific state via a web UI.
You might want to consider using a python web API rather than javascript based, so you can directly use the quixstreams library rather than relying on a inter-process communication via reading standard output.