1.4.2 • Published 3 years ago
atomese v1.4.2
Atomese
Atomese is an open source javascript & typescript library that provides powerful in-memory database capabilities to both browser and server applications.
Installing
nguyenthanh1205tb@atomese:~$ npm install atomese
nguyenthanh1205tb@atomese:~$ yarn add atomese
Create a DB
Just pass in a JSON array:
import Atomese from "atomese";
var products = Atomese([
{
"name" : "Nathan",
"age" : 22,
"work" : {
"at": "VN",
"major": "IT"
}
},
{
"name" : "Ayu",
"age" : 2,
"work" : {
"at": "VN",
"major": "BABY"
}
},
...
]);
Queries
products.where({ name: "Nathan" }).get();
// get by regex
products.where({ name: /Ayu/ }).get();
var deepQuery = products.where({ work: { at: "VN" } });
deepQuery.first();
deepQuery.last();
Query with Operator !, <, >
products.where({ '!name': 'Nathan' }).get()
products.where({ '!name': 'Nathan', '<age': 5 }).get()
products.where({ '!name': 'Ayu', age: 22 }).get()
products.where({ '!name': /Ayu/ }).get()
products.where({ work: { '!major': 'BABY' }}).get()
Query with callback
product.where(record => record.name !== 'Nathan').get()
product.where(record => record.work.major === 'BABY').get()
Get Options
Options
limit: number
// example
// limit must be bigger than 0, if limit limit = 0, return original array
products.get({ limit: 2 })
products.where({ work: { '!major': 'BABY' }}).get({ limit: 2 })
orderBy: string, 'ASC' | 'DESC' Sort with number, alphabet, date string
// example
products.get({ orderBy: ['name', 'DESC'] })
/**
* [
* { name: 'Ayu', ... },
* { name: 'Nathan' , ...}
* ]
* /
Update and Delete record
// Update record
products.where({ work: { major: "IT" } }).set({ name: "Vy" });
// => [ { name: "Vy", age: 22, work: { at: "VN", major: "IT" } }, ... ]
// Delete record
products.where({ name: /Nathan/ }).drop();
// => [ name: "Ayu", age: 2, work: { at: "VN", major: "BABY" } ]
// Delete all
products.drop(); // => []
Add new record
// default: end
// Push record at start of array db
products.push({ name: 'Mary', age: 47, address: 'HCM' }, 'start')
// push record at start of index in array db
products.where({ age: 2 }).push({ name: 'Mary', age: 47, address: 'HCM' }, 'start')
// push record at end of index in array db
products.where({ age: 2 }).push({ name: 'Mary', age: 47, address: 'HCM' }, 'end')
// OR
products.where({ age: 2 }).push({ name: 'Mary', age: 47, address: 'HCM' })