@ronin/compiler
Advanced tools
Comparing version 0.7.1 to 0.7.2-leo-ron-1083-experimental-127
@@ -190,3 +190,3 @@ // src/utils/helpers.ts | ||
} | ||
if (modelField.type === "reference" && isNested) { | ||
if (modelField.type === "link" && isNested) { | ||
const keys = Object.keys(value); | ||
@@ -198,3 +198,3 @@ const values = Object.values(value); | ||
} else { | ||
const relatedModel = getModelBySlug(models, modelField.target.slug); | ||
const relatedModel = getModelBySlug(models, modelField.target); | ||
const subQuery = { | ||
@@ -495,4 +495,4 @@ get: { | ||
slug: "model", | ||
type: "reference", | ||
target: { slug: "model" }, | ||
type: "link", | ||
target: "model", | ||
required: true | ||
@@ -504,4 +504,4 @@ }, | ||
{ slug: "autoIncrement", type: "boolean" }, | ||
// Only allowed for fields of type "reference". | ||
{ slug: "target", type: "reference", target: { slug: "model" } }, | ||
// Only allowed for fields of type "link". | ||
{ slug: "target", type: "string" }, | ||
{ slug: "kind", type: "string" }, | ||
@@ -523,4 +523,4 @@ { slug: "actions", type: "group" }, | ||
slug: "model", | ||
type: "reference", | ||
target: { slug: "model" }, | ||
type: "link", | ||
target: "model", | ||
required: true | ||
@@ -543,4 +543,4 @@ }, | ||
slug: "model", | ||
type: "reference", | ||
target: { slug: "model" }, | ||
type: "link", | ||
target: "model", | ||
required: true | ||
@@ -561,4 +561,4 @@ }, | ||
slug: "model", | ||
type: "reference", | ||
target: { slug: "model" }, | ||
type: "link", | ||
target: "model", | ||
required: true | ||
@@ -578,4 +578,4 @@ }, | ||
for (const field of model.fields || []) { | ||
if (field.type === "reference" && !field.slug.startsWith("ronin.")) { | ||
const relatedModel = getModelBySlug(models, field.target.slug); | ||
if (field.type === "link" && !field.slug.startsWith("ronin.")) { | ||
const relatedModel = getModelBySlug(models, field.target); | ||
let fieldSlug = relatedModel.slug; | ||
@@ -591,9 +591,9 @@ if (field.kind === "many") { | ||
slug: "source", | ||
type: "reference", | ||
target: { slug: model.slug } | ||
type: "link", | ||
target: model.slug | ||
}, | ||
{ | ||
slug: "target", | ||
type: "reference", | ||
target: { slug: relatedModel.slug } | ||
type: "link", | ||
target: relatedModel.slug | ||
} | ||
@@ -612,4 +612,4 @@ ] | ||
for (const field of model.fields || []) { | ||
if (field.type === "reference" && !field.slug.startsWith("ronin.")) { | ||
const relatedModel = getModelBySlug(list, field.target.slug); | ||
if (field.type === "link" && !field.slug.startsWith("ronin.")) { | ||
const relatedModel = getModelBySlug(list, field.target); | ||
if (field.kind === "many") continue; | ||
@@ -624,3 +624,3 @@ defaultPresets.push({ | ||
with: { | ||
// Compare the `id` field of the related model to the reference field on | ||
// Compare the `id` field of the related model to the link field on | ||
// the root model (`field.slug`). | ||
@@ -643,3 +643,3 @@ id: { | ||
const field = subModel.fields?.find((field2) => { | ||
return field2.type === "reference" && field2.target.slug === model.slug; | ||
return field2.type === "link" && field2.target === model.slug; | ||
}); | ||
@@ -685,3 +685,3 @@ if (!field) return null; | ||
var typesInSQLite = { | ||
reference: "TEXT", | ||
link: "TEXT", | ||
string: "TEXT", | ||
@@ -694,3 +694,3 @@ date: "DATETIME", | ||
}; | ||
var getFieldStatement = (field) => { | ||
var getFieldStatement = (models, model, field) => { | ||
if (field.type === "group") return null; | ||
@@ -703,5 +703,16 @@ let statement = `"${field.slug}" ${typesInSQLite[field.type]}`; | ||
statement += ` DEFAULT ${field.defaultValue}`; | ||
if (field.type === "reference") { | ||
if (field.collation) statement += ` COLLATE ${field.collation}`; | ||
if (field.increment === true) statement += " AUTOINCREMENT"; | ||
if (typeof field.check !== "undefined") { | ||
const symbol = getSymbol(field.check); | ||
statement += ` CHECK (${parseFieldExpression(model, "to", symbol?.value)})`; | ||
} | ||
if (typeof field.computedAs !== "undefined") { | ||
const { kind, value } = field.computedAs; | ||
const symbol = getSymbol(value); | ||
statement += ` GENERATED ALWAYS AS (${parseFieldExpression(model, "to", symbol?.value)}) ${kind}`; | ||
} | ||
if (field.type === "link") { | ||
const actions = field.actions || {}; | ||
const targetTable = convertToSnakeCase(pluralize(field.target.slug)); | ||
const targetTable = getModelBySlug(models, field.target).table; | ||
statement += ` REFERENCES ${targetTable}("id")`; | ||
@@ -716,7 +727,6 @@ for (const trigger in actions) { | ||
}; | ||
var addModelQueries = (models, queryDetails, dependencyStatements) => { | ||
const { queryType, queryModel, queryInstructions: queryInstructionsRaw } = queryDetails; | ||
const queryInstructions = queryInstructionsRaw; | ||
if (!["create", "set", "drop"].includes(queryType)) return queryInstructions; | ||
if (!SYSTEM_MODEL_SLUGS.includes(queryModel)) return queryInstructions; | ||
var addModelQueries = (models, dependencyStatements, queryDetails) => { | ||
const { queryType, queryModel, queryInstructions } = queryDetails; | ||
if (!["create", "set", "drop"].includes(queryType)) return; | ||
if (!SYSTEM_MODEL_SLUGS.includes(queryModel)) return; | ||
const instructionName = mappedInstructions[queryType]; | ||
@@ -749,18 +759,4 @@ const instructionList = queryInstructions[instructionName]; | ||
const slug = instructionList?.slug?.being || instructionList?.slug; | ||
if (!slug) { | ||
throw new RoninError({ | ||
message: `When ${queryTypeReadable} ${kind}, a \`slug\` field must be provided in the \`${instructionName}\` instruction.`, | ||
code: "MISSING_FIELD", | ||
fields: ["slug"] | ||
}); | ||
} | ||
const modelInstruction = instructionList?.model; | ||
const modelSlug = modelInstruction?.slug?.being || modelInstruction?.slug; | ||
if (kind !== "models" && !modelSlug) { | ||
throw new RoninError({ | ||
message: `When ${queryTypeReadable} ${kind}, a \`model.slug\` field must be provided in the \`${instructionName}\` instruction.`, | ||
code: "MISSING_FIELD", | ||
fields: ["model.slug"] | ||
}); | ||
} | ||
const usableSlug = kind === "models" ? slug : modelSlug; | ||
@@ -801,3 +797,3 @@ const tableName = convertToSnakeCase(pluralize(usableSlug)); | ||
dependencyStatements.push({ statement: statement2, params }); | ||
return queryInstructions; | ||
return; | ||
} | ||
@@ -854,17 +850,10 @@ if (kind === "triggers") { | ||
dependencyStatements.push({ statement: statement2, params }); | ||
return queryInstructions; | ||
return; | ||
} | ||
const statement = `${tableAction} TABLE "${tableName}"`; | ||
if (kind === "models") { | ||
if (queryType === "create" || queryType === "set") { | ||
const modelWithFields = addDefaultModelFields( | ||
queryInstructions.to, | ||
queryType === "create" | ||
); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
queryInstructions.to = modelWithPresets; | ||
} | ||
if (queryType === "create") { | ||
const { fields } = queryInstructions.to; | ||
const columns = fields.map(getFieldStatement).filter(Boolean); | ||
const newModel = queryInstructions.to; | ||
const { fields } = newModel; | ||
const columns = fields.map((field) => getFieldStatement(models, newModel, field)).filter(Boolean); | ||
dependencyStatements.push({ | ||
@@ -874,3 +863,3 @@ statement: `${statement} (${columns.join(", ")})`, | ||
}); | ||
models.push(queryInstructions.to); | ||
models.push(newModel); | ||
} else if (queryType === "set") { | ||
@@ -890,3 +879,3 @@ const newSlug = queryInstructions.to?.pluralSlug; | ||
} | ||
return queryInstructions; | ||
return; | ||
} | ||
@@ -903,3 +892,3 @@ if (kind === "fields") { | ||
dependencyStatements.push({ | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(instructionList)}`, | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`, | ||
params: [] | ||
@@ -922,3 +911,2 @@ }); | ||
} | ||
return queryInstructions; | ||
}; | ||
@@ -1227,3 +1215,3 @@ | ||
const fieldDetails = getFieldFromModel(model, fieldSlug, "to"); | ||
if (fieldDetails.field.type === "reference" && fieldDetails.field.kind === "many") { | ||
if (fieldDetails.field.type === "link" && fieldDetails.field.kind === "many") { | ||
delete toInstruction[fieldSlug]; | ||
@@ -1293,7 +1281,2 @@ const associativeModelSlug = composeAssociationModelSlug(model, fieldDetails.field); | ||
const returning = options?.returning ?? true; | ||
instructions = addModelQueries( | ||
models, | ||
{ queryType, queryModel, queryInstructions: instructions }, | ||
dependencyStatements | ||
); | ||
if (instructions && Object.hasOwn(instructions, "for")) { | ||
@@ -1429,2 +1412,116 @@ instructions = handleFor(model, instructions); | ||
// src/utils/meta.ts | ||
var ACTION_REGEX = /(?=[A-Z])/; | ||
var transformMetaQuery = (models, dependencyStatements, query) => { | ||
if ("addModel" in query) { | ||
const details = query.addModel; | ||
const modelWithFields = addDefaultModelFields(details, true); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
const instructions = { | ||
to: modelWithPresets | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "create", | ||
queryModel: "model", | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
create: { | ||
model: instructions | ||
} | ||
}; | ||
} | ||
if ("removeModel" in query) { | ||
const slug = query.removeModel; | ||
const instructions = { | ||
with: { slug } | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "drop", | ||
queryModel: "model", | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
drop: { | ||
model: instructions | ||
} | ||
}; | ||
} | ||
if ("alterModel" in query) { | ||
const slug = query.alterModel; | ||
const fullAction = Object.keys(query).filter((key) => key !== "alterModel")[0]; | ||
if (fullAction === "to") { | ||
const modelWithFields = addDefaultModelFields(query.to, false); | ||
const modelWithPresets = addDefaultModelPresets(models, modelWithFields); | ||
const instructions2 = { | ||
with: { slug }, | ||
to: modelWithPresets | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "set", | ||
queryModel: "model", | ||
queryInstructions: instructions2 | ||
}); | ||
return { | ||
set: { | ||
model: instructions2 | ||
} | ||
}; | ||
} | ||
const [action, type] = fullAction.split(ACTION_REGEX).map((part) => part.toLowerCase()); | ||
if (action === "add") { | ||
const item = query[fullAction]; | ||
const completeItem = { slug: item.slug || `${type}_slug`, ...item }; | ||
const instructions2 = { | ||
to: { | ||
model: { slug }, | ||
...completeItem | ||
} | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "create", | ||
queryModel: type, | ||
queryInstructions: instructions2 | ||
}); | ||
return { | ||
create: { | ||
[type]: instructions2 | ||
} | ||
}; | ||
} | ||
if (action === "alter") { | ||
const [itemSlug2, newItem] = query[fullAction]; | ||
const instructions2 = { | ||
with: { model: { slug }, slug: itemSlug2 }, | ||
to: newItem | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "set", | ||
queryModel: type, | ||
queryInstructions: instructions2 | ||
}); | ||
return { | ||
set: { | ||
[type]: instructions2 | ||
} | ||
}; | ||
} | ||
const itemSlug = query[fullAction]; | ||
const instructions = { | ||
with: { model: { slug }, slug: itemSlug } | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "drop", | ||
queryModel: type, | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
drop: { | ||
[type]: instructions | ||
} | ||
}; | ||
} | ||
return query; | ||
}; | ||
// src/index.ts | ||
@@ -1441,4 +1538,9 @@ var compileQueries = (queries, models, options) => { | ||
for (const query of queries) { | ||
const transformedQuery = transformMetaQuery( | ||
modelListWithPresets, | ||
dependencyStatements, | ||
query | ||
); | ||
const result = compileQueryInput( | ||
query, | ||
transformedQuery, | ||
modelListWithPresets, | ||
@@ -1445,0 +1547,0 @@ options?.inlineParams ? null : [] |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.7.1", | ||
"version": "0.7.2-leo-ron-1083-experimental-127", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Compiles RONIN queries to SQL statements.", |
Sorry, the diff of this file is too big to display
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
440055
7379
1