MonguitoDB
Utility to perform CRUD operations over the localStorage, sessionStorage, or any object implementing the Storage Interface defined by the W3C.
This library was inspired by MongoDB, and some of its functions are syntactically similar to how they are in Mongo, with some differences and limitations.
NOTE: This utility works synchronously.
Installation
Download the minified library (which is under 10KB) and include it in your html. Alternatively you can download the development version.
Getting started
Initialize a new database instance with new MonguitoDB(storage, collections) where:
- storage: Is an object providing the storage mechanism. You can send localStorage or sessionStorage if the browser supports HTML5. Send null to use the default mechanism which stores collections in memory. Alternatively, you can send your own object implementing the Storage Interface defined by the W3C.
- collections: Is an array containing the names of the collections to be manipulated (analogous to tables in a relational database). You can send a single name, or an array of names if you want to manipulate several collections.
// An object to perform CRUD operations over the HTML5 localStorage.var db = localStorage "orders"; // An object to perform CRUD operations over the HTML5 sessionStorage.var db = sessionStorage "orders" "users"; // An object to perform CRUD operations over collections stored in memory.var db = null "orders" "users";
Collection
A collection is a set of documents. It is equivalent to a table in the relational world, with the difference that a collection does not enforce a schema.
The following actions can be performed on a Collection: insert, update, remove, find, findOne, get, count.
Collections are initialized by MonguitoDB() constructor, they can't be initialized by yourself.
// The following code shows how to reference collections initialized by// MonguitoDB() constructor.var db = localStoraCoge "orders" "users"; var ordersCollection = dborders;var usersCollection = dbusers;
Document}
Collection.insert(obj) → {Inserts a new object into the collection and returns a reference to the inserted Document.
NOTE: _id property is the document's "primary key" wich is automatically assigned. It can be of two types:
- Auto-numeric: when _id is omitted in the passed-in obj.
- UUID: When _id is set to "uuid" in the passed-in obj.
Parameter | Type | Description |
---|---|---|
obj | object | Document to insert into the collection |
Returns: Document
var db = localStorage "orders"; // case 1) _id will be automatically assigned as an auto-numeric value.var order = dborders;var documentId = order_id; // case 2) _id will be automatically assigned as an UUID.var order = dborders;var documentId = order_id;
Cursor}
Collection.update(query, obj) → {Updates one or several documents in the collection and returns a Cursor containing the updated documents.
NOTE: _id property can't be modified.
Parameter | Type | Description |
---|---|---|
query | object, function | Selection criteria for the update |
obj | object | The modifications to apply (_id is omitted) |
Returns: Cursor
var db = localStorage "orders"; // Updates a single document (that one matching _id = 1). dborders; // Updates several documents (those matching recipient = "Juan"). dborders;
Collection.remove(query)
Removes one or several documents from the collection.
NOTE: If no query is passed-in, all documents in the collection will be removed.
var db = localStorage "orders"; // Removes a single document (that one matching _id = 1).dborders; // Removes several documents (those matching recipient = "Juan").dborders; // Removes all documents in the collection.dborders;
Cursor}
Collection.find(query) → {Retrieves all documents in the collection matching the specified query. If no query is passed-in, all documents within the collection will be returned.
This function returns a Cursor that can be manipulated as an array plus the following actions: update, remove, find, findOne, get, first, last, sort, pretty, count.
Parameter | Type | Description |
---|---|---|
query | object, function | Specifies selection criteria |
Returns: Cursor
var db = localStorage "orders";var orders = dborders; // Each element in the cursor is of type Documentorders; // Applying conditions to retrieve the data.orders = dborders;orders = dborders;orders = dborders; // Sorting the data.orders = dborders;orders = dborders;orders = dborders; // Executing many actions in cascade.var firstOrder = dborders;var lastOrder = dborders; // Printing the whole collection.console;
Document | null}
Collection.findOne(query) → {Returns a Document that satisfies the specified query. If multiple documents satisfy the query, it will be returned the first document found (according to insertion order). If there is no matching document within the collection, it will be returned null.
The following actions can be performed on the returned document: update, remove, pretty.
Parameter | Type | Description |
---|---|---|
query | object, function | Specifies selection criteria |
Returns: Document | null
var db = localStorage "orders";var order = dborders; console; // Prints the document.order; // Updates the document.order; // Removes the document. // Using findOne() with a function criteria.var order = dborders;
Document | null}
Collection.get(documentId) → {Gets the Document that matches the specified _id. If there is no matching document within the collection, it will be returned null.
The following actions can be performed on the returned document: update, remove, pretty.
NOTE: get() is faster than find() and findOne().
Parameter | Type | Description |
---|---|---|
documentId | number, string | Document _id |
Returns: Document | null
var db = localStorage "orders";var order = dborders; console; // Prints the document.order; // Updates the document.order; // Removes the document.
Collection.count() → {number}
Counts the number of documents in the collection.
Returns: number
var db = localStorage "orders";var count = dborders;
Document
A document belongs to a Collection. It is a JSON structure composed by key-value pairs. It can be thought as a "record" belonging to a table in the relational world.
The following actions can be performed on a document: update, remove, pretty.
Documents are retrieved through methods in the Collection class, they can't be initialized by yourself.
var db = localStorage "orders"; // You get a reference to a Document when you insert one.var order = dborders; // You can get a Document with get() if you know its _id.var order = dborders; // Individual elements retrieved from find() are of type Document.var order = dborders0; // By using findOne you can get the first Document retrieved from a query.var order = dborders; // You can also get a reference to a Document by using first() or last().var order = dborders;
Document}
Document.update(obj) → {Updates the document in the storage.
NOTE: _id property can't be modified.
Parameter | Type | Description |
---|---|---|
obj | object | The modifications to apply (_id is omitted) |
Returns: Document
var db = localStorage "orders";var order = dborders; // Update with arguments.order; // Update without arguments.orderstatus = "Delivered";order;
Document.remove()
Removes the document from the storage.
NOTE: Once you call remove() on a document, you can't perform remove() or update() on the document anymore, because an exception will be thrown.
var db = localStorage "orders";var order = dborders; order;
Document.pretty() → {string}
Gets a "pretty" JSON representation of the document.
Returns: string
var db = localStorage "orders";var order = dborders; console;
Cursor
A cursor is a set of documents. It can be manipulated as an array plus the following actions: update, remove, find, findOne, get, first, last, sort, pretty, count.
Cursors are initialized by Collection.find(), they can't be initialized by yourself.
var db = localStorage "orders";var cursor = dborders;
Cursor}
Cursor.update(obj) → {Updates all documents in the cursor (changes are applied both in the cursor and the storage).
NOTE: _id property can't be modified.
Parameter | Type | Description |
---|---|---|
obj | object | The modifications to apply (_id is omitted) |
Returns: Cursor
var db = localStorage "orders";var cursor = dborders; // Updates all orders belonging to "Juan" with status "Delivered".cursor;
Cursor.remove()
Removes all documents in the cursor (changes are applied both in the cursor and the storage).
NOTE: Once you call remove() on a cursor the cursor will be empty.
var db = localStorage "orders";var cursor = dborders; // Removes all orders belonging to "Juan".cursor;
Cursor}
Cursor.find(query) → {Retrieves all documents within the cursor that match the specified query.
NOTE: This function creates and returns a new cursor (the original one won't be modified).
Parameter | Type | Description |
---|---|---|
query | object, function | Specifies selection criteria |
Returns: Cursor
var db = localStorage "orders";var cursor = dborders; // Gets all orders belonging to "Juan" with status "Pending".var pending = cursor; // Gets all orders belonging to "Juan" with total >= 1000.var expensive = cursor;
Document | null}
Cursor.findOne(query) → {Returns a Document within the cursor matching the specified query. If multiple documents satisfy the query, it will be returned the first document found. If there is no matching document within the cursor, it will be returned null.
Parameter | Type | Description |
---|---|---|
query | object, function | Specifies selection criteria |
Returns: Document | null
var db = localStorage "orders";var cursor = dborders; // Gets the order belonging to "Juan" with number "107-1".var order = cursor; // Another way to do the same above.var order = cursor;
Document | null}
Cursor.get(documentId) → {Gets the Document within the cursor matching the specified _id. If there is no matching document within the cursor, it will be returned null.
Parameter | Type | Description |
---|---|---|
documentId | number, string | Document _id |
Returns: Document | null
var db = localStorage "orders";var cursor = dborders; // Gets the order belonging to "Juan" with _id = 1.var order = cursor;
Document | null}
Cursor.first() → {Returns the first Document within the cursor. If the cursor is empty, it will be returned null.
Returns: Document | null
var db = localStorage "orders";var cursor = dborders; // Gets the first order belonging to "Juan".var order = cursor;
Document | null}
Cursor.last() → {Returns the last Document within the cursor. If the cursor is empty, it will be returned null.
Returns: Document | null
var db = localStorage "orders";var cursor = dborders; // Gets the last order belonging to "Juan".var order = cursor;
Cursor}
Cursor.sort(sortExpression) → {Sorts the cursor and returns a reference to the new sorted cursor.
NOTE: This function creates and returns a new cursor (the original one won't be modified).
Parameter | Type | Description |
---|---|---|
sortExpression | string | Sort expression (You can use ASC or DESC to specify sort direction |
Returns: Cursor
var db = localStorage "orders";var cursor = dborders; // Sorts the documents by seller (Default sort direction is ascending).var orders = cursor; // Sorts the documents by seller and total.var orders = cursor; // Sorts the documents by seller (ascending) and total (descending).var orders = cursor;
Cursor.pretty() → {string}
Gets a "pretty" JSON representation of the cursor.
Returns: string
var db = localStorage "orders";var cursor = dborders; // Prints all orders belonging to "Juan".console;
Cursor.count() → {number}
Counts the number of documents within the cursor.
Returns: number
var db = localStorage "orders";var cursor = dborders; // Counts the number of orders belonging to "Juan"var count = cursor;
TODO List
- Full demo page
- Pagination
- Import data
- Agregation framework
- Full text search
- Replication
Creator
Juan Cuartas
Copyright and license
Code and documentation released under the MIT license