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

crud-operator

v1.1.0

Published

make a simple CRUD operator

Downloads

1

Readme

crud-operator · GitHub license npm version

Usage: Make a simple CRUD.

Installation

# with npm
npm install --save crud-operator
# with yarn
yarn add crud-operator

commonJS

const crud = require('crud-operator');
const users = new crud();

ES module

import crud from 'crud-operator';
const users = new crud();

Methods

There are four methods, create, read, update and delete. Down here is the example of some codes. These codes are when the strictMode is false

import crud from 'crud-operator';

const users = new crud();
  • create

    Create new object by passing the required properties which is known as NEVER NULL value. (by default it will be name and id).

    NOTE

    • Every id must be the unique value.
    • id will be stringified.
    • When you provide an id as an integer number, don't put the 0 infront. It will cause an error.

    RETURN

    New object you created.

    example

    users.create({ id: '123abc', name: 'Peter', age: 21 });
    users.create({ id: 2189, name: 'Susan', email: '[email protected]', age: 16 }); // this id will get stringified
  • read

    Get the object by finding its required properties. When there is no argument, it will return all the items into single array. If there was more than one matched, it will return a filtered array.

    If strictMode is false, it will match string to both lower-case and upper-case

    ex. { name: 'john' } will include { name: 'John' }

    RETURN

    Single object or an array.

    example

    const allUsers = users.read(); // get all the users
    console.log(allUsers);
    // output: 
    //       [{ id: '123abc', name: 'Peter' },
    //         { id: '2189', name: 'Susan', email: '[email protected]' }]
    
    console.log(
       users.read({ name: 'peter' });
    ) // log only users with name peter (include upper-case)
  • update

    Update the object. You can pass any required properties when the option strictMode is false. But better to use this method by passing id.

    Types

    You need to provide a second parameter of string to specify what type of update you want to do.

    • set : Set or change the properties. You can change what ever you want except id. In the third parameter, you will include the objects to set.
    • remove : Remove properties. You can remove all properties that are not in required lists. In the third parameter, you will include the array of string. Those are the properties name that you want to remove.

    RETURN

    Nothing

    emample

    // Even though the name Susan was started with a capital 'S', it will matched when using non-strictMode
    users.update({ name: 'susan'}, 'delete', ['email', 'age'])
    
    // id needs to be exact same, therefore this code won't work.
    users.update({ name: 'peter', id: '123ABC'}, 'set', { email: '[email protected]', age: 22 });
  • delete

    Delete the object. This is similar to update. You can delete multiple items by filtering with required properties, when strictMode is false.

    RETURN

    Objects that were deleted.

    example

    const deletedUser = users.delete({ id: '123abc' });
    
    console.log( users.read() ); // only one user left inside the array
    console.log( users.read({ name: 'peter' }) ); // this will not log anything because there is no user with name peter
    
    console.log(`${deletedUser.name} has lefted`);

Example code

import crud from 'crud-operator';

// By default, strictMode is set to false.
const users = new crud();

// By default, there is required properties of 'name' and 'id' and you need to include them.
users.create({ name: 'Jack', id: 'qog2b28b', optional: 'some random text'});
users.create({ name: 'Susan', id: 179128 });

console.log(users.read());
// output: 
//       [{ id: 'qog2b28b', name: 'Jack', optional: 'some random text' },
//         { id: '179128', name: 'Susan' }]

// This works only when strictMode is false
// Otherwise you need to pass only id
const deletedUser = users.delete({ name: 'Susan'});

Options

In the constructor, you can provide three options. This will help the project match to your purpose.

option | type -------|------ requiredProps| string[] strictMode | boolean defaultData | object[]

  • requiredProps

requiredProps is an Array of string. You will provide the properties every objects need to have. By default, it will contains [ 'name', 'id' ]. If you want to make a custom properties, make sure to put 'id' because it is required to every objects.

bad-example.js

import crud from 'crud-operator';

const users = new crud(['message', 'name']); // error

good-example.js

import crud from 'crud-operator';

const users = new crud(['name', 'id', 'email']);

users.create({ name: 'Luke', id: 'i3gw9h1b', email: '[email protected]'});
  • strictMode

By default, this value is set to false.

Differences

  • true

    • Every string needs to be the exact match.
    • Can not update, delete other than id.
  • false

    • Every string will matched both of upper-case and lower-case.
    • Can update, delete by any required properties.

NOTE

When you are using non-strictMode, the string of id won't match in both upper-case and lower-case. Although the id needs to be exact same, it can match number and string.

ex. { id: 123 } will be { id: '123' }

  • defaultData

You can provide the data what you already had, or the data you fetch. However, those data need to have the required properties.

example

const Crud = require('crud-operator');
const fetch = require('fetch').fetchUrl;

let items;

function fetchDataIntoCRUD(url){
   return new Promise((resolve, reject) => {
      fetch(url, (error, meta, body) => {
         resolve(decodeURI(body));
      })
   })
}

fetchDataIntoCRUD('https://jsonplaceholder.typicode.com/users')
   .then((data) => {
      const json = JSON.parse(data);
      items = new Crud(['id', 'name'], true, json);
   })
   .then(() => {
      console.log(items.read({ id: 3 }));
   })