Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure correct model-specific queries #156

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ class Transaction {
});
}

// Track which models are being addressed by the query, in order to ensure that
// its results are being formatted correctly.
this.#internalQueries[index].models = modelList;

return modelList.map((model) => {
const instructions = Object.assign(
{},
Expand Down Expand Up @@ -181,11 +177,8 @@ class Transaction {
this.statements.push(...subStatements);

// Update the internal query with additional information.
this.#internalQueries[index].selectedFields = selectedFields;

if (this.#internalQueries[index].models.length === 0) {
this.#internalQueries[index].models = [model];
}
this.#internalQueries[index].selectedFields.push(selectedFields);
this.#internalQueries[index].models.push(model);
}

this.models = modelsWithPresets;
Expand Down Expand Up @@ -481,7 +474,10 @@ class Transaction {
const { on: onInstruction, ...restInstructions } = (queryInstructions ||
{}) as AllQueryInstructions;

for (const model of affectedModels) {
for (let index = 0; index < affectedModels.length; index++) {
const model = affectedModels[index];
const fields = selectedFields[index];

const instructions = Object.assign(
{},
restInstructions,
Expand All @@ -493,7 +489,7 @@ class Transaction {
instructions,
model,
absoluteResults[resultIndex++],
selectedFields,
fields,
false,
);

Expand All @@ -503,13 +499,14 @@ class Transaction {
finalResults.push({ models });
} else {
const model = affectedModels[0];
const fields = selectedFields[0];

const result = this.formatIndividualResult<RecordType>(
queryType,
queryInstructions,
model,
absoluteResults[resultIndex++],
selectedFields,
fields,
queryModel !== model.pluralSlug,
);

Expand Down
2 changes: 1 addition & 1 deletion src/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export interface InternalQuery {
/** The RONIN query for which the SQL statement was generated. */
query: Query;
/** The RONIN model fields that were selected for the SQL statement. */
selectedFields: Array<InternalModelField>;
selectedFields: Array<Array<InternalModelField>>;
/** The RONIN models that are being affected by the query. */
models: Array<PrivateModel>;
}
Expand Down
11 changes: 4 additions & 7 deletions tests/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ test('get all records of all models with model-specific instructions', async ()
on: {
teams: {
limitedTo: 10,
// It is important to test an instruction here that is not already present
// in the global instructions, to make sure it is working correctly.
selecting: ['id'],
},
},
},
Expand All @@ -415,7 +418,7 @@ test('get all records of all models with model-specific instructions', async ()
returning: true,
},
{
statement: `SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy" FROM "teams" ORDER BY "ronin.createdAt" DESC LIMIT 11`,
statement: `SELECT "id" FROM "teams" ORDER BY "ronin.createdAt" DESC LIMIT 11`,
params: [],
returning: true,
},
Expand Down Expand Up @@ -446,12 +449,6 @@ test('get all records of all models with model-specific instructions', async ()
teams: {
records: new Array(2).fill({
id: expect.stringMatching(RECORD_ID_REGEX),
ronin: {
createdAt: expect.stringMatching(RECORD_TIMESTAMP_REGEX),
createdBy: null,
updatedAt: expect.stringMatching(RECORD_TIMESTAMP_REGEX),
updatedBy: null,
},
}),
modelFields: expect.objectContaining({
id: 'string',
Expand Down
Loading