Skip to main content

Connector

The table below lists functions a custom Read Model's connector should implement.

Function NameDescription
connectInitializes a connection to storage.
disconnectCloses the storage connection.
dropRemoves the Read Model's data from storage.
disposeDispose of this connector's unmanaged resources.

connectā€‹

Initializes a connection to storage. An implementation should return a store object.

Argumentsā€‹

Argument NameDescription
readModelNameA read model for which to establish a connection.

Exampleā€‹

const connect = async (readModelName) => {
fs.writeFileSync(`${prefix}${readModelName}.lock`, 'true', { flag: 'wx' })
readModels.add(readModelName)
const store = {
get() {
return JSON.parse(String(fs.readFileSync(`${prefix}${readModelName}`)))
},
set(value) {
fs.writeFileSync(`${prefix}${readModelName}`, JSON.stringify(value))
},
}
return store
}

disconnectā€‹

Closes the storage connection.

Argumentsā€‹

Argument NameDescription
storeA store object.
readModelNameThe read model to disconnect.

Exampleā€‹

const disconnect = async (store, readModelName) => {
safeUnlinkSync(`${prefix}${readModelName}.lock`)
readModels.delete(readModelName)
}

dropā€‹

Removes the Read Model's data from storage.

Argumentsā€‹

Argument NameDescription
storeA store object.
readModelNameA Read Model whose data to remove.

Exampleā€‹

const drop = async (store, readModelName) => {
safeUnlinkSync(`${prefix}${readModelName}.lock`)
safeUnlinkSync(`${prefix}${readModelName}`)
}

disposeā€‹

Dispose of all unmanaged resources provided by this connector.

Exampleā€‹

const dispose = async () => {
for (const readModelName of readModels) {
safeUnlinkSync(`${prefix}${readModelName}.lock`)
}
readModels.clear()
}