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
139
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.8.4 to 0.8.5-leo-ron-1083-experimental-168

403

dist/index.js

@@ -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,47 +632,117 @@ 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 && query.alter ? Object.keys(query.alter).filter((key) => key !== "model")[0] : queryType;
const actionReadable = action === "create" ? "creating" : action === "alter" ? "altering" : "dropping";
const entity = subAltering && query.alter ? Object.keys(query.alter[action])[0] : "model";
let slug = entity === "model" && action === "create" ? null : query[queryType].model;
let modelSlug = slug;
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.alter) {
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 tableName = convertToSnakeCase(pluralize(modelSlug));
const model = action === "create" && entity === "model" ? null : getModelBySlug(models, modelSlug);
const statement = `${tableAction} TABLE "${tableName}"`;
if (entity === "model") {
let queryTypeDetails;
if (action === "create") {
const newModel = jsonValue;
const modelWithFields = addDefaultModelFields(newModel, 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" && model) {
const newModel = jsonValue;
const modelWithFields = addDefaultModelFields(newModel, 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(model, modelWithPresets);
queryTypeDetails = {
with: {
slug
},
to: modelWithPresets
};
}
if (action === "drop" && model) {
models.splice(models.indexOf(model), 1);
dependencyStatements.push({ statement, params: [] });
queryTypeDetails = { with: { slug } };
}
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" && model) {
if (action === "create") {
const field = jsonValue;
field.type = field.type || "string";
dependencyStatements.push({
statement: `${statement} ADD COLUMN ${getFieldStatement(models, model, field)}`,
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" && model) {
const index = jsonValue;
const indexName = convertToSnakeCase(slug);
const unique = instructionList?.unique;
const filterQuery = instructionList?.filter;
const fields = instructionList?.fields;
const params = [];
let statement2 = `${tableAction}${unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
if (queryType === "add") {
const model = targetModel;
const columns = fields.map((field) => {
let statement2 = `${tableAction}${index?.unique ? " UNIQUE" : ""} INDEX "${indexName}"`;
if (action === "create") {
const columns = index.fields.map((field) => {
let fieldSelector = "";

@@ -832,3 +752,3 @@ if ("slug" in field) {

} else if ("expression" in field) {
fieldSelector = parseFieldExpression(model, "to", field.expression, model);
fieldSelector = parseFieldExpression(model, "to", field.expression);
}

@@ -840,9 +760,4 @@ if (field.collation) fieldSelector += ` COLLATE ${field.collation}`;

statement2 += ` ON "${tableName}" (${columns.join(", ")})`;
if (filterQuery) {
const withStatement = handleWith(
models,
targetModel,
params,
filterQuery
);
if (index.filter) {
const withStatement = handleWith(models, model, params, index.filter);
statement2 += ` WHERE (${withStatement})`;

@@ -852,19 +767,14 @@ }

dependencyStatements.push({ statement: statement2, params });
return;
}
if (queryModel === "trigger") {
if (entity === "trigger" && model) {
const triggerName = convertToSnakeCase(slug);
const params = [];
let statement2 = `${tableAction} TRIGGER "${triggerName}"`;
if (queryType === "add") {
const currentModel = targetModel;
const { when, action } = instructionList;
const statementParts = [`${when} ${action}`];
const effectQueries = instructionList?.effects;
const filterQuery = instructionList?.filter;
const fields = instructionList?.fields;
if (fields) {
if (action !== "UPDATE") {
if (action === "create") {
const trigger = jsonValue;
const statementParts = [`${trigger.when} ${trigger.action}`];
if (trigger.fields) {
if (trigger.action !== "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",

@@ -874,4 +784,4 @@ fields: ["action"]

}
const fieldSelectors = fields.map((field) => {
return getFieldFromModel(currentModel, field.slug, "to").fieldSelector;
const fieldSelectors = trigger.fields.map((field) => {
return getFieldFromModel(model, field.slug, "to").fieldSelector;
});

@@ -881,19 +791,19 @@ statementParts.push(`OF (${fieldSelectors.join(", ")})`);

statementParts.push("ON", `"${tableName}"`);
if (filterQuery || effectQueries.some((query) => findInObject(query, RONIN_MODEL_SYMBOLS.FIELD))) {
if (trigger.filter || trigger.effects.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;
if (trigger.filter) {
const tableAlias = trigger.action === "DELETE" ? RONIN_MODEL_SYMBOLS.FIELD_PARENT_OLD : RONIN_MODEL_SYMBOLS.FIELD_PARENT_NEW;
const withStatement = handleWith(
models,
{ ...currentModel, tableAlias },
{ ...model, tableAlias },
params,
filterQuery
trigger.filter
);
statementParts.push("WHEN", `(${withStatement})`);
}
const effectStatements = effectQueries.map((effectQuery) => {
const effectStatements = trigger.effects.map((effectQuery) => {
return compileQueryInput(effectQuery, models, params, {
returning: false,
parentModel: currentModel
parentModel: model
}).main.statement;

@@ -907,53 +817,18 @@ });

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";
let json = `json_${jsonAction}(${RONIN_MODEL_SYMBOLS.FIELD}${pluralType}, '$.${slug}'`;
if (jsonValue) json += `, ${prepareStatementValue(statementParams, jsonValue)}`;
json += ")";
return {
set: {
model: {
with: { slug: modelSlug },
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: []
});
}
}
};
};

@@ -960,0 +835,0 @@

{
"name": "@ronin/compiler",
"version": "0.8.4",
"version": "0.8.5-leo-ron-1083-experimental-168",
"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