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

siamdb

v1.0.6

Published

Serverside In App Memory Database

Downloads

11

Readme


Serverside In-App Memory DB

Siam DB is a simple, lightweight in-app memory database for Node.js that provides support for multiple collections of documents. It's designed to be easy to use and to provide a basic set of features for storing and retrieving data, without requiring a lot of configuration or setup.

IMPORTANT: Siam DB does not currently include any persistence mechanism to store data beyond the lifecycle of the application. Therefore, when the application is restarted, all the data in the database will be lost.

Key Features

  • Ideal for quickly storing and retrieving data during the testing and prototyping phase of development, without the need for a full database.
  • Efficient caching for frequently accessed data in memory for quick access.
  • Suitable for storing chat messages in a real-time application, where the persistence of the message history is trivial.

Installation


npm install siamdb

Usage


Create database

You can import the createDatabase function from the package:

import { createDatabase } from "siamdb"
// or
const { createDatabase } = require("siamdb")

Then, create a new instance of the database by calling createDatabase and passing in an options object and a schema object:

const options = {
  generateIds: "autoinc"
}

const schema = {
  users: {
    name: "string",
    email: "string",
    age: "number",
    active: "boolean",
    children: "object"
  },
  posts: {
    title: "string",
    content: "string",
    authorId: "string"
  }
}

const database = createDatabase(options, schema)

Or you may omit options and schema entirely, opting for uuids for ids, and no limitations to the collections you can create and key value types you can use within:

const database = createDatabase()

Once you have a SiamDatabase instance, you can access the collections by calling the collection method:

const users = database.collection("users")
const posts = database.collection("posts")

Each collection has methods for finding, creating, updating, and deleting documents:

// Create a new document
const userId = users.create({ name: "John", email: "[email protected]", age: 25 }) 

// Find an array of documents
const activeUser = users.find({ name: "John" })

// Update documents that match a query
users.update({ id: userId }, { age: 26 })

// Delete documents that match a query
users.delete({ id: userId })

Advanced queries

| Operator | Definition | Input Data Type | Description | Example | |-----------|-----------------------------------|-----------------|---------------------------------------------------------|-----------------------------------------------------| | $exists | Does this exist or not | boolean | Check if a field exists in the document | .find({age: { $exists: true }}) | | $gte | Greater than or equal to | number | Check if a field is greater than or equal to a value | .find({age: { $gte: 18 }}) | | $gt | Greater than | number | Check if a field is greater than a value | .find({age: { $gt: 18 }}) | | $lte | Less than or equal to | number | Check if a field is less than or equal to a value | .find({age: { $lte: 24 }}) | | $lt | Less than | number | Check if a field is less than a value | .find({age: { $lt: 24 }}) | | $in | Matches any value in an array | array | Check if a field's value matches any value in an array | .find({favoriteColors: { $in: ["red", "blue"] }}) | | $nin | Matches none of the values in an array. | array | Check if a field's value does not match any value in an array | .find({favoriteColors: { $nin: ["green", "yellow"] }})| | $ne | Not equal to | any | Check if a field's value is not equal to a specified value | .find({status: { $ne: "inactive" }}) | | $or | Logical OR | array | Check if any of the conditions in the array are true | .find({$or: [{ city: "New York" }, { city: "San Francisco" }]}) | | $and | Logical AND | array | Check if all conditions in the array are true | .find({ $and: [{ active: true },{ verified: true }]}) |

The following is an example utilizing all available operators:

db.collection("user").find({
   age: { $gte: 18, $lte: 24 },
   email: { $exists: true },
   status: { $ne: "inactive" },
   score: { $gt: 0, $lt: 100 },
   favoriteColors: { $in: ["red", "blue"], $nin: ["green", "yellow"] },
   $or: [{ city: "New York" }, { city: "San Francisco" }],
   $and: [
     { active: true },
     { verified: true },
     {
       createdAt: {
         $gte: new Date("2023-01-01"),
         $lt: new Date("2023-04-01"),
       },
     },
   ],
 })

Responses


find()

The find() method returns an array of documents

[
  ...,
  {
    id: 1,
    content: { ... }, // the document content
    version: 1
  }
  ...
]

update()

The update() method returns an array of updated documents

[
  ...,
  {
    id: 1,
    content: { ... }, // the updated document content
    version: 2
  }
  ...
]

create()

The create() method returns a string of new document's id

"1"
//or
"3f9b4392-5257-4cb2-9cad-6bb5dad95424"

delete()

The delete() method returns an array of the deleted document ids

["1"]
//or
["3f9b4392-5257-4cb2-9cad-6bb5dad95424"]

Database Parameters


Options

The createDatabase function accepts an optional options object as the first parameter and an optional schema object as the second parameter:

const database = createDatabase(options, schema)

The options object contains:

  • generateIds (optional): Specifies how to generate document IDs. Can be set to "autoinc" to generate IDs automatically as integers starting from 1, or "uuid" to generate UUIDs using the crypto.randomUUID method.

Example:

const database = createDatabase({
  generateIds: "autoinc"
}, ...)

Schema

The optional schema object passed to createDatabase should be an object where each key represents a collection name, and each value represents the schema for that collection. The schema for each collection should be an object where each key represents a field name, and each value represents the data type for that field. The supported data types are that of Javascript's typeof:

  • string
  • number
  • boolean
  • object
  • function

Example:

const database = createDatabase(..., {
  users: {
    name: "string",
    email: "string",
    age: "number",
    active: "boolean",
    children: "object"
  },
  posts: {
    title: "string",
    content: "string",
    authorId: "string"
  })

ToDo:


  • Implement some type of ttl on collections and/or documents.