A local database implementation using localStorage API
This is a simple wrapper for javascript's localStorage API for a more convenient use.
Installation
yarn add local-db --dev
or
npm install local-db --save-dev
or
<!-- local-db will assign itself to window.LocalDB -->
<script src='https://unpkg.com/local-db'></script>
Usage
import LocalDB from 'local-db'
or
var LocalDB = require('local-db)
then
var usersTable = new LocalDB('users')
Methods
.create(object)
If object has an .id
property, LocalDB will try to update the object in the table with
regarding id.
usersTable.insert({username:'kucukkanat',city:'Izmir'})
returns : the user object created in table with generated ID
.read(query)
Returns an array of the elements matching the query
// Guys you are not allowed to buy cigarettes!
usersTable.query({age:{$lt:{18}}})
returns : the user object created in table with generated ID
.update(query,object)
Updates all objects in table matching the query. The update method does a deep merge
using lodash
's _.merge
method. _.merge
// Moves kucukkanat from Izmir to Amsterdam
usersTable.update({username:'kucukkanat'},{city:'Amsterdam'})
returns : LocalDB instance itself
.delete(query)
Deletes all objects in table matching the query
// Are you not a bit old for skating grandpa ?
usersTable.delete({age:{$gt:70}})
returns : LocalDB instance itself
.drop()
Empties the table
usersTable.drop()
returns : LocalDB instance itself
.on(eventName,fn)
Listens for events : insert
, create
, update
, remove
, delete
, drop
users.on('$insert',(object)=>{
// console.log(object, ' inserted!')
})
users.on('$delete',(object)=>{
// console.log(object, ' deleted!')
})
users.on('$update',(object,nextObject)=>{
console.log(object, ' updated to ',nextObject)
})
returns : LocalDB instance itself
Supported Operators:
See MongoDB's advanced queries for more info.
$in
array value must be $in the given query:
Intersecting two arrays:
//filtered: ['Brazil']table;
Here's another example. This acts more like the $or operator:
table;
$nin
Opposite of $in:
//filtered: ['Haiti','Peru','Chile']table;
$exists
Checks if whether a value exists:
//filtered: ['Craig','Tim']table;
You can also filter out values that don't exist
//filtered: [{ name: 'Craig', city: 'Minneapolis' }]table;
$gte
Checks if a number is >= value:
//filtered: [2, 3]table;
$gt
Checks if a number is > value:
//filtered: [3]table;
$lte
Checks if a number is <= value.
//filtered: [0, 1, 2]table;
$lt
Checks if number is < value.
//filtered: [0, 1]table;
$eq
Checks if query == value. Note that $eq can be omitted. For $eq, and $ne
//filtered: [{ state: 'MN' }]table;
Or:
//filtered: [{ state: 'MN' }]table;
$ne
Checks if query != value.
//filtered: [{ state: 'CA' }, { state: 'WI'}]table;
$mod
Modulus:
//filtered: [300, 600]table;
$all
values must match everything in array:
//filtered: [ { tags: ['books','programming','travel' ]} ]table;
$and
ability to use an array of expressions. All expressions must test true.
//filtered: [ { name: 'Craig', state: 'MN' }] table;
$or
OR array of expressions.
//filtered: [ { name: 'Craig', state: 'MN' }, { name: 'Tim', state: 'MN' }]table;
$nor
opposite of or:
//filtered: [ { name: 'Tim', state: 'MN' }, { name: 'Joe', state: 'CA' }]table;
$size
Matches an array - must match given size:
//filtered: ['food','cooking']table;
$type
Matches a values based on the type
table; //returns single datetable; //returns ['hello world']
$regex
Matches values based on the given regular expression
table; // ["fred", "frost"]table; // ["fred", "frost"]
$where
Matches based on some javascript comparison
table; // ["frank"]table; // ["frank"]
$elemMatch
Matches elements of array
var bills = month: 'july' casts: id: 1 value: 200 id: 2 value: 1000 month: 'august' casts: id: 3 value: 1000 id: 4 value: 4000 ; var result = table; // {month:'august', casts:[{id:2, value: 1000},{id: 4, value: 4000}]}
$not
Not expression:
table; //['jake']table; //['tim','jake']
sub object Searching
var people = name: 'craig' address: city: 'Minneapolis' name: 'tim' address: city: 'St. Paul' ; var queried = table; // count = 1 //orvar queried = table;//count = 1