Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ronin/compiler

Package Overview
Dependencies
Maintainers
0
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ronin/compiler - npm Package Compare versions

Comparing version 0.7.1 to 0.7.2-leo-ron-1083-experimental-129

242

dist/index.js

@@ -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,120 @@ 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 instructions = {
with: { slug },
to: modelWithPresets
};
addModelQueries(models, dependencyStatements, {
queryType: "set",
queryModel: "model",
queryInstructions: instructions
});
return {
set: {
model: instructions
}
};
}
if ("add" in query.alter) {
const type = Object.keys(query.alter.add)[0];
const item = query.alter.add[type];
const completeItem = { slug: item.slug || `${type}_slug`, ...item };
const instructions = {
to: {
model: { slug },
...completeItem
}
};
addModelQueries(models, dependencyStatements, {
queryType: "create",
queryModel: type,
queryInstructions: instructions
});
return {
create: {
[type]: instructions
}
};
}
if ("alter" in query.alter) {
const type = Object.keys(query.alter.alter)[0];
const itemSlug = query.alter.alter[type];
const newItem = query.alter.alter.options;
const instructions = {
with: { model: { slug }, slug: itemSlug },
to: newItem
};
addModelQueries(models, dependencyStatements, {
queryType: "set",
queryModel: type,
queryInstructions: instructions
});
return {
set: {
[type]: instructions
}
};
}
if ("remove" in query.alter) {
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

@@ -1441,4 +1542,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 +1551,0 @@ options?.inlineParams ? null : []

{
"name": "@ronin/compiler",
"version": "0.7.1",
"version": "0.7.2-leo-ron-1083-experimental-129",
"type": "module",

@@ -5,0 +5,0 @@ "description": "Compiles RONIN queries to SQL statements.",

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc