Skip to content

Commit

Permalink
maxParameters 100 D1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars-Erik Roald committed Feb 14, 2025
1 parent 2b70c7c commit a71b3d5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/d1/newTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function newResolveTransaction(domain, pool, { readonly = false } = {}) {
rdb.pool = pool;
}
rdb.engine = 'sqlite';
rdb.maxParameters = 100;
rdb.encodeBoolean = encodeBoolean;
rdb.decodeJSON = decodeJSON;
rdb.encodeJSON = JSON.stringify;
Expand Down
73 changes: 52 additions & 21 deletions src/getManyDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const newQuery = require('./getManyDto/newQuery');
const negotiateRawSqlFilter = require('./table/column/negotiateRawSqlFilter');
const strategyToSpan = require('./table/strategyToSpan');
const executeQueries = require('./table/executeQueries');
const getSessionSingleton = require('./table/getSessionSingleton');

async function getManyDto(context, table, filter, strategy, spanFromParent, updateParent) {
filter = negotiateRawSqlFilter(context, filter, table);
if (strategy && strategy.where) {
let arg = typeof strategy.where === 'function' ? strategy.where(context, table) : strategy.where;//todo
let arg = typeof strategy.where === 'function' ? strategy.where(context, table) : strategy.where;
filter = filter.and(context, arg);
}

Expand Down Expand Up @@ -179,7 +180,6 @@ async function decode(context, strategy, span, rows, keys = rows.length > 0 ? Ob
}
}
const column = columns[j];
//todo
outRow[column.alias] = column.decode(context, row[keys[j]]);
}

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

outRows[i] = outRow;
// if (parentRows)
// parentRows[i][parentProp] = outRow;
if (updateParent)
updateParent(outRow, i);
if (shouldCreateMap) {
Expand All @@ -212,8 +210,6 @@ async function decode(context, strategy, span, rows, keys = rows.length > 0 ? Ob
all.push(decodeManyRelations(context, strategy, span));
all.push(decodeRelations2(context, strategy, span, rows, outRows, keys));
}
// decodeRelations2(strategy, span, rows, outRows, keys);
// }
else
all.push(decodeRelations2(context, strategy, span, rows, outRows, keys));

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

async function decodeManyRelations(context, strategy, span) {
const maxParameters = getSessionSingleton(context, 'maxParameters');
const maxRows = maxParameters
? maxParameters * span.table._primaryColumns.length
: undefined;

const promises = [];
const c = {};
c.visitJoin = () => { };
c.visitOne = c.visitJoin;

// Helper function to split an array into chunks
function chunk(array, size) {
const results = [];
for (let i = 0; i < array.length; i += size) {
results.push(array.slice(i, i + size));
}
return results;
}

c.visitMany = function(leg) {
const name = leg.name;
const table = span.table;
const relation = table._relations[name];
const rowsMap = span._rowsMap;

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

const p = getManyDto(context, relation.childTable, filter, strategy[name], leg.span, updateParent);
// .then(subRows => {
// for (let i = 0; i < subRows.length; i++) {
// const key = extractKey(subRows[i]);
// const parentRow = extractFromMap(key);
// parentRow[name].push(subRows[i]);
// }
// });
// If maxRows is defined, chunk the IDs before calling getManyDto
if (maxRows) {
const chunkedIds = chunk(span._ids, maxRows);
for (const idsChunk of chunkedIds) {
const filter = createOneFilter(context, relation, idsChunk);
const p = getManyDto(
context,
relation.childTable,
filter,
strategy[name],
leg.span,
updateParent
);
promises.push(p);
}
} else {
// Otherwise, do the entire set in one go
const filter = createOneFilter(context, relation, span._ids);
const p = getManyDto(
context,
relation.childTable,
filter,
strategy[name],
leg.span,
updateParent
);
promises.push(p);
}

function updateParent(subRow) {
const key = extractKey(subRow);
const parentRow = extractFromMap(key);
parentRow[name].push(subRow);
}

promises.push(p);
};

function createExtractKey(leg) {
if (leg.columns.length === 1) {
const alias = leg.columns[0].alias;
return (row) => row[alias];
}
else {
} else {
const aliases = leg.columns.map(column => column.alias);
return (row) => aliases.map(alias => row[alias]);
}
}

function createExtractFromMap(map, primaryColumns) {
if (primaryColumns.length === 1)
if (primaryColumns.length === 1) {
return (key) => map.get(key);
else
} else {
return getFromMap.bind(null, map, primaryColumns);
}
}

// Visit all legs
span.legs.forEach(onEachLeg);

function onEachLeg(leg) {
leg.accept(c);
}

// Wait until all promises resolve
await Promise.all(promises);
}

async function decodeRelations2(context, strategy, span, rawRows, resultRows, keys) {
const c = {};
c.visitJoin = function(leg) {
Expand Down

0 comments on commit a71b3d5

Please sign in to comment.