Skip to content

Instantly share code, notes, and snippets.

@samliu
Created June 9, 2017 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samliu/ab6c22e91205066281e57aa93408df05 to your computer and use it in GitHub Desktop.
Save samliu/ab6c22e91205066281e57aa93408df05 to your computer and use it in GitHub Desktop.
SQLite-based Key-value store for use on glitch.com node.js projects. Raw
// This is a module that uses SQLite as a flat-file database to implement a simple key-value store.
//
// Exported methods:
// get(key, callback) - Retrieve the value for a given key in the store.
// insert(key, value) - Add a key/value to the store. If the key already exists, update its value.
// getAll(callback) - Retrieve all values in the database
// resetdb() - Reset the database
//
// Since this uses SQLite, it's locking and will likely have scalability issues when many concurrent users are
// accessing the database. Sequelize allows you to replace sqlite with a more production-ready database such
// as MySQL or Postgres. For a simple chat bot under 5 concurrent users this is fine.
//
// Originally designed for use in glitch projects (glitch.com).
//
// Original Author: Sam Liu (sam@ambushnetworks.com)
// License: MIT or WTFPL -- whatever you like or works best for you.
'use strict';
var Sequelize = require('sequelize');
// Start an ORM for database access. Example code here: https://glitch.com/edit/#!/sqlite3-db?path=server.js
// Setup a new database using database credentials set in `.env`.
var sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, {
host: '0.0.0.0',
dialect: 'sqlite',
pool: {
max: 5,
min: 0,
idle: 10000
},
// Security note: the database is saved to the file `database.sqlite` on the local filesystem. It's deliberately placed in the `.data` directory
// which doesn't get copied if someone remixes the project.
storage: '.data/database.sqlite'
});
// Authenticate with the database and define a new table to hold our data.
var KVStore;
sequelize.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
KVStore = sequelize.define('kvstore', {
k: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
v: {
type: Sequelize.STRING,
allowNull: false
}
});
})
.catch(function (err) {
console.log('Unable to connect to the database: ', err);
});
module.exports = {};
// Populate data store with default data (a single value containing )
module.exports.resetdb = function() {
KVStore.sync({force: true}) // using 'force' it drops the table kvstore if it already exists, and creates a new one
.then(function(){});
}
// Gets all key-value pairs in the database and returns them in a list via callback.
module.exports.getAll = function(callback) {
KVStore.findAll().then(function(kvpairs) { // find all entries in the kvstore table
var dbContents=[];
kvpairs.forEach(function(kvpair) {
dbContents.push([kvpair.k,kvpair.v]); // adds their info to the dbContents value
});
callback(dbContents);
});
}
// Retrieves a single value given a key and returns it via callback.
module.exports.get = function(key, callback) {
KVStore.findAll({
where: {
k: key
}
}).then(function(results){
var queryResult;
results.forEach(function(result){
if (result.dataValues.v) {
queryResult = result.dataValues.v;
}
});
if (queryResult) {
callback(queryResult);
} else {
// If no response was sent (this query failed) just send a 404 not found.
callback(false);
}
});
}
// Returns a promise to either update or insert the key to a new value.
module.exports.insert = function(key, value) {
return KVStore.upsert({ k: key, v: value});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment