Skip to content

Commit a71b3d5

Browse files
author
Lars-Erik Roald
committed
maxParameters 100 D1
1 parent 2b70c7c commit a71b3d5

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

src/d1/newTransaction.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function newResolveTransaction(domain, pool, { readonly = false } = {}) {
1414
rdb.pool = pool;
1515
}
1616
rdb.engine = 'sqlite';
17+
rdb.maxParameters = 100;
1718
rdb.encodeBoolean = encodeBoolean;
1819
rdb.decodeJSON = decodeJSON;
1920
rdb.encodeJSON = JSON.stringify;

src/getManyDto.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const newQuery = require('./getManyDto/newQuery');
33
const negotiateRawSqlFilter = require('./table/column/negotiateRawSqlFilter');
44
const strategyToSpan = require('./table/strategyToSpan');
55
const executeQueries = require('./table/executeQueries');
6+
const getSessionSingleton = require('./table/getSessionSingleton');
67

78
async function getManyDto(context, table, filter, strategy, spanFromParent, updateParent) {
89
filter = negotiateRawSqlFilter(context, filter, table);
910
if (strategy && strategy.where) {
10-
let arg = typeof strategy.where === 'function' ? strategy.where(context, table) : strategy.where;//todo
11+
let arg = typeof strategy.where === 'function' ? strategy.where(context, table) : strategy.where;
1112
filter = filter.and(context, arg);
1213
}
1314

@@ -179,7 +180,6 @@ async function decode(context, strategy, span, rows, keys = rows.length > 0 ? Ob
179180
}
180181
}
181182
const column = columns[j];
182-
//todo
183183
outRow[column.alias] = column.decode(context, row[keys[j]]);
184184
}
185185

@@ -190,8 +190,6 @@ async function decode(context, strategy, span, rows, keys = rows.length > 0 ? Ob
190190
}
191191

192192
outRows[i] = outRow;
193-
// if (parentRows)
194-
// parentRows[i][parentProp] = outRow;
195193
if (updateParent)
196194
updateParent(outRow, i);
197195
if (shouldCreateMap) {
@@ -212,8 +210,6 @@ async function decode(context, strategy, span, rows, keys = rows.length > 0 ? Ob
212210
all.push(decodeManyRelations(context, strategy, span));
213211
all.push(decodeRelations2(context, strategy, span, rows, outRows, keys));
214212
}
215-
// decodeRelations2(strategy, span, rows, outRows, keys);
216-
// }
217213
else
218214
all.push(decodeRelations2(context, strategy, span, rows, outRows, keys));
219215

@@ -242,64 +238,99 @@ async function decode(context, strategy, span, rows, keys = rows.length > 0 ? Ob
242238
}
243239

244240
async function decodeManyRelations(context, strategy, span) {
241+
const maxParameters = getSessionSingleton(context, 'maxParameters');
242+
const maxRows = maxParameters
243+
? maxParameters * span.table._primaryColumns.length
244+
: undefined;
245+
245246
const promises = [];
246247
const c = {};
247248
c.visitJoin = () => { };
248249
c.visitOne = c.visitJoin;
249250

251+
// Helper function to split an array into chunks
252+
function chunk(array, size) {
253+
const results = [];
254+
for (let i = 0; i < array.length; i += size) {
255+
results.push(array.slice(i, i + size));
256+
}
257+
return results;
258+
}
259+
250260
c.visitMany = function(leg) {
251261
const name = leg.name;
252262
const table = span.table;
253263
const relation = table._relations[name];
254264
const rowsMap = span._rowsMap;
255265

256-
const filter = createOneFilter(context, relation, span._ids);
257266
const extractKey = createExtractKey(leg);
258267
const extractFromMap = createExtractFromMap(rowsMap, table._primaryColumns);
259268

260-
const p = getManyDto(context, relation.childTable, filter, strategy[name], leg.span, updateParent);
261-
// .then(subRows => {
262-
// for (let i = 0; i < subRows.length; i++) {
263-
// const key = extractKey(subRows[i]);
264-
// const parentRow = extractFromMap(key);
265-
// parentRow[name].push(subRows[i]);
266-
// }
267-
// });
269+
// If maxRows is defined, chunk the IDs before calling getManyDto
270+
if (maxRows) {
271+
const chunkedIds = chunk(span._ids, maxRows);
272+
for (const idsChunk of chunkedIds) {
273+
const filter = createOneFilter(context, relation, idsChunk);
274+
const p = getManyDto(
275+
context,
276+
relation.childTable,
277+
filter,
278+
strategy[name],
279+
leg.span,
280+
updateParent
281+
);
282+
promises.push(p);
283+
}
284+
} else {
285+
// Otherwise, do the entire set in one go
286+
const filter = createOneFilter(context, relation, span._ids);
287+
const p = getManyDto(
288+
context,
289+
relation.childTable,
290+
filter,
291+
strategy[name],
292+
leg.span,
293+
updateParent
294+
);
295+
promises.push(p);
296+
}
268297

269298
function updateParent(subRow) {
270299
const key = extractKey(subRow);
271300
const parentRow = extractFromMap(key);
272301
parentRow[name].push(subRow);
273302
}
274-
275-
promises.push(p);
276303
};
277304

278305
function createExtractKey(leg) {
279306
if (leg.columns.length === 1) {
280307
const alias = leg.columns[0].alias;
281308
return (row) => row[alias];
282-
}
283-
else {
309+
} else {
284310
const aliases = leg.columns.map(column => column.alias);
285311
return (row) => aliases.map(alias => row[alias]);
286312
}
287313
}
314+
288315
function createExtractFromMap(map, primaryColumns) {
289-
if (primaryColumns.length === 1)
316+
if (primaryColumns.length === 1) {
290317
return (key) => map.get(key);
291-
else
318+
} else {
292319
return getFromMap.bind(null, map, primaryColumns);
320+
}
293321
}
294322

323+
// Visit all legs
295324
span.legs.forEach(onEachLeg);
296325

297326
function onEachLeg(leg) {
298327
leg.accept(c);
299328
}
300329

330+
// Wait until all promises resolve
301331
await Promise.all(promises);
302332
}
333+
303334
async function decodeRelations2(context, strategy, span, rawRows, resultRows, keys) {
304335
const c = {};
305336
c.visitJoin = function(leg) {

0 commit comments

Comments
 (0)