@ronin/compiler 0.15.0 → 0.16.0-leo-ron-1099-1-experimental-365
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 +8 -4
- package/dist/index.js +54 -24
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -391,7 +391,11 @@ type MultipleRecordResult<T = ResultRecord> = {
|
|
391
391
|
type AmountResult = {
|
392
392
|
amount: number;
|
393
393
|
};
|
394
|
-
type
|
394
|
+
type RegularResult<T = ResultRecord> = SingleRecordResult<T> | MultipleRecordResult<T> | AmountResult;
|
395
|
+
type ExpandedResult<T = ResultRecord> = {
|
396
|
+
models: Record<Model['slug'], RegularResult<T>>;
|
397
|
+
};
|
398
|
+
type Result<T = ResultRecord> = RegularResult<T> | ExpandedResult<T>;
|
395
399
|
|
396
400
|
interface TransactionOptions {
|
397
401
|
/** A list of models that already exist in the database. */
|
@@ -407,10 +411,10 @@ declare class Transaction {
|
|
407
411
|
statements: Array<Statement>;
|
408
412
|
models: Array<Model>;
|
409
413
|
constructor(queries: Array<Query>, options?: TransactionOptions);
|
410
|
-
formatResults<
|
411
|
-
formatResults<
|
414
|
+
formatResults<RecordType>(results: Array<Array<ObjectRow>>, raw?: false): Array<Result<RecordType>>;
|
415
|
+
formatResults<RecordType>(results: Array<Array<RawRow>>, raw?: true): Array<Result<RecordType>>;
|
412
416
|
}
|
413
417
|
|
414
418
|
declare const CLEAN_ROOT_MODEL: PublicModel;
|
415
419
|
|
416
|
-
export { type AddInstructions, type AddQuery, type AddInstructions as AddQueryInstructions, type AlterQuery, type CombinedInstructions, type CountInstructions, type CountQuery, type CountInstructions as CountQueryInstructions, type CreateQuery, type DropQuery, type GetInstructions, type GetQuery, type GetInstructions as GetQueryInstructions, type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, QUERY_SYMBOLS, type Query, type QueryInstructionType as QueryInstruction, type QuerySchemaType, type QueryType, CLEAN_ROOT_MODEL as ROOT_MODEL, type RemoveInstructions, type RemoveQuery, type RemoveInstructions as RemoveQueryInstructions, type Result, type ResultRecord, RoninError, type SetInstructions, type SetQuery, type SetInstructions as SetQueryInstructions, type Statement, type StoredObject, Transaction, type WithInstruction, getQuerySymbol };
|
420
|
+
export { type AddInstructions, type AddQuery, type AddInstructions as AddQueryInstructions, type AlterQuery, type CombinedInstructions, type CountInstructions, type CountQuery, type CountInstructions as CountQueryInstructions, type CreateQuery, type DropQuery, type ExpandedResult, type GetInstructions, type GetQuery, type GetInstructions as GetQueryInstructions, type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, QUERY_SYMBOLS, type Query, type QueryInstructionType as QueryInstruction, type QuerySchemaType, type QueryType, CLEAN_ROOT_MODEL as ROOT_MODEL, type RegularResult, type RemoveInstructions, type RemoveQuery, type RemoveInstructions as RemoveQueryInstructions, type Result, type ResultRecord, RoninError, type SetInstructions, type SetQuery, type SetInstructions as SetQueryInstructions, type Statement, type StoredObject, Transaction, type WithInstruction, getQuerySymbol };
|
package/dist/index.js
CHANGED
@@ -566,8 +566,14 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
|
|
566
566
|
// src/instructions/using.ts
|
567
567
|
var handleUsing = (model, instructions) => {
|
568
568
|
const normalizedUsing = Array.isArray(instructions.using) ? Object.fromEntries(instructions.using.map((presetSlug) => [presetSlug, null])) : instructions.using;
|
569
|
+
if ("links" in normalizedUsing) {
|
570
|
+
for (const field of model.fields) {
|
571
|
+
if (field.type !== "link" || field.kind === "many") continue;
|
572
|
+
normalizedUsing[field.slug] = null;
|
573
|
+
}
|
574
|
+
}
|
569
575
|
for (const presetSlug in normalizedUsing) {
|
570
|
-
if (!Object.hasOwn(normalizedUsing, presetSlug)) continue;
|
576
|
+
if (!Object.hasOwn(normalizedUsing, presetSlug) || presetSlug === "links") continue;
|
571
577
|
const arg = normalizedUsing[presetSlug];
|
572
578
|
const preset = model.presets?.find((preset2) => preset2.slug === presetSlug);
|
573
579
|
if (!preset) {
|
@@ -1509,6 +1515,7 @@ var ROOT_MODEL = {
|
|
1509
1515
|
{ slug: "presets", type: "json", defaultValue: "{}" }
|
1510
1516
|
]
|
1511
1517
|
};
|
1518
|
+
var ROOT_MODEL_WITH_ATTRIBUTES = addDefaultModelAttributes(ROOT_MODEL, true);
|
1512
1519
|
var getSystemModels = (models, model) => {
|
1513
1520
|
const addedModels = [];
|
1514
1521
|
for (const field of model.fields || []) {
|
@@ -1990,14 +1997,14 @@ var Transaction = class {
|
|
1990
1997
|
* @returns The composed SQL statements.
|
1991
1998
|
*/
|
1992
1999
|
#compileQueries = (queries, models, options) => {
|
1993
|
-
const modelsWithAttributes =
|
2000
|
+
const modelsWithAttributes = models.map((model) => {
|
1994
2001
|
return addDefaultModelAttributes(model, true);
|
1995
2002
|
});
|
1996
2003
|
const modelsWithFields = [
|
1997
2004
|
...modelsWithAttributes.flatMap((model) => {
|
1998
2005
|
return getSystemModels(modelsWithAttributes, model);
|
1999
2006
|
}),
|
2000
|
-
...modelsWithAttributes
|
2007
|
+
...[ROOT_MODEL_WITH_ATTRIBUTES, ...modelsWithAttributes]
|
2001
2008
|
].map((model) => {
|
2002
2009
|
return addDefaultModelFields(model, true);
|
2003
2010
|
});
|
@@ -2005,7 +2012,19 @@ var Transaction = class {
|
|
2005
2012
|
return addDefaultModelPresets(modelsWithFields, model);
|
2006
2013
|
});
|
2007
2014
|
const statements = [];
|
2008
|
-
|
2015
|
+
const expandedQueries = queries.flatMap((query, expansionIndex) => {
|
2016
|
+
const { queryType, queryModel, queryInstructions } = splitQuery(query);
|
2017
|
+
if (queryModel === "all") {
|
2018
|
+
return modelsWithAttributes.map((model) => {
|
2019
|
+
const query2 = {
|
2020
|
+
[queryType]: { [model.pluralSlug]: queryInstructions }
|
2021
|
+
};
|
2022
|
+
return { query: query2, expansionIndex };
|
2023
|
+
});
|
2024
|
+
}
|
2025
|
+
return { query };
|
2026
|
+
});
|
2027
|
+
for (const { query, expansionIndex } of expandedQueries) {
|
2009
2028
|
const { dependencies, main, selectedFields } = compileQueryInput(
|
2010
2029
|
query,
|
2011
2030
|
modelsWithPresets,
|
@@ -2019,7 +2038,8 @@ var Transaction = class {
|
|
2019
2038
|
...subStatements.map((statement) => ({
|
2020
2039
|
...statement,
|
2021
2040
|
query,
|
2022
|
-
selectedFields
|
2041
|
+
selectedFields,
|
2042
|
+
expansionIndex
|
2023
2043
|
}))
|
2024
2044
|
);
|
2025
2045
|
}
|
@@ -2119,10 +2139,20 @@ var Transaction = class {
|
|
2119
2139
|
return Object.values(row);
|
2120
2140
|
});
|
2121
2141
|
});
|
2122
|
-
|
2123
|
-
(rows, index) => {
|
2124
|
-
const { returning, query, selectedFields } = this.#internalStatements[index];
|
2125
|
-
if (!returning) return
|
2142
|
+
return normalizedResults.reduce(
|
2143
|
+
(finalResults, rows, index) => {
|
2144
|
+
const { returning, query, selectedFields, expansionIndex } = this.#internalStatements[index];
|
2145
|
+
if (!returning) return finalResults;
|
2146
|
+
const addResult = (result2) => {
|
2147
|
+
if (typeof expansionIndex !== "undefined") {
|
2148
|
+
let match = finalResults[expansionIndex];
|
2149
|
+
if (!match) match = finalResults[expansionIndex] = { models: {} };
|
2150
|
+
match.models[queryModel] = result2;
|
2151
|
+
} else {
|
2152
|
+
finalResults.push(result2);
|
2153
|
+
}
|
2154
|
+
return finalResults;
|
2155
|
+
};
|
2126
2156
|
const { queryType, queryModel, queryInstructions } = splitQuery(query);
|
2127
2157
|
const model = getModelBySlug(this.models, queryModel);
|
2128
2158
|
const isMeta = queryModel === "model" || queryModel === "models";
|
@@ -2130,32 +2160,32 @@ var Transaction = class {
|
|
2130
2160
|
model.fields.map((field) => [field.slug, field.type])
|
2131
2161
|
);
|
2132
2162
|
if (queryType === "count") {
|
2133
|
-
return { amount: rows[0][0] };
|
2163
|
+
return addResult({ amount: rows[0][0] });
|
2134
2164
|
}
|
2135
2165
|
const single = queryModel !== model.pluralSlug;
|
2136
2166
|
if (single) {
|
2137
|
-
return {
|
2167
|
+
return addResult({
|
2138
2168
|
record: rows[0] ? this.#formatRows(selectedFields, rows, true, isMeta) : null,
|
2139
2169
|
modelFields
|
2140
|
-
};
|
2170
|
+
});
|
2141
2171
|
}
|
2142
2172
|
const pageSize = queryInstructions?.limitedTo;
|
2143
|
-
const
|
2173
|
+
const result = {
|
2144
2174
|
records: this.#formatRows(selectedFields, rows, false, isMeta),
|
2145
2175
|
modelFields
|
2146
2176
|
};
|
2147
|
-
if (pageSize &&
|
2148
|
-
if (
|
2177
|
+
if (pageSize && result.records.length > 0) {
|
2178
|
+
if (result.records.length > pageSize) {
|
2149
2179
|
if (queryInstructions?.before) {
|
2150
|
-
|
2180
|
+
result.records.shift();
|
2151
2181
|
} else {
|
2152
|
-
|
2182
|
+
result.records.pop();
|
2153
2183
|
}
|
2154
2184
|
const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
|
2155
|
-
const lastRecord =
|
2185
|
+
const lastRecord = result.records.at(
|
2156
2186
|
direction === "moreAfter" ? -1 : 0
|
2157
2187
|
);
|
2158
|
-
|
2188
|
+
result[direction] = generatePaginationCursor(
|
2159
2189
|
model,
|
2160
2190
|
queryInstructions.orderedBy,
|
2161
2191
|
lastRecord
|
@@ -2163,20 +2193,20 @@ var Transaction = class {
|
|
2163
2193
|
}
|
2164
2194
|
if (queryInstructions?.before || queryInstructions?.after) {
|
2165
2195
|
const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
|
2166
|
-
const firstRecord =
|
2196
|
+
const firstRecord = result.records.at(
|
2167
2197
|
direction === "moreAfter" ? -1 : 0
|
2168
2198
|
);
|
2169
|
-
|
2199
|
+
result[direction] = generatePaginationCursor(
|
2170
2200
|
model,
|
2171
2201
|
queryInstructions.orderedBy,
|
2172
2202
|
firstRecord
|
2173
2203
|
);
|
2174
2204
|
}
|
2175
2205
|
}
|
2176
|
-
return
|
2177
|
-
}
|
2206
|
+
return addResult(result);
|
2207
|
+
},
|
2208
|
+
[]
|
2178
2209
|
);
|
2179
|
-
return formattedResults.filter((result) => result !== null);
|
2180
2210
|
}
|
2181
2211
|
};
|
2182
2212
|
var CLEAN_ROOT_MODEL = omit(ROOT_MODEL, ["system"]);
|