-
Notifications
You must be signed in to change notification settings - Fork 2
Home
MongOGX is a simple JavaScript based document store/database inspired by MongoDB. It supports local storage and application data storage (via Cordova - cordova-plugin-file) storage modes. It also supports encryption (via code.google.com/p/crypto-js - added to this repo).
insert
insertOne
insertMany
update
updateOne
updateMany
replaceOne
deleteOne
deleteMany
find
toArray
toJSON
eq
neq
gt
gte
lt
lte
in
nin
text
regex
mod
all
size
elemMatch [no support]
currentDate
inc
min
max
mul
rename
set
setOnInsert [no support]
unset
let mongogx = new OGX.Mongogx(_SELECTED_DATABASE_, _SELECTED_COLLECTION_, _OPTIONS_);
//_SELECTED_DATABASE_, optional, the database to select upon start, defaults to null
//_SELECTED_COLLECTION_, optional, the collection to select upon start, defaults to null
//_OPTIONS_, optional.
MongOGX will create the database SELECTED_DATABASE if passed and does not exist, as well as the collection SELECTED_COLLECTION.
{
storage: OGX.Mongogx.LOCAL_STORAGE | OGX.Mongogx.APP_STORAGE, //defaults to OGX.Mongogx.LOCAL_STORAGE
format: OGX.Mongogx.FORMAT_ARRAY | OGX.Mongogx.FORMAT_OBJECT, //defaults to OGX.Mongogx.FORMAT_OBJECT
write_concern:{
mode: OGX.Mongogx.WRITE_DIRECT, //Only supported mode for now, default
delay:5 //timeout before operation
}
encryption:false|{scheme:'AES', key:'mysecretkey'} //AES Encyption
}
MongOGX by default output the result of a search (find) as a collection (FORMAT_OBJECT) or as an array (FORMAT_ARRAY)
MongOGX supports AES encryption. To write/read encrypted data, you have to declare the scheme (AES only for now) and the key, such as
{..., encryption:{scheme:'AES', key:'mysecretkey'}}
//Create instance
let mongogx = new OGX.Mongogx();
//Create a db
mongogx.createDatabase('my_project');
//Set as active/current db
mongogx.setDatabase('my_project');
//Create a collection
mongogx.createCollection('users');
//Set as active/current collection
mongogx.setCollection('users');
//Insert a bunch of non normalized documents
mongogx.insert({first_name:'Eric', age:42, sex:'male', location:{state:'ON', city:'Toronto'}, favorites:{meals:[{name:'pizza'}, {name:'pasta'}]}});
mongogx.insert({first_name:'Tania', age:38, sex:'female', location:{state:'ON', city:'Toronto'}});
mongogx.insert({first_name:'Julien', age:45, sex:'male', location:{state:null, city:'Stockholm'}});
mongogx.insert({first_name:'George', age:55, sex:'male', location:{state:'QC', city:'Montreal'}, arts:{martial:['kickboxing', 'wresting']}});
let collection;
//Find all documents given a state
collection = mongogx.find({'location.state':'ON'});
//Find all users older than 20
collection = mongogx.find({age:{$gt:20}});
//Find all males older than 20 living in Toronto
collection = mongogx.find({age:{$gt:20}, sex:'male', 'location.city':'Toronto'});
//Find all users with favorite meal matching pizza
collection = mongogx.find({'favorites.meals.name':'pizza'});
//Specifically target an array to match a property
collection = mongogx.find({'favorites.meals.1.name':'pasta'});
If you are using APP_STORAGE via Cordova, you must pass and wait for a callback fired when the database is ready, such as
function onDbReady(){
//you can now use mongogx
}
let options = {
storage:OGX.Mongogx.APP_STORAGE,
write_concern:{
mode:OGX.Mongogx.WRITE_DIRECT, //Only supported mode for now, default
delay:5 //timeout before operation
},
callback:onDbReady
};
let mongogx = new OGX.Mongogx(null, null, options);
elemMatch
Pipeline/Aggregation
Seperate db/collections files in APP_STORAGE mode + load_concern