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

@werkin/clientdb

v0.1.4

Published

#### Instalation

Downloads

19

Readme

CLIENT DB

Instalation

This package gives you possibility to create your own client database and work with it. All what YOUR_DB_NAME_HERE.createCollection('some_collection_name');

 npm i @werkin/clientdb 
 // or
 yarn add @werkin/clientdb

Collections:

You need add data to the collection so:

YOUR_DB_NAME_HERE.some_collection_name.add('object_here');

Return:

response = { 
  all_docs: ['docs_with_added_data'],
  added_doc: { /** doc which you have add */ },
  status: "success" 
}
YOUR_DB_NAME_HERE.some_collection_name.bulkAdd('array_here');

Return:


response = { 
  all_docs: ['docs_with_added_data'],
  status: 'success',
  added_docs: [ /** Your added docs */ ]
}

In case if you use bulkAdd and some of added data already exists at the store, will be added only uniq data, and you will get next response:

rsponse = {
  added_docs: ['only docs which have been added'],
  all_docs:  ['docs_with_updated_data'],
  status: 'added with warnings'
}

For updating use:

    YOUR_DB_NAME_HERE.some_collection_name.update('object_here');

Response for update:

response = {
  all_docs: [/** docs_with_updated_data */],
  updated_doc: {/** Updated doc */},
  old_doc: {/** Doc before update */},
  status: 'success'
}

For updating two or more docs use bulk update:

    YOUR_DB_NAME_HERE.some_collection_name.bulkUpdate('array_here');

Response for bulk updating:

response = {
  all_docs: [/** docs_with_updated_data */],
  updated_docs: [/** Updated docs */],
  old_docs: [/** Docs before update */],
  status: 'success'
}

In case if during updating some doc or docs will not be founded, they will be passed and you will get response:

response = {
  all_docs: [/** docs_with_updated_data */],
  updated_docs: [/** Only updated docs */],
  old_docs: [/** Docs before update which was updated */],
  passed_data: [ /** Docs whic were not founded */ ],
  status: "Not existed docs were not updated"
}

Sometimes you need add some data and update existed data, but not remove else data. For this you can use:

YOUR_DB_NAME_HERE.some_collection_name.upsert('object_here');

Return:

response = {
  all_docs: this.docs,
  upserted_doc: { /** Added or updated doc */ },
  status: 'success'
 }
YOUR_DB_NAME_HERE.some_collection_name.bulkUpsert('array_here');

Both methods will return:


response = { 
  all_docs: ['docs_with_updated_data'],
  upserted_docs:  [ /** Added or updated docs */],
  status: 'success'
}

If you need just get all docs from collection, you can do it by next way:

YOUR_DB_NAME_HERE.some_collection_name.docs;

But some times you need not only get all docs but sort or limit them. So you can use

YOUR_DB_NAME_HERE.some_collection_name.getAll();

Also you may want take some filtered data, you can use where method got this. As argument you coud set an object or filter function which should return true or false.

YOUR_DB_NAME_HERE.some_collection_name.where('filter options here, object with required fields or filter function');

In some cases you need to get first element of collection, for this you could use

YOUR_DB_NAME_HERE.some_collection_name.getFirst();

In some cases you want to get one doc by id or by some fields:

YOUR_DB_NAME_HERE.some_collection_name.getById(/** doc id */);

if you nedd fine some doc by specific params you can use

YOUR_DB_NAME_HERE.some_collection_name.getOne('filter options here, object with required fields or filter function');

These methods will return you first matches at your collection.

Subscriptions

You can subscribe on changes at the collection by fields. If some field will be changed, listeners who listen this filed update will be triggered. for one listener could be set unlimited list of fields.

YOUR_DB_NAME_HERE
.some_collection_name
.subscribe({
  next:({ all_docs: [], updated: [] }) => { /** doc id */ },
  keys: ['title', 'desc'],
  options: {}
});

When you subscribe, it will be triggered and return you arrays with docs.

Also subscription could get options clustered_changes and clustered_all, they are boolean and false by default.

  • clustered_changes - if false will just return array of updated items. If true then this items will be wrapped at the cluster, which you can sort offset or limit; Then you will need to execute it by exec();

  • clustered_all - same as with clustered_changes but with all docs.

Collection chain's

Cluster instance have next methods.

  • sort - this method is sorting data by fields at the array. At the second argument ( this is optional ) you could describe direction of sorting desc or asc for fields one by one;
  • limit - data will be chunked to tha pages with docs on each equal didgit at the method's argument.
  • offset - remove number of docs from start.
  • page - change current page.
  • exec - return data property value. Could be used as end of chain.
YOUR_DB_NAME_HERE
  .some_collection_name
  .getAll()
  .sort('name') // Will be sorted by name field
  .limit(9); // Will be take 9 items from 0

responseWithoutExec = {
  data: [/** Docs sorted limited etc. */],
  current_page: 0 /** Index of the page at the pages array */,
  pages: [/** Array of pages */]

  /** ...List of methods */
}

YOUR_DB_NAME_HERE
  .some_collection_name
  .where({ /** filter options here */ })
  .offset(9) // start index for limit is 8
  .sort('name') // Will be sorted by name field
  .limit(9) // Will be take 9 items from 8th
  .exec()

/** ...Response will be value of the data property */