Skip to content

Instantly share code, notes, and snippets.

@ptpaterson
Last active June 30, 2020 16:18
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 ptpaterson/f09d723dada1dc9e9a8e4e5d3460942d to your computer and use it in GitHub Desktop.
Save ptpaterson/f09d723dada1dc9e9a8e4e5d3460942d to your computer and use it in GitHub Desktop.
Connect or Create-and-Connect, FaunaDB UDF body definition
{
name: "up_nect",
body: Query(
Lambda(
["commitSha", "filesMeta"],
Let(
{
createdGitRefDoc: Create(Collection("GitRef"), {
data: { commitSha: Var("commitSha") }
}),
createdGitRefRef: Select(["ref"], Var("createdGitRefDoc")),
filesMetaShas: Map(
Var("filesMeta"),
Lambda("fileMeta", Select(["sha"], Var("fileMeta")))
),
filesMetaMatches: Filter(
Map(
Var("filesMetaShas"),
Lambda(
"sha",
Let(
{
match: Match(
Index("shaAndRef_of_FileMeta_by_sha"),
Var("sha")
)
},
If(
Exists(Var("match")),
Select(["data", 0], Paginate(Var("match"))),
null
)
)
)
),
Lambda("match", Not(IsNull(Var("match"))))
),
existingFilesMetaShas: Map(
Var("filesMetaMatches"),
Lambda("match", Select(0, Var("match")))
),
existingFilesMetaRefs: Map(
Var("filesMetaMatches"),
Lambda("match", Select(1, Var("match")))
),
newFilesMetaShas: Difference(
Var("filesMetaShas"),
Var("existingFilesMetaShas")
),
newFilesMeta: Filter(
Var("filesMeta"),
Lambda(
"fileMeta",
IsNonEmpty(
Intersection(
[Select("sha", Var("fileMeta"))],
Var("newFilesMetaShas")
)
)
)
),
createdFilesMeta: Map(
Var("newFilesMeta"),
Lambda(
"fileMeta",
Create(Collection("FileMeta"), { data: Var("fileMeta") })
)
),
createdLinksOnNew: Map(
Var("createdFilesMeta"),
Lambda(
"fileMeta",
Create(Collection("fileMeta_gitRefs"), {
data: {
gitRefID: Var("createdGitRefRef"),
fileMetaID: Select(["ref"], Var("fileMeta"))
}
})
)
),
createdLinksOnExisting: Map(
Var("existingFilesMetaRefs"),
Lambda(
"fileMetaRef",
Create(Collection("fileMeta_gitRefs"), {
data: {
gitRefID: Var("createdGitRefRef"),
fileMetaID: Var("fileMetaRef")
}
})
)
)
},
{
gitRef: Var("createdGitRefDoc"),
newFiles: Var("createdFilesMeta"),
newLinks: Union(
Var("createdLinksOnNew"),
Var("createdLinksOnExisting")
)
}
)
)
)
}
{
name: "shaAndRef_of_FileMeta_by_sha",
unique: false,
serialized: true,
source: "FileMeta",
terms: [
{
field: ["data", "sha"]
}
],
values: [
{
field: ["data", "sha"]
},
{
field: ["ref"]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment