-
Notifications
You must be signed in to change notification settings - Fork 0
Mongo
- Mongo
-
Class representing the MongoDB Client Interface.
- Insert
-
Class representing the MongoDB Insert Interface.
- Get
-
Class representing the MongoDB Get Interface.
- Update
-
Class representing the MongoDB Update Interface.
- Delete
-
Class representing the MongoDB Delete Interface.
- Aggregate
-
Class representing the MongoDB Delete Interface.
- Monitor
-
The Monitor Interface.
Class representing the MongoDB Client Interface.
Kind: global class
-
Mongo
- new Mongo(appId, url, options, realTime)
-
.get(collection) ⇒
Get
-
.insert(collection) ⇒
Insert
-
.update(collection) ⇒
Update
-
.delete(collection) ⇒
Delete
-
.aggr(collection) ⇒
Delete
-
.monitor(collection) ⇒
Monitor
-
.profile(id) ⇒
Promise
-
.editProfile(id, email, name, pass) ⇒
Promise
-
.profiles() ⇒
Promise
-
.signIn(email, pass) ⇒
Promise.<AuthResponse>
-
.signUp(email, name, pass, role) ⇒
Promise.<AuthResponse>
Create an instance of the MongoDB Client Interface.
Param | Type |
---|---|
appId | string |
url | string |
options | Object |
realTime | Object |
Example
const { API } = require('space-api-node');
const api = new API('my-project');
const db = api.Mongo();
mongo.get(collection) ⇒ Get
Returns a MongoDB Get Object
Kind: instance method of Mongo
Returns: Get
- MongoDB Get Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to query documents. |
mongo.insert(collection) ⇒ Insert
Returns a MongoDb Insert Object
Kind: instance method of Mongo
Returns: Insert
- MongoDB Insert Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to insert documents. |
mongo.update(collection) ⇒ Update
Returns a MongoDb Update Object
Kind: instance method of Mongo
Returns: Update
- MongoDB Update Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to update documents. |
mongo.delete(collection) ⇒ Delete
Returns a MongoDb Delete Object
Kind: instance method of Mongo
Returns: Delete
- MongoDB Insert Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to delete documents in. |
mongo.aggr(collection) ⇒ Delete
Returns a MongoDb Aggregate Object
Kind: instance method of Mongo
Returns: Delete
- MongoDB Insert Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to delete documents in. |
mongo.monitor(collection) ⇒ Monitor
Returns a Monitor Object
Kind: instance method of Mongo
Returns: Monitor
- Monitor Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to monitor. |
Example
const onSnapshot = (snapshot, type, docs) => {
if (type === 'monitor') {
console.log('Monitored successfully ', snapshot)
return
}
console.log(type, snapshot, docs)
}
const onError = (err) => {
console.log('Monitor error', err)
}
let unsubscribe = db.monitor('posts').where().subscribe(onSnapshot, onError)
unsubscribe()
Fetches the user profile
Kind: instance method of Mongo
Returns: Promise
- Returns a promise containing response from server
Param | Type | Description |
---|---|---|
id | string |
The unique user id |
Example
db.profile(id).then(res => {
if (res.status === 200) {
// res.data.user contains user details
console.log('Response:', res.data.user);
return;
}
// Request failed
}).catch(ex => {
// Exception occured while processing request
});
Updates the user profile
Kind: instance method of Mongo
Returns: Promise
- Return a promise containing response from server
Param | Type | Description |
---|---|---|
id | string |
The unique user id |
string |
The new email id | |
name | string |
The new name |
pass | string |
The new password |
Example
db.editProfile(id, email, name, pass).then(res => {
if (res.status === 200) {
// User account has been updates successfully
return;
}
// Request failed
}).catch(ex => {
// Exception occured while processing request
});
Fetches all the user profiles
Kind: instance method of Mongo
Returns: Promise
- Returns a promise containing response from server
Example
db.profiles().then(res => {
if (res.status === 200) {
// res.data.users contains user details
console.log('Response:', res.data.users);
return;
}
// Request failed
}).catch(ex => {
// Exception occured while processing request
});
mongo.signIn(email, pass) ⇒ Promise.<AuthResponse>
Sends a sign in query to the server
Kind: instance method of Mongo
Returns: Promise.<AuthResponse>
- Returns a promise containing response from server
Param | Type | Description |
---|---|---|
string |
The user's email id. | |
pass | string |
The user's password. |
Example
db.signIn('[email protected]', '1234').then(res => {
if (res.status === 200) {
// Set the token id to enable crud operations
api.setToken(res.data.token)
// res.data contains request payload
console.log('Response:', res.data);
return;
}
// Request failed
}).catch(ex => {
// Exception occured while processing request
});
mongo.signUp(email, name, pass, role) ⇒ Promise.<AuthResponse>
Sends a sign up query to the server
Kind: instance method of Mongo
Returns: Promise.<AuthResponse>
- Returns a promise containing response from server
Param | Type | Description |
---|---|---|
string |
The user's email id. | |
name | string |
The user's name. |
pass | string |
The user's password. |
role | string |
The user's role. |
Example
db.signUp('[email protected]', 'UserName', '1234', 'default').then(res => {
if (res.status === 200) {
// Set the token id to enable crud operations
api.setToken(res.data.token)
// res.data contains request payload
console.log('Response:', res.data);
return;
}
// Request failed
}).catch(ex => {
// Exception occured while processing request
});
Class representing the MongoDB Insert Interface.
Kind: global class
-
Insert
- new Insert(appId, collection, url, options)
-
.one(doc) ⇒
Promise
-
.all(docs) ⇒
Promise
Create an instance of the MongoDB Insert Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
Example
const { API, cond, or, and } = require('space-api-node')
const api = new API('my-project');
const db = api.Mongo();
const doc = { author: 'John', title: 'Title1', _id: 1 };
db.insert('posts').one(doc).then(res => {
if (res.status === 200) {
// Document was inserted successfully
return;
}
}).catch(ex => {
// Exception occured while processing request
});
Makes the query to insert a single document.
Kind: instance method of Insert
Returns: Promise
- Returns a promise containing response from server.
Param | Type | Description |
---|---|---|
doc | Object |
The document to be inserted. |
Example
const doc = { author: 'John', title: 'Title1', _id: 1 };
db.insert('posts').one(doc).then(res => ...)
Makes the query to insert multiple documents.
Kind: instance method of Insert
Returns: Promise
- Returns a promise containing response from server.
Param | Type | Description |
---|---|---|
docs | Array.<Object> |
The documents to be inserted. |
Example
const docs = [{ author: 'John', title: 'Title1', _id: 1 }];
db.insert('posts').all(docs).then(res => ...)
Class representing the MongoDB Get Interface.
Kind: global class
-
Get
- new Get(appId, collection, url, options)
- .where(...conditions)
- .select(select)
- .sort(...array)
- .skip(num)
- .limit(num)
-
.one() ⇒
Promise
-
.all() ⇒
Promise
-
.distinct() ⇒
Promise
- .count()
Create an instance of the MongoDB Get Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
Example
const { API, cond, or, and } = require('space-api-node')
const api = new API('my-project');
const db = api.Mongo();
db.get('posts').where(and(cond('title', '==', 'Title1'))).all().then(res => {
if (res.status === 200) {
// res.data contains the documents returned by the database
console.log('Response:', res.data);
return;
}
}).catch(ex => {
// Exception occured while processing request
});
Prepares the find query
Kind: instance method of Get
Param | Type | Description |
---|---|---|
...conditions | Object |
The condition logic. |
Sets the fields to be selected
Kind: instance method of Get
Param | Type | Description |
---|---|---|
select | Object |
The select object. |
Example
// Given query will only select author and title fields
const select = { author: 1, title: 1 }
db.get('posts').select(select).all().then(res => ...)
Sets the fields to order result by.
Kind: instance method of Get
Param | Type | Description |
---|---|---|
...array | string |
The fields to order result by. |
Example
// Given query will order results first by age (asc) then by age (desc)
db.get('posts').sort('title', '-age').all().then(res => ...)
Sets the number of documents to skip in the array.
Kind: instance method of Get
Param | Type | Description |
---|---|---|
num | number |
The number of documents to skip. |
Example
// Given query will skip the first 10 documents
db.get('posts').skip(10).all().then(res => ...)
Sets the limit on number of documents returned by the query.
Kind: instance method of Get
Param | Type | Description |
---|---|---|
num | number |
The limit on number of documents. |
Example
// Given query will limit the result to 10 documents
db.get('posts').limit(10).all().then(res => ...)
Makes the query to return a single document as an object. If no documents are returned, the status code is 400.
Kind: instance method of Get
Returns: Promise
- Returns a promise containing response from server.
Example
db.get('posts').one().then(res => ...)
Makes the query to return a multiple documents as an array. It is possible for an empty array to be returned.
Kind: instance method of Get
Returns: Promise
- Returns a promise containing response from server.
Example
db.get('posts').all().then(res => ...)
Makes the query to return an array of all the distinct values for the given field. It is possible for an empty array to be returned.
Kind: instance method of Get
Returns: Promise
- Returns a promise containing response from server.
Example
db.get('posts').distinct('category').then(res => ...)
Makes the query to return the count of total number of documents that were queried.
Kind: instance method of Get
Example
// Given query counts the total number of posts in the 'posts' collection
db.get('posts').count().then(res => ...)
Class representing the MongoDB Update Interface.
Kind: global class
Create an instance of the MongoDB Update Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
Example
const { API, cond, or, and } = require('space-api-node')
const api = new API('my-project');
const db = api.Mongo();
db.update('posts').where(and(cond('title', '==', 'Title1'))).set({ title: 'Title2' }).all().then(res => {
if (res.status === 200) {
// The documents were updated successfully
return;
}
}).catch(ex => {
// Exception occured while processing request
});
Prepares the find query
Kind: instance method of Update
Param | Type | Description |
---|---|---|
...conditions | Object |
The condition logic. |
Sets the value of a field in a document.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The Object containing fields to set. |
Example
db.update('posts').set({ author: 'Drake' }).all().then(res => ...)
Adds an item to an array.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The Object containing fields to set. |
Example
db.update('posts').push({ author: 'Drake' }).all().then(res => ...)
Removes the specified field from a document.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
...fields | string |
The fields to remove. |
Example
db.update('posts').remove('age', 'likes').all().then(res => ...)
Renames the specified field.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to rename. |
Example
db.update('posts').rename({ mobile: 'contact' }).all().then(res => ...)
Increments the value of the field by the specified amount.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to increment along with the value. |
Example
// The value of added with 1
db.update('posts').inc({ views: 1 }).all().then(res => ...)
Multiplies the value of the field by the specified amount.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to multiply along with the value. |
Example
// The value of amount will be multiplied by 4
db.update('posts').mul({ amount: 4 }).all().then(res => ...)
Only updates the field if the specified value is greater than the existing field value.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to set. |
Example
db.update('posts').max({ highScore: 1200 }).all().then(res => ...)
Only updates the field if the specified value is lesser than the existing field value.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to set. |
Example
db.update('posts').min({ lowestScore: 300 }).all().then(res => ...)
Sets the value of a field to current timestamp.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to set. |
Example
db.update('posts').currentTimestamp('lastModified').all().then(res => ...)
Sets the value of a field to current date.
Kind: instance method of Update
Param | Type | Description |
---|---|---|
obj | Object |
The object containing fields to set. |
Example
db.update('posts').currentDate('lastModified').all().then(res => ...)
Makes the query to update a single document which matches first.
Kind: instance method of Update
Returns: Promise
- Returns a promise containing response from server
Makes the query to update all documents which matches.
Kind: instance method of Update
Returns: Promise
- Returns a promise containing response from server
Makes the query to update all, else insert a document.
Kind: instance method of Update
Class representing the MongoDB Delete Interface.
Kind: global class
-
Delete
- new Delete(appId, collection, url, options)
- .where(...conditions)
-
.one() ⇒
Promise
-
.all() ⇒
Promise
Create an instance of the MongoDB Delete Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
Example
const { API, cond, or, and } = require('space-api-node')
const api = new API('my-project');
const db = api.Mongo();
db.delete('posts').where(and(cond('title', '==', 'Title1'))).all().then(res => {
if (res.status === 200) {
// The documents were deleted successfully
return;
}
}).catch(ex => {
// Exception occured while processing request
});
Prepares the find query
Kind: instance method of Delete
Param | Type | Description |
---|---|---|
...conditions | Object |
The condition logic. |
Makes the query to delete a single document which matches first.
Kind: instance method of Delete
Returns: Promise
- Returns a promise containing response from server.
Example
db.delete('posts').one().then(res => ...)
Makes the query to delete all the documents which match.
Kind: instance method of Delete
Returns: Promise
- Returns a promise containing response from server.
Example
db.delete('posts').all().then(res => ...)
Class representing the MongoDB Delete Interface.
Kind: global class
-
Aggregate
- new Aggregate(appId, collection, url, options)
- .pipe(pipeObj)
-
.one() ⇒
Promise
-
.all() ⇒
Promise
Create an instance of the MongoDB Delete Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
Example
const { API, cond, or, and } = require('space-api-node')
const api = new API('my-project');
const db = api.Mongo();
const pipe = [
{ $match: { status: 'A' } },
{ $group: { _id: '$cust_id', total: { $sum: '$amount' } } }
]
db.aggr('posts').pipe(pipe).many().then(res => {
if (res.status === 200) {
// res.data contains the documents returned by the database
console.log('Response:', res.data);
return
}
}).catch(ex => {
// Exception occured while processing request
});
Prepares the Pipe query
Kind: instance method of Aggregate
Param | Type | Description |
---|---|---|
pipeObj | Array.<Object> |
The pipeline object. |
Makes the query to return single object.
Kind: instance method of Aggregate
Returns: Promise
- Returns a promise containing response from server.
Example
db.aggr('posts').pipe([...]).one().then(res => ...)
Makes the query to return all objects.
Kind: instance method of Aggregate
Returns: Promise
- Returns a promise containing response from server.
Example
db.aggr('posts').pipe([...]).all().then(res => ...)
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
_id | string |
The user's unique id. |
string |
The user's email id. | |
name | string |
The user's name. |
role | string |
The user's role. |
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
status | number |
The http status code of response. |
data | Object |
The response payload. |
data.token | string |
The signed token generated for the user. |
data.user | User |
Information of the user. |
The Monitor Interface.
Kind: global external
See: https://github.com/spaceuptech/space-api-node/wiki/Realtime