Quick Start
The core module of DataBus is https://github.com/apitable/apitable/tree/develop/packages/core/src/databus
The entry point of DataBus is a DataBus instance. Creating a DataBus instance requires two prodivers, an IDataStorageProdiver and an IStoreProvider. An IDataStorageProvider is responsible for loading data packs from a storage system, as well as storing changesets resulted from user actions or client requests. An IStoreProvider creates redux stores from the data packs returned from the IDataStorageProvider.
An example use of DataBus in back-end:
class ServerDataStorageProvider implements databus.IDataStorageProvider {
loadDatasheetPack(dstId, options) {
// reading datasheet pack from database ...
}
loadDashboardPack(dsbId, options) {
// reading dashboard pack from database ...
}
saveOps(ops, options) {
// save changesets into database and update corresponding datasheet data ...
}
}
class ServerStoreProvider implements databus.IStoreProvider {
createDatasheetStore(datasheetPack): Store<IReduxState> {
// create a redux store from datasheet pack ...
}
createDashboardStore(dashboardPack): Store<IReduxState> {
// create a redux store from dashboard pack ...
}
}
// Create a DataBus instance
const databus = DataBus.create({
dataStorageProvider: new ServerDataStorageProvider(),
storeProvider: new ServerStoreProdiver(),
})
// Get a Database instance. A Database corresponds to a space in APITable.
const database = databus.getDatabase();
// Get a Datasheet instance from the database
const datasheet = await database.getDatasheet(datasheetId, {
loadOptions: {
// options that will be passed to IDataStorageProdiver.loadDatasheetPack()
},
storeOptions: {
// options that will be passed to IStoreProvider.createDatasheetStore()
}
})
// Get a view of the datasheet
const view = await datasheet.getView(viewId);
// Get record list from the view
const records = await view.getRecords({});DataBus incorporates collaboration logic, so the user is able to perform operations on entity classes of DataBus:
const executionResult = await datasheet.updateRecords(
[
{
recordId: 'rec1',
fieldId: 'fld1',
value: [ ... ] // new cell value
}
],
{
// options that will be passed to IDataStorageProvider.saveOps() if the command is executed successfully.
}
);In the front-end, the usage of DataBus is nearly identical, the user provides an implementation of IDataStorageProvider, which loads data packs from the server and sends changesets to the server. The implementation of IStoreProvider simply dispatch actions to the global redux state and return the store.