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

doubledb

v3.0.0

Published

An on disk database that indexes everything for fast querying.

Downloads

16

Readme

doubledb

An on disk database that indexes everything for fast querying.

Installation

npm install --save doubledb

Usage

import createDoubledb from 'doubledb';
const doubledb = await createDoubledb('./data');

doubledb.insert({
  id: undefined, // defaults to uuid, must be unique
  firstName: 'Joe',
  lastName: 'Bloggs',
  stats: {
    wins: 10,
    loses: 5
  },
  skills: ['cooking', 'running']
});

doubledb.get(record.id);
doubledb.find('firstName', 'Joe');
doubledb.find('stats.wins', 10);
doubledb.find('skills', 'cooking');
doubledb.find('firstName', v => v.startsWith('J'), { skip: 20, gt: 'J', lt: 'K' });
doubledb.filter('firstName', 'Joe');
doubledb.filter('firstName', v => v.startsWith('J'));
doubledb.filter('firstName', v => v.startsWith('J'), { limit: 10, skip: 20, gt: 'J', lt: 'K' });
doubledb.replace(record.id, { firstName: 'Joe', lastName: 'Bloggs' });
doubledb.patch(record.id, { firstName: 'Bob' });
doubledb.remove(record.id);

.get(id)

Get a single record by it's .id property.

If a record is found, the whole record will be returned. If no record is found, undefined will be returned.

.find(field, value, { skip })

Quickly find a single record by any field (use.dot.notation.for.nested.properties) and it's exact value.

If multiple records exist, a skip option can be provided to ignore the first number of finds.

If a record is found, the whole record will be returned. If no record is found, undefined will be returned.

.find(field, matcherFunction: (value: string) => boolean), { limit, skip, gt, lt, gte, lte })

Slow find a single record by any field and test against a matcherFunction.

If multiple records exist:

  • a skip option can be provided to ignore the first number of finds.
  • a limit option can be provided to stop after number of finds.

Find using a matcherFunction will work without a gt and lt, but the indexing will be pretty useless, as it will need to scan every single record.

You should provide a gt and/or lt to let the indexer know where to begin/end.

For example, the query below will scan every first name from A all the way to Z

doubledb.find('firstName', v => v.startsWith('Jo'))

Let's tell it to start from Jo.

doubledb.find('firstName', v => v.startsWith('Jo'), { gt: 'Jo' })

This will skip all indexes lower than Jo. However if it can't find any records, it will keep checking, even if the firstName is Zelda

So we should help the indexer by giving it a lt.

doubledb.find('firstName', v => v.startsWith('Jo'), { gt: 'Jo', lt: 'K' })

Let's look at some more examples:

doubledb.find('favouriteNumber', v => v > 5 && v < 10, { gt: 5, lt: 10 })
doubledb.find('firstName', v => ['Dave', 'Peter'].includes(v), { gt: 'Dave', lte: 'Peter' })

If a record is found, the whole record will be returned. If no record is found, undefined will be returned.

.filter(field, matcherFunction: (value: string) => boolean), { limit, skip, gt, lt, gte, lte })

This works the exact same as .find but will return an array.

If records are found, an array will be returned containing every complete found record. If no records are found, an empty array will be returned.

.replace(key, object)

Completely replace a key with a new object, losing all previous fields in the record.

.patch(key, object)

Merge the new object in with the existing record.

For example, if the following record exists:

{
  "id": "1",
  "firstName": "Joe",
  "lastName": "Bloggs"
}

And you run the following .patch.

doubledb.patch('1', { fullName: 'Joe Bloggs' })

The final record will be:

{
  "id": "1",
  "firstName": "Joe",
  "lastName": "Bloggs",
  "fullName": "Joe Bloggs"
}

.remove(key)

Query the database using a complex query object. This method allows for advanced querying using a combination of fields and operators.

.query(queryObject)

Query the database using a complex query object. This method allows for advanced querying using a combination of fields and operators.

Example:

const records = await doubledb.query({
  location: 'London',
  category: 'b',
  $or: [
    { firstName: { $eq: 'Joe' } },
    { firstName: { $eq: 'joe' } }
  ]
});

The queryObject can contain various fields and operators to filter the records. The following operators are supported:

Operators:

  • $eq: Equal to a value.
  • $ne: Not equal to a value.
  • $gt: Greater than a value.
  • $gte: Greater than or equal to a value.
  • $lt: Less than a value.
  • $lte: Less than or equal to a value.
  • $in: Value is in the provided array.
  • $nin: Value is not in the provided array.
  • $all: Array contains all the provided values.
  • $exists: Field exists or does not exist.
  • $not: Negates the condition.

Example Usage of Operators:

const records = await doubledb.query({
  age: { $gte: 18, $lt: 30 },
  status: { $in: ['active', 'pending'] },
  $or: [
    { role: { $eq: 'admin' } },
    { role: { $eq: 'user' } }
  ],
  preferences: { $exists: true }
});

How Operators Work:

  • $eq: Matches documents where the field is equal to the specified value.
  • $ne: Matches documents where the field is not equal to the specified value.
  • $gt / $gte: Matches documents where the field is greater than (or greater than or equal to) the specified value.
  • $lt / $lte: Matches documents where the field is less than (or less than or equal to) the specified value.
  • $in: Matches documents where the field value is in the specified array.
  • $nin: Matches documents where the field value is not in the specified array.
  • $all: Matches documents where the array field contains all the specified values.
  • $exists: Matches documents where the field exists (or does not exist if set to false).
  • $not: Matches documents that do not match the specified condition.

This query method is powerful and allows combining multiple conditions and operators to fetch the desired records from the database.