Skip to content

Commit

Permalink
Ensure even cleaner logic
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Feb 24, 2025
1 parent 744170c commit 9ed5558
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
37 changes: 21 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ class Transaction {
constructor(queries: Array<Query>, options?: TransactionOptions) {
const models = options?.models || [];

this.#internalQueries = queries.map((query) => ({ query, selectedFields: [] }));
this.#internalQueries = queries.map((query) => ({
query,
selectedFields: [],
models: [],
}));

this.#compileQueries(models, options);
}

Expand Down Expand Up @@ -105,28 +110,28 @@ class Transaction {
const { for: forInstruction, ...restInstructions } = (queryInstructions ||
{}) as AllQueryInstructions;

let modelList = modelsWithAttributes;
let modelList = modelsWithPresets.filter((model) => {
return model.slug !== ROOT_MODEL.slug;
});

// If a `for` instruction was provided, that means we only want to select the
// related models of the model that was provided in `for`, instead of selecting
// all models at once.
if (forInstruction) {
const mainModel = getModelBySlug(modelsWithAttributes, forInstruction);
const mainModel = getModelBySlug(modelList, forInstruction);

modelList = Object.values(mainModel.fields || {})
.filter((field) => field.type === 'link')
.map((field) => {
return modelsWithAttributes.find(
return modelList.find(
(model) => model.slug === field.target,
) as PrivateModel;
});
}

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

return modelList.map((model) => {
const query: Query = {
Expand All @@ -141,7 +146,7 @@ class Transaction {
});

for (const { query, index } of expandedQueries) {
const { dependencies, main, selectedFields } = compileQueryInput(
const { dependencies, main, selectedFields, model } = compileQueryInput(
query,
modelsWithPresets,
options?.inlineParams ? null : [],
Expand All @@ -168,6 +173,10 @@ class Transaction {

// 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.models = modelsWithPresets;
Expand Down Expand Up @@ -431,7 +440,7 @@ class Transaction {

return this.#internalQueries.reduce(
(finalResults: Array<Result<RecordType>>, internalQuery) => {
const { query, selectedFields, affectedModels } = internalQuery;
const { query, selectedFields, models: affectedModels } = internalQuery;
const { queryType, queryModel, queryInstructions } = splitQuery(query);

// If the provided results are raw (rows being arrays of values, which is the most
Expand All @@ -457,14 +466,10 @@ class Transaction {
});
}) as Array<Array<Array<RawRow>>>);

if (queryModel === 'all' && affectedModels) {
const modelList = affectedModels.map((slug) => {
return getModelBySlug(this.models, slug);
});

if (queryModel === 'all') {
const models: ExpandedResult<RecordType>['models'] = {};

for (const model of modelList) {
for (const model of affectedModels) {
const result = this.formatIndividualResult<RecordType>(
queryType,
queryInstructions,
Expand All @@ -479,7 +484,7 @@ class Transaction {

finalResults.push({ models });
} else {
const model = getModelBySlug(this.models, queryModel);
const model = affectedModels[0];

const result = this.formatIndividualResult<RecordType>(
queryType,
Expand Down
8 changes: 3 additions & 5 deletions src/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ModelIndex,
ModelPreset,
ModelTrigger,
Model as PrivateModel,
PublicModel,
} from '@/src/types/model';
import { QUERY_SYMBOLS } from '@/src/utils/helpers';
Expand Down Expand Up @@ -227,11 +228,8 @@ export interface InternalQuery {
query: Query;
/** The RONIN model fields that were selected for the SQL statement. */
selectedFields: Array<InternalModelField>;
/**
* If the query addresses multiple models at once, this contains the list of models
* that are being addressed.
*/
affectedModels?: Array<PublicModel['slug']>;
/** The RONIN models that are being affected by the query. */
models: Array<PrivateModel>;
}

export interface InternalDependencyStatement extends Statement {
Expand Down
9 changes: 8 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { handleSelecting } from '@/src/instructions/selecting';
import { handleTo } from '@/src/instructions/to';
import { handleUsing } from '@/src/instructions/using';
import { handleWith } from '@/src/instructions/with';
import { getModelBySlug, transformMetaQuery } from '@/src/model';
import {
ROOT_MODEL_WITH_ATTRIBUTES,
getModelBySlug,
transformMetaQuery,
} from '@/src/model';
import type { InternalModelField, Model } from '@/src/types/model';
import type {
CombinedInstructions,
Expand Down Expand Up @@ -57,6 +61,7 @@ export const compileQueryInput = (
dependencies: Array<InternalDependencyStatement>;
main: Statement;
selectedFields: Array<InternalModelField>;
model: Model;
} => {
// A list of write statements that are required to be executed before the main read
// statement. Their output is not relevant for the main statement, as they are merely
Expand All @@ -83,6 +88,7 @@ export const compileQueryInput = (
dependencies: [],
main: dependencyStatements[0],
selectedFields: [],
model: ROOT_MODEL_WITH_ATTRIBUTES,
};

// Split out the individual components of the query.
Expand Down Expand Up @@ -334,5 +340,6 @@ export const compileQueryInput = (
dependencies: dependencyStatements,
main: mainStatement,
selectedFields,
model,
};
};

0 comments on commit 9ed5558

Please sign in to comment.