@ronin/compiler 0.13.13 → 0.13.14-leo-ron-1099-1-experimental-310
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 +2 -13
- package/dist/index.js +58 -43
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -368,23 +368,12 @@ interface TransactionOptions {
|
|
368
368
|
expandColumns?: boolean;
|
369
369
|
}
|
370
370
|
declare class Transaction {
|
371
|
+
#private;
|
371
372
|
statements: Array<Statement>;
|
372
373
|
models: Array<Model>;
|
373
|
-
private internalStatements;
|
374
374
|
constructor(queries: Array<Query>, options?: TransactionOptions);
|
375
|
-
/**
|
376
|
-
* Composes SQL statements for the provided RONIN queries.
|
377
|
-
*
|
378
|
-
* @param queries - The RONIN queries for which SQL statements should be composed.
|
379
|
-
* @param models - A list of models.
|
380
|
-
* @param options - Additional options to adjust the behavior of the statement generation.
|
381
|
-
*
|
382
|
-
* @returns The composed SQL statements.
|
383
|
-
*/
|
384
|
-
private compileQueries;
|
385
|
-
private formatRows;
|
386
|
-
formatResults<Record>(results: Array<Array<RawRow>>, raw?: true): Array<Result<Record>>;
|
387
375
|
formatResults<Record>(results: Array<Array<ObjectRow>>, raw?: false): Array<Result<Record>>;
|
376
|
+
formatResults<Record>(results: Array<Array<RawRow>>, raw?: true): Array<Result<Record>>;
|
388
377
|
}
|
389
378
|
|
390
379
|
declare const CLEAN_ROOT_MODEL: PublicModel;
|
package/dist/index.js
CHANGED
@@ -125,7 +125,6 @@ var getProperty = (obj, path) => {
|
|
125
125
|
return path.split(".").reduce((acc, key) => acc?.[key], obj);
|
126
126
|
};
|
127
127
|
var setProperty = (obj, path, value) => {
|
128
|
-
if (!obj) return setProperty({}, path, value);
|
129
128
|
const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim());
|
130
129
|
const _set = (node) => {
|
131
130
|
if (segments.length > 1) {
|
@@ -139,9 +138,7 @@ var setProperty = (obj, path, value) => {
|
|
139
138
|
node[segments[0]] = value;
|
140
139
|
}
|
141
140
|
};
|
142
|
-
|
143
|
-
_set(cloned);
|
144
|
-
return cloned;
|
141
|
+
_set(obj);
|
145
142
|
};
|
146
143
|
var splitQuery = (query) => {
|
147
144
|
const queryType = Object.keys(query)[0];
|
@@ -302,7 +299,7 @@ var handleFor = (model, instructions) => {
|
|
302
299
|
};
|
303
300
|
|
304
301
|
// src/instructions/including.ts
|
305
|
-
var handleIncluding = (models, model, statementParams, instruction) => {
|
302
|
+
var handleIncluding = (models, model, statementParams, single, instruction) => {
|
306
303
|
let statement = "";
|
307
304
|
let tableSubQuery;
|
308
305
|
for (const ephemeralFieldSlug in instruction) {
|
@@ -315,10 +312,10 @@ var handleIncluding = (models, model, statementParams, instruction) => {
|
|
315
312
|
let joinType = "LEFT";
|
316
313
|
let relatedTableSelector = `"${relatedModel.table}"`;
|
317
314
|
const tableAlias = composeIncludedTableAlias(ephemeralFieldSlug);
|
318
|
-
const
|
315
|
+
const subSingle = queryModel !== relatedModel.pluralSlug;
|
319
316
|
if (!modifiableQueryInstructions?.with) {
|
320
317
|
joinType = "CROSS";
|
321
|
-
if (
|
318
|
+
if (subSingle) {
|
322
319
|
if (!modifiableQueryInstructions) modifiableQueryInstructions = {};
|
323
320
|
modifiableQueryInstructions.limitedTo = 1;
|
324
321
|
}
|
@@ -350,7 +347,9 @@ var handleIncluding = (models, model, statementParams, instruction) => {
|
|
350
347
|
);
|
351
348
|
statement += ` ON (${subStatement})`;
|
352
349
|
}
|
353
|
-
if (
|
350
|
+
if (single && !subSingle) {
|
351
|
+
tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
|
352
|
+
}
|
354
353
|
}
|
355
354
|
return { statement, tableSubQuery };
|
356
355
|
};
|
@@ -392,7 +391,7 @@ var handleOrderedBy = (model, instruction) => {
|
|
392
391
|
};
|
393
392
|
|
394
393
|
// src/instructions/selecting.ts
|
395
|
-
var handleSelecting = (models, model, statementParams, instructions, options) => {
|
394
|
+
var handleSelecting = (models, model, statementParams, single, instructions, options) => {
|
396
395
|
let loadedFields = [];
|
397
396
|
let expandColumns = false;
|
398
397
|
let statement = "*";
|
@@ -408,8 +407,8 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
|
|
408
407
|
isJoining = true;
|
409
408
|
expandColumns = Boolean(options?.expandColumns || queryInstructions?.selecting);
|
410
409
|
const tableAlias = composeIncludedTableAlias(key);
|
411
|
-
const
|
412
|
-
if (!
|
410
|
+
const subSingle = queryModel !== subQueryModel.pluralSlug;
|
411
|
+
if (single && !subSingle) {
|
413
412
|
model.tableAlias = `sub_${model.table}`;
|
414
413
|
}
|
415
414
|
const queryModelFields = queryInstructions?.selecting ? subQueryModel.fields.filter((field) => {
|
@@ -421,7 +420,13 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
|
|
421
420
|
})
|
422
421
|
);
|
423
422
|
for (const field of queryModelFields) {
|
424
|
-
loadedFields.push({
|
423
|
+
loadedFields.push({
|
424
|
+
...field,
|
425
|
+
parentField: {
|
426
|
+
slug: key,
|
427
|
+
single: subSingle
|
428
|
+
}
|
429
|
+
});
|
425
430
|
if (expandColumns) {
|
426
431
|
const newValue2 = parseFieldExpression(
|
427
432
|
{ ...subQueryModel, tableAlias },
|
@@ -608,6 +613,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
|
|
608
613
|
models,
|
609
614
|
model,
|
610
615
|
statementParams,
|
616
|
+
single,
|
611
617
|
{
|
612
618
|
selecting: instructions?.selecting,
|
613
619
|
including: instructions?.including
|
@@ -638,6 +644,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
|
|
638
644
|
models,
|
639
645
|
model,
|
640
646
|
statementParams,
|
647
|
+
single,
|
641
648
|
instructions?.including
|
642
649
|
);
|
643
650
|
if (tableSubQuery) {
|
@@ -1851,10 +1858,10 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
|
|
1851
1858
|
var Transaction = class {
|
1852
1859
|
statements = [];
|
1853
1860
|
models = [];
|
1854
|
-
internalStatements = [];
|
1861
|
+
#internalStatements = [];
|
1855
1862
|
constructor(queries, options) {
|
1856
1863
|
const models = options?.models || [];
|
1857
|
-
this
|
1864
|
+
this.#compileQueries(queries, models, options);
|
1858
1865
|
}
|
1859
1866
|
/**
|
1860
1867
|
* Composes SQL statements for the provided RONIN queries.
|
@@ -1865,7 +1872,7 @@ var Transaction = class {
|
|
1865
1872
|
*
|
1866
1873
|
* @returns The composed SQL statements.
|
1867
1874
|
*/
|
1868
|
-
compileQueries = (queries, models, options) => {
|
1875
|
+
#compileQueries = (queries, models, options) => {
|
1869
1876
|
const modelsWithAttributes = [ROOT_MODEL, ...models].map((model) => {
|
1870
1877
|
return addDefaultModelAttributes(model, true);
|
1871
1878
|
});
|
@@ -1890,7 +1897,7 @@ var Transaction = class {
|
|
1890
1897
|
);
|
1891
1898
|
const subStatements = [...result.dependencies, result.main];
|
1892
1899
|
this.statements.push(...subStatements);
|
1893
|
-
this
|
1900
|
+
this.#internalStatements.push(
|
1894
1901
|
...subStatements.map((statement) => ({
|
1895
1902
|
...statement,
|
1896
1903
|
query,
|
@@ -1901,40 +1908,48 @@ var Transaction = class {
|
|
1901
1908
|
this.models = modelsWithPresets;
|
1902
1909
|
return statements;
|
1903
1910
|
};
|
1904
|
-
formatRows(fields, rows, single, isMeta) {
|
1911
|
+
#formatRows(fields, rows, single, isMeta) {
|
1905
1912
|
const records = [];
|
1906
|
-
for (
|
1907
|
-
const
|
1908
|
-
for (let valueIndex = 0; valueIndex < row.length; valueIndex++) {
|
1909
|
-
const value = row[valueIndex];
|
1910
|
-
const field = fields[valueIndex];
|
1913
|
+
for (const row of rows) {
|
1914
|
+
const record = fields.reduce((acc, field, fieldIndex) => {
|
1911
1915
|
let newSlug = field.slug;
|
1912
|
-
let newValue =
|
1916
|
+
let newValue = row[fieldIndex];
|
1917
|
+
if (field.parentField) {
|
1918
|
+
const arrayKey = field.parentField.single ? "" : "[0]";
|
1919
|
+
newSlug = `${field.parentField.slug}${arrayKey}.${field.slug}`;
|
1920
|
+
}
|
1913
1921
|
if (field.type === "json") {
|
1914
|
-
newValue = JSON.parse(
|
1922
|
+
newValue = JSON.parse(newValue);
|
1915
1923
|
} else if (field.type === "boolean") {
|
1916
|
-
newValue = Boolean(
|
1917
|
-
}
|
1918
|
-
const parentFieldSlug = field.parentField;
|
1919
|
-
let usableRowIndex = rowIndex;
|
1920
|
-
if (parentFieldSlug) {
|
1921
|
-
if (rows.length === 1) {
|
1922
|
-
newSlug = `${parentFieldSlug}.${field.slug}`;
|
1923
|
-
} else {
|
1924
|
-
newSlug = `${parentFieldSlug}[${rowIndex}].${field.slug}`;
|
1925
|
-
usableRowIndex = 0;
|
1926
|
-
}
|
1924
|
+
newValue = Boolean(newValue);
|
1927
1925
|
}
|
1928
1926
|
if (isMeta && PLURAL_MODEL_ENTITIES_VALUES.includes(newSlug)) {
|
1929
1927
|
newValue = newValue ? Object.entries(newValue).map(([slug, attributes]) => {
|
1930
1928
|
return { slug, ...attributes };
|
1931
1929
|
}) : [];
|
1932
1930
|
}
|
1933
|
-
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1937
|
-
|
1931
|
+
setProperty(acc, newSlug, newValue);
|
1932
|
+
return acc;
|
1933
|
+
}, {});
|
1934
|
+
const existingRecord = record.id ? records.find((existingRecord2) => {
|
1935
|
+
return existingRecord2.id === record.id;
|
1936
|
+
}) : null;
|
1937
|
+
if (!existingRecord) {
|
1938
|
+
records.push(record);
|
1939
|
+
continue;
|
1940
|
+
}
|
1941
|
+
const joinFields = fields.reduce(
|
1942
|
+
(acc, field) => {
|
1943
|
+
if (!field.parentField) return acc;
|
1944
|
+
const { single: single2, slug } = field.parentField;
|
1945
|
+
return single2 || acc.includes(slug) ? acc : acc.concat([slug]);
|
1946
|
+
},
|
1947
|
+
[]
|
1948
|
+
);
|
1949
|
+
for (const parentField of joinFields) {
|
1950
|
+
const currentValue = existingRecord[parentField];
|
1951
|
+
const newValue = record[parentField];
|
1952
|
+
currentValue.push(...newValue);
|
1938
1953
|
}
|
1939
1954
|
}
|
1940
1955
|
return single ? records[0] : records;
|
@@ -1965,7 +1980,7 @@ var Transaction = class {
|
|
1965
1980
|
returning,
|
1966
1981
|
query,
|
1967
1982
|
fields: rawModelFields
|
1968
|
-
} = this
|
1983
|
+
} = this.#internalStatements[index];
|
1969
1984
|
if (!returning) return null;
|
1970
1985
|
const { queryType, queryModel, queryInstructions } = splitQuery(query);
|
1971
1986
|
const model = getModelBySlug(this.models, queryModel);
|
@@ -1979,13 +1994,13 @@ var Transaction = class {
|
|
1979
1994
|
const single = queryModel !== model.pluralSlug;
|
1980
1995
|
if (single) {
|
1981
1996
|
return {
|
1982
|
-
record: rows[0] ? this
|
1997
|
+
record: rows[0] ? this.#formatRows(rawModelFields, rows, true, isMeta) : null,
|
1983
1998
|
modelFields
|
1984
1999
|
};
|
1985
2000
|
}
|
1986
2001
|
const pageSize = queryInstructions?.limitedTo;
|
1987
2002
|
const output = {
|
1988
|
-
records: this
|
2003
|
+
records: this.#formatRows(rawModelFields, rows, false, isMeta),
|
1989
2004
|
modelFields
|
1990
2005
|
};
|
1991
2006
|
if (pageSize && output.records.length > 0) {
|