@ronin/compiler 0.10.2-leo-ron-1083-experimental-214 → 0.10.3-leo-ron-1083-experimental-215

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/dist/index.js +60 -40
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -65,6 +65,23 @@ var convertToCamelCase = (str) => {
65
65
  return sanitize(str).split(SPLIT_REGEX).map((part, index) => index === 0 ? part.toLowerCase() : capitalize(part)).join("");
66
66
  };
67
67
  var isObject = (value) => value != null && typeof value === "object" && Array.isArray(value) === false;
68
+ var getSymbol = (value) => {
69
+ if (!isObject(value)) return null;
70
+ const objectValue = value;
71
+ if (RONIN_MODEL_SYMBOLS.QUERY in objectValue) {
72
+ return {
73
+ type: "query",
74
+ value: objectValue[RONIN_MODEL_SYMBOLS.QUERY]
75
+ };
76
+ }
77
+ if (RONIN_MODEL_SYMBOLS.EXPRESSION in objectValue) {
78
+ return {
79
+ type: "expression",
80
+ value: objectValue[RONIN_MODEL_SYMBOLS.EXPRESSION]
81
+ };
82
+ }
83
+ return null;
84
+ };
68
85
  var findInObject = (obj, pattern, replacer) => {
69
86
  let found = false;
70
87
  for (const key in obj) {
@@ -85,10 +102,11 @@ var findInObject = (obj, pattern, replacer) => {
85
102
  var flatten = (obj, prefix = "", res = {}) => {
86
103
  for (const key in obj) {
87
104
  const path = prefix ? `${prefix}.${key}` : key;
88
- if (typeof obj[key] === "object" && obj[key] !== null) {
89
- flatten(obj[key], path, res);
105
+ const value = obj[key];
106
+ if (typeof value === "object" && value !== null && !getSymbol(value)) {
107
+ flatten(value, path, res);
90
108
  } else {
91
- res[path] = obj[key];
109
+ res[path] = value;
92
110
  }
93
111
  }
94
112
  return res;
@@ -278,23 +296,6 @@ var formatIdentifiers = ({ identifiers }, queryInstructions) => {
278
296
  [type]: newNestedInstructions
279
297
  };
280
298
  };
281
- var getSymbol = (value) => {
282
- if (!isObject(value)) return null;
283
- const objectValue = value;
284
- if (RONIN_MODEL_SYMBOLS.QUERY in objectValue) {
285
- return {
286
- type: "query",
287
- value: objectValue[RONIN_MODEL_SYMBOLS.QUERY]
288
- };
289
- }
290
- if (RONIN_MODEL_SYMBOLS.EXPRESSION in objectValue) {
291
- return {
292
- type: "expression",
293
- value: objectValue[RONIN_MODEL_SYMBOLS.EXPRESSION]
294
- };
295
- }
296
- return null;
297
- };
298
299
 
299
300
  // src/instructions/with.ts
300
301
  var getMatcher = (value, negative) => {
@@ -1183,32 +1184,44 @@ var handleOrderedBy = (model, instruction) => {
1183
1184
  };
1184
1185
 
1185
1186
  // src/instructions/selecting.ts
1186
- var handleSelecting = (model, statementParams, instructions) => {
1187
+ var handleSelecting = (models, model, statementParams, instructions, options) => {
1187
1188
  let isJoining = false;
1188
1189
  let statement = instructions.selecting ? instructions.selecting.map((slug) => {
1189
1190
  return getFieldFromModel(model, slug, "selecting").fieldSelector;
1190
1191
  }).join(", ") : "*";
1191
1192
  if (instructions.including) {
1192
- const filteredObject = Object.entries(instructions.including).map(([key, value]) => {
1193
+ const filteredObject = Object.entries(instructions.including).flatMap(([key, value]) => {
1193
1194
  const symbol = getSymbol(value);
1194
- if (symbol) {
1195
- if (symbol.type === "query") {
1196
- isJoining = true;
1197
- return null;
1198
- }
1199
- if (symbol.type === "expression") {
1200
- value = parseFieldExpression(model, "including", symbol.value);
1201
- }
1195
+ if (symbol?.type === "query") {
1196
+ isJoining = true;
1197
+ if (!options?.expandColumns) return null;
1198
+ const { queryModel: queryModelSlug } = splitQuery(symbol.value);
1199
+ const queryModel = getModelBySlug(models, queryModelSlug);
1200
+ const tableName = `including_${key}`;
1201
+ const duplicatedFields = queryModel.fields.filter((field) => {
1202
+ if (field.type === "group") return null;
1203
+ return model.fields.some((modelField) => modelField.slug === field.slug);
1204
+ }).filter((item) => item !== null);
1205
+ return duplicatedFields.map((field) => ({
1206
+ key: `${tableName}.${field.slug}`,
1207
+ value: {
1208
+ [RONIN_MODEL_SYMBOLS.EXPRESSION]: `${RONIN_MODEL_SYMBOLS.FIELD}${field.slug}`
1209
+ }
1210
+ }));
1202
1211
  }
1203
- return [key, value];
1204
- }).filter((entry) => entry !== null);
1212
+ return { key, value };
1213
+ }).filter((entry) => entry !== null).map((entry) => [entry.key, entry.value]);
1205
1214
  const newObjectEntries = Object.entries(flatten(Object.fromEntries(filteredObject)));
1206
1215
  if (newObjectEntries.length > 0) {
1207
1216
  statement += ", ";
1208
1217
  statement += newObjectEntries.map(([key, value]) => {
1209
- if (typeof value === "string" && value.startsWith('"'))
1210
- return `(${value}) as "${key}"`;
1211
- return `${prepareStatementValue(statementParams, value)} as "${key}"`;
1218
+ const symbol = getSymbol(value);
1219
+ if (symbol?.type === "expression") {
1220
+ value = `(${parseFieldExpression(model, "including", symbol.value)})`;
1221
+ } else {
1222
+ value = prepareStatementValue(statementParams, value);
1223
+ }
1224
+ return `${value} as "${key}"`;
1212
1225
  }).join(", ");
1213
1226
  }
1214
1227
  }
@@ -1355,10 +1368,16 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
1355
1368
  if (instructions && Object.hasOwn(instructions, "for")) {
1356
1369
  instructions = handleFor(model, instructions);
1357
1370
  }
1358
- const { columns, isJoining } = handleSelecting(model, statementParams, {
1359
- selecting: instructions?.selecting,
1360
- including: instructions?.including
1361
- });
1371
+ const { columns, isJoining } = handleSelecting(
1372
+ models,
1373
+ model,
1374
+ statementParams,
1375
+ {
1376
+ selecting: instructions?.selecting,
1377
+ including: instructions?.including
1378
+ },
1379
+ options
1380
+ );
1362
1381
  let statement = "";
1363
1382
  switch (queryType) {
1364
1383
  case "get":
@@ -1518,7 +1537,8 @@ var Transaction = class {
1518
1537
  const result = compileQueryInput(
1519
1538
  query,
1520
1539
  modelListWithPresets,
1521
- options?.inlineParams ? null : []
1540
+ options?.inlineParams ? null : [],
1541
+ { expandColumns: options?.expandColumns }
1522
1542
  );
1523
1543
  dependencyStatements.push(...result.dependencies);
1524
1544
  mainStatements.push(result.main);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.10.2-leo-ron-1083-experimental-214",
3
+ "version": "0.10.3-leo-ron-1083-experimental-215",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {