Hey there,
started today with Netlify & FaunaDb, while I’m sure it will be great once it’s running, currently I feel super overwhelmed with all the FaunaDB lingo
I am trying to upsert
data received from an API call, inside a Netlify function.
Data received in POST request:
[
{
key: "f90b38d9-77d7-47f1-920c-79b2b4bb2c8d",
value: {
"542e2364-5911-4b7e-b5f8-e29a5461cca0": 0,
"652e2364-5911-4b7e-b5f8-e29a5461sxa2": 0,
},
},
{
key: "f90b38d9-77d7-47f1-920c-79b2b4bb2c8d",
value: {
"542e2364-5911-4b7e-b5f8-e29a5461cca0": 0,
"652e2364-5911-4b7e-b5f8-e29a5461sxa2": 0,
},
}
]
This data is variable in length, so it can be one object, or many.
Now I’m trying to save this (With my extremely limited FaunDB knowledge),
in a collection, so that I can later on query it by looking up the key
uuid.
I’ve found a post in here #488 that explains how to do it, I’ve adjusted it, but no dice.
Getting this error:
description: 'Value not found at path [data,key].',
requestResult: RequestResult {
method: 'POST',
path: '',
query: null,
requestRaw: '{"map":{"lambda":["d"],"expr":{"if":{"exists":{"match":{"index":"landing_page_redirects_search_by_uuid"},"terms":{"select":["data","key"],"from":{"var":"d"}}}},"then":{"replace":{"select":"key","from":{"get":{"match":{"index":"landing_page_redirects_search_by_uuid"},"terms":{"select":["data","key"],"from":{"var":"d"}}}}},"params":{"var":"d"}},"else":{"create":{"collection":"landing_page_redirects"},"params":{"var":"d"}}}},"collection":[{"object":{"key":"f90b38d9-77d7-47f1-920c-79b2b4bb2c8d","value":{"object":{"542e2364-5911-4b7e-b5f8-e29a5461cca0":0}}}}]}',
requestContent: Expr { raw: [Object] },
responseRaw: '{"errors":[{"position":["map","expr","if","exists","terms","from"],"code":"value not found","description":"Value not found at path [data,key]."}]}',
When using this method:
const multiUpsert = (data) => {
return q.Map(
data,
q.Lambda(
["d"],
q.If(
q.Exists(
q.Match(
q.Index(indexMap[params.collection]),
q.Select(["data", "key"], q.Var("d"))
)
),
q.Replace(
q.Select(
"key",
q.Get(
q.Match(
q.Index(indexMap[params.collection]),
q.Select(["data", "key"], q.Var("d"))
)
)
),
q.Var("d")
),
q.Create(q.Collection(collectionMap[params.collection]), q.Var("d"))
)
)
);
};
let res;
try {
res = await client.query(multiUpsert(data));
console.log(res);
return {
statusCode: 200,
body: JSON.stringify("ok"),
};
} catch (err) {
console.error(err);
return {
statusCode: 500,
body: JSON.stringify("Could not perform this action"),
};
}
And this is my schema:
client
.query(
CreateCollection({ name: "landing_page_redirects", history_days: 0 })
)
.then(() => {
client.query(
CreateCollection({ name: "offer_redirects", history_days: 0 })
);
})
.then(() => {
return client.query(
CreateIndex({
name: "landing_page_redirects_search_by_uuid",
permissions: { read: "public" },
source: Collection("landing_page_redirects"),
terms: [{ field: ["data", "key"] }],
values: [{ field: ["data", "key"] }, { field: ["data", "value"] }],
unique: true,
})
);
})
.then(() => {
return client.query(
CreateIndex({
name: "offer_redirects_search_by_uuid",
permissions: { read: "public" },
source: Collection("offer_redirects"),
terms: [{ field: ["data", "key"] }],
values: [{ field: ["data", "key"] }, { field: ["data", "value"] }],
unique: true,
})
);
})
Where am I going wrong here? (I know, probably everywhere haha)