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

strato-db

v3.8.0

Published

NoSQL-hybrid with Event Sourcing based on sqlite

Downloads

36

Readme

Strato-DB

MaybeSQL with Event Sourcing based on SQLite

The overall concept is to be a minimal wrapper that keeps SQL close by, but allows schemaless storage for where you want it.

Install

npm install strato-db

Usage

Simple CRUD DB:

import {DB, JsonModel} from 'strato-db'

const db = new DB({file: 'data/mydb.sqlite3', verbose: true})

class Things extends JsonModel {
	constructor(options) {
		super({
			...options,
			name: 'things',
			columns: {
				id: {type: 'INTEGER'},
				count: {type: 'INTEGER', index: 'SPARSE'},
			},
		})
	}
}

db.addModel(Things)

// db only opens the file once this runs
await db.store.things.set({id: 5, name: 'hi', count: 3})
// Get all items that have count 3
console.log(await db.store.things.search({count: 3}))

DB with Event Sourcing:

import {DB, EventQueue, EventSourcingDB, ESModel} from 'strato-db'

const qDb = qFile && qFile !== file ? new DB({file: qFile, verbose}) : db
qDb.addModel(EventQueue, {name: 'queue'})
const queue = qDB.store.queue

class ESThings extends ESModel {
	constructor(options) {
		super({
			...options,
			name: 'things',
			columns: {
				id: {type: 'INTEGER'},
				count: {type: 'INTEGER', index: 'SPARSE'},
			},
		})
	}
}

const eSDB = new EventSourcingDB({
	db,
	queue,
	models: {things: {Model: ESThings}},
})

await eSDB.store.things.set({id: 5, name: 'hi', count: 3})
console.log(await eSDB.store.things.search({count: 3}))
// See the created events
console.log(await eSDB.queue.all())

API

The API is class-based. There are types in JSDoc and in types.d.ts, which are the only documentation for now.

The design of EventSourcingDB is discussed in [Server Side Redux](./Server Side Redux.md)

Classes:

  • SQLite: Wraps a Sqlite3 database with a lazy-init promise interface
  • DB: Adds models and migrations to SQLite3
  • JsonModel: Stores given objects in a DB instance as JSON fields with an id column, other columns can be calculated or be virtual. You can perform searches via the wrapper on defined columns.
  • EventQueue: Stores events. Minimal message queue.
  • EventSourcingDB: Implements the Event Sourcing concept using EventQueue. See [Server Side Redux](./Server Side Redux.md).
  • ESModel: A drop-in replacement for JsonModel to use EventSourcingDB. Modifications are dispatched as events and awaited

With the TypeScript definitions you can provide a Type for the stored objects and the config each model uses. This allows typechecking CRUD inputs and results, even in plain JS (with JSDoc comments).

Status

This project is used in production environments.

Since it wraps SQLite, the actual storage of data is rock-solid.

It works fine with multi-GB databases, and if you choose your queries and indexes well, you can have <1ms query times.

The important things are tested, our goal is 100% coverage.

Multi-process behavior is not very worked out for the EventSourcingDB:

  • Since it's layering a single-locking queue on top of SQLite, it works without problems, but no effort is made yet to avoid double work. It would require workers "locking" events and watching each other's timestamps.
  • To have DB slaves, the idea would be to either use distributed SQLite as implemented by BedrockDB, or to distribute the event queue to slaves and have them derive their own copy of the data.

Take a look at the planned improvements.

License

MIT © Wout Mertens