Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Latest commit

 

History

History
259 lines (211 loc) · 5.62 KB

README.md

File metadata and controls

259 lines (211 loc) · 5.62 KB

Bongo.js (deprecated)

Bongo.js is a JavaScript library for storing and querying structured data on the browser. Lots of it.

It is built on IndexedDB.

It is tested in Chrome 27, Chrome 29, Firefox 22, and Internet Explorer 10.

Features

  • Insert, save, remove, find, findOne
  • Comparison query operators ($all, $lt, $lte, $gt, $gte, $in, $nin)
  • Mongo-esque key generator (on _id)
  • Pick, limit, skip
  • Automatic versioning and database upgrades
  • Custom filters
  • 11k

Get started

Install

Use Bower:

bower install bongo.js

And include the file in your app:

<script src='/components/bongo.js/dist/bongo.min.js'></script>

You can also download the compressed, production version or the uncompressed, development version.

Define database

bongo.db({
  name: 'acme',
  collections: ["users"]
});

insert

bongo.db('acme').collection('users').insert({
  name: "John Doe",
  email: "[email protected]"
},function(error,id) {
  if(!error) {
    // success
  }
}
});

save

bongo.db('acme').collection('users').save({
  _id: "[key]", //optional
  name: "Jane Doe",
  email: "[email protected]",
  pets: 3
});

get

bongo.db('acme').collection('users').get("[key]", function(error,data) {
  if(!error) {
    //success
  }
});

find

bongo.db('acme').collection('users').find({
  name: "John Doe"
}).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

Regular expressions

bongo.db('acme').collection('users').find({
  name: /john/i
}).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

Comparison query operators

bongo.db('acme').collection('users').find({
  pets: {$gt: 2}
}).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

$all, $lt, $lte, $gt, $gte, $in, $nin are supported.

findOne

bongo.db('acme').collection('users').findOne({
  name: "John Doe"
}),function(error,record) {
  if(!error) {
    //success
  }
});

filter

bongo.db('acme').collection('users').filter(function(doc) {
  return doc.age > 30;
}).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

fields

bongo.db('acme').collection('users').find({
  age: {$gt: 30}
},{
  name: 1,
  email: 1
}).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

or

bongo.db('acme').collection('users').find({
  age: {$gt: 30}
}).pick(['name','email']).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

limit

bongo.db('acme').collection('users').find({
  age: {$gt: 30}
}).limit(5).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

skip

bongo.db('acme').collection('users').find({
  age: {$gt: 30}
}).skip(5).limit(5).toArray(function(error,results) {
  if(!error) {
    //success
  }
});

remove

bongo.db('acme').collection('users').remove({
  email: "[email protected]"
}, function(error, data) {
  if(!error) {
    //success
  }
})

Or just use the key:

bongo.db('acme').collection('users').remove("[key]", function(error, data) {
  if(!error) {
    //success
  }
});

Delete the database

bongo.delete(function(error) {
  if(!error) {
    // Success
  }
});

Check for support

if(bongo.supported) {
  // Woo hoo!
}

Known issue

  • After many(?) database version upgrades, sometimes Chrome needs to be restarted.
  • Redefining the same database multiple times in the same pageload is problematic.

License

MIT. Forking is form of flattery.

See also