Code Monkey home page Code Monkey logo

db.js's Introduction

db.js

Simple database without dependencies in a single JavaScript file.

  • opinionated: all objects have atleast id, created and updated keys
  • minimalist: only 3 operations: get, set and del
  • tiny: less than 100 LOC including comments and whitespace

Installation

Install with npm:

npm i -S https://github.com/eremt/db.js

or just download the file:

wget https://raw.github.com/eremt/db.js/main/db.js

Documentation

dbjs(options?)

const dbjs = require('db.js')
const db = new dbjs()

options.file: string

An optional file to use for data persistence, if it doesn't exist it will be created. If undefined the storage will default to memory.

Example

const dbjs = require('db.js')
const db = new dbjs({ file: './db.json' })

get(query?)

get(query: undefined)

Without query (or any falsy value such as null, '', etc.) an array containing all items is returned.

Example

db.get()
// => [ { id, created, updated, ... }, ... ]

get(query: string)

If query is a string it is treated as the key and returns an array with a single match if found.

Example

db.get('my-key')
// => [ { id: 'my-key', created, updated, ... } ]

get(query: { [key]: string | number | boolean | RegExp, ... })

When query is an object it's used as filter and returns an array with the items that matched.

The value can be any of type: string, number, boolean and RegExp.

Example

db.get({ value: /example/ })
// => [ { id, created, updated, value: 'My example', ... }, ... ]

set(data?, key?)

set(data: undefined, key: undefined)

Without arguments a default object is created with id, created and updated keys. Returns the created object.

Example

db.set()
// => { id, created, updated }

set(data: any, key: undefined)

With data but no key a new object is created with the default keys and the data object. Returns the created object.

Example

db.set({ value: 'My example' })
// => { id, created, updated, value: 'My example' }

set(data: any, key: string)

When data and key are passed it does one of two things:

If the key is found it updates that object with the data and a new updated value.

If no key is found it creates a new object with the data and all default values except id which gets its value from key.

Returns the updated or created object.

Example

db.set({ value: 'My example' }, 'my-key')
// => { id: 'my-key', created, updated, value: 'My example' }

del(key)

del(key: string)

Returns either the key that was deleted, or undefined if no such key exists.

Example

db.del('my-key')
// => 'my-key'

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.