Skip to content
Jayesh Choudhary edited this page Nov 1, 2018 · 5 revisions

Classes

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.

Typedefs

User
AuthResponse

External

Monitor

The Monitor Interface.

Mongo

Class representing the MongoDB Client Interface.

Kind: global class

new Mongo(appId, url, options, realTime)

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()

mongo.profile(id) ⇒ Promise

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
});

mongo.editProfile(id, email, name, pass) ⇒ Promise

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
email 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
});

mongo.profiles() ⇒ Promise

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
email 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
email 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
});

Insert

Class representing the MongoDB Insert Interface.

Kind: global class

new Insert(appId, collection, url, options)

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
});

insert.one(doc) ⇒ Promise

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 => ...)

insert.all(docs) ⇒ Promise

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 => ...)

Get

Class representing the MongoDB Get Interface.

Kind: global class

new Get(appId, collection, url, options)

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
});

get.where(...conditions)

Prepares the find query

Kind: instance method of Get

Param Type Description
...conditions Object The condition logic.

get.select(select)

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 => ...)

get.sort(...array)

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 => ...)

get.skip(num)

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 => ...)

get.limit(num)

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 => ...)

get.one() ⇒ Promise

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 => ...)

get.all() ⇒ Promise

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 => ...)

get.distinct() ⇒ Promise

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 => ...)

get.count()

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 => ...)

Update

Class representing the MongoDB Update Interface.

Kind: global class

new Update(appId, collection, url, options)

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
});

update.where(...conditions)

Prepares the find query

Kind: instance method of Update

Param Type Description
...conditions Object The condition logic.

update.set(obj)

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 => ...)

update.push(obj)

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 => ...)

update.remove(...fields)

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 => ...)

update.rename(obj)

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 => ...)

update.inc(obj)

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 => ...)

update.mul(obj)

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 => ...)

update.max(obj)

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 => ...)

update.min(obj)

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 => ...)

update.currentTimestamp(obj)

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 => ...)

update.currentDate(obj)

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 => ...)

update.one() ⇒ Promise

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

update.all() ⇒ Promise

Makes the query to update all documents which matches.

Kind: instance method of Update
Returns: Promise - Returns a promise containing response from server

update.upsert()

Makes the query to update all, else insert a document.

Kind: instance method of Update

Delete

Class representing the MongoDB Delete Interface.

Kind: global class

new Delete(appId, collection, url, options)

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
});

delete.where(...conditions)

Prepares the find query

Kind: instance method of Delete

Param Type Description
...conditions Object The condition logic.

delete.one() ⇒ Promise

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 => ...)

delete.all() ⇒ Promise

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 => ...)

Aggregate

Class representing the MongoDB Delete Interface.

Kind: global class

new Aggregate(appId, collection, url, options)

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
});

aggregate.pipe(pipeObj)

Prepares the Pipe query

Kind: instance method of Aggregate

Param Type Description
pipeObj Array.<Object> The pipeline object.

aggregate.one() ⇒ Promise

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 => ...)

aggregate.all() ⇒ Promise

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 => ...)

User

Kind: global typedef
Properties

Name Type Description
_id string The user's unique id.
email string The user's email id.
name string The user's name.
role string The user's role.

AuthResponse

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.

Monitor

The Monitor Interface.

Kind: global external
See: https://github.com/spaceuptech/space-api-node/wiki/Realtime

Clone this wiki locally