@ronin/compiler 0.17.8 → 0.17.9-leo-ron-1099-experimental-398
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +21 -0
- package/dist/index.js +127 -88
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -269,6 +269,13 @@ type ModelField = ModelFieldBasics & ({
|
|
269
269
|
/** Whether the field should be related to one record, or many records. */
|
270
270
|
kind: 'many';
|
271
271
|
});
|
272
|
+
/** An extended version of `ModelField`, for internal use within the compiler. */
|
273
|
+
type InternalModelField = ModelField & {
|
274
|
+
/** The path on the final record where the value of the field should be mounted. */
|
275
|
+
mountingPath: string;
|
276
|
+
/** A custom value that was provided in the query, which is not stored in the DB. */
|
277
|
+
mountedValue?: unknown;
|
278
|
+
};
|
272
279
|
type ModelIndexField<T extends ModelEntityList<ModelField> = ModelEntityList<ModelField>> = {
|
273
280
|
/** The collating sequence used for text placed inside the field. */
|
274
281
|
collation?: ModelFieldCollation;
|
@@ -423,6 +430,20 @@ declare class Transaction {
|
|
423
430
|
statements: Array<Statement>;
|
424
431
|
models: Array<Model>;
|
425
432
|
constructor(queries: Array<Query>, options?: TransactionOptions);
|
433
|
+
/**
|
434
|
+
* Formats an individual result of a query (each query has one individual result).
|
435
|
+
*
|
436
|
+
* @param queryType - The type of query that is being executed.
|
437
|
+
* @param queryInstructions - The instructions of the query that is being executed.
|
438
|
+
* @param model - The model for which the query is being executed.
|
439
|
+
* @param rows - The rows that were returned from the database for the query (in the
|
440
|
+
* form of an array containing arrays that contain strings).
|
441
|
+
* @param selectedFields - The model fields that were selected by the query.
|
442
|
+
* @param single - Whether a single or multiple records are being affected by the query.
|
443
|
+
*
|
444
|
+
* @returns A formatted RONIN result for a particular query.
|
445
|
+
*/
|
446
|
+
formatIndividualResult<RecordType>(queryType: QueryType, queryInstructions: CombinedInstructions, model: Model, rows: Array<Array<RawRow>>, selectedFields: Array<InternalModelField>, single: boolean): RegularResult<RecordType>;
|
426
447
|
formatResults<RecordType>(results: Array<Array<ObjectRow>>, raw?: false): Array<Result<RecordType>>;
|
427
448
|
formatResults<RecordType>(results: Array<Array<RawRow>>, raw?: true): Array<Result<RecordType>>;
|
428
449
|
}
|
package/dist/index.js
CHANGED
@@ -1035,7 +1035,12 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
|
|
1035
1035
|
}
|
1036
1036
|
);
|
1037
1037
|
if (query === null)
|
1038
|
-
return {
|
1038
|
+
return {
|
1039
|
+
dependencies: [],
|
1040
|
+
main: dependencyStatements[0],
|
1041
|
+
selectedFields: [],
|
1042
|
+
model: ROOT_MODEL_WITH_ATTRIBUTES
|
1043
|
+
};
|
1039
1044
|
const parsedQuery = splitQuery(query);
|
1040
1045
|
const { queryType, queryModel, queryInstructions } = parsedQuery;
|
1041
1046
|
const model = getModelBySlug(models, queryModel);
|
@@ -1192,7 +1197,8 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
|
|
1192
1197
|
return {
|
1193
1198
|
dependencies: dependencyStatements,
|
1194
1199
|
main: mainStatement,
|
1195
|
-
selectedFields
|
1200
|
+
selectedFields,
|
1201
|
+
model
|
1196
1202
|
};
|
1197
1203
|
};
|
1198
1204
|
|
@@ -2065,21 +2071,25 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query,
|
|
2065
2071
|
var Transaction = class {
|
2066
2072
|
statements = [];
|
2067
2073
|
models = [];
|
2068
|
-
#
|
2074
|
+
#internalQueries = [];
|
2069
2075
|
constructor(queries, options) {
|
2070
2076
|
const models = options?.models || [];
|
2071
|
-
this.#
|
2077
|
+
this.#internalQueries = queries.map((query) => ({
|
2078
|
+
query,
|
2079
|
+
selectedFields: [],
|
2080
|
+
models: []
|
2081
|
+
}));
|
2082
|
+
this.#compileQueries(models, options);
|
2072
2083
|
}
|
2073
2084
|
/**
|
2074
2085
|
* Composes SQL statements for the provided RONIN queries.
|
2075
2086
|
*
|
2076
|
-
* @param queries - The RONIN queries for which SQL statements should be composed.
|
2077
2087
|
* @param models - A list of models.
|
2078
2088
|
* @param options - Additional options to adjust the behavior of the statement generation.
|
2079
2089
|
*
|
2080
2090
|
* @returns The composed SQL statements.
|
2081
2091
|
*/
|
2082
|
-
#compileQueries = (
|
2092
|
+
#compileQueries = (models, options) => {
|
2083
2093
|
const modelsWithAttributes = models.map((model) => {
|
2084
2094
|
return addDefaultModelAttributes(model, true);
|
2085
2095
|
});
|
@@ -2095,30 +2105,33 @@ var Transaction = class {
|
|
2095
2105
|
return addDefaultModelPresets(modelsWithFields, model);
|
2096
2106
|
});
|
2097
2107
|
const statements = [];
|
2098
|
-
const expandedQueries =
|
2108
|
+
const expandedQueries = this.#internalQueries.flatMap(({ query }, index) => {
|
2099
2109
|
const { queryType, queryModel, queryInstructions } = splitQuery(query);
|
2100
2110
|
if (queryModel === "all") {
|
2101
2111
|
const { for: forInstruction, ...restInstructions } = queryInstructions || {};
|
2102
|
-
let modelList =
|
2112
|
+
let modelList = modelsWithPresets.filter((model) => {
|
2113
|
+
return model.slug !== ROOT_MODEL.slug;
|
2114
|
+
});
|
2103
2115
|
if (forInstruction) {
|
2104
|
-
const mainModel = getModelBySlug(
|
2116
|
+
const mainModel = getModelBySlug(modelList, forInstruction);
|
2105
2117
|
modelList = Object.values(mainModel.fields || {}).filter((field) => field.type === "link").map((field) => {
|
2106
|
-
return
|
2118
|
+
return modelList.find(
|
2107
2119
|
(model) => model.slug === field.target
|
2108
2120
|
);
|
2109
2121
|
});
|
2110
2122
|
}
|
2123
|
+
this.#internalQueries[index].models = modelList;
|
2111
2124
|
return modelList.map((model) => {
|
2112
2125
|
const query2 = {
|
2113
2126
|
[queryType]: { [model.pluralSlug]: restInstructions }
|
2114
2127
|
};
|
2115
|
-
return { query: query2,
|
2128
|
+
return { query: query2, index };
|
2116
2129
|
});
|
2117
2130
|
}
|
2118
|
-
return { query };
|
2131
|
+
return { query, index };
|
2119
2132
|
});
|
2120
|
-
for (const { query,
|
2121
|
-
const { dependencies, main, selectedFields } = compileQueryInput(
|
2133
|
+
for (const { query, index } of expandedQueries) {
|
2134
|
+
const { dependencies, main, selectedFields, model } = compileQueryInput(
|
2122
2135
|
query,
|
2123
2136
|
modelsWithPresets,
|
2124
2137
|
options?.inlineParams ? null : [],
|
@@ -2129,14 +2142,10 @@ var Transaction = class {
|
|
2129
2142
|
const postDependencies = dependencies.map(({ after, ...rest }) => after ? rest : null).filter((item) => item != null);
|
2130
2143
|
const subStatements = [...preDependencies, main, ...postDependencies];
|
2131
2144
|
this.statements.push(...subStatements);
|
2132
|
-
this.#
|
2133
|
-
|
2134
|
-
|
2135
|
-
|
2136
|
-
selectedFields,
|
2137
|
-
expansionIndex
|
2138
|
-
}))
|
2139
|
-
);
|
2145
|
+
this.#internalQueries[index].selectedFields = selectedFields;
|
2146
|
+
if (this.#internalQueries[index].models.length === 0) {
|
2147
|
+
this.#internalQueries[index].models = [model];
|
2148
|
+
}
|
2140
2149
|
}
|
2141
2150
|
this.models = modelsWithPresets;
|
2142
2151
|
return statements;
|
@@ -2210,6 +2219,68 @@ var Transaction = class {
|
|
2210
2219
|
}
|
2211
2220
|
return single ? records[0] : records;
|
2212
2221
|
}
|
2222
|
+
/**
|
2223
|
+
* Formats an individual result of a query (each query has one individual result).
|
2224
|
+
*
|
2225
|
+
* @param queryType - The type of query that is being executed.
|
2226
|
+
* @param queryInstructions - The instructions of the query that is being executed.
|
2227
|
+
* @param model - The model for which the query is being executed.
|
2228
|
+
* @param rows - The rows that were returned from the database for the query (in the
|
2229
|
+
* form of an array containing arrays that contain strings).
|
2230
|
+
* @param selectedFields - The model fields that were selected by the query.
|
2231
|
+
* @param single - Whether a single or multiple records are being affected by the query.
|
2232
|
+
*
|
2233
|
+
* @returns A formatted RONIN result for a particular query.
|
2234
|
+
*/
|
2235
|
+
formatIndividualResult(queryType, queryInstructions, model, rows, selectedFields, single) {
|
2236
|
+
const modelFields = Object.fromEntries(
|
2237
|
+
Object.entries(model.fields).map(([slug, rest]) => [slug, rest.type])
|
2238
|
+
);
|
2239
|
+
if (queryType === "count") {
|
2240
|
+
return { amount: rows[0][0] };
|
2241
|
+
}
|
2242
|
+
if (single) {
|
2243
|
+
return {
|
2244
|
+
record: rows[0] ? this.#formatRows(selectedFields, rows, true) : null,
|
2245
|
+
modelFields
|
2246
|
+
};
|
2247
|
+
}
|
2248
|
+
const pageSize = queryInstructions?.limitedTo;
|
2249
|
+
const result = {
|
2250
|
+
records: this.#formatRows(selectedFields, rows, false),
|
2251
|
+
modelFields
|
2252
|
+
};
|
2253
|
+
if (pageSize && result.records.length > 0) {
|
2254
|
+
if (result.records.length > pageSize) {
|
2255
|
+
if (queryInstructions?.before) {
|
2256
|
+
result.records.shift();
|
2257
|
+
} else {
|
2258
|
+
result.records.pop();
|
2259
|
+
}
|
2260
|
+
const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
|
2261
|
+
const lastRecord = result.records.at(
|
2262
|
+
direction === "moreAfter" ? -1 : 0
|
2263
|
+
);
|
2264
|
+
result[direction] = generatePaginationCursor(
|
2265
|
+
model,
|
2266
|
+
queryInstructions.orderedBy,
|
2267
|
+
lastRecord
|
2268
|
+
);
|
2269
|
+
}
|
2270
|
+
if (queryInstructions?.before || queryInstructions?.after) {
|
2271
|
+
const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
|
2272
|
+
const firstRecord = result.records.at(
|
2273
|
+
direction === "moreAfter" ? -1 : 0
|
2274
|
+
);
|
2275
|
+
result[direction] = generatePaginationCursor(
|
2276
|
+
model,
|
2277
|
+
queryInstructions.orderedBy,
|
2278
|
+
firstRecord
|
2279
|
+
);
|
2280
|
+
}
|
2281
|
+
}
|
2282
|
+
return result;
|
2283
|
+
}
|
2213
2284
|
/**
|
2214
2285
|
* Format the results returned from the database into RONIN records.
|
2215
2286
|
*
|
@@ -2223,78 +2294,46 @@ var Transaction = class {
|
|
2223
2294
|
* RONIN record, an array of RONIN records, or a RONIN count result.
|
2224
2295
|
*/
|
2225
2296
|
formatResults(results, raw = false) {
|
2226
|
-
const
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2231
|
-
return Object.values(row);
|
2232
|
-
});
|
2233
|
-
});
|
2234
|
-
return normalizedResults.reduce(
|
2235
|
-
(finalResults, rows, index) => {
|
2236
|
-
const { returning, query, selectedFields, expansionIndex } = this.#internalStatements[index];
|
2237
|
-
if (!returning) return finalResults;
|
2238
|
-
const addResult = (result2) => {
|
2239
|
-
if (typeof expansionIndex !== "undefined") {
|
2240
|
-
let match = finalResults[expansionIndex];
|
2241
|
-
if (!match) match = finalResults[expansionIndex] = { models: {} };
|
2242
|
-
match.models[queryModel] = result2;
|
2243
|
-
} else {
|
2244
|
-
finalResults.push(result2);
|
2245
|
-
}
|
2246
|
-
return finalResults;
|
2247
|
-
};
|
2297
|
+
const cleanResults = results.filter((_, index) => this.statements[index].returning);
|
2298
|
+
let resultIndex = 0;
|
2299
|
+
return this.#internalQueries.reduce(
|
2300
|
+
(finalResults, internalQuery) => {
|
2301
|
+
const { query, selectedFields, models: affectedModels } = internalQuery;
|
2248
2302
|
const { queryType, queryModel, queryInstructions } = splitQuery(query);
|
2249
|
-
const
|
2250
|
-
|
2251
|
-
|
2252
|
-
|
2253
|
-
|
2254
|
-
return addResult({ amount: rows[0][0] });
|
2255
|
-
}
|
2256
|
-
const single = queryModel !== model.pluralSlug;
|
2257
|
-
if (single) {
|
2258
|
-
return addResult({
|
2259
|
-
record: rows[0] ? this.#formatRows(selectedFields, rows, true) : null,
|
2260
|
-
modelFields
|
2303
|
+
const absoluteResults = raw ? cleanResults : cleanResults.map((rows) => {
|
2304
|
+
return rows.map((row) => {
|
2305
|
+
if (Array.isArray(row)) return row;
|
2306
|
+
if (queryType === "count") return [row.amount];
|
2307
|
+
return Object.values(row);
|
2261
2308
|
});
|
2262
|
-
}
|
2263
|
-
|
2264
|
-
|
2265
|
-
|
2266
|
-
|
2267
|
-
|
2268
|
-
|
2269
|
-
if (result.records.length > pageSize) {
|
2270
|
-
if (queryInstructions?.before) {
|
2271
|
-
result.records.shift();
|
2272
|
-
} else {
|
2273
|
-
result.records.pop();
|
2274
|
-
}
|
2275
|
-
const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
|
2276
|
-
const lastRecord = result.records.at(
|
2277
|
-
direction === "moreAfter" ? -1 : 0
|
2278
|
-
);
|
2279
|
-
result[direction] = generatePaginationCursor(
|
2280
|
-
model,
|
2281
|
-
queryInstructions.orderedBy,
|
2282
|
-
lastRecord
|
2283
|
-
);
|
2284
|
-
}
|
2285
|
-
if (queryInstructions?.before || queryInstructions?.after) {
|
2286
|
-
const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
|
2287
|
-
const firstRecord = result.records.at(
|
2288
|
-
direction === "moreAfter" ? -1 : 0
|
2289
|
-
);
|
2290
|
-
result[direction] = generatePaginationCursor(
|
2309
|
+
});
|
2310
|
+
if (queryModel === "all") {
|
2311
|
+
const models = {};
|
2312
|
+
for (const model of affectedModels) {
|
2313
|
+
const result = this.formatIndividualResult(
|
2314
|
+
queryType,
|
2315
|
+
queryInstructions,
|
2291
2316
|
model,
|
2292
|
-
|
2293
|
-
|
2317
|
+
absoluteResults[resultIndex++],
|
2318
|
+
selectedFields,
|
2319
|
+
false
|
2294
2320
|
);
|
2321
|
+
models[model.pluralSlug] = result;
|
2295
2322
|
}
|
2323
|
+
finalResults.push({ models });
|
2324
|
+
} else {
|
2325
|
+
const model = affectedModels[0];
|
2326
|
+
const result = this.formatIndividualResult(
|
2327
|
+
queryType,
|
2328
|
+
queryInstructions,
|
2329
|
+
model,
|
2330
|
+
absoluteResults[resultIndex++],
|
2331
|
+
selectedFields,
|
2332
|
+
queryModel !== model.pluralSlug
|
2333
|
+
);
|
2334
|
+
finalResults.push(result);
|
2296
2335
|
}
|
2297
|
-
return
|
2336
|
+
return finalResults;
|
2298
2337
|
},
|
2299
2338
|
[]
|
2300
2339
|
);
|