@ronin/compiler
Advanced tools
Comparing version 0.7.1 to 0.7.2-leo-ron-1083-experimental-133
@@ -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,15 +879,9 @@ const newSlug = queryInstructions.to?.pluralSlug; | ||
} | ||
return queryInstructions; | ||
return; | ||
} | ||
if (kind === "fields") { | ||
if (queryType === "create") { | ||
if (!instructionList.type) { | ||
throw new RoninError({ | ||
message: `When ${queryTypeReadable} fields, a \`type\` field must be provided in the \`to\` instruction.`, | ||
code: "MISSING_FIELD", | ||
fields: ["type"] | ||
}); | ||
} | ||
if (!instructionList.type) instructionList.type = "string"; | ||
dependencyStatements.push({ | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(instructionList)}`, | ||
statement: `${statement} ADD COLUMN ${getFieldStatement(models, targetModel, instructionList)}`, | ||
params: [] | ||
@@ -921,3 +904,2 @@ }); | ||
} | ||
return queryInstructions; | ||
}; | ||
@@ -1226,3 +1208,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]; | ||
@@ -1292,7 +1274,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")) { | ||
@@ -1428,2 +1405,118 @@ instructions = handleFor(model, instructions); | ||
// src/utils/meta.ts | ||
var transformMetaQuery = (models, dependencyStatements, query) => { | ||
if (query.add) { | ||
const init = query.add.model; | ||
const details = "options" in query.add ? { slug: init, ...query.add.options } : init; | ||
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 (query.remove) { | ||
const slug = query.remove.model; | ||
const instructions = { | ||
with: { slug } | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "drop", | ||
queryModel: "model", | ||
queryInstructions: instructions | ||
}); | ||
return { | ||
drop: { | ||
model: instructions | ||
} | ||
}; | ||
} | ||
if (query.alter) { | ||
const slug = query.alter.model; | ||
if ("options" in query.alter) { | ||
const modelWithFields = addDefaultModelFields(query.alter.options, 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 | ||
} | ||
}; | ||
} | ||
if ("add" in query.alter) { | ||
const type2 = Object.keys(query.alter.add)[0]; | ||
const item = query.alter.add[type2]; | ||
const completeItem = { slug: item.slug || `${type2}_slug`, ...item }; | ||
const instructions2 = { | ||
to: { | ||
model: { slug }, | ||
...completeItem | ||
} | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "create", | ||
queryModel: type2, | ||
queryInstructions: instructions2 | ||
}); | ||
return { | ||
create: { | ||
[type2]: instructions2 | ||
} | ||
}; | ||
} | ||
if ("alter" in query.alter) { | ||
const type2 = Object.keys(query.alter.alter)[0]; | ||
const itemSlug2 = query.alter.alter[type2]; | ||
const newItem = query.alter.alter.options; | ||
const instructions2 = { | ||
with: { model: { slug }, slug: itemSlug2 }, | ||
to: newItem | ||
}; | ||
addModelQueries(models, dependencyStatements, { | ||
queryType: "set", | ||
queryModel: type2, | ||
queryInstructions: instructions2 | ||
}); | ||
return { | ||
set: { | ||
[type2]: instructions2 | ||
} | ||
}; | ||
} | ||
const type = Object.keys(query.alter.remove)[0]; | ||
const itemSlug = query.alter.remove[type]; | ||
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 | ||
@@ -1440,4 +1533,9 @@ var compileQueries = (queries, models, options) => { | ||
for (const query of queries) { | ||
const transformedQuery = transformMetaQuery( | ||
modelListWithPresets, | ||
dependencyStatements, | ||
query | ||
); | ||
const result = compileQueryInput( | ||
query, | ||
transformedQuery, | ||
modelListWithPresets, | ||
@@ -1444,0 +1542,0 @@ options?.inlineParams ? null : [] |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.7.1", | ||
"version": "0.7.2-leo-ron-1083-experimental-133", | ||
"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
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
444547
7501
0
1