Skip to content

Commit

Permalink
fix: chunked backup creation (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Sep 27, 2021
1 parent ae5423a commit 4c8cf17
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/db/fauna/resources/Function/createUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,36 @@ const body = Query(
},
If(
IsNonEmpty(Var('uploadMatch')),
Get(Var('uploadMatch')),
Do(
Foreach(
Select('backupUrls', Var('data')),
Lambda(
['url'],
Let(
{
upload: Select('ref', Get(Var('uploadMatch'))),
backupMatch: Match(
Index('backup_by_upload_and_url'),
Var('upload'),
Var('url')
)
},
If(
IsNonEmpty(Var('backupMatch')),
Get(Var('backupMatch')),
Create('Backup', {
data: {
upload: Var('upload'),
url: Var('url'),
created: Now()
}
})
)
)
)
),
Get(Var('uploadMatch'))
),
Let(
{
upload: Create('Upload', {
Expand Down
35 changes: 35 additions & 0 deletions packages/db/fauna/resources/Index/backupByUploadAndUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fauna from 'faunadb'

const {
CreateIndex,
Collection,
If,
Index,
Exists,
Not
} = fauna

/**
* Usage:
*
* Match(
* Index('backup_by_upload_and_url'),
* Ref(Collection('Upload'), Var('uploadId')),
* Var('url')
* )
*/
const index = {
name: 'backup_by_upload_and_url',
source: Collection('Backup'),
terms: [
{ field: ['data', 'upload'] },
{ field: ['data', 'url'] }
]
}

// indexes cannot be updated
export default If(
Not(Exists(Index(index.name))),
CreateIndex(index),
Index(index.name)
)

0 comments on commit 4c8cf17

Please sign in to comment.