Hi, and thank you for your advice on separating a sync table for lines. We implemented a new sync table following your suggestion, but we’ve run into several issues that we hope you can help us with. Here’s a brief overview of our setup and the code we used:
const BATCH_SIZE = 600;
const BATCH_SIZE2 = 120;
const InvoiceLinesSchema = coda.makeObjectSchema({
properties: {
id: {
type: coda.ValueType.Number,
fromKey: "rowid",
},
quantity: {
type: coda.ValueType.Number,
fromKey: "qty",
},
totalHT: {
type: coda.ValueType.Number,
fromKey: "total_ht",
},
VATRate: {
type: coda.ValueType.Number,
fromKey: "tva_tx",
},
totalTTC: {
type: coda.ValueType.Number,
fromKey: "total_ttc",
},
fkfacture: {
type: coda.ValueType.String,
fromKey: "fk_facture",
},
},
displayProperty: "quantity",
idProperty: "id"
});
pack.addSyncTable({
name: "InvoiceLines",
description: "",
identityName: "InvoiceLine",
schema: InvoiceLinesSchema, /aquí
formula: {
name: "SyncInvoiceLines",
description: ".",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
name: "sortField",
description: "",
autocomplete: ["t.ref", "t.date"],
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "sortOrder",
description: "",
autocomplete: ["ASC", "DESC"],
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.Number,
name: "limit",
description: "Número a mostrarse como límite",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "sqlfilters",
description: "",
optional: true,
}),
],
execute: async function ([sortField, sortOrder, limit, sqlfilters], context) {
let page = Number(context.sync.continuation?.page) || 0;
let fetchedData = [ ];
let url = `${context.endpoint}api/index.php/invoices`;
while (true) {
let queryParams = {
sortfield: sortField,
sortorder: sortOrder,
limit: BATCH_SIZE2,
page: page,
sqlfilters: sqlfilters,
};
let response = await context.fetcher.fetch({
method: "GET",
url: coda.withQueryParams(url, queryParams),
headers: { "Content-Type": "application/json" },
});
let invoices = response.body;
if (!invoices.length) break;
for (let invoice of invoices) {
let lines = invoice.lines || [];
for (let line of lines) {
if (!line.rowid) continue;
fetchedData.push({
...line,
id: line.rowid,
});
}
}
page++;
if (invoices.length < BATCH_SIZE) break;
}
return {
result: fetchedData,
continuation: fetchedData.length ? { page: page } : undefined,
};
},
},
});
We’re facing three specific problems:
1.-The table functions correctly up to 1,000 rows. However, increasing the limit to 10,000 rows causes issues beyond page 8 of the GET requests, despite the URL being valid.
Error:
Overview: A continued refresh of sync table InvoiceLines failed: Error at line 714: 404 - {"error":{"code":404,"message":"Not Found: No invoice found"}}
User ID -1
Connection ID 76b169ab-6e38-47c7-8706-47d74ca54b8d
Pack Version 125
Pack ID 30791
Request ID 39539fd1-a4cf-454a-9ff9-67de34fd60f6
Created at May 14, 2024 @ 10:54:15
Request type invokeSyncFormulaRequest
Duration 1398ms
Error Error at line 714: 404 - {"error":{"code":404,"message":"Not Found: No invoice found"}}
Stack trace at async execute (code.ts:714:20) at async Object.exec [as execute] (coda:@codahq/packs-sdk:4:40497
2.- When we switch the batch size check in the loop from BATCH_SIZE
to BATCH_SIZE2
like this:
page++;
if (invoices.length < BATCH_SIZE2) break;
}
we get this error:
3.- Reducing the batch size to enhance the granularity of data fetching (e.g., setting it to intervals of 10) drastically increases the load time. And get the past 60 seconds of loading error.
Could you suggest how we might resolve these issues, particularly regarding pagination limits and batch size adjustments? Any insights or alternative strategies would be greatly appreciated. Thank you!