Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Introduction

A Connector project is similar in structure to a project. This guide covers how to manage your Connector project.

...

The following is a list of directories and files that can be found in a Connector project:

File/Folder NameDescription
app.jsThe entry point to the connector for testing, which launches a server instance.
appc.jsonProject configuration file. Do not modify this file.
confContains configuration files in JSON format for the connector. The file default.js is used for testing the connector. You can create an example configuration file, which is copied to the project when it is installed. See the  Connector Configuration File section.
index.jsThe entry point to the connector.
libContains the logic for your connector. Requires a index.js file. See the Connector Logic section.
logsContains generated log files when running your project locally. If you test the connector, the generated log files will get packaged with the application. You may want to disable logging by setting the transactionLogEnabled property to false in the conf/default.js file.
modelsContains Model JavaScript files, used to declare the schema for your data and generate API endpoints for the connector. See Models and API Endpoints below.
node_modulesContains project dependencies. API Builder automatically installs any project dependencies declared in the package.json file.
package.jsonNPM configuration file to declare project dependencies and other build or runtime configurations.

...

API Builder is based on Arrow and starting with API Builder 1.2.48 or Appcelerator CLI 4.1.3, the connector configuration object passed to the Connector.extend() method supports a capabilities property. The first time you run a project after adding a Capability constant to the capabilities property, API Builder will generate boilerplate logic, which you can modify. The table below explains what each capability constant exposes and creates. If a certain connector is not exposed through a capability, you can implement the method in the object passed to the Connector.extend() method.

Capability ConstantLocationExposed Connector Methods
ConnectToADataSource./lib/lifecycle
  • connect
  • disconnect
ValidatesConfiguration./lib/metadata
  • fetchMetadata
AddsCustomTypes./lib/metadata 
  • coerceCustomType
  • getCustomType
GenerateModels./lib/schema
  • createModelsFromSchema
  • fetchSchema
ContainsModels./modelsCreates a boilerplate model in the models folder.
CanCreate./lib/methods
  • create
CanRetrieve./lib/methods
  • distinct
  • findAll
  • findById
  • query
CanUpdate./lib/methods
  • findAndModify
  • save
  • upsert
CanDelete./lib/methods 
  • delete
  • deleteAll

AuthenticatesThroughConnector

./lib/lifecycle
  • login
  • loginRequired

Initialization

If you need to add some custom initialization logic when creating the connector, implement the following methods. The connector instance is the value passed to this in the functions. The functions do not take any arguments or return any values:

...

When the connector is loaded, the following methods are executed (in order and if defined). You do not need to implement any of the methods. Each method is passed a callback. After completing the operation, invoke the callback function and pass it an Error object (or null if successful) as the first parameter, and the results of the operation as the second parameter. None of the methods have a return value.

Method SignatureCapabilityBoiler Plate FileDescriptionResult to Pass to the Callback
fetchMetadata(callback) ValidatesConfiguration./lib/metadata/fetchMetadata.jsRetrieves the metadata of the data source. The metadata is used to validate the configuration object.Metadata object. Set the fields key to an array of Metadata objects to verify the keys in the configuration object.
fetchConfig(callback) --Retrieves the configuration of the data source.Configuration object. Key-value pairs describing the configuration of the connector.
connect(callback) ConnectToADataSource./lib/lifecycle/connect.jsConnects to the data source.None.
fetchSchema(callback) GenerateModels./lib/schema/fetchSchema.jsRetrieves the model schema of the data source.Schema object.
disconnect(callback) ConnectToADataSource./lib/lifecycle/connect.js Disconnect from the data source.None.

CRUD methods

To access data from the Connector, you need to implement the following methods. Each method is passed the Model class as its first parameter and a callback as its last parameter. After completing the operation, invoke the callback function and pass it an Error object (or null if successful) as the first parameter, and the results of the operation as the second parameter. None of the methods have a return value.

Method SignatureCapabilityBoiler Plate FileDescriptionResult to Pass to the Callback
create(Model, values, callback)CanCreate./lib/methods/create.js Creates a new model using the passed values.New model
delete(Model, instance, callback)CanDelete./lib/methods/delete.js Deletes the model instance.Deleted model
deleteAll(Model, callback)CanDelete./lib/methods/deleteAll.js Deletes all the models.An array of deleted models
findAll(Model, callback)CanRetrieve./lib/methods/findAll.js Retrieves all the models.An array of models
findById(Model, id, callback)CanRetrieve./lib/methods/findById.js Retrieves one model with the specified ID (id parameter).A model
query(Model, options, callback)CanRetrieve./lib/methods/query.js Retrieves all models based on the query options.An array of found models
distinct(Model, field, options, callback)CanRetrieve./lib/methods/ditinct.js Retrieves a distinct set of models based on the field(s) and query options.An array of distinct models
save(Model, instance, callback)CanUpdate./lib/methods/save.jsUpdates the model instance.Updated model
findAndModify(Model, options, doc, args, callback) CanUpdate./lib/methods/findAndModify.jsFinds one model instance and modifies it.Modified model
upsert(Model, id, doc, callback) CanUpdate./lib/methods/upsert.jsUpdates the model instance if found or creates a new model instance.Updated or new model

Request lifecycle

...

If a request requires a login or if you want to intercept the request before or after it completes, you can implement the following methods:

Method SignatureCapabilityBoiler Plate FileDescription
startRequest(methodName, args, request, next) --

Request interceptor invoked before the request is initiated and the login method is invoked. The method is passed the name of the method that started the request and the arguments passed to the method. Invoke the next function when the operation completes.

loginRequired(request, callback)

AuthenticatesThroughConnector

./lib/lifecycle/loginRequired.js Determines if the request required a login. Pass an error message (or null if successful) as the first parameter to callback and a boolean value indicating if the login method needs to be executed (true) or not (false) as the second parameter.
login(request, next) AuthenticatesThroughConnector ./lib/lifecycle/login.js Logs into the data source to make a request. Invoke the next function when the operation completes.
endRequest(methodName, args, request, next) --Request interceptor invoked after the request completes . The method is passed the name of the method that started the request and the arguments passed to the method. Invoke the next function when the operation completes.

Anchor
Connector Configuration File
Connector Configuration File
Connector configuration file

...