Giter Site home page Giter Site logo

sync-via-ftp's Introduction

sync-via-ftp

Provides simple, lightweight persistence for tiny apps.

Ideal for toy apps that don't quite justify a database server.

What it does

  1. Creates a global object which can be used to store small amounts of data.
  2. Persists the data to ./<NAMESPACE>.json
  3. Optionally backs up the data via FTP on a predefined interval (default: 20 seconds if changed)

Installation

npm i -s sync-via-ftp

Usage

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);
});

Optional assignment

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

Passing a custom configuration with a custom persistence interval

const syncViaFtp = require('sync-via-ftp');

global.postData = syncViaFtp('postData', { interval: 10 });

Using custom paths

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/' });

Use a custom callback

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

Enabling FTP sync via environment variables

Here is an empty .env template for enabling FTP sync.

FTP_HOST=''
FTP_USER=''
FTP_PASS=''

Usage with LowDb

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);

Module Lifecycle

  1. Upon invoking global.postData = syncViaFtp('postData');, postData will immediately be assigned with an empty object (or the current state of global[namespace])
  2. syncViaFtp will attempt to read ./postData.json and assign the contents to global.postData.
  3. If a callback has been passed to syncViaFtp as its third parameter, it will be executed.
  4. If FTP_HOST, FTP_USER, & FTP_PASS are present in process.env, syncViaFtp() will connect to the FTP server and attempt to retrieve postData.json.
  5. postData.json will be downloaded to overwrite ./postData.json.
  6. syncViaFtp will attempt to read ./postData.json and assign the contents to global.postData.
  7. If a callback has been passed to syncViaFtp as its third parameter, it will be executed.
  8. Every 20 seconds (or an interval declared in config), the contents of global.postData will be written to disk as ./postData.json.
  9. If FTP_HOST, FTP_USER, & FTP_PASS are present, syncViaFtp() will connect to the FTP server and attempt to write the contents of global.postData to the remote server.

sync-via-ftp's People

Contributors

kidgodzilla avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.