Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dfordivam/persistent-cdc
Change data capture for persistent
https://github.com/dfordivam/persistent-cdc
Last synced: 6 days ago
JSON representation
Change data capture for persistent
- Host: GitHub
- URL: https://github.com/dfordivam/persistent-cdc
- Owner: dfordivam
- License: bsd-3-clause
- Created: 2016-11-06T05:07:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-23T16:39:52.000Z (almost 8 years ago)
- Last Synced: 2024-12-23T11:13:11.461Z (11 days ago)
- Language: Haskell
- Size: 39.1 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Change data capture for persistent
Use this library to enable change data capture when using persistent.
Usage:
1. Use the `share` provided by `Database.Persist.CDC.TH`.
2. Use `updateWithCDC` or `replaceWithCDC` API in place of `update` and `replace` to capture changes.The `share` API will create additional data for every entry
For example
```haskell
share "User" [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
username Text
email Text
password ByteStringWiki
title Text
overview Text Maybe
content Text
|]
```This will create the following data types
```haskell
data User = User {
userUsername :: Text,
userEmail :: Text,
userPassword :: ByteString
}data Wiki = Wiki {
wikiTitle :: Text,
wikiOverview :: Maybe Text,
wikiContent :: Text
}data UserHistory = UserHistory {
userHistoryEditAuthorId :: UserId,
userHistoryUser :: UserId,
userHistoryTimeStamp :: UTCTime,
userHistoryUsername :: Maybe Text,
userHistoryEmail :: Maybe Text,
userHistoryPassword :: Maybe ByteString
}data WikiHistory = WikiHistory {
wikiHistoryEditAuthorId :: UserId,
wikiHistoryWiki :: WikiId,
wikiHistoryTimeStamp :: UTCTime,
wikiHistoryTitle :: Maybe Text,
wikiHistoryOverview :: (Maybe Text, Bool),
wikiHistoryContent :: Maybe Text
}
```For each data type specified, this API will create a related `History data type.
The fields in these data type are described below
- The `EditAuthorId` field in the stores the identity of the `User` who did the update.
The type of this has to be specified in the `share` API.
- Next the id of original data is stored.
- Timestamp is the time of creation of history object which is same as update time.
- For every field in the original data a field is present in the history object.
The type of this field has `Maybe`, which indicates whether that field got
modified or not. If the field is modifed then the old value is stored in the
history object.
If the original data definition had `Maybe` attribute to a field then we create
a tuple of original data with Bool. The value of Bool determines whether there is
any change in the data.
So if `wikiOverview` got modified from `Nothing` to `Just "overview"` then the
history will contain `(Nothing, True)` and if there is no change it will have
`(Nothing, False)`.These APIs are available to modify the data and capture the change automatically
in history objects.```haskell
updateWithCDC ::
=> Key (EditAuthorType backend)
-> Key record
-> [Update record]
-> ReaderT backend m ()replaceWithCDC ::
=> Key (EditAuthorType backend)
-> Key record
-> record
-> ReaderT backend m ()
```Example yesod code to handle update
```haskell
-- postEditWikiR :: WikiId -> Handler Html
postEditWikiR wikiKey = douserKey <- getCurrentUser -- some API to get current user
-- maybeAuthId etc.updateWithCDC userKey wikiKey [WikiContent =. newContent]
...```