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

opaldb

v0.8.1

Published

A minimal in-memory database with zero dependencies.

Downloads

1

Readme

OpalDB

OpalDB is a minimal in-memory database with zero dependencies written in JavaScript for Node.js. It is primarily for learning and demonstration purposes and as such should not be considered production ready.

Models

First make a schema for your records, then convert that schema into a model, and then create records.

const opal = require('opal')
const Schema = opal.Schema;
const Model = opal.Model;

const person = new Schema({
  name: 'STRING',
  age: 'NUMBER',
  hobbies: 'ARRAY'
})

const PersonModel = opal.model(person)

const joe = new PersonModel({
  name: 'Joe Shmoe',
  age: 28,
  hobbies: ['soccer', 'baseball']
})

The schema does simple validation and the model provides an onBeforeSave hook.

const bob = new PersonModel({
  name: 'Bob',
  age: 23,
  hobbies: 'Baskeball, etc'
})
// The above code will throw an error because hobbies is not an array.

UserModel.onBeforeSave((record) => {
  record.password = hashAndSalt(record.password)
})
// Now our users will have secure passwords.

Tables

First create a database, then add a table to it, then insert records into the table.

const db = new opal.Database()

db.addTable('people')

db.in('people').insert(joe)

Events

You can add listeners for insert, update and delete.

db.in('people').listen('insert', (record) => {
  console.log(record)
})

Queries

Queries at the table level include getById and findWhere. The former is a quick table lookup and the latter is an expensive match between the records in the database and the object passed into the findWhere function. You can also delete by index which is fairly straightforward.

db.in('people').getById(0) // returns Joe

db.in('people').findWhere({ name: 'Joe Shmoe' }) // also returns Joe

db.in('people').delete(0) // deletes Joe

Use the 'where' method to return a query on a property of the records in a specific table. The 'where' method returns a query object that has type specific methods for matching records.


// .contains() only works with array properties
db.in('people').where('hobbies').contains('soccer')

// .lessThan() and .greaterThan() work with number properties
db.in('people').where('age').lessThan(30)

// .equals() works with numbers and string but not arrays
db.in('people').where('name').equals('Joe')

For nested records, the 'where' method can take an array of properties.

db.in('people').where(['address', 'city']).equals('Palo Alto')

Secondary indexes

You can create an index from any attribute on a record.

db.in('people').createIndex('name')
db.in('people').getByIndex('name', 'Joe') // returns all the people named Joe