Provides simple, lightweight persistence for tiny apps.
Ideal for toy apps that don't quite justify a database server.
- Creates a global object which can be used to store small amounts of data.
- Persists the data to
./<NAMESPACE>.json
- Optionally backs up the data via FTP on a predefined interval (default: 20 seconds if changed)
npm i -s sync-via-ftp
const syncViaFtp = require('sync-via-ftp');
// Creates a global postData object that syncs to postData.json
// & to a remote FTP server
syncViaFtp('postData');
// You now have a global object in the `postData` namespace which you can do anything with.
// It will persist data on a fixed interval, to the local disk, and optionally to and from FTP.
// Just don't re-declare the type (it needs to remain an object)
// Manipulate the postData object to add a posts array
if (!postData.posts) postData.posts = [];
// Add a new post to the postData object
postData.posts.push({
name: "Post title",
date: (+ new Date()),
body: 'Hello world'
});
// Get posts
postData.posts.forEach(post => {
console.log(post.name, post.body);
});
If you've enabled a linter you may appreciate the optional assignment to the value you will be reading and writing to/from. This may help with code readability as well.
const syncViaFtp = require('sync-via-ftp');
let postData = syncViaFtp('postData'); // postData will be assigned {} immediately after invocation, and then updated to match the synced values, when available
// Alternatively
global.postData = syncViaFtp('postData'); // Lets other collaborators know this is assigned to the global namespace
const syncViaFtp = require('sync-via-ftp');
global.postData = syncViaFtp('postData', { interval: 10 });
You can specify custom paths (local or remote) with the configuration options localPath
and remotePath
.
Both should end in a slash, and either be a relative directory from your app, or an absolute directory.
const syncViaFtp = require('sync-via-ftp');
global.postData = syncViaFtp('postData', { interval: 10, localPath: './storage/', remotePath: 'storage/' });
This will be executed each time data is read to memory (once when reading from a local file, and again if updated from a remote FTP server)
const syncViaFtp = require('sync-via-ftp');
global.postData = syncViaFtp('postData', { interval: 10 }, namespace => {
console.log('Updated global.${ namespace } from ./${ namespace }.json')
});
// -> Updated global.postData from ./postData.json
Here is an empty .env
template for enabling FTP sync.
FTP_HOST=''
FTP_USER=''
FTP_PASS=''
You can use sync-via-ftp in conjunction with LowDb (https://github.com/typicode/lowdb) for a simple, persistent database.
Useful for tiny apps.
const FileSync = require('lowdb/adapters/FileSync');
const syncViaFtp = require('sync-via-ftp');
const low = require('lowdb');
const adapter = new FileSync('dbStore.json');
let db = low(adapter);
syncViaFtp('dbStore', { interval: 10 }, namespace => {
db = low(adapter); // Re-initialized lowDb with updated data
});
// dbStore will be a side-effect of calling syncViaFtp -- which you should not access directly.
db.defaults({ posts: [] })
.write();
const result = db.get('posts')
.push({ title: process.argv[2] })
.write();
console.log(result);
- Upon invoking
global.postData = syncViaFtp('postData');
, postData will immediately be assigned with an empty object (or the current state ofglobal[namespace]
) syncViaFtp
will attempt to read./postData.json
and assign the contents toglobal.postData
.- If a callback has been passed to
syncViaFtp
as its third parameter, it will be executed. - If
FTP_HOST, FTP_USER, & FTP_PASS
are present inprocess.env
,syncViaFtp()
will connect to the FTP server and attempt to retrievepostData.json
. postData.json
will be downloaded to overwrite./postData.json
.syncViaFtp
will attempt to read./postData.json
and assign the contents toglobal.postData
.- If a callback has been passed to
syncViaFtp
as its third parameter, it will be executed. - Every 20 seconds (or an interval declared in
config
), the contents ofglobal.postData
will be written to disk as./postData.json
. - If
FTP_HOST, FTP_USER, & FTP_PASS
are present,syncViaFtp()
will connect to the FTP server and attempt to write the contents ofglobal.postData
to the remote server.