1.0.2 • Published 5 months ago

collis v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

collis

A package used for listening changes in mongodb's collections.

Installation

$ npm install collis

Example

const GetLiveData = require("collis");
const getLiveData = new GetLiveData(collectionInstance);
const stream = getLiveData.listen();
stream.on("create", (data) => {
    console.log("New Document Created :", data);
});
stream.on("delete", ({ _id }) => {
    console.log("Document Deleted Id :", _id);
});
stream.on("update", (data) => {
    console.log("Document Updated :", data);
});
const user1 = getLiveData.addUser("U1");
user1.addDocId("docId", {
    notifyDelete : true,
    notifyUpdate : true
});
user1.addDocFields("docId", ["name", "age", "extra.*"]);

"GetLiveData" instance methods

MethodDescription
listenIt returns an instance of EventEmitter class.This instance will emit changes happened to documents.
disconnectThe stream instance will stop recieving further changes from the collection.
addUser(uid)It takes one parameter uid, which is of type string. It returns an instance of UserInstance class. With the help of this instance, each user can get live document data with custom configurations.
removeUser(userInstance)The user instance is removed from getting collection's documents updates.
getSingleUser(uid)It takes one parameter uid, which is of type string. It returns the instance of UserInstance class based on the uid.
getUsersIt returns an array of all uids linked to GetLiveData class instance.

"UserInstance" instance properties

PropertyDescription
DOC_CREATEThe type of this property is boolean. If true, the user will be notified if a new document is added to the collection.

"UserInstance" instance methods

MethodDescription
addDocId(docId, options = {})It takes 2 parameters, first one is a string, which is the documentId, second one being the fields of documents on which user will get to know about the changes in the document of _id as docId. options parameter is an object having two fields viz. notifyDelete && notifyUpdate, both of them taken boolean values. So, option.notifyUpdate && notifyDelete configures the setting of wether to notify the user about the document's update & delete operation, respectively. If options not provided, both it's field's values is true.
removeDocId(docId)It takes a single parameter of type string. Now, this docId's changes will not be notified to the user anymore.
addDocFields(docId, fields = [])It takes two parameters, second being an optional one. First parameter's type is string. So, it configures the setting of listening to the dedicated fields of the document whose _id is docId.
removeDocFields(docId, fields = [])It takes two parameters, second being an optional one. First parameter's type is string. So, it removes the fields, i.e any changes to those fields in document will not be notified to the user.
getDocFields(docId)The parameter's type is string. It returns an array of string, which are the fields of document associated with an user.
getUid()It returns the uid string which we have provided it in the addUser function.
getDocIds()It returns an array of string. These items are the document ids associated with the user.
getDocFlags(docId)The parameter's type is string. It returns the object { DOC_DELETE : true, DOC_UPDATE : true } associated with the document. It's not neccessary that both the fields are true.
changeUpdateFlag(docId, flag)The parameter's type is string for first & boolean for second. If flag is true, the user will be notified about the changes in the document whose _id is docId.
changeDeleteFlag(docId, flag)The parameter's type is string for first & boolean for second. If flag is true, the user will be notified about the deletion of the document whose _id is docId.