-
Notifications
You must be signed in to change notification settings - Fork 7
- DB
-
Class representing the DB Client Interface.
- Insert
-
Class representing the DB Insert Interface.
- Get
-
Class representing the DB Get Interface.
- Update
-
Class representing the DB Update Interface.
- Delete
-
Class representing the DB Delete Interface.
- Aggregate
-
Class representing the DB Aggregate Interface.
- LiveQuery
-
The LiveQuery Interface.
Class representing the DB Client Interface.
Kind: global class
-
DB
- new DB(appId, url, options, db, realTime)
-
.get(collection) ⇒
Get
-
.getOne(collection) ⇒
Get
-
.insert(collection) ⇒
Insert
-
.update(collection) ⇒
Update
-
.updateOne(collection) ⇒
Update
-
.upsert(collection) ⇒
Update
-
.delete(collection) ⇒
Delete
-
.deleteOne(collection) ⇒
Delete
-
.aggr(collection) ⇒
Delete
-
.liveQuery(collection) ⇒
LiveQuery
-
.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 DB Client Interface.
Param | Type |
---|---|
appId | string |
url | string |
options | Object |
db | string |
realTime | Object |
Example
import { API } from 'space-api';
const api = new API('my-project', 'http://localhost:4122');
const db = api.DB("mongo");
dB.get(collection) ⇒ Get
Returns a DB Get Object
Kind: instance method of DB
Returns: Get
- DB Get Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to query documents. |
dB.getOne(collection) ⇒ Get
Returns a DB Get Object to get one particular object
Kind: instance method of DB
Returns: Get
- DB Get Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to query documents. |
dB.insert(collection) ⇒ Insert
Returns a DB Insert Object
Kind: instance method of DB
Returns: Insert
- DB Insert Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to insert documents. |
dB.update(collection) ⇒ Update
Returns a DB Update Object to update all matching documents
Kind: instance method of DB
Returns: Update
- DB Update Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to update documents. |
dB.updateOne(collection) ⇒ Update
Returns a DB Update Object to update a particular document
Kind: instance method of DB
Returns: Update
- DB Update Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to update document. |
dB.upsert(collection) ⇒ Update
Returns a DB Update Object to upsert a particular document
Kind: instance method of DB
Returns: Update
- DB Update Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to update document. |
dB.delete(collection) ⇒ Delete
Returns a DB Delete Object
Kind: instance method of DB
Returns: Delete
- DB Insert Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to delete documents in. |
dB.deleteOne(collection) ⇒ Delete
Returns a DB Delete Object to delete a particular document
Kind: instance method of DB
Returns: Delete
- DB Delete Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to delete document. |
dB.aggr(collection) ⇒ Delete
Returns a DB Aggregate Object
Kind: instance method of DB
Returns: Delete
- DB Insert Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to aggregate documents in. |
dB.liveQuery(collection) ⇒ LiveQuery
Returns a LiveQuery Object
Kind: instance method of DB
Returns: LiveQuery
- LiveQuery Object
Param | Type | Description |
---|---|---|
collection | string |
The collection to query documents. |
Example
const onSnapshot = (snapshot, type, changedDoc) => {
console.log(type, snapshot, changedDoc)
}
const onError = (err) => {
console.log('Operation failed:', err)
}
let subscription = db.liveQuery('posts').where({}).subscribe(onSnapshot, onError)
// Unsubscribe to clean up
subscription.unsubscribe()
Fetches the user profile
Kind: instance method of DB
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 DB
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 DB
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
});
dB.signIn(email, pass) ⇒ Promise.<AuthResponse>
Sends a sign in query to the server
Kind: instance method of DB
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
});
dB.signUp(email, name, pass, role) ⇒ Promise.<AuthResponse>
Sends a sign up query to the server
Kind: instance method of DB
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 DB Insert Interface.
Kind: global class
-
Insert
- new Insert(appId, collection, url, options, db)
.one(doc) ⇒Promise
.all(docs) ⇒Promise
-
.apply(docs) ⇒
Promise
- .docs(docs)
- .doc(doc)
Create an instance of the DB Insert Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
db | string |
Example
import { API, cond, or, and } from 'space-api';
const api = new API('my-project', 'http://localhost:4122');
const db = api.DB("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
});
Deprecated
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 => ...)
Deprecated
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 => ...)
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').docs(docs).apply().then(res => ...)
Accepts the documents to be inserted.
Kind: instance method of Insert
Param | Type | Description |
---|---|---|
docs | Array.<Object> |
The documents to be inserted. |
Accepts the document to be inserted.
Kind: instance method of Insert
Param | Type | Description |
---|---|---|
doc | Array.<Object> |
The document to be inserted. |
Class representing the DB Get Interface.
Kind: global class
-
Get
- new Get(appId, collection, url, options, db, op)
- .where(...conditions)
- .select(select)
- .sort(...array)
- .skip(num)
- .limit(num)
.one() ⇒Promise
.all() ⇒Promise
-
.apply() ⇒
Promise
.distinct() ⇒Promise
.count()
Create an instance of the DB Get Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
db | string |
op | string |
Example
import { API, cond, or, and } from 'space-api';
const api = new API('my-project', 'http://localhost:4122');
const db = api.DB("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 sort result by.
Kind: instance method of Get
Param | Type | Description |
---|---|---|
...array | string |
The fields to sort result by. |
Example
// Given query will sort 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 => ...)
Deprecated
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 => ...)
Deprecated
Makes the query to return 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 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').apply().then(res => ...)
Deprecated
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 => ...)
Deprecated
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 DB Update Interface.
Kind: global class
Create an instance of the DB Update Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
db | string |
op | string |
Example
import { API, cond, or, and } from 'space-api';
const api = new API('my-project', 'http://localhost:4122');
const db = api.DB("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 |
---|---|---|
...values | string |
The 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 |
---|---|---|
...values | string |
The fields to set. |
Example
db.update('posts').currentDate('lastModified').all().then(res => ...)
Deprecated
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
Deprecated
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 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
Returns: Promise
- Returns a promise containing response from server
Class representing the DB Delete Interface.
Kind: global class
-
Delete
- new Delete(appId, collection, url, options, db, op)
- .where(...conditions)
.one() ⇒Promise
.all() ⇒Promise
-
.apply() ⇒
Promise
Create an instance of the DB Delete Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
db | string |
op | string |
Example
import { API, cond, or, and } from 'space-api';
const api = new API('my-project', 'localhost:4122');
const db = api.DB("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. |
Deprecated
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 => ...)
Deprecated
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 => ...)
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').apply().then(res => ...)
Class representing the DB Aggregate Interface.
Kind: global class
-
Aggregate
- new Aggregate(appId, collection, url, options, db, op)
- .pipe(pipeObj)
.one() ⇒Promise
.all() ⇒Promise
-
.apply() ⇒
Promise
Create an instance of the DB Aggregate Interface.
Param | Type |
---|---|
appId | string |
collection | string |
url | string |
options | Object |
db | string |
op | string |
Example
import { API, cond, or, and } from 'space-api';
const api = new API('my-project', 'http://localhost:4122');
const db = api.DB("mongo");
const pipe = [
{ $match: { status: 'A' } },
{ $group: { _id: '$cust_id', total: { $sum: '$amount' } } }
]
db.aggr('posts').pipe(pipe).apply().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. |
Deprecated
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 => ...)
Deprecated
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 => ...)
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([...]).apply().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 LiveQuery Interface.
Kind: global external
See: https://github.com/spaceuptech/space-api-js/wiki/Realtime