@ronin/compiler
Advanced tools
Comparing version 0.8.4 to 0.8.5-leo-ron-1083-experimental-162
@@ -321,147 +321,2 @@ // src/utils/helpers.ts | ||
// src/utils/meta.ts | ||
var PLURAL_MODEL_ENTITIES = { | ||
field: "fields", | ||
index: "indexes", | ||
trigger: "triggers", | ||
preset: "presets" | ||
}; | ||
var transformMetaQuery = (models, dependencyStatements, statementParams, query) => { | ||
if (query.create) { | ||
const init = query.create.model; | ||
const details = "to" in query.create ? { slug: init, ...query.create.to } : init; | ||
const modelWithFields = addDefaultModelFields(details, true); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
const instructions = { | ||
to: modelWithPresets | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "add", | ||
queryModel: "model", | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
add: { | ||
model: instructions | ||
} | ||
}; | ||
} | ||
if (query.drop) { | ||
const slug = query.drop.model; | ||
const instructions = { | ||
with: { slug } | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "remove", | ||
queryModel: "model", | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
remove: { | ||
model: instructions | ||
} | ||
}; | ||
} | ||
if (query.alter) { | ||
const slug = query.alter.model; | ||
if ("to" in query.alter) { | ||
const modelWithFields = addDefaultModelFields(query.alter.to, false); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
const instructions = { | ||
with: { slug }, | ||
to: modelWithPresets | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "set", | ||
queryModel: "model", | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
set: { | ||
model: instructions | ||
} | ||
}; | ||
} | ||
if ("create" in query.alter) { | ||
const type2 = Object.keys(query.alter.create)[0]; | ||
const pluralType2 = PLURAL_MODEL_ENTITIES[type2]; | ||
const item = query.alter.create[type2]; | ||
const completeItem = { slug: item.slug || `${type2}Slug`, ...item }; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "add", | ||
queryModel: type2, | ||
queryInstructions: { | ||
to: { | ||
model: { slug }, | ||
...completeItem | ||
} | ||
} | ||
}); | ||
const value = prepareStatementValue(statementParams, completeItem); | ||
const json2 = `json_insert(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType2}, '$.${completeItem.slug}', ${value})`; | ||
const expression2 = { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json2 }; | ||
return { | ||
set: { | ||
model: { | ||
with: { slug }, | ||
to: { | ||
[pluralType2]: expression2 | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
if ("alter" in query.alter) { | ||
const type2 = Object.keys(query.alter.alter)[0]; | ||
const pluralType2 = PLURAL_MODEL_ENTITIES[type2]; | ||
const itemSlug2 = query.alter.alter[type2]; | ||
const newItem = query.alter.alter.to; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "set", | ||
queryModel: type2, | ||
queryInstructions: { | ||
with: { model: { slug }, slug: itemSlug2 }, | ||
to: newItem | ||
} | ||
}); | ||
const value = prepareStatementValue(statementParams, newItem); | ||
const json2 = `json_patch(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType2}, '$.${itemSlug2}', ${value})`; | ||
const expression2 = { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json2 }; | ||
return { | ||
set: { | ||
model: { | ||
with: { slug }, | ||
to: { | ||
[pluralType2]: expression2 | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
const type = Object.keys(query.alter.drop)[0]; | ||
const pluralType = PLURAL_MODEL_ENTITIES[type]; | ||
const itemSlug = query.alter.drop[type]; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "remove", | ||
queryModel: type, | ||
queryInstructions: { | ||
with: { model: { slug }, slug: itemSlug } | ||
} | ||
}); | ||
const json = `json_insert(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${itemSlug}')`; | ||
const expression = { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json }; | ||
return { | ||
set: { | ||
model: { | ||
with: { slug }, | ||
to: { | ||
[pluralType]: expression | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
return query; | ||
}; | ||
// src/utils/model.ts | ||
@@ -736,7 +591,2 @@ import title from "title"; | ||
}; | ||
var mappedInstructions = { | ||
add: "to", | ||
set: "with", | ||
remove: "with" | ||
}; | ||
var typesInSQLite = { | ||
@@ -782,45 +632,124 @@ link: "TEXT", | ||
}; | ||
var addModelQueries = (models, dependencyStatements, queryDetails) => { | ||
const { queryType, queryModel, queryInstructions } = queryDetails; | ||
if (!["add", "set", "remove"].includes(queryType)) return; | ||
if (!["model", "field", "index", "trigger", "preset"].includes(queryModel)) return; | ||
const instructionName = mappedInstructions[queryType]; | ||
const instructionList = queryInstructions[instructionName]; | ||
let tableAction = "ALTER"; | ||
let queryTypeReadable = null; | ||
switch (queryType) { | ||
case "add": { | ||
if (queryModel === "model" || queryModel === "index" || queryModel === "trigger") { | ||
tableAction = "CREATE"; | ||
var PLURAL_MODEL_ENTITIES = { | ||
field: "fields", | ||
index: "indexes", | ||
trigger: "triggers", | ||
preset: "presets" | ||
}; | ||
var transformMetaQuery = (models, dependencyStatements, statementParams, query) => { | ||
const { queryType } = splitQuery(query); | ||
const subAltering = query.alter && !("to" in query.alter); | ||
const action = subAltering ? Object.keys(query.alter).filter((key) => key !== "model")[0] : queryType; | ||
const actionReadable = action === "create" ? "creating" : action === "alter" ? "altering" : "dropping"; | ||
const entity = subAltering ? Object.keys(query.alter[action])[0] : "model"; | ||
let slug; | ||
let modelSlug; | ||
let jsonValue; | ||
if (query.create) { | ||
const init = query.create.model; | ||
jsonValue = "to" in query.create ? { slug: init, ...query.create.to } : init; | ||
slug = modelSlug = jsonValue.slug; | ||
} | ||
if (query.drop) { | ||
slug = modelSlug = query.drop.model; | ||
} | ||
if (query.alter) { | ||
slug = modelSlug = query.alter.model; | ||
if ("to" in query.alter) { | ||
jsonValue = query.alter.to; | ||
} else { | ||
slug = query.alter[action][entity]; | ||
if ("create" in query.alter) { | ||
const item = query.alter.create[entity]; | ||
slug = item.slug || `${entity}Slug`; | ||
jsonValue = { slug, ...item }; | ||
} | ||
queryTypeReadable = "creating"; | ||
break; | ||
if ("alter" in query.alter) { | ||
jsonValue = query.alter.alter.to; | ||
} | ||
} | ||
case "set": { | ||
if (queryModel === "model") tableAction = "ALTER"; | ||
queryTypeReadable = "updating"; | ||
break; | ||
} | ||
if (!(modelSlug && slug)) return query; | ||
const tableAction = ["model", "index", "trigger"].includes(entity) ? action.toUpperCase() : "ALTER"; | ||
const usableSlug = entity === "model" ? slug : modelSlug; | ||
const tableName = convertToSnakeCase(pluralize(usableSlug)); | ||
const targetModel = entity === "model" && action === "create" ? null : getModelBySlug(models, usableSlug); | ||
const statement = `${tableAction} TABLE "${tableName}"`; | ||
if (entity === "model") { | ||
let queryTypeDetails; | ||
if (action === "create") { | ||
const modelWithFields = addDefaultModelFields(jsonValue, true); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
const { fields } = modelWithPresets; | ||
const columns = fields.map((field) => getFieldStatement(models, modelWithPresets, field)).filter(Boolean); | ||
dependencyStatements.push({ | ||
statement: `${statement} (${columns.join(", ")})`, | ||
params: [] | ||
}); | ||
models.push(modelWithPresets); | ||
queryTypeDetails = { to: modelWithPresets }; | ||
} | ||
case "remove": { | ||
if (queryModel === "model" || queryModel === "index" || queryModel === "trigger") { | ||
tableAction = "DROP"; | ||
if (action === "alter") { | ||
const modelWithFields = addDefaultModelFields(jsonValue, false); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
const newSlug = modelWithPresets.pluralSlug; | ||
if (newSlug) { | ||
const newTable = convertToSnakeCase(newSlug); | ||
dependencyStatements.push({ | ||
statement: `${statement} RENAME TO "${newTable}"`, | ||
params: [] | ||
}); | ||
} | ||
queryTypeReadable = "deleting"; | ||
break; | ||
Object.assign(targetModel, modelWithPresets); | ||
queryTypeDetails = { | ||
with: { | ||
slug: usableSlug | ||
}, | ||
to: modelWithPresets | ||
}; | ||
} | ||
if (action === "drop") { | ||
models.splice(models.indexOf(targetModel), 1); | ||
dependencyStatements.push({ statement, params: [] }); | ||
queryTypeDetails = { | ||
with: { slug: usableSlug } | ||
}; | ||
} | ||
const queryTypeAction = action === "create" ? "add" : action === "alter" ? "set" : "remove"; | ||
return { | ||
[queryTypeAction]: { | ||
model: queryTypeDetails | ||
} | ||
}; | ||
} | ||
const slug = instructionList?.slug?.being || instructionList?.slug; | ||
const modelInstruction = instructionList?.model; | ||
const modelSlug = modelInstruction?.slug?.being || modelInstruction?.slug; | ||
const usableSlug = queryModel === "model" ? slug : modelSlug; | ||
const tableName = convertToSnakeCase(pluralize(usableSlug)); | ||
const targetModel = queryModel === "model" && queryType === "add" ? null : getModelBySlug(models, usableSlug); | ||
if (queryModel === "index") { | ||
if (entity === "field") { | ||
if (action === "create") { | ||
if (!jsonValue?.type) jsonValue.type = "string"; | ||
dependencyStatements.push({ | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, jsonValue)}`, | ||
params: [] | ||
}); | ||
} else if (action === "alter") { | ||
const newSlug = jsonValue?.slug; | ||
if (newSlug) { | ||
dependencyStatements.push({ | ||
statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`, | ||
params: [] | ||
}); | ||
} | ||
} else if (action === "drop") { | ||
dependencyStatements.push({ | ||
statement: `${statement} DROP COLUMN "${slug}"`, | ||
params: [] | ||
}); | ||
} | ||
} | ||
if (entity === "index") { | ||
const indexName = convertToSnakeCase(slug); | ||
const unique = instructionList?.unique; | ||
const filterQuery = instructionList?.filter; | ||
const fields = instructionList?.fields; | ||
const unique = jsonValue?.unique; | ||
const filterQuery = jsonValue?.filter; | ||
const fields = jsonValue?.fields; | ||
const params = []; | ||
let statement2 = `${tableAction}${unique ? " UNIQUE" : ""} INDEX "${indexName}"`; | ||
if (queryType === "add") { | ||
if (action === "create") { | ||
const model = targetModel; | ||
@@ -850,19 +779,18 @@ const columns = fields.map((field) => { | ||
dependencyStatements.push({ statement: statement2, params }); | ||
return; | ||
} | ||
if (queryModel === "trigger") { | ||
if (entity === "trigger") { | ||
const triggerName = convertToSnakeCase(slug); | ||
const params = []; | ||
let statement2 = `${tableAction} TRIGGER "${triggerName}"`; | ||
if (queryType === "add") { | ||
if (action === "create") { | ||
const currentModel = targetModel; | ||
const { when, action } = instructionList; | ||
const statementParts = [`${when} ${action}`]; | ||
const effectQueries = instructionList?.effects; | ||
const filterQuery = instructionList?.filter; | ||
const fields = instructionList?.fields; | ||
const { when, action: action2 } = jsonValue; | ||
const statementParts = [`${when} ${action2}`]; | ||
const effectQueries = jsonValue?.effects; | ||
const filterQuery = jsonValue?.filter; | ||
const fields = jsonValue?.fields; | ||
if (fields) { | ||
if (action !== "UPDATE") { | ||
if (action2 !== "UPDATE") { | ||
throw new RoninError({ | ||
message: `When ${queryTypeReadable} ${PLURAL_MODEL_ENTITIES[queryModel]}, targeting specific fields requires the \`UPDATE\` action.`, | ||
message: `When ${actionReadable} ${PLURAL_MODEL_ENTITIES[entity]}, targeting specific fields requires the \`UPDATE\` action.`, | ||
code: "INVALID_MODEL_VALUE", | ||
@@ -878,7 +806,7 @@ fields: ["action"] | ||
statementParts.push("ON", `"${tableName}"`); | ||
if (filterQuery || effectQueries.some((query) => findInObject(query, RONIN_MODEL_SYMBOLS.FIELD))) { | ||
if (filterQuery || effectQueries.some((query2) => findInObject(query2, RONIN_MODEL_SYMBOLS.FIELD))) { | ||
statementParts.push("FOR EACH ROW"); | ||
} | ||
if (filterQuery) { | ||
const tableAlias = action === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW; | ||
const tableAlias = action2 === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW; | ||
const withStatement = handleWith( | ||
@@ -904,53 +832,19 @@ models, | ||
dependencyStatements.push({ statement: statement2, params }); | ||
return; | ||
} | ||
const statement = `${tableAction} TABLE "${tableName}"`; | ||
if (queryModel === "model") { | ||
if (queryType === "add") { | ||
const newModel = queryInstructions.to; | ||
const { fields } = newModel; | ||
const columns = fields.map((field) => getFieldStatement(models, newModel, field)).filter(Boolean); | ||
dependencyStatements.push({ | ||
statement: `${statement} (${columns.join(", ")})`, | ||
params: [] | ||
}); | ||
models.push(newModel); | ||
} else if (queryType === "set") { | ||
const newSlug = queryInstructions.to?.pluralSlug; | ||
if (newSlug) { | ||
const newTable = convertToSnakeCase(newSlug); | ||
dependencyStatements.push({ | ||
statement: `${statement} RENAME TO "${newTable}"`, | ||
params: [] | ||
}); | ||
const pluralType = PLURAL_MODEL_ENTITIES[entity]; | ||
const jsonAction = action === "create" ? "insert" : action === "alter" ? "patch" : "remove"; | ||
const deepValue = action === "create" ? { ...jsonValue, model: void 0 } : action === "alter" ? jsonValue : null; | ||
let json = `json_${jsonAction}(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${slug}'`; | ||
if (deepValue) json += `, ${prepareStatementValue(statementParams, deepValue)}`; | ||
json += ")"; | ||
return { | ||
set: { | ||
model: { | ||
with: { slug: usableSlug }, | ||
to: { | ||
[pluralType]: { [RONIN_MODEL_SYMBOLS.EXPRESSION]: json } | ||
} | ||
} | ||
Object.assign(targetModel, queryInstructions.to); | ||
} else if (queryType === "remove") { | ||
models.splice(models.indexOf(targetModel), 1); | ||
dependencyStatements.push({ statement, params: [] }); | ||
} | ||
return; | ||
} | ||
if (queryModel === "field") { | ||
if (queryType === "add") { | ||
if (!instructionList.type) instructionList.type = "string"; | ||
dependencyStatements.push({ | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`, | ||
params: [] | ||
}); | ||
} else if (queryType === "set") { | ||
const newSlug = queryInstructions.to?.slug; | ||
if (newSlug) { | ||
dependencyStatements.push({ | ||
statement: `${statement} RENAME COLUMN "${slug}" TO "${newSlug}"`, | ||
params: [] | ||
}); | ||
} | ||
} else if (queryType === "remove") { | ||
dependencyStatements.push({ | ||
statement: `${statement} DROP COLUMN "${slug}"`, | ||
params: [] | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -957,0 +851,0 @@ |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.8.4", | ||
"version": "0.8.5-leo-ron-1083-experimental-162", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Compiles RONIN queries to SQL statements.", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
0
441379
7349
1