Scorocode.Object
- .Object
- new Object(collName) ⇒
Scorocode.Object - .getById(_id, options) ⇒
promise.Scorocode.Object - .get(key)
- .getFileLink(field) ⇒
String - .uploadFile(field, filename, file, options) ⇒
promise.<String> - .save(options) ⇒
promise.Scorocode.Object - .remove(options) ⇒
promise.{count: Number, docs: Array} - .extend(collName, childObject) ⇒
[Scorocode.Object] - .set(data)
- .push(key, value)
- .pull(key, value)
- .pullAll(key, value)
- .addToSet(key, value)
- .pop(key, pos)
- .inc(key, amount)
- .currentDate()
- .mul(key, number)
- .min()
- .max()
- new Object(collName) ⇒
new Object(collName)
Scorocode.Object represents the application data object and includes methods for handling this data. The constructor creates a minimal basic "wrap" for user data.
| Parameter | Type | Description |
|---|---|---|
| collName | String |
Name of the collection where the object is added |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var questItem = new Scorocode.Object("items");
questItem.set("name", "Water chip").set("relatedquests", ["huNr3L7QDh"]);
questItem.save()
.then((saved) => {
console.log(saved);
})
.catch((error) => {
console.log(error)
});
See.
Exceptions:
Error"Invalid collection name"
{
errCode: 404,
errMsg: 'Invalid collection: \'items\'',
error: true
}
.getById(_id, options)
Method for retrieving a collection object from DB by its _id.
Parameters
| Parameter | Type | Description |
|---|---|---|
| _id | String |
Object identifier |
| options | Object |
Success and error callbacks for the executed query. |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var getItem = new Scorocode.Object("items");
getItem.getById("NseSaqqd5v")
.then((success) => {
console.log(success);
})
.catch((error) => {
console.log(error)
});
see
Returns: Promise.<Scorocode.Object> - Returns promise that returns the requested object.
{
_id: 'NseSaqqd5v',
name: 'Water chip',
relatedquests: [ 'huNr3L7QDh' ],
createdAt: Mon May 23 2016 19:37:04 GMT+0300 (RTZ 2 (зима)),
updatedAt: Mon May 23 2016 19:37:04 GMT+0300 (RTZ 2 (зима)),
readACL: [],
updateACL: [],
removeACL: []
}
Exception:
Error"Document not found"
[Error: Document not found]
.get(key)
Method for retrieving data from a specified object field.
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be retrieved |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var getItem = new Scorocode.Object("items");
getItem.getById("NseSaqqd5v")
.then((success) => {
console.log(getItem.get("name"));
})
.catch((error) => {
console.log(error)
});
See
Returns: value - Returns the field value
Water chip
.uploadFile(field, filename, file, options)
File upload method
| Parameter | Type | Description |
|---|---|---|
| field | String |
Collection field name |
| filename | String |
File name |
| file | String |
File content in base64 format |
| options | Object |
Success and error callbacks for the executed query. |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey",
FileKey: "fileKey"
});
var attachToItem = new Scorocode.Object("items");
attachToItem.getById("xL0uOFtiJx")
.then((success)=>{
attachToItem.uploadFile("attachment", "file.txt", "VEhJUyBJUyBGSUxFLUUtRS1FLUUtRS1FIQ==")
.then((uploaded)=>{
console.log(uploaded);
})
.catch((error) => {
console.log(error)
});
})
.catch((error) => {
console.log(error)
});
See
Returns: promise.<String> - Returns promise that returns the uploaded file name
file.txt
Exceptions:
String'You must first create a document'
.save(options)
The method saves the object in the data warehouse or updates an object that already exists there
| Parameter | Type | Description |
|---|---|---|
| options | Object |
Success and error callbacks for the executed query. |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var questItem = new Scorocode.Object("items");
questItem.set("name", "Water chip").set("relatedquests", ["huNr3L7QDh"]);
questItem.save()
.then((saved) => {
console.log("saved");
})
.catch((error) => {
console.log(error);
});
See
Returns: Promise.<Scorocode.Object> - Returns promise that returns the saved object.
{
_id: 'NseSaqqd5v',
name: 'Water chip',
relatedquests: [ 'huNr3L7QDh' ],
createdAt: Mon May 23 2016 19:37:04 GMT+0300 (RTZ 2 (зима)),
updatedAt: Mon May 23 2016 19:37:04 GMT+0300 (RTZ 2 (зима)),
readACL: [],
updateACL: [],
removeACL: []
}
.removeFile(options)
Method for removing the file
| Parameter | Type | Description |
|---|---|---|
| field | String |
Collection field name |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var getItem = new Scorocode.Object("items");
getItem.getById("hejJU4BEGP")
.then((success)=>{
getItem.removeFile("attachment")
.then((removed)=>{
console.log(removed);
})
.catch((error) => {
console.log(error)
});
})
.catch((error) => {
console.log(error)
});
See
Returns: Promise.{error: Boolean} - Returns promise that returns the result:
{
error: false
}
.remove(options)
Method for removing the selected object
| Parameter | Type | Description |
|---|---|---|
| options | Object |
Success and error callbacks for the executed query. |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var getItem = new Scorocode.Object("items");
getItem.getById("hejJU4BEGP")
.then((success)=>{
getItem.remove(success)
.then((removed)=>{
console.log(removed);
})
.catch((error) => {
console.log(error)
});
})
.catch((error) => {
console.log(error)
});
See
Returns: Promise.{count: Number, docs: Array} - Returns promise that returns the object:
- "count" -
Number- number of removed objects - "doc" -
Array- array of removed objects' Ids
{
count: 1,
docs: [ 'hejJU4BEGP' ]
}
.extend(collName, childObject)
Method for converting the Scorocode.Query sample data into individiual Scorocode.Object instances
| Parameter | Type | Description |
|---|---|---|
| collName | String |
Collection name |
| childObject | Object |
Data to be converted |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var data = new Scorocode.Query("items");
data.find()
.then((finded) =>{
var objects = finded.result.map((data)=>{
return Scorocode.Object.extend("items", data)
});
return objects;
})
.then((result) => {
console.log(result);
})
.catch((err)=>{
console.log(err)
});
See
- new Object(collName)
- .find(options) ⇒
Promise.{error: Boolean, limit: Number, skip: Number, result: [{Scorocode.Object}]}
Returns: Scorocode.Object - Returns Scorocode.Object
.set(data)
Method for transferring data to object
| Parameter | Type | Description |
|---|---|---|
| data | Object |
Data in {"key", "value"} format, where key is the collection field name. |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var questItem = new Scorocode.Object("items");
questItem.set("_id", "NseSaqqd5v").set("name", "Water chip");
questItem.save()
.then((success) => {
console.log(success);
})
.catch((error) => {
console.log(error)
});
See
.push(key, value)
Method for adding an element to an array
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| value | String / Number / Boolean / Date / Array / Object |
Value of the new array element |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("NseSaqqd5v")
.then((success)=>{
console.log(success);
Item.push("arrayField", "new element");
Item.save()
.then((saved)=>{
console.log(saved);
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error)
});
.pull(key, value)
Method for removing all array elements whose values are the same as the specified one.
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| value | String / Number / Boolean / Date / Array / Object |
Value to be removed |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("MgYs9BEQUM")
.then((getted) => {
Item.pull("arrayField", {"Delete": true});
Item.save()
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error)
});
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error"For a new document use the method Set"Error'Field must by a type of array'
.pullAll(key, value)
Method for removing all array elements whose values are the same as one of the specified values.
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| value | Array |
Array of values to be removed |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("CrT49joIxn")
.then((getted) => {
Item.pullAll("arrayField", ["Delete me", 42, {"important": false}]);
Item.save()
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error)
});
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of array'Error'Value must by a type of array'
.addToSet(key, value)
Method for adding an element to an array only if there are no elements with the same name in the array.
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| value | String / Number / Boolean / Date / Array / Object |
Value of the added element |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("CrT49joIxn")
.then((getted) => {
Item.addToSet("arrayField", ["First element", {"isSecond": true}]);
Item.save()
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error)
});
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of array'
.pop(key, pos)
Method for removing the first or the last array element
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| pos | Number |
Position of the element to be removed in the array: -1 for the first element and 1 for the last |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var getItem = new Scorocode.Object("items");
getItem.getById("NseSaqqd5v")
.then((success)=>{
console.log(success);
getItem.pop("arrayField", 1);
getItem.save()
.then((saved)=>{
console.log(saved);
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of array'
.inc(key, amount)
The method increments the numeric field value by a defined number
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| amount | Number |
Increment step |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("gNxzwAfvDj")
.then((success)=>{
console.log(Item.get("price")); //44.42
Item.inc("price", -2.42);
Item.save()
.then((incremented)=>{
console.log(Item.get("price")); // 42
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of number'
.currentDate()
Sets the current time as the field's value
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| type | String / Boolean |
Date type. Accepts the following values: true, "date" or "timestamp" |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.set("_id", "gNxzwAfvDj").currentDate("someDate", true);
Item.save()
.then((success)=>{
console.log(Item.get("someDate")); // 2016-05-27T14:10:00.013+03:00
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Invalid type'
.mul(key, number)
The method multiplies the numeric field value by a defined number
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| number | Number |
Multiplier |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("8Qcfll2GwE")
.then((success)=>{
console.log(Item.get("price")); // 42
Item.mul("price", -1);
Item.save()
.then((incremented)=>{
console.log(Item.get("price")); // -42
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of number'Error'Value must by a type of number'
.min()
The method updates the numeric field value only if the new value is less than the current field value
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| number | Number |
New value |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("CrT49joIxn")
.then((success)=>{
console.log(Item.get("price")); // 42
Item.min("price", 41.999);
Item.save()
.then((changed)=>{
console.log(Item.get("price")); // 41.999
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of number'Error'Value must by a type of number'
.max()
The method updates the numeric field value only if the new value is greater than the current field value
| Parameter | Type | Description |
|---|---|---|
| key | String |
Name of the field whose value should be updated |
| number | Number |
New value |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Item = new Scorocode.Object("items");
Item.getById("CrT49joIxn")
.then((success)=>{
console.log(Item.get("price")); // 41.999
Item.max("price", 40);
Item.save()
.then((changed)=>{
console.log(Item.get("price")); // 41.999
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error'For a new document use the method Set'Error'Field must by a type of number'Error'Value must by a type of number'