ZetabaseJS
A NoSQL database for quick deployment
Get started
Installation
npm install --save zetabasejs
Hello World
const Zetabase = const db = "database.json" //Specify a filename. dblet data = dbconsole // Read DB: Hello World !!
Structure
A data node contains 2 parts, data
& childNodes
.
Write
operation will update the value in data
, which can be retrieved by Read
function.
Append
operation will update the value in childNodes
, which can be retrieved by List
function.
Operations
All operations are synchronized.
Write
db;let retrieve = db;console //The data is retrieved already. db; //The missing path will be created automatically. db //Just write the object into db.
Read
let balance = db //Retrieve the value in data part.Output: balance: 3515 timestamp: 1565485663 // The value will be null if empty.
Append
dbdbdbdb //The objects will be added to the childNodes with an unique key.
List
//List all appended objects.db Product: "productCode":"#704FV4""price":30"timestamp":1565485663"key":"2b0941aab466211b1c0752719c3d693b"Key: 2b0941aab466211b1c0752719c3d693b Product: "productCode":"#604PG4""price":209"timestamp":1565485703"key":"0c0381d3010fa7197d1658733cf2002a"Key: 0c0381d3010fa7197d1658733cf2002a ... //Nested list. let receiptKey = db dbdbdbdb db
Wipe
//The empty folder will be removed automatically. dbdb// In memory "data": null "childNodes": "A": "id": "A" "data": null "childNodes": "B": "id": "B" "data": "Content in B" "childNodes": "C": "id": "C" "data": null "childNodes": "D": "id": "D" "data": "Content in D" "childNodes": {} db// In memory, folder C is removed automatically "data": null "childNodes": "A": "id": "A" "data": null "childNodes": "B": "id": "B" "data": "Content in B" "childNodes": {}
Query
let aliceId = dblet katyId = dblet bobId = dblet tomId = db let waterId = dblet breadId = db db db // Query all female/not gender specified customerslet customers = db// Output// [ { name: 'Alice', gender: 'F' },// { name: 'Katy', gender: 'F' },// { name: 'Tom', gender: '-' } ] // Query all transactions for Alice and Katylet transactions = db// Output// [ { customerId: '6a7b5c84159b2e05837a2c4a3fcf5a6b',// products: [ [Object], [Object] ],// key: '9d705f589d16f6e69067679f479bab2f' } ] // Query all transactions that contains water.transactions = dbtransactions // Output// Customer: Alice// Product: [// {// "productCode": "17978e7bdb1b9eb1deffa6e37883c34a8DEbA",// "quantity": 10// },// {// "productCode": "fd08c2ebcef2c7243017a058ad650f898DEbA",// "quantity": 6// }// ]
Monitor
Monitor changes in that level only.
// There are 3 action codes, WRITE, APPEND and WIPE dbdb let orderKey = db // Output// An order is made. // orderId:, #o001,// totalAmount:, 100 db // Output// This order is cancelled. // orderId:, #o001,// totalAmount:, 100 db; // Output// Supermarket's status is changed: Closed
Modules
ORM
const Zetabase = const crypto = // Require the Model class, which contains 3 properties// __ctime__: creation time// __mtime__: last modify time// __key__: entity's keyconst Model = // Pass option { orm: true } to Zetabase constructor// Default is falseconst db = './ormDemo.json' orm: true // { super thisname = name; thisproductCode = productCode; thisprice = price; } { console } dbfor let i = 0; i < 5; i++ let name = en: crypto zh: crypto name crypto Math + 1 let options = filters: v > 50 //Filter fields: '{__key__, name, name.zh, productCode, price}' // Select the response value, default is all. get: false //Default is true, which return an instance. Set to false to return the object contains the selected fields only. let prods = Product; //Pass options object as params // prods __key__: '350295370a6b98fb63cc0c8495f00984' name: en: 'b3ca' zh: 'c3700382' 'name.zh': 'c3700382' productCode: '2bc6fb0714f2585e' price: 95 __key__: 'eba710dc0a6185b6512ba0dfad2d75a3' name: en: '55ab' zh: '206a6a6e' 'name.zh': '206a6a6e' productCode: '52ac26b1796b657d' price: 93 let firstProd = prods0 //The query result is an object.firstProddescription // firstProdProduct __ctime__: 1574498716475000 __mtime__: 1574498716475000 __key__: '350295370a6b98fb63cc0c8495f00984' name: en: 'b3ca' zh: 'c3700382' productCode: '2bc6fb0714f2585e' price: 95 //Output Product name: [object Object], Product Code: 8b81f7f2d494d061, Price: 90