Containerized util to set Redis hashes from JS/JSON file
Note that for properties that are not strings or numbers, we apply JSON.stringify
The implementation is essentially as follows:
const content = await getStdin();
const object = JSON.parse(content);
await multiExecAsync(redis, multi => {
Object.keys(object).forEach(key => {
const value = object[key];
if (typeof value === 'object') {
multi.hset(config.key, key, JSON.stringify(value));
} else {
multi.hset(config.key, key, value);
}
});
});It's designed to be containerized:
echo '{"url": "https://news.ycombinator.com"}' |
docker run -i --network=host -e redisHost=127.0.0.1 \
-e key=myconfig evanxsummers/config-hmset- the image of the application container is
evanxsummers/config-hmset(DockerHub) - JSON content is piped as standard input into
docker run -iof this app image - the
redisHostand hasheskeyare specified using-e(environment variables for the app) - the app will
HMSETa Redishasheskey from the JSON input content
Note that since this is a container, usually redisHost it will not be localhost unless bridged e.g. via --network=host.
We can inspect the hashes key to verify that its fields were set as expected:
$ redis-cli hgetall myconfig
1) "url"
2) "https://news.ycombinator.com"The Docker image can be built as follows:
docker build -t config-hmset https://github.com/evanx/config-hmset.git
From the Dockerfile
FROM mhart/alpine-node
ADD package.json .
RUN npm install
ADD components components
ADD src src
CMD ["node", "--harmony", "src/index.js"]
Or use evanxsummers/config-hmset i.e. prebuilt on DockerHub.
You can check the version using npm as follows
$ docker run evanxsummers/config-hmset npm version | head -1
{ 'config-hmset': '0.2.0',or
$ docker run evanxsummers/config-hmset cat package.json | grep version
"version": "0.2.0",