npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nosqlax

v0.0.8

Published

NoSQLax is a lightweight JavaScript library designed to simplify and streamline CRUD operations with CouchDB. NoSQLax provides a unified and intuitive repository pattern to handle your data effortlessly.

Downloads

482

Readme

NoSQLax 💤 - A Relaxed Repository for CouchDB

NoSQLax is a lightweight JavaScript library designed to simplify and streamline CRUD operations with CouchDB. Inspired by the laid-back Snorlax and CouchDB's "Relax" philosophy, NoSQLax provides a unified and intuitive repository pattern to handle your data effortlessly.

Key Features

  • Seamless CRUD Operations: Easily manage documents in CouchDB with consistent and easy-to-use methods.
  • Built-In JSON Schema Validation: Ensure data integrity with schema validation powered by AJV.
  • Extensible Architecture: Extend and customize repositories for more complex use cases.
  • Developer-Friendly: Focus on building your application with minimal boilerplate and maximum clarity.
  • Simplifies CouchDB Interactions: A clean and consistent API makes working with CouchDB as relaxed as its motto.

Installation

Getting started

Example:

// example.js
const { CouchRepository, BaseEntity } = require('nosqlax');

// Define a validation schema for your User documents using JSON Schema syntax
const validationSchema = {
  type: "object",
  properties: {
    name: { type: "string" },
    email: { type: "string"},
  },
  required: ["name", "email"], // Define required fields
  additionalProperties: false // Do not allow additional properties
};

const fields = [{ "name": null, "email": null }]
class UserEntity extends BaseEntity {

  name;
  email;

  constructor(data = {}) {
    super(data); // Call the BaseEntity constructor


    this.name = data.name || '';
    this.email = data.email || '';


  }

  // Static validation schema for UserEntity
  static schemaOrSchemaId = validationSchema

  // Static docType for UserEntity
  static docType = 'user'
}


class UserRepository extends CouchRepository {
  constructor(nanoConnection) {
    super(nanoConnection, {allErrors: true
    }, UserEntity); // Pass the connection and UserEntity to the base class
  }

}


class UserService {
  constructor(userRepository) {
    this.userRepository = userRepository;
  }

  async createUser(userData) {
    // Create a new user using the UserRepository
    return await this.userRepository.create(userData);
  }

  async findUserByName(name) {
    return await this.userRepository.findByName(name);
  }

  async findUserByEmailName(email, name) {
    return await this.userRepository.findByEmailName(email, name);
  }

  async getUserById(id) {
    // Fetch a user by ID
    return await this.userRepository.read(id);
  }

  async updateUser(id, userData) {
    // Update user information
    return await this.userRepository.update(id, userData);
  }

  async deleteUser(id) {
    // Delete a user by ID
    return await this.userRepository.delete(id);
  }
}

// example.js
const Nano = require('nano');

// Example: Create a CouchDB connection using Nano (or your custom connection)
const nano = Nano('http://localhost:5984');
const usersDb = nano.db.use('users'); // Use the 'users' database

const userRepository = new UserRepository(usersDb);
const userService = new UserService(userRepository); // Create an instance of UserService

(async () => {
  try {
    // 1. Create a new user
    const newUser = new UserEntity({
      name: 'Jane Doe',
      email: '[email protected]'
    });


    const createdUser = await userService.createUser(newUser);
    console.log('User created:', createdUser);

    // 2. Get the user by ID
    const fetchedUser = await userService.getUserById(createdUser.id);
    console.log('Fetched User:', fetchedUser);

    const fetchedUserByName = await userService.findUserByEmailName('[email protected]','Jane Doe')
    console.log('Fetched User by name:', fetchedUserByName);

    // 3. Update the user
    const updatedUser = await userService.updateUser(createdUser.id, {
      name: 'Jane Smith',
      email: '[email protected]',
    });
    console.log('Updated User:', updatedUser);

    // 4. Delete the user
    const deleteResponse = await userService.deleteUser(createdUser.id);
    console.log(deleteResponse);
  } catch (error) {
    console.error('Error:', error.message);
  }
})();