The Complete Guide to Node Persist for Effortless Node.js Data Storage

Introduction to Node Persist

Node Persist is a powerful, zero-dependency, JavaScript-based key-value storage system designed specifically for Node.js developers. This lightweight library seamlessly integrates with your projects, providing both synchronous and asynchronous data storage capabilities. Whether you’re developing a small application or a complex system, Node Persist ensures your data is persistently stored and easily retrievable.

Getting Started

First, you’ll need to install Node Persist through npm:

npm install node-persist

Initial Configuration

Before using Node Persist, you need to initialize and configure your storage. Here’s how you can do it:

const storage = require('node-persist');
async function initStorage() {
  await storage.init({
    dir: './myData',
    stringify: JSON.stringify,
    parse: JSON.parse,
    encoding: 'utf8',
    logging: false,
    continuous: true,
    interval: false,
    ttl: false
  });
} initStorage();

Basic API Methods

Set Item

Store a key-value pair using the setItem method:

await storage.setItem('name', 'John Doe');

Get Item

Retrieve a value using the getItem method:

const name = await storage.getItem('name'); console.log(name); // Output: John Doe

Remove Item

Remove a stored value using the removeItem method:

await storage.removeItem('name');

Clear Storage

Clear all stored values with the clear method:

await storage.clear();

Keys

Get a list of all stored keys using the keys method:

const keys = await storage.keys(); console.log(keys); // Output: ['name']

TTL (Time to Live)

Set a time-to-live for stored values:

await storage.setItem('session', 'abc123', { ttl: 60000 }); // 1 minute TTL

Advanced Usage

Synchronous API

You can also use synchronous methods if needed:

storage.initSync(); storage.setItemSync('name', 'John Doe'); const name = storage.getItemSync('name'); console.log(name); // Output: John Doe

Example Application

Let’s create a simple application that demonstrates the use of Node Persist.

const storage = require('node-persist');
async function main() {
  await storage.init();

  // Set some data
  await storage.setItem('username', 'john_doe');
  await storage.setItem('email', 'john.doe@example.com');

  // Retrieve data
  const username = await storage.getItem('username');
  console.log('Username:', username); // Output: 'john_doe'

  const email = await storage.getItem('email');
  console.log('Email:', email); // Output: 'john.doe@example.com'

  // Use TTL
  await storage.setItem('session', 'abc123', { ttl: 3000 }); // 3 seconds TTL

  setTimeout(async () => {
    const session = await storage.getItem('session');
    console.log('Session:', session); // Output: null (expired)
  }, 4000);
} main();

With Node Persist, managing persisted data within your Node.js applications is simple and efficient. Whether you’re creating a small script or a full-fledged app, Node Persist has you covered with its versatile API and ease of use.

Hash: d32d22ae2388e157c428e50afae2da442bf564ae16c459377824132d3ddf5165

Leave a Reply

Your email address will not be published. Required fields are marked *