Connector
The table below lists functions a custom Read Model's connector should implement.
Function Name | Description |
---|---|
connect | Initializes a connection to storage. |
disconnect | Closes the storage connection. |
drop | Removes the Read Model's data from storage. |
dispose | Dispose of this connector's unmanaged resources. |
connectā
Initializes a connection to storage. An implementation should return a store object.
Argumentsā
Argument Name | Description |
---|---|
readModelName | A 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 Name | Description |
---|---|
store | A store object. |
readModelName | The 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 Name | Description |
---|---|
store | A store object. |
readModelName | A 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()
}