Advanced
Datasource & Adapters
By default, Featurevisor CLI uses the file system for reading and writing data in your project, given it's a Git repository after all. But the configuration API allows you to switch to any source via adapters.
Accessing datasource#
It's unlikely that you will make use of the Datasource API yourself directly, unless you are a plugin developer.
The datasource
object allows you to read and write data from/to the Featurevisor project, so that you don't have to deal with the file system (or any other custom source of your project data) directly.
You can refer to the full datasource API for more details.
Datasource methods#
Once you have access to the datasource
object, you can use the following methods from its instance:
Revision#
See state files for more details.
const revision = await datasource.readRevision()await datasource.writeRevision(revision + 1)
Features#
See features for more details.
const features = await datasource.listFeatures()const fooFeatureExists = await datasource.featureExists('foo')const fooFeature = await datasource.readFeature('foo')await datasource.writeFeature('foo', { ...fooFeature, ...newData })await datasource.deleteFeature('foo')
Segments#
See segments for more details.
const segments = await datasource.listSegments()const fooSegmentExists = await datasource.segmentExists('foo')const fooSegment = await datasource.readSegment('foo')await datasource.writeSegment('foo', { ...fooSegment, ...newData })await datasource.deleteSegment('foo')
Attributes#
See attributes for more details.
const attributes = await datasource.listAttributes()const fooAttributeExists = await datasource.attributeExists('foo')const fooAttribute = await datasource.readAttribute('foo')await datasource.writeAttribute('foo', { ...fooAttribute, ...newData })await datasource.deleteAttribute('foo')
Groups#
See groups for more details.
const groups = await datasource.listGroups()const fooGroupExists = await datasource.groupExists('foo')const fooGroup = await datasource.readGroup('foo')await datasource.writeGroup('foo', { ...fooGroup, ...newData })await datasource.deleteGroup('foo')
Tests#
See testing for more details.
const tests = await datasource.listTests()const fooTest = await datasource.readTest('foo')await datasource.writeTest('foo', { ...fooTest, ...newData })await datasource.deleteTest('foo')
State#
See state files for more details.
const existingState = await datasource.readState(environment)datasource.writeState(environment, { ...existingState, ...newState })
History#
To get history of changes made to a specific entity:
const fooChanges = await datasource.listHistoryEntries('feature', 'foo')
The first argument for entity type can be one of:
feature
segment
attribute
group
test
Adapters#
Because a Featurevisor project is a Git repository by default, Featurevisor CLI ships with a default adapter that reads and writes data from/to the file system which is called FilesystemAdapter
.
You don't have to configure this adapter explicitly anywhere, unless you are writing a custom one.
Writing a custom adapter#
You can write your own custom datasource adapter as follows:
import { Adapter } from '@featurevisor/core'export class CustomAdapter extends Adapter { // ...implement the methods here}
Refer to the implementation of FilesystemAdapter
to understand more.
Using a custom adapter#
You can swap out the default file system adapter with your custom adapter via you configuration file as found in featurevisor.config.js
:
const { CustomAdapter } = require('./adapters/custom-adapter')module.exports = { environments: ['staging', 'production'], tags: ['web', 'mobile'], adapter: CustomAdapter,}