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

json-crud

v1.0.0

Published

A simple CRUD JSON database using either a JSON file or a folder of JSON files.

Downloads

5

Readme

JSON Crud v1.0.0

A simple CRUD JSON database using either a JSON file or a folder of JSON files.

var jsonDB = require('json-crud');

jsonDB

Generator function for creating a JSON DB. The database is equivalent to a a single table or collection. The generator returns a promise that will resolve to the JSON databaes if everything is ok.

Parameters

  • file [String] Path to file/folder to contain the JSON database
  • options [Object] Object containing the options for the database.
    • options.path [String] Path to file/folder to contain JSON database if not given as first argument
    • options.id [String] Field of data objects to be used as the key
    • options.cacheKeys [Boolean] Cache the keys of the objects in the database
    • options.cacheValues [Boolean] Cache the objects in the database

Returns Promise<JsonDBInstance> A promise that will resolve to a JsonDB instance if everything checks out

Typing

export = JsonDB;

declare function JsonDB(path?: string; options?: Options): Promise<JsonDBInstance>;

declare namespace JsonDB {

  export type Id = string | number;

  export type IdOrError = Id | Error;

  export type Results = { [key: Id]: any };

  export type Data = { [key: Id]: any } | any[];

  export type FieldFilter =
    /// Matches values that are equal to a specified value.
    { $eq: any }
    /// Matches values that are greater than a specified value.
    | { $gt: any }
    /// Matches values that are greater than or equal to a specified value.
    | { $gte: any }
    /// Matches values that are less than a specified value.
    | { $lt: any }
    /// Matches values that are less than or equal to a specified value.
    | { $lte: any }
    /// Matches all values that are not equal to a specified value.
    | { $ne: any }
    /// Matches any of the values specified in an array.
    | { $in: any[] }
    /// Matches none of the values specified in an array.
    | { $nin: any[] };

  export type Filter = Id | Id[] | {
    /// Field value comparison
    [field: string]: FieldFilter | any,
    /// Logical AND
    $and?: Filter[],
    /// Logical OR
    $or?: Filter[],
    /// Logical NOT
    $not?: Filter,
  };

  export interface JsonDBInstance {
    /**
     * Inserts data into the JSON database.
     *
     * @param data Either an object of key-value pairs, an array
     *   containing key/value pairs ([key, value,...]) or, if the key field has
     *   been specified, an array of object values each with the key field set
     *
     * @returns {Promise} A promise that will resolve with an array containing
     *   keys or errors in creating the data of the inserted data.
     */
    create: (data: Data) => Promise<IdOrError[]>;
    /**
     * Retrieve values from the database
     *
     * @param [filter] Filter to use to match the values to return. If not
     *   given, all values will be returned
     *
     * @returns A promise that will resolve to an object containing the
     *   key/values of the values matched
     */
    read: (filter?: Filter) => Promise<Results>;
    /**
     * Updates data in the JSON database. Data can either be given as
     * key-value parameter pairs, OR if the key field has been specified Object
     * values. New values will be merge into any existing values.
     *
     * @param data Either:
     *   - an array of key-value pairs,
     *   - an array of object value(s) containing the key value (if a key has
     *     been specified)
     * @param filter true if the existing items should be replaced
     *   instead of or false to merge existing values (if values are mergeable)
     *
     * @returns {Promise} A promise that will resolve with an array containing
     *   keys of the updated data.
     */
    update: (data: Data, filter: Filter | boolean) => Promise<IdOrError[]>;
    /**
     * Updates data in the JSON database. Data can either be given as
     * key-value parameter pairs, OR if the key field has been specified Object
     * values. New values will be merge into any existing values.
     *
     * @param data Property values to update of any objects that
     *   match the given filter
     * @param [filter] A filter to select the items that
     *   should be updated
     *
     * @returns {Promise} A promise that will resolve with an array containing
     *   keys of the updated data.
     */
    update: (data: { [ property: string]: any }, filter: Filter) => Promise<IdOrError[]>;
    /**
     * Deletes values from the database
     *
     * @param [filter] Filter to use to match the values to delete. If true
     *   all values will be deleted
     *
     * @returns A promise resolving to an array of the Ids of the values
     *   deleted
     */
    delete: (filter: Filter | true) => Promise;
    /**
     * Closes the CRUD database instance
     */
    close: () => void;
  };

  export interface Options {
    /// Path to file/folder to contain JSON database
    path: string;
    /// Field of data objects to be used as the key for the object
    id: string;
    /// Cache keys of the values in the database
    cacheKeys: boolean;
    /// Cache the values in the database
    cacheValues: boolean;
  };
};