config object {storage: IStorageDriver}
whether or not to generate IDs automatically
A HashMap of all the indices keyed by the fieldName.
StorageDriver that is used for this Datastore
Count documents
Create Unique ID that contains timestamp
Return fill provided promise array with promises from the storage driver
Ensure an index on the datastore
Example:
return Users.ensureIndex({fieldName: "username", unique: true});
Find documents
Examples:
Users.find()
.sort({age: -1})
.skip(1)
.limit(10)
.exec()
.then((users) => console.log(users))
.catch((e) => console.log(e));
Users.find({$or: [{name: "a"}, {name: "b"}]})
.then((docs) => console.log(docs.length)) // 2 if unique
.catch((e) => console.log(e));
return Users.find({age: {$gt: 0, $lte: 27, $ne: 5}});
Get Document by ID/s Used internally
sort limit skip options
ID or Array of IDs
Get Date from ID ... do we need this on the dataStore?
Example:
let id = "UE9UQVJWd0JBQUE9cmZ4Y2MxVzNlOFk9TXV4dmJ0WU5JUFk9d0FkMW1oSHY2SWs9"; // an ID
Users.getIdDate(id); // date object -> 2017-05-26T17:14:48.252Z
the _id
of the document to get date of
Retrieve the indices of this datastore.
Example:
Users.getIndices()
.then((indices) => {
let usernameIndex = indices.get("username");
if(usernameIndex) {
return usernameIndex.toJSON(); // a method on index bTree
}
}); // no reject, will always resolve
Insert a single document and insert any indices of document into its respective binary tree.
Users.insert({name: "xyz", age: 30})
.then((doc) => console.log(doc)) // {_id: "...", name: "xyz", age: 30}
.catch((e) => console.log(e));
document to insert
Insert a stored index into the index of this datastore
the index fieldName
the key value pair obj
Removes document/s by query - uses find method to retrieve ids. multi always
Remove index will delete the index from the Map which also holds the Btree of the indices. If you need to remove the index from the persisted version in from the storage driver, call the removeIndex from the storage driver from a different source. This method should not assume you saved the index.
Field that needs index removed
Save the index currently in memory to the persisted version if need be through the storage driver.
Search for IDs, chooses best strategy. Handles logical operators($or, $and) Returns array of IDs Used Internally
element name or query start $or/$and
string,number,date,null - or [{ field: value }, { field: value }]
Search collection by field, essentially a collection scan Returns array of IDs
field to search
value to search by
Search for IDs, chooses best strategy, preferring to search indices if they exist for the given field. Returns array of IDs
Search indices by field Example 1: dbName.searchIndices("fieldName", "1234"); will return the value id of that key as an array of one element. Example 2: dbName.searchIndices("fieldName", { $gte: 1, $lte 10, $ne: 3 } will return an array of ids from the given range. Returns array of IDs
field to search
value to search by
Update document/s
Examples: -> lets say two users {name: "bob", age: 1}, {name: "slydel", age: 45, companies: {name: "Initech", alternate: "Intertrode"}}
Users.update({name: "bob"},{$rename: {name: "first"}, $set: {job: "consultant"}},
{returnUpdatedDocs: true})
.then((docs) => console.log(docs[0]) // {_id: "...", first: "bob", age: 1, job: "consultant"}
.catch();
Users.update({first: "bob"},{$unset: {"companies.alternate": ""}, $inc: {age: -44},
$mul: {age: 5}}, {returnUpdatedDocs: true})
.then((docs) => console.log(docs[0]) // {_id: "...", name: "bob", age: 5, companies: {name: "Initech"}}
.catch();
Users.update({name: "Charles", age: 22}, {$inc: {age: 4}}, {upser: true, returnUpdatedDocs: true}})
.then((docs) => console.log(docs[0]) // {_id: "...", name: "Charles", age: 26}
.catch();
Users.update({age: {$gt: 0}},{$unset: {age: "", name: ""}},{ multi: true, returnUpdatedDocs: true})
.then((docs) => console.log(docs)) // {_id: ".."}, {_id: ".."}, {_id: "..", companies: {name: "Initech"}}
.catch();
query document/s to update
update operation, either a new doc or modifies portions of a document(eg. $set
)
{ fieldName, unique?, compareKeys?, checkKeyEquality? }
Actual method to update the documents and associated indices
an array of documents to be updated
reference to promise array to be resolved later
reference to promise array to be resolved later
operation query from update method
array of operators passed by reference from the update method
Each key from the query. could be less than operators array length
passed reference to reject from update method.
Generated using TypeDoc
Datastore class
Example:
const UserStorage = new yourStorageClass("users"); const Users = new Datastore({storage: UserStorage});
Creates a new Datastore using a specified storageDriver