Skip to content

with-tiny/nosql-db

Repository files navigation

tiny-nosql-db 🤏💾

Tiny NoSql Database for little projects


version-badge license-badge

Installation

Go to the root of your project and run

npm install -D @tiny-apps/nosql-db

Usage

Import the server and start it.

import nosqlServer from '@tiny-apps/nosql-db'

const server = nosqlServer()

Then set the database you want to use

const db = server.use('test_database')

And just choose a collection and run any method on it (ex: collection 'test_people')

db.test_people.insertOne({
  name: 'John',
  surname: 'Dow',
  age: 31,
})

Here is the complete example

import nosqlServer from '@tiny-apps/nosql-db'

const server = nosqlServer()
const db = server.use('test_database')

db.test_people.insertOne({
  name: 'John',
  surname: 'Dow',
  age: 31,
})
// returns { ok: true, _id: xxx, count: 1 }

db.test_people.find({
  age: 31,
})
// returns [{ _id: xxx, name: 'John', surname: 'Dow', age: 31 }]

And modification

db.test_people.updateMany({ name: 'John' }, { name: 'Juan' })
// returns { ok: true, _ids: [ xxx ], count: 1 }

db.test_people.find({
  age: 31,
})
// returns [{ _id: xxx, name: 'Juan', surname: 'Dow', age: 31 }]

Custom storage path or server name

You can change the default storage path or server name by passing them as named arguments in server creation

import nosqlServer from '@tiny-apps/nosql-db'

const server = nosqlServer({
  name: 'Main Db Server',
  path: './custom-storage'
})

Make sure to add the path to the .gitignore file

Special operators

Special operators are reserved keys that allow build complex queries. All special operators starts with and underscore.

const discountedUsers = db.test_people.find({
  _or: [
    { age: { _lt: 18 } },
    { age: { _gt: 65 } },
  ]
})

Available operators

Comparation operators

  • _eq. Filter where field is equal to value
  • _neq. Filter where field is not equal to value
  • _gt. Filter where field greater than value
  • _gte. Filter where field greater or equal than value
  • _lt. Filter where field lower than value
  • _lte. Filter where field lower or equal than value
  • _in. Filter where field is in value array
  • _nin. Filter where field is not in value array

Logical operators

  • _and. Filter where all condition array items matches
  • _or. Filter where at leats one condition array item matches
  • _nor. Filter where none condition array item matches
  • _not. Filter where condition not matches

Available methods

Server

  • use(_dbName). Sets the current database to _dbName
  • listDatabases(). Return an array of all databases of the server
  • createDatabase(_name). Creates a database (implicit in use())
  • dropDatabase(_name). Deletes a database

Database

  • collection(_name) o db[_name]. Sets the current collection to _name
  • listCollections(). Return an array of all collections of the database
  • createCollection(_name). Creates a collection (implicit in db.[_name])
  • dropCollection(_name). Deletes a collection

Collection

  • find(_filter). Query the collection and returns matched records
  • findOne(_filter). Returns a record if only one matches
  • count(_filter). Count the records mathed on a query
  • distinct(_field). Return distinct values of _field in a collection
  • insertOne(_record). Insert a record
  • insertMany(_recordList). Insert a list of records
  • updateOne(_filter, _record). Update a record if only one matches
  • updateMany(_filter, _record). Update records that matches
  • deleteOne(_filter). Delete a record if only one matches
  • deleteMany(_filter). Delete records that matches

Next Features

  • More query and update special operators

About

Tiny no-sql database for small projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors