Home Reference Source

Function

Static Public Summary
public

afterDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

afterDelete — executes decorated function after a record delete operation occurs

public

afterSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

afterSave — executes decorated function after a record save operation occurs

public

beforeDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

beforeDelete — executes decorated function before a record delete operation occurs

public

beforeSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

beforeSave — executes decorated function before a record save operation occurs

public

async configModule(moduleName: *, options: *)

Import and config the cloud code plugin module.

public

createLogger(name: *, context: {}, opts: {}): *

public

errorResponseFromMessage(message: *): {"error": *}

public

event(name: String, func: function(param: *), options: Object)

You can register the cloud code to run at skygear life cycle event.

public

every(cron: String, func: function(param: *), options: Object)

You can register the cloud code to get run at specific time intervals like a cron job.

public

fromJSON(attrs: Object): Object

Convert object from JSON representation

public

getContainer(userId: String): CloudCodeContainer

Get a configured CloudCodeContainer.

public

Return a shared signer for the current configuration.

public

handler(path: string, func: function(request: *, options: *): object, options: Object)

You can configure the cloud code as an HTTP handler, which can respond to requests coming from outside the SDK.

public

hook(name: String, func: function(newRecord: *, oldRecord: *, pool: *, options: *), options: Object)

You can register the cloud code to run at database event.

public

isAsset(obj: *): Boolean

Returns whether an object is a Skygear Asset.

public

isLocalStorageValid(): boolean

public

isRecord(obj: *): Boolean

Returns whether an object is a Skygear Record.

public

isValueType(value: *): *

public

op(name: String, func: function(param: Object, options: *), options: Object)

You can register the cloud code as lambda, with JSON input and output.

public

parseInteger(val: *, defaultValue: *): *

public

poolConnect(callback: function(err: *, client: *, done: function): *)

Connect to a pg connection provided by pg pool.

public

provides(providerType: string, providerID: string, ProviderCls: object, options: object)

Authentication Provider allows a plugin to authenticate user based on credentials from a third-party.

public

async publishEventsToChannels(channels: String[], eventsData: Object[]): Promise<CloudCodeContainer>

Publish multiple events to multiple channels.

public

staticAsset(mountPoint: string, func: function(): string)

staticAsset — declare certain path to be serve in static asset.

public

staticAssetHandler(req: *): *

staticAssetHandler — default handler for serving static assets with during development.

public

toJSON(v: Object): Object

Returns the specified value to a JSON representation.

Static Public

public afterDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

afterDelete — executes decorated function after a record delete operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.afterDelete('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public afterSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

afterSave — executes decorated function after a record save operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.afterSave('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public beforeDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

beforeDelete — executes decorated function before a record delete operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.beforeDelete('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public beforeSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

beforeSave — executes decorated function before a record save operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered asynchronously

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.beforeSave('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public async configModule(moduleName: *, options: *) source

Import and config the cloud code plugin module.

This function will load the specified module by name using the require function. The specified module must exists or an error is thrown.

It is expected that the module will exports a function called includeme. This function will also call the includeme function to config the module.

Params:

NameTypeAttributeDescription
moduleName *
options *

public createLogger(name: *, context: {}, opts: {}): * source

Params:

NameTypeAttributeDescription
name *
context {}
  • optional
  • default: {}
opts {}
  • optional
  • default: {}

Return:

*

public errorResponseFromMessage(message: *): {"error": *} source

import {errorResponseFromMessage} from 'skygear-build/packages/skygear-sso/lib/util.js'

Params:

NameTypeAttributeDescription
message *

Return:

{"error": *}

public event(name: String, func: function(param: *), options: Object) source

You can register the cloud code to run at skygear life cycle event.

Params:

NameTypeAttributeDescription
name String

skygear life cycle event name

func function(param: *)

function to be registered

options Object
  • optional

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.event('after-plugins-ready', function() {
  console.log('Meow');
});

public every(cron: String, func: function(param: *), options: Object) source

You can register the cloud code to get run at specific time intervals like a cron job.

Params:

NameTypeAttributeDescription
cron String

time interval in cron job syntax

func function(param: *)

function to be registered

options Object
  • optional

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.every('@daily', function() {
  console.log('Meow');
});

public fromJSON(attrs: Object): Object source

Convert object from JSON representation

It will descends into array and object to convert Skygear Data Type from JSON representation. If the specified value is null, null is returned.

This function is the opposite of toJSON.

Params:

NameTypeAttributeDescription
attrs Object

the object or value in JSON representation

Return:

Object

the result in Skygear Data Type

public getContainer(userId: String): CloudCodeContainer source

Get a configured CloudCodeContainer.

Params:

NameTypeAttributeDescription
userId String
  • optional

user ID of the user. If not specified, the default is determined by the server.

Return:

CloudCodeContainer

the cloud code container that acts as the specified user.

public getSigner(): Signer source

Return a shared signer for the current configuration.

Return:

Signer

public handler(path: string, func: function(request: *, options: *): object, options: Object) source

You can configure the cloud code as an HTTP handler, which can respond to requests coming from outside the SDK. A custom HTTP endpoint can be created using the handler function.

A custom HTTP endpoint can be useful for the followings:

  • receiving requests from outside the Skygear SDK
  • allowing a third party webhook to call upon (e.g. payment service)

Params:

NameTypeAttributeDescription
path string

The path of the handler to be mount.

func function(request: *, options: *): object

function to be registered.

options Object
  • optional
options.method String[] | String
  • optional

handler methods, e.g. GET, POST

options.keyRequired Boolean
  • optional

require api key to call the lambda

options.userRequired Boolean
  • optional

require user to call the lambda

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.handler('handler1', function(req) {
    // cloud code handling the request
    return 'String';
}, {
    method: ['GET', 'POST'],
    userRequired: false
});
skygearCloud.handler('private', function(req, options) {
    // cloud code handling the request
    const {
      context,
      container  // a cloud code container for the current request context
    } = options;
    return {
      status: 'ok',
      user_id: context.user_id // only available if userRequired=true
    };
}, {
    method: ['GET', 'POST'],
    userRequired: true
});

public hook(name: String, func: function(newRecord: *, oldRecord: *, pool: *, options: *), options: Object) source

You can register the cloud code to run at database event.

Params:

NameTypeAttributeDescription
name String

function name

func function(newRecord: *, oldRecord: *, pool: *, options: *)

function to be registered

options Object
  • optional
options.type String
  • optional

record type

options.trigger String
  • optional

type of database event that trigger the hook

options.async Boolean
  • optional

true if the function triggered asynchronously

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.hook('before-save', function(newRecord, oldRecord, pool) {
  console.log('Meow');
  return newRecord;
});

public isAsset(obj: *): Boolean source

Returns whether an object is a Skygear Asset.

Params:

NameTypeAttributeDescription
obj *

Return:

Boolean

true if the specified object is a Skygear Asset.

public isLocalStorageValid(): boolean source

import {isLocalStorageValid} from 'skygear-build/packages/skygear-core/lib/util.js'

Return:

boolean

public isRecord(obj: *): Boolean source

Returns whether an object is a Skygear Record.

Params:

NameTypeAttributeDescription
obj *

Return:

Boolean

true if the specified object is a Skygear Record.

public isValueType(value: *): * source

Params:

NameTypeAttributeDescription
value *

Return:

*

public op(name: String, func: function(param: Object, options: *), options: Object) source

You can register the cloud code as lambda, with JSON input and output.

This is a convenient way to inject custom logic to Skygear server and can be called easily with skygear.lambda in the client side.

Params:

NameTypeAttributeDescription
name String

lambda name

func function(param: Object, options: *)

function to be registered

options Object
  • optional
options.keyRequired Boolean
  • optional

require api key to call the lambda

options.userRequired Boolean
  • optional

require user to call the lambda

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.op('greeting', function(param) {
    return {
      'content': 'Hello, ' + param.name,
    };
}, {
    userRequired: false
});

public parseInteger(val: *, defaultValue: *): * source

Params:

NameTypeAttributeDescription
val *
defaultValue *

Return:

*

public poolConnect(callback: function(err: *, client: *, done: function): *) source

Connect to a pg connection provided by pg pool.

The pg connection is automatically set to use the application's database schema.

Params:

NameTypeAttributeDescription
callback function(err: *, client: *, done: function): *

function called after setting up the database connection

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.poolConnect(function (err, client, done) {
  // if err is undefined or null, make query using the client
  client.query(
    'SELECT id FROM _user WHERE username = $1',
    ['johndoe'],
    function(queryErr, result) {
      done();
      // use the query result here
    }
  );
}

public provides(providerType: string, providerID: string, ProviderCls: object, options: object) source

Authentication Provider allows a plugin to authenticate user based on credentials from a third-party. The Authentication Provider responds to Skygear Server whether the credentials are accepted.

Params:

NameTypeAttributeDescription
providerType string

Type of the provider, only auth is supported now.

providerID string

unique identifier to the provider

ProviderCls object

a provider class

options object
  • optional

options for setting

Example:

const skygearCloud = require('skygear/cloud');¬

skygearCloud.provides('auth', 'com.facebook', Provider);

public async publishEventsToChannels(channels: String[], eventsData: Object[]): Promise<CloudCodeContainer> source

import {publishEventsToChannels} from 'skygear-build/packages/skygear-core/lib/cloud/container.js'

Publish multiple events to multiple channels.

Params:

NameTypeAttributeDescription
channels String[]
  • optional

name of the channels

eventsData Object[]
  • optional

events data to be published

Return:

Promise<CloudCodeContainer>

promise with the cloud code container

public staticAsset(mountPoint: string, func: function(): string) source

staticAsset — declare certain path to be serve in static asset.

All asset will be serve with the prefix /static. i.e. if the mount ping is declare as /css. The final URL for requesting assets will be /static/css.

Params:

NameTypeAttributeDescription
mountPoint string

the target mount point

func function(): string

function to return the absolute path of the static assets.

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.staticAsset('/styles', function() {
    // Return the absolute path of the static assets directory
    // http://<yourapp>.skygeario.com/static/styles will be serving files
    // located at '<project_path>/css`
    return __dirname + '/css/';
});

public staticAssetHandler(req: *): * source

staticAssetHandler — default handler for serving static assets with during development.

Params:

NameTypeAttributeDescription
req *

Return:

*

public toJSON(v: Object): Object source

Returns the specified value to a JSON representation.

It will descends into array and object to convert Skygear Data Type into JSON representation. If the specified value is null, null is returned.

The output of this function differ from Record.toJSON when specifying a Record. This function will wrap a Record in JSON representation with a $type=record object.

This function is the opposite of fromJSON.

Params:

NameTypeAttributeDescription
v Object

the object or value value to convert to JSON

Return:

Object

the result in JSON representation