Skip to content

Instantly share code, notes, and snippets.

@louisbuchbinder
Last active May 12, 2022 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisbuchbinder/5fa7a8b009ad29847bb8c5d16a5d79e9 to your computer and use it in GitHub Desktop.
Save louisbuchbinder/5fa7a8b009ad29847bb8c5d16a5d79e9 to your computer and use it in GitHub Desktop.
Twisp Trigger Fibonacci
This is a contrived example to demonstrate transaction triggers in Twisp.

For each touch of a record in the source table the fibonacci number corresponding to the count of touches is recorded in the derived table.

Transform: AWS::Serverless-2016-10-31
Resources:
Source:
Type: Twisp::Database::Ledger
Properties:
Name: source
Document:
id: uuid
Indexes:
id:
Partition:
- id: document.id
Properties:
Unique: true
Triggers:
init:
Events:
- ON_INSERT
Execute:
- Name: insert_derived
Action: DO_INSERT
Where:
Table: derived
Document:
id: context.source.id
a: '1'
fibonacci:
Events:
- ON_UPDATE
Execute:
- Name: update_derived
Action: DO_UPDATE
Where:
Table: derived
Index: derived.indexes.id
Partition:
id: context.source.id
Document:
a: 'document.fibonacci'
fibonacci: 'document.a + document.fibonacci'
index: 'document.index + 1'
Derived:
Type: Twisp::Database::Ledger
Properties:
Name: derived
Document:
id: uuid
a: int
fibonacci: int
index: int
Indexes:
id:
Partition:
- id: document.id
Properties:
Unique: true
References:
source:
Index: source.indexes.id
Partition:
id: document.id
Properties:
Unique: true
mutation insert {
source {
insert (
input: {
document: {
id:"uuid(context.vars.uuid)"
}
}
) {
record { id }
}
}
}
mutation update($uuid:String!) {
source {
update (
input: {
index: id
where: {
partition: [ $uuid ]
}
document: {}
}
) {
records { id }
}
}
}
query get($uuid:String!) {
derived (
index: id
where: {
partition: [ $uuid ]
}
) {
history {
fibonacci
index
}
}
}
{
"uuid": "00000000-0000-0000-0000-000000000000"
}
@louisbuchbinder
Copy link
Author

twisp-fibonacci

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment