Userito

5.0.0 · active · verified Wed Apr 22

Userito is a Node.js library designed for managing user data, offering persistence options via either a local JSON file or a MongoDB database. The current stable version is 5.0.0, released in 2022, which introduced support for Node.js 16 and dropped compatibility with older Node.js versions. While there isn't a strict release cadence, new major versions typically align with Node.js End-of-Life (EOL) cycles, dropping support for older runtimes, and incorporating dependency updates like Mongoose. Its primary differentiator is the flexibility to switch between simple file-based storage, suitable for development or small applications, and a robust MongoDB backend for more scalable solutions, all through a unified, function-based API. It provides core CRUD operations for user management.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Userito with file-based storage and perform basic CRUD operations (create, get all, get one, remove) for users.

import useritoFactory from 'userito';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function main() {
  const useritoFile = useritoFactory({
    type: 'file',
    path: path.join(__dirname, 'users.json') // Use a specific path for the quickstart
  });

  // Create a new user
  await new Promise(resolve => {
    useritoFile.create({ username: 'testuser', password: 'securepassword' }, (error, msg) => {
      if (error) console.error('Error creating user:', error);
      else console.log('User created:', msg);
      resolve();
    });
  });

  // Get all users
  await new Promise(resolve => {
    useritoFile.all((error, users) => {
      if (error) console.error('Error getting all users:', error);
      else console.log('All users:', users);
      resolve();
    });
  });

  // Get a specific user
  await new Promise(resolve => {
    useritoFile.get('testuser', (error, user) => {
      if (error) console.error('Error getting user:', error);
      else console.log('Specific user:', user);
      resolve();
    });
  });

  // Remove the user
  await new Promise(resolve => {
    useritoFile.remove('testuser', (error, info) => {
      if (error) console.error('Error removing user:', error);
      else console.log('User removed:', info);
      resolve();
    });
  });
}

main().catch(console.error);

view raw JSON →