Building a Data Connector
WireBootstrap client-side Data Connectors use service providers to connect to web services.
A WireBootstrap service provider is a JavaScript function or class that implements the methods and properties expected by the data source based on the functionality of the service provider.
This section documents a sample service provider created to query the public JSON Placeholder web service at https://jsonplaceholder.typicode.com. The sample is available in a sub-project inside any edition of the WireBootstrap for Gentelella download. Once downloaded, go to the\docs\building-a-data-connector directory to open the project containing the examples in this section. Double-click the index.html file to run the sample.

Create the Provider

The service provider is a simple JavaScript class or function that takes one configuration parameter. If needed, use the dataSource property on the configuration parameter to get a reference to the configuration passed into the calling data source.
1
const JsonPlaceHolderServiceProvider = function (config) {
2
3
const datasource = config.dataSource;
4
...
5
6
}
Copied!

Provider Configuration

Any service provider specific information for the data connector can be passed in using the Provider attribute when configuring the data source.
The example below passes in an API key that will be used by the service provider to authenticate calls to the web service. This key is not needed for the JSON Placeholder web service. Its used here only for demonstration.
1
const source = new wire.data.DataSource("jsonplaceholder", {
2
...
3
Provider: {
4
apiKey: "my-test-key"
5
}
6
});
Copied!
This configuration can then be read in and used by the service provider.
1
const JsonPlaceHolderServiceProvider = function (config) {
2
3
const datasource = config.dataSource;
4
const provider = datasource.Provider;
5
const apiKey = provider.apiKey;
6
...
7
8
}
Copied!
For more information on data source configuration, visit DataSource.

Register the Provider

Register the service provider with the WireBootstrap framework by calling the wire.data.provider.register method.
1
wire.data.provider.register({
2
id: "jsonplaceholder",
3
provider: JsonPlaceHolderServiceProvider
4
});
Copied!

Allow

The service provider in this example is able to transform WireBootstrap table queries into requests to the data service for data. The service provider tells the data source about this feature by setting the allow.tableQuery property to true.
1
...
2
3
this.allow = {
4
tableQuery: true
5
}
Copied!

Execute Queries

The execAsync method on the service provider will be called by a data source to execute queries against the data service. This method takes a TableQuery parameter with the inputs to the query.
In the example below, the getUrl method in the project will return the transformed query as a string URL that can be used to call the web service.
The wire.get method will make an asynchronous call to the web service to retrieve the data for the query.
1
this.execAsync = function (query) {
2
3
const url = getUrl(query);
4
5
return wire.get(url);
6
7
}
Copied!

DataTable Transform

After calling the asynchronous execAsync method on a service provider, the data source passes the data returned from the call to the getResponseTable method on the service provider. This method is responsible for handling any data service specific formatting and returning the data in a standard DataTable.
The data returned from calls to the JSON Placeholder web service is a simple array of objects so the data table is easily created by passing the data into the data table's constructor.
1
this.getResponseTable = function (data) {
2
3
return new wire.data.DataTable(data);
4
5
}
Copied!
Visit the ExecAsync documentation for more details on this method.

Putting It All Together

Consumers can now make calls to the data service using the service provider with a data source.
The first parameter to the DataSource constructor below is the id jsonplaceholder used when the service provider was registered with WireBootstrap above. The entity on the query is todos which is filtered touserId equal to1.
1
const source = new wire.data.DataSource("jsonplaceholder");
2
3
const query = wire.data.select().from("todos").where().eq("userId", 1);
4
5
const result = await source.execAsync(query);
Copied!
The code above will result in the following call to the web service.
1
GET: https://jsonplaceholder.typicode.com/todos?userId=1
Copied!