@ronin/compiler
Advanced tools
Comparing version 0.10.0 to 0.10.1-leo-ron-1083-experimental-208
@@ -651,3 +651,3 @@ // src/utils/helpers.ts | ||
}; | ||
var composeSystemModelStatement = (models, dependencyStatements, action, systemModel) => { | ||
var handleSystemModel = (models, dependencyStatements, action, systemModel, newModel) => { | ||
const { system: _, ...systemModelClean } = systemModel; | ||
@@ -657,2 +657,6 @@ const query = { | ||
}; | ||
if (action === "alter" && newModel) { | ||
const { system: _2, ...newModelClean } = newModel; | ||
query.alter.to = newModelClean; | ||
} | ||
const statement = compileQueryInput(query, models, []); | ||
@@ -717,8 +721,3 @@ dependencyStatements.push(...statement.dependencies); | ||
getSystemModels(models, modelWithPresets).map((systemModel) => { | ||
return composeSystemModelStatement( | ||
models, | ||
dependencyStatements, | ||
"create", | ||
systemModel | ||
); | ||
return handleSystemModel(models, dependencyStatements, "create", systemModel); | ||
}); | ||
@@ -750,8 +749,3 @@ } | ||
models.filter(({ system }) => system?.model === model.slug).map((systemModel) => { | ||
return composeSystemModelStatement( | ||
models, | ||
dependencyStatements, | ||
"drop", | ||
systemModel | ||
); | ||
return handleSystemModel(models, dependencyStatements, "drop", systemModel); | ||
}); | ||
@@ -766,2 +760,3 @@ } | ||
} | ||
const modelBeforeUpdate = structuredClone(model); | ||
const existingModel = model; | ||
@@ -781,2 +776,4 @@ const pluralType = PLURAL_MODEL_ENTITIES[entity]; | ||
const statement = `ALTER TABLE "${existingModel.table}"`; | ||
const existingField = existingEntity; | ||
const existingLinkField = existingField?.type === "link" && existingField.kind === "many"; | ||
if (action === "create") { | ||
@@ -794,3 +791,3 @@ const field2 = jsonValue; | ||
const newSlug = jsonValue?.slug; | ||
if (newSlug) { | ||
if (newSlug && !existingLinkField) { | ||
dependencyStatements.push({ | ||
@@ -801,10 +798,7 @@ statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`, | ||
} | ||
} else if (action === "drop") { | ||
const existingField = existingEntity; | ||
if (!(existingField.type === "link" && existingField.kind === "many")) { | ||
dependencyStatements.push({ | ||
statement: `${statement} DROP COLUMN "${slug}"`, | ||
params: [] | ||
}); | ||
} | ||
} else if (action === "drop" && !existingLinkField) { | ||
dependencyStatements.push({ | ||
statement: `${statement} DROP COLUMN "${slug}"`, | ||
params: [] | ||
}); | ||
} | ||
@@ -901,3 +895,3 @@ } | ||
const targetEntity = existingModel[pluralType]; | ||
targetEntity[targetEntityIndex] = jsonValue; | ||
Object.assign(targetEntity[targetEntityIndex], jsonValue); | ||
break; | ||
@@ -915,11 +909,31 @@ } | ||
const newSystemModels = getSystemModels(models, existingModel); | ||
const matchSystemModels = (oldSystemModel, newSystemModel) => { | ||
const conditions = [ | ||
oldSystemModel.system?.model === newSystemModel.system?.model | ||
]; | ||
if (oldSystemModel.system?.associationSlug) { | ||
const oldFieldIndex = modelBeforeUpdate?.fields.findIndex((item) => { | ||
return item.slug === newSystemModel.system?.associationSlug; | ||
}); | ||
const newFieldIndex = existingModel.fields.findIndex((item) => { | ||
return item.slug === oldSystemModel.system?.associationSlug; | ||
}); | ||
conditions.push(oldFieldIndex === newFieldIndex); | ||
} | ||
return conditions.every((condition) => condition === true); | ||
}; | ||
for (const systemModel of currentSystemModels) { | ||
const exists = newSystemModels.find((model2) => model2.slug === systemModel.slug); | ||
if (exists) continue; | ||
composeSystemModelStatement(models, dependencyStatements, "drop", systemModel); | ||
const exists = newSystemModels.find(matchSystemModels.bind(null, systemModel)); | ||
if (exists) { | ||
if (exists.slug !== systemModel.slug) { | ||
handleSystemModel(models, dependencyStatements, "alter", systemModel, exists); | ||
} | ||
continue; | ||
} | ||
handleSystemModel(models, dependencyStatements, "drop", systemModel); | ||
} | ||
for (const systemModel of newSystemModels) { | ||
const exists = currentSystemModels.find((model2) => model2.slug === systemModel.slug); | ||
const exists = currentSystemModels.find(matchSystemModels.bind(null, systemModel)); | ||
if (exists) continue; | ||
composeSystemModelStatement(models, dependencyStatements, "create", systemModel); | ||
handleSystemModel(models, dependencyStatements, "create", systemModel); | ||
} | ||
@@ -1211,2 +1225,7 @@ return { | ||
const subQueryModel = getModelBySlug(models, subQueryModelSlug); | ||
if (subQueryInstructions?.selecting) { | ||
const currentFields = new Set(subQueryInstructions.selecting); | ||
currentFields.add("id"); | ||
subQueryInstructions.selecting = Array.from(currentFields); | ||
} | ||
const subQuerySelectedFields = subQueryInstructions?.selecting; | ||
@@ -1234,3 +1253,15 @@ const subQueryIncludedFields = subQueryInstructions?.including; | ||
} | ||
return compileQueryInput(symbol.value, models, statementParams).main.statement; | ||
let statement2 = ""; | ||
if (subQuerySelectedFields) { | ||
const selectedFields = [ | ||
...subQueryFields, | ||
...defaultFieldsToAdd.map(([key]) => key) | ||
]; | ||
const columns = selectedFields.map((field) => { | ||
return getFieldFromModel(model, field, "to").fieldSelector; | ||
}); | ||
statement2 = `(${columns.join(", ")}) `; | ||
} | ||
statement2 += compileQueryInput(symbol.value, models, statementParams).main.statement; | ||
return statement2; | ||
} | ||
@@ -1499,7 +1530,18 @@ Object.assign(toInstruction, defaultFields); | ||
prepareResults(results) { | ||
return results.map((result, index) => { | ||
const relevantResults = results.filter((_, index) => { | ||
return this.statements[index].returning; | ||
}); | ||
return relevantResults.map((result, index) => { | ||
const query = this.queries.at(-index); | ||
const { queryModel } = splitQuery(query); | ||
const model = getModelBySlug(this.models, queryModel); | ||
return { record: this.formatRecord(model, result[0]) }; | ||
const single = queryModel !== model.pluralSlug; | ||
if (single) { | ||
return { record: this.formatRecord(model, result[0]) }; | ||
} | ||
return { | ||
records: result.map((resultItem) => { | ||
return this.formatRecord(model, resultItem); | ||
}) | ||
}; | ||
}); | ||
@@ -1506,0 +1548,0 @@ } |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.10.0", | ||
"version": "0.10.1-leo-ron-1083-experimental-208", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Compiles RONIN queries to SQL statements.", |
450875
7556