BrowseDB is a javascript clientside library for managing localStorage data in mongoose style.
GPL-3.0 License
BrowseDB is a JavaScript clientside library for managing localStorage data in mongoose style. BrowseDB provides its users, methods for managing localStorage data efficiently. It can be used for simple CRUD operations. BrowseDB's data is stored in string format but all data operations are done in JSON format.
Collections
- All BrowseDB instances are called collections, as they are just collections(arrays) of objects(documents).
Documents
- As you might have guessed, are simply objects.
Schema
- A schema is simply an object that describes the expected structure or blueprint of documents of a particular collection. This could be used for some sort of type checking. It is optional.
First, to use browsedb. Include/import browsedb, then create an instance of browsedb. Example
import BrowseDB from "browsedb";
const schema = {
text: {
type: String,
required: true
},
done: Boolean,
date: {
type: Number,
required: true,
default: Date.now
}
};
const todos = new BrowseDB("todos", schema);
create
- Used for creating documents. It accepts an object containing required data for object creation and returns a copy of the new document inserted into localStorage with its ID. BrowseDB generates a unique ID for the newly inserted document. todos.create({ text: "Learn BrowseDB", done: false });
find
- For finding documents. It accepts two optional parameters: query and key. query is used in similar fashion to 'WHERE' in SQL like databases, While key is used for filtering results. Example todos.find(); // Returns all documents
todos.find({}) // Returns all documents
todos.find({ done: true }); // Returns all documents where done equal true
todos.find({ done: false }, {done: 0, id: 0}); // Returns documents where done equal false but doesn't show done and id's of each document.
findById
- For finding documents based on id. It accepts one parameter: id. id(string) represents id of a document. It returns a document with the passed id. todos.findById("zUVxUed827"); // Returns Document with id 'zUVxUed827'
update
- For updating data in document(s). It accepts two parameters: query and data. Here, query is used as 'WHERE' would be used Relational databases, while data represents the new data to be put into selected documents. The below example will find all documents where done = false, and update the text field. todos.update({done: false}, {text: "Todo is not yet done"});
updateById
- For updating data in a document based on id. It finds a document by id, updates its content and returns the updated content. It accepts two parameters: id and data.
updateAll
- For updating all documents data at once. It accepts one parameter: data.
delete
- Finds and deletes documents. Accepts one parameter: query - Where the delete operation should take place.
deleteById
- Finds, deletes and returns a document with id passed to it. Accepts one parameter id.
deleteAll
- Deletes all documents in a collection.
remove
- This function is a bit similar in behaviour to delete
.
🚀CDN Link https://unpkg.com/[email protected]/browsedb.min.js
HTML Tag
<script src="https://unpkg.com/[email protected]/browsedb.min.js"><script>
npm install browsedb
Like mongoose, BrowseDB supports schema. But in BrowseDB uses the builtin types available to javascript for type checking. Hence, Date
isn't a type as it produces a number.
But there is a work around for this. Instead of:
{
detail: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
}
One should do:
{
detail: {
type: String,
required: true
},
date: {
type: Number,
default: Date.now
}
}
Please star this repository if this project helped you!
Please read the CONTRIBUTING.md file to see how to contribute.
Browsedb works across all browsers that support localStorage. You could check for compatibility here:
Copyright (c) Spiff Jekey-Green