@ronin/compiler
Advanced tools
Comparing version 0.7.1 to 0.7.2-leo-ron-1083-experimental-124
@@ -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")`; | ||
@@ -748,18 +759,4 @@ for (const trigger in actions) { | ||
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; | ||
@@ -865,4 +862,5 @@ const tableName = convertToSnakeCase(pluralize(usableSlug)); | ||
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({ | ||
@@ -872,3 +870,3 @@ statement: `${statement} (${columns.join(", ")})`, | ||
}); | ||
models.push(queryInstructions.to); | ||
models.push(newModel); | ||
} else if (queryType === "set") { | ||
@@ -900,3 +898,3 @@ const newSlug = queryInstructions.to?.pluralSlug; | ||
dependencyStatements.push({ | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(instructionList)}`, | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`, | ||
params: [] | ||
@@ -1223,3 +1221,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]; | ||
@@ -1424,2 +1422,65 @@ const associativeModelSlug = composeAssociationModelSlug(model, fieldDetails.field); | ||
// src/utils/meta.ts | ||
var ACTION_REGEX = /(?=[A-Z])/; | ||
var transformMetaQuery = (query) => { | ||
if ("addModel" in query) { | ||
const details = query.addModel; | ||
return { | ||
create: { | ||
model: { | ||
to: details | ||
} | ||
} | ||
}; | ||
} | ||
if ("removeModel" in query) { | ||
const slug = query.removeModel; | ||
return { | ||
drop: { | ||
model: { | ||
with: { slug } | ||
} | ||
} | ||
}; | ||
} | ||
if ("alterModel" in query) { | ||
const slug = query.alterModel; | ||
const fullAction = Object.keys(query).filter((key) => key !== "alterModel")[0]; | ||
if (fullAction === "to") { | ||
return { | ||
set: { | ||
model: { | ||
with: { slug }, | ||
to: query.to | ||
} | ||
} | ||
}; | ||
} | ||
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 }; | ||
return { | ||
create: { | ||
[type]: { | ||
to: { | ||
model: { slug }, | ||
...completeItem | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
const itemSlug = query[fullAction]; | ||
return { | ||
drop: { | ||
[type]: { | ||
with: { model: { slug }, slug: itemSlug } | ||
} | ||
} | ||
}; | ||
} | ||
return query; | ||
}; | ||
// src/index.ts | ||
@@ -1436,4 +1497,5 @@ var compileQueries = (queries, models, options) => { | ||
for (const query of queries) { | ||
const transformedQuery = transformMetaQuery(query); | ||
const result = compileQueryInput( | ||
query, | ||
transformedQuery, | ||
modelListWithPresets, | ||
@@ -1440,0 +1502,0 @@ options?.inlineParams ? null : [] |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.7.1", | ||
"version": "0.7.2-leo-ron-1083-experimental-124", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Compiles RONIN queries to SQL statements.", |
Sorry, the diff of this file is too big to display
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
438933
7339
1