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