Git Product home page Git Product logo

pixiedevpraveen / pixiedb Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 43 KB

A tiny in-memory javascript database with indexing and sql like filters.A small javascript in memory database with indexing and filters.

Home Page: https://www.npmjs.com/package/pixiedb

License: MIT License

TypeScript 99.66% JavaScript 0.34%
database in-memory-database in-memory-storage javascript-database js-database small-database

pixiedb's Introduction

PixieDB

npm version npm downloads bundle JSDocs License

A tiny in-memory javascript database with indexing and sql like filters.

PixieDb perform all operations (insert, delete, get) in log(n) time.

Warning

Please keep in mind that PixieDb is still in under active development.

Usage

import { PixieDb } from "pixiedb";

const products = [
    { id: 1, name: "Apple", price: 5, category: "Fruit" },
    { id: 2, name: "Banana", price: 10, category: "Fruit" },
    { id: 3, name: "Grapes", price: 6, category: "Fruit" },
    { id: 4, name: "Orange", price: 8, category: "Fruit" },
    { id: 5, name: "Potato", price: 18, category: "Vegetable" },
    { id: 6, name: "Milk", price: 7, category: "Dairy" },
    // ...
]

// provide unique key, data and indexes for better performance
// 3rd param data is optional can be load after using the load method
const pd = new PixieDb('id', ["price", "category"], products) 
// or
const pd = new PixieDb<Product>('id', ["price", "category"]) // pass type if using typescript
pd.load(products) // to load data later

const byId = pd.select().eq("id", 2).single()
console.log(byId); // { id: 2, name: "Banana", price: 10, category: "Fruit" }

// can also pass array of fields to select method to pick only those fields/properties
const fruitBelow10 = pd.select(["id", "name", "price"]).eq("category", "Fruit").lte("price", 10).orderBy(["name", ["price", "desc"]]).range(2, 3).data()
console.log(fruitBelow10); // [{ id: 3, name: "Grapes", price: 6 }, ...]

const updatedBanana = pd.where().eq("name", "Banana").update({price: 100})
// [{ id: 2, name: "Banana", price: 100, category: "Fruit" }, ...]

// delete all docs where name equals "Apple"
const deletedApples = pd.where().eq("name", "Apple").delete()
// [{ id: 1, name: "Apple", price: 5, category: "Fruit"}, ...]

Installation

# using npm
npm install pixiedb

# using pnpm
pnpm add pixiedb

# using yarn
yarn add pixiedb

# using bun
bun add pixiedb

Docs

PixieDb

This is a class which create an PixieDb instance to use.

// pass type/interface if using typescript
const pd = new PixieDb<Product>('id', ["price", "category"]) 

// or with data
const pd = new PixieDb<Product>('id', ["price", "category"], products)

Methods

load

Used to import data without cloning (so don't mutate the data or clone before load). Pass true as second parameter to clear the previous data and indexes state. (default: false).

pd.load(products)
// or
pd.load(products, true)
// remove previous data and index state

get

Get single doc/row using key (primary key/unique id). Returns doc/row if present else undefined.

pd.get(2)
// { id: 2, name: "Banana", price: 10, category: "Fruit" }

select

Get single doc/row using key (primary key/unique id). Returns doc/row if present else undefined.

pd.select().eq("category", "Fruit").gte("price", 6).data()
// [{ id: 2, name: "Banana", price: 10, category: "Fruit" }, { id: 3, name: "Grapes", price: 6, category: "Fruit" }, ...]

pd.select(["id", "name", "price"]).eq("category", "Fruit").lte("price", 6).data()
// [{ id: 1, name: "Apple", price: 5 }, ...]

pd.select().eq("category", "Fruit").between("price", [6, 10]).data()
// [{ id: 2, name: "Banana", price: 10, category: "Fruit" }, { id: 3, name: "Grapes", price: 6, category: "Fruit" }, { id: 4, name: "Orange", price: 8, category: "Fruit" }, ...]

where

used to perform delete/update with complex filtering

// this will delete and return all the docs according to the filters
pd.where().eq("category", "Fruit").gte("price", 6).delete()
// [{ id: 2, name: "Banana", price: 10, category: "Fruit" }, { id: 3, name: "Grapes", price: 6, category: "Fruit" }, ...]

pd.where().eq("category", "Fruit").between("price", [6, 10]).update({price: 11})
// [{ id: 2, name: "Banana", price: 11, category: "Fruit" }, { id: 3, name: "Grapes", price: 11, category: "Fruit" }, { id: 4, name: "Orange", price: 11, category: "Fruit" }, ...]

data

Get all docs/rows ordered respect to primary key/unique id. Pass false to get all without clone (don't modify). default: true

pd.data()
// [{ id: 1, name: "Apple", price: 5, category: "Fruit" }, ...]

count

Get all docs/rows ordered respect to primary key/unique id. Pass false to get all without clone (don't modify). default: true

pd.select().count()
// 6

pd.select().eq("category", "Fruit").between("price", [6, 10]).count()
// 4

close

to close/quit/terminate the database and remove all data/indexes and fire "Q" ("quit") event. Pass true to not emit events. default: false

pd.close()
// or
pd.close(true) // doesn't fire event

toJson

return JSON of all data (without cloning), key and index names.

pd.toJSON()
// { key: "id", indexes: ["price", "category", {name: "id", unique: true}], data: [{ id: 1, name: "Apple", price: 10, category: "Fruit" }, ...]

// this will call the above toJSON method
JSON.stringify(pd)

view more

Roadmap

  • load docs
  • get all docs
  • get docs with key
  • Events (load, change, insert, update, delete, quit)
  • orderBy with multiple keys (sorting)
  • single doc with filters
  • count of docs with filters
  • update of docs with filters
  • delete of docs with filters
  • filters
    • eq (where value equal)
    • neq (where value not equal)
    • in (where value in)
    • nin (where value not in)
    • between (where value between to values)
    • nbetween (where value not between to values)
    • gt (where value greater than)
    • gte (where value greater than or equal to)
    • lt (where value less than)
    • lte (where value less than or equal to)
    • custom query method
  • range offset (from) and count (limit of docs to return)

pixiedb's People

Contributors

pixiedevpraveen avatar

Watchers

 avatar  avatar

Forkers

tienld

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.