memsdb
TypeScript icon, indicating that this package has built-in type declarations

0.11.1 • Public • Published

memsdb

A simple in memory DB - Created to experiment more with typescript and classes

minified minified + gzip dependency count tree shaking Rate on Openbase

Documentation:

Initialising a new database:

import { DB } from 'memsdb'

const db = new DB('My DB Name')

Creating and assigning new collections to the db:

const comments = new DBCollection(db, {
  name: 'comments',
  structure: {
    text: '',
    by: '',
    parent: '',
  },
})

Adding documents to an existing collection:

comments.insertOne({
  text: `My fantastic comment`,
  by: 'me'
})

Finding documents in a collection:

/**
 * Find all documents where the `by` field is equal to 'me'
 */
comments.find([{
  key: 'by',
  operation: '===',
  comparison: 'me'
}])
// Or with the QueryBuilder:
comments.find(QueryBuilder.where('by', '===', 'me'))

/**
 * Find a document by it's id
 */
comments.id('50d4047b-6c6d-47e8-b487-42ca2a7a4c00')

Full example:

import { DB } from 'memsdb'

const db = new DB('My DB Name')

const comments = new DBCollection(db, {
  name: 'comments',
  structure: {
    text: '',
    by: '',
    parent: '',
  },
})

comments.insertOne({
  text: `My fantastic comment`,
  by: 'me'
})

const docs = comments.find([{
  key: 'by',
  operation: '===',
  comparison: 'me'
}])
// Or with the QueryBuilder:
const docs = comments.find(QueryBuilder.where('by', '===', 'me'))

// docs should contain the one comment we added

The query language is still rather simple but can support the operators: < | > | <= | >= | === | || | includes | isContainedIn | hasAllOf | all>than | all<than | all>=to | all<=to | all===to | some>than | some<than | some>=to | some<=to | some===to - where || runs an OR using an array of Query objects (supports multi-level ORing). Additionally, you can use the length of an array as the "key" to compare values against. If you for example had the following document:

{
  "friends": [
    // ...
  ]
}

You would be able to query against the length of that array like so:

users.find(QueryBuilder.where('friends.length', '===', 0))

An example query object of:

const query = {
  key: 'by',
  operation: '===',
  comparison: 'me'
}
// Or with the QueryBuilder:
const query = QueryBuilder.where('by', '===', 'me')

should be read as document.data.by (key to get value of) '===' 'me' (value)

The some===to key (some*, all*) can be used to find similarities between different arrays

all===to makes sure that all of the specified key is equal to the comparison. This would usually be useful with query keys that specify an array with objects in it like the following: folder.children.[].exists. In the previous example you'd use all===to to make sure that all child folders of the first level have the property exists and that it's equal to whatever comparison value you set in the query. Eg.:

const query = {
  key: 'folder.children.[].exists',
  operation: 'all===to',
  comparison: true
}
// OR using the query builder:
const query = QueryBuilder
  .where('folder.children.[].exists', 'all===to', true)

Other functions on the database, collections, and documents can be read in the documentation

Readme

Keywords

Package Sidebar

Install

npm i memsdb

Weekly Downloads

1

Version

0.11.1

License

SEE LICENSE IN LICENSE

Unpacked Size

271 kB

Total Files

123

Last publish

Collaborators

  • brocococonut