CompassDB is an open soruce ORM for [Gaia] (https://github.com/blockstack/gaia) storage.
It provides an abstraction of a database over the file-based storage system provided by Gaia. It’s compatible with the Blockstack ecosystem which leverages the Gaia storage, to provide database methods for developers.
To get started,
npm install compass-db
Or you can download the compassDB-min-js from here.
// Create reliable connection with CompassDB
const compassdb = new CompassDB();
// Creating a collection
const collection = compassdb.newCollectionInstance(COLLECTION_NAME, true);
collection.createCollection()
.then((successResponse) => {
// Promise Resolved
.catch((failureResponse) => {
// Promise Rejected
});
});
CompassDB uses abstraction concepts to render the database and create a relationship with the actual Gaia storage.
The two main components are:
-
Documents: These are the basic structural unit of the database. Every document contains a group of key-value pairs. These are used to identify and manage request of files in the storage. A basic constructor declaration is done with
constructor(collectionName, encrypt_decrypt).
-
Collections Collections are described as a set of Documents. Every Collection instance contains the name of all the Documents within that set and their status.
To setup a new connection, we must first create an instance of CompassDB followed by creating a new collection for usage(as shown in the example previously).
- createCollection: Creates a new collection.
- dropCollection: Drops an existing collection
- insert: inserts value within a document
- update: updates document with help of Id reference
- findOne: Finds document within collection
- findAll: Finds all documents with certain properties within a collection
- deleteOne: Deletes one document from the collection
- deleteAll: Deletes all documents with certain properties within a collection
-
successResponse: Status code: 200, Status text:
SUCCESS
, Description and Payload -
failureResponse: Status code: 400, Status text:
FAILURE
, Description
.createCollection()
creates a new collection. It fetches the list of existing collections and verifies if the requested collection exists or not.
No arguments; But to instantiate a Collection object, collectionName, encrypt_decrypt these two infomration are required.
Promise.
const collection = compassdb.newCollectionInstance(COLLECTION_NAME, true);
collection.createCollection()
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.dropCollection()
implements a soft delete of the collection. It changes the activeState
to false.
No arguments;
Promise.
collection.dropCollection()
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.insert(newDocument)
puts a document into the collection.
newDocument
, reference to the document that is to be pushed into the collection.
Promise.
If successful then will return the inserted Document Object
in successResponse.payload
collection.insert(newDocument)
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.update(id,queries)
is used for updating documents already inserted in a collection.
id
and query
.
The id is used to find the particular document in the collection and after that, the requested queries are processed if eligible.
Promise.
If successful then will return the updated Document Object
in successResponse.payload
const id = 1;
collection.update(id, { 'title' : 'CompassDB', 'note' : 'Most promising ORM' })
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.findOne(queries)
fetches the first document in the collection that contains the given queries.
query
.
The search result brings the first document that matches the description of the key-value pair of the queries. If not found, it gives a failureResponse
in return.
Promise
If successful then will return the Document Object
in successResponse.payload
collection.findOne({ 'title' : 'CompassDB', 'note' : 'Most promising ORM' })
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.findAll(queries)
fetches all the documents in the collection that contains the given queries as an array of object.
query
.
The search result brings all documents that match the description of the key-value pair of the queries. If not a single document found, it gives a failureResponse
in return.
Promise
If successful then will return an array of Document Objects
in successResponse.payload
collection.findAll({ 'title' : 'CompassDB', 'note' : 'Most promising ORM' })
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.deleteOne(query)
removes the first document that is found matching the query. Here, the isActive
state of the document is changed to false.
query
deletes the first document that matches the description of the key-value pair of the queries. If not found, it gives a failureResponse
in return.
Promise.
If successful then will return the deleted Document Object
in successResponse.payload
collection.deleteOne({ 'title' : 'another', 'note' : 'test' })
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});
.deleteAll(query)
removes the all the documents that match the query. Here, the isActive
state of the documents are changed to false.
query
deletes all documents that match the description of the key-value pair of the queries. If not found, it gives a failureResponse
in return.
Promise.
If successful then will return an array of deleted Document Objects
in successResponse.payload
collection.deleteAll({ 'title' : 'check', 'note' : 'check' })
.then((successResponse) => {
// Promise Resolved
})
.catch((failureResponse) => {
// Promise Rejected
});