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
157
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.4.0 to 0.5.0

168

dist/index.js

@@ -99,5 +99,5 @@ // src/utils/helpers.ts

// src/utils/statement.ts
var prepareStatementValue = (statementValues, value) => {
var prepareStatementValue = (statementParams, value) => {
if (value === null) return "NULL";
if (!statementValues) return JSON.stringify(value);
if (!statementParams) return JSON.stringify(value);
let formattedValue = value;

@@ -109,6 +109,6 @@ if (Array.isArray(value) || isObject(value)) {

}
const index = statementValues.push(formattedValue);
const index = statementParams.push(formattedValue);
return `?${index}`;
};
var composeFieldValues = (schemas, schema, statementValues, instructionName, value, options) => {
var composeFieldValues = (schemas, schema, statementParams, instructionName, value, options) => {
const { field: schemaField, fieldSelector: selector } = getFieldFromSchema(

@@ -128,4 +128,4 @@ schema,

schemas,
statementValues
).readStatement})`;
statementParams
).main.statement})`;
} else if (typeof value === "string" && value.startsWith(RONIN_SCHEMA_SYMBOLS.FIELD)) {

@@ -146,7 +146,7 @@ let targetTable = `"${options.rootTable}"`;

if (collectStatementValue) {
const preparedValue = prepareStatementValue(statementValues, value);
const preparedValue = prepareStatementValue(statementParams, value);
conditionValue = `IIF(${conditionSelector} IS NULL, ${preparedValue}, json_patch(${conditionSelector}, ${preparedValue}))`;
}
} else if (collectStatementValue) {
conditionValue = prepareStatementValue(statementValues, value);
conditionValue = prepareStatementValue(statementParams, value);
}

@@ -157,7 +157,7 @@ if (options.type === "fields") return conditionSelector;

};
var composeConditions = (schemas, schema, statementValues, instructionName, value, options) => {
var composeConditions = (schemas, schema, statementParams, instructionName, value, options) => {
const isNested = isObject(value) && Object.keys(value).length > 0;
if (isNested && Object.keys(value).every((key) => key in WITH_CONDITIONS)) {
const conditions = Object.entries(value).map(
([conditionType, checkValue]) => composeConditions(schemas, schema, statementValues, instructionName, checkValue, {
([conditionType, checkValue]) => composeConditions(schemas, schema, statementParams, instructionName, checkValue, {
...options,

@@ -183,3 +183,3 @@ condition: conditionType

schema,
statementValues,
statementParams,
instructionName,

@@ -213,3 +213,3 @@ value,

schema,
statementValues,
statementParams,
instructionName,

@@ -224,3 +224,3 @@ recordTarget,

const nestedFieldSlug = options.fieldSlug ? `${options.fieldSlug}.${field}` : field;
return composeConditions(schemas, schema, statementValues, instructionName, value2, {
return composeConditions(schemas, schema, statementParams, instructionName, value2, {
...options,

@@ -239,3 +239,3 @@ fieldSlug: nestedFieldSlug

schema,
statementValues,
statementParams,
instructionName,

@@ -299,7 +299,7 @@ filter,

};
var handleWith = (schemas, schema, statementValues, instruction, rootTable) => {
var handleWith = (schemas, schema, statementParams, instruction, rootTable) => {
const subStatement = composeConditions(
schemas,
schema,
statementValues,
statementParams,
"with",

@@ -614,3 +614,3 @@ instruction,

};
var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements) => {
var addSchemaQueries = (schemas, queryDetails, dependencyStatements) => {
const {

@@ -667,3 +667,5 @@ queryType,

}
const tableName = convertToSnakeCase(pluralize(kind === "schemas" ? slug : schemaSlug));
const usableSlug = kind === "schemas" ? slug : schemaSlug;
const tableName = convertToSnakeCase(pluralize(usableSlug));
const targetSchema = kind === "schemas" && queryType === "create" ? null : getSchemaBySlug(schemas, usableSlug);
if (kind === "indexes") {

@@ -673,2 +675,3 @@ const indexName = convertToSnakeCase(slug);

const filterQuery = instructionList?.filter;
const params = [];
let statement2 = `${tableAction}${unique ? " UNIQUE" : ""} INDEX "${indexName}"`;

@@ -678,7 +681,6 @@ if (queryType === "create") {

if (filterQuery) {
const targetSchema = getSchemaBySlug(schemas, schemaSlug);
const withStatement = handleWith(
schemas,
targetSchema,
statementValues,
params,
filterQuery

@@ -689,3 +691,3 @@ );

}
writeStatements.push(statement2);
dependencyStatements.push({ statement: statement2, params });
return queryInstructions;

@@ -695,2 +697,3 @@ }

const triggerName = convertToSnakeCase(slug);
const params = [];
let statement2 = `${tableAction} TRIGGER "${triggerName}"`;

@@ -706,3 +709,2 @@ if (queryType === "create") {

if (filterQuery) {
const targetSchema = getSchemaBySlug(schemas, schemaSlug);
const tablePlaceholder = cause.endsWith("DELETE") ? RONIN_SCHEMA_SYMBOLS.FIELD_OLD : RONIN_SCHEMA_SYMBOLS.FIELD_NEW;

@@ -712,3 +714,3 @@ const withStatement = handleWith(

targetSchema,
statementValues,
params,
filterQuery,

@@ -720,5 +722,5 @@ tablePlaceholder

const effectStatements = effectQueries.map((effectQuery) => {
return compileQueryInput(effectQuery, schemas, statementValues, {
disableReturning: true
}).readStatement;
return compileQueryInput(effectQuery, schemas, params, {
returning: false
}).main.statement;
});

@@ -730,3 +732,3 @@ if (effectStatements.length > 1) statementParts.push("BEGIN");

}
writeStatements.push(statement2);
dependencyStatements.push({ statement: statement2, params });
return queryInstructions;

@@ -744,2 +746,3 @@ }

statement += ` (${columns.join(", ")})`;
schemas.push(queryInstructions.to);
} else if (queryType === "set") {

@@ -751,4 +754,7 @@ const newSlug = queryInstructions.to?.pluralSlug;

}
Object.assign(targetSchema, queryInstructions.to);
} else if (queryType === "drop") {
schemas.splice(schemas.indexOf(targetSchema), 1);
}
writeStatements.push(statement);
dependencyStatements.push({ statement, params: [] });
return queryInstructions;

@@ -774,3 +780,3 @@ }

}
writeStatements.push(statement);
dependencyStatements.push({ statement, params: [] });
}

@@ -799,3 +805,3 @@ return queryInstructions;

var CURSOR_NULL_PLACEHOLDER = "RONIN_NULL";
var handleBeforeOrAfter = (schema, statementValues, instructions, rootTable) => {
var handleBeforeOrAfter = (schema, statementParams, instructions, rootTable) => {
if (!(instructions.before || instructions.after)) {

@@ -826,6 +832,6 @@ throw new RoninError({

if (field.type === "boolean") {
return prepareStatementValue(statementValues, value === "true");
return prepareStatementValue(statementParams, value === "true");
}
if (field.type === "number") {
return prepareStatementValue(statementValues, Number.parseInt(value));
return prepareStatementValue(statementParams, Number.parseInt(value));
}

@@ -835,3 +841,3 @@ if (field.type === "date") {

}
return prepareStatementValue(statementValues, value);
return prepareStatementValue(statementParams, value);
});

@@ -879,3 +885,3 @@ const compareOperators = [

// src/instructions/for.ts
var handleFor = (schemas, schema, statementValues, instruction, rootTable) => {
var handleFor = (schemas, schema, statementParams, instruction, rootTable) => {
let statement = "";

@@ -901,3 +907,3 @@ if (!instruction) return statement;

schema,
statementValues,
statementParams,
"for",

@@ -913,3 +919,3 @@ replacedForFilter,

// src/instructions/including.ts
var handleIncluding = (schemas, statementValues, schema, instruction, rootTable) => {
var handleIncluding = (schemas, statementParams, schema, instruction, rootTable) => {
let statement = "";

@@ -948,5 +954,5 @@ let rootTableSubQuery;

schemas,
statementValues
statementParams
);
relatedTableSelector = `(${subSelect.readStatement})`;
relatedTableSelector = `(${subSelect.main.statement})`;
}

@@ -962,3 +968,3 @@ statement += `${joinType} JOIN ${relatedTableSelector} as ${tableAlias}`;

relatedSchema,
statementValues,
statementParams,
"including",

@@ -1017,3 +1023,3 @@ queryInstructions?.with,

// src/instructions/selecting.ts
var handleSelecting = (schema, statementValues, instructions) => {
var handleSelecting = (schema, statementParams, instructions) => {
let statement = instructions.selecting ? instructions.selecting.map((slug) => {

@@ -1029,3 +1035,3 @@ return getFieldFromSchema(schema, slug, "selecting").fieldSelector;

}).map(([key, value]) => {
return `${prepareStatementValue(statementValues, value)} as "${key}"`;
return `${prepareStatementValue(statementParams, value)} as "${key}"`;
}).join(", ");

@@ -1037,3 +1043,3 @@ }

// src/instructions/to.ts
var handleTo = (schemas, schema, statementValues, queryType, writeStatements, instructions, rootTable) => {
var handleTo = (schemas, schema, statementParams, queryType, dependencyStatements, instructions, rootTable) => {
const currentTime = (/* @__PURE__ */ new Date()).toISOString();

@@ -1080,3 +1086,3 @@ const { with: withInstruction, to: toInstruction } = instructions;

}
return compileQueryInput(subQuery, schemas, statementValues).readStatement;
return compileQueryInput(subQuery, schemas, statementParams).main.statement;
}

@@ -1097,3 +1103,3 @@ Object.assign(toInstruction, defaultFields);

if (value) recordDetails.target = value;
const { readStatement } = compileQueryInput(
return compileQueryInput(
{

@@ -1105,18 +1111,17 @@ [subQueryType]: {

schemas,
statementValues,
{ disableReturning: true }
);
return readStatement;
[],
{ returning: false }
).main;
};
if (Array.isArray(fieldValue)) {
writeStatements.push(composeStatement("drop"));
dependencyStatements.push(composeStatement("drop"));
for (const record of fieldValue) {
writeStatements.push(composeStatement("create", record));
dependencyStatements.push(composeStatement("create", record));
}
} else if (isObject(fieldValue)) {
for (const recordToAdd of fieldValue.containing || []) {
writeStatements.push(composeStatement("create", recordToAdd));
dependencyStatements.push(composeStatement("create", recordToAdd));
}
for (const recordToRemove of fieldValue.notContaining || []) {
writeStatements.push(composeStatement("drop", recordToRemove));
dependencyStatements.push(composeStatement("drop", recordToRemove));
}

@@ -1129,3 +1134,3 @@ }

schema,
statementValues,
statementParams,
"to",

@@ -1142,3 +1147,3 @@ toInstruction,

schema,
statementValues,
statementParams,
"to",

@@ -1159,6 +1164,5 @@ toInstruction,

// src/utils/index.ts
var compileQueryInput = (query, defaultSchemas, statementValues, options) => {
var compileQueryInput = (query, schemas, statementParams, options) => {
const parsedQuery = splitQuery(query);
const { queryType, querySchema, queryInstructions } = parsedQuery;
const schemas = addSystemSchemas(defaultSchemas);
const schema = getSchemaBySlug(schemas, querySchema);

@@ -1168,10 +1172,10 @@ const single = querySchema !== schema.pluralSlug;

let table = getTableForSchema(schema);
const writeStatements = [];
const dependencyStatements = [];
const returning = options?.returning ?? true;
instructions = addSchemaQueries(
schemas,
statementValues,
{ queryType, querySchema, queryInstructions: instructions },
writeStatements
dependencyStatements
);
const columns = handleSelecting(schema, statementValues, {
const columns = handleSelecting(schema, statementParams, {
selecting: instructions?.selecting,

@@ -1205,3 +1209,3 @@ including: instructions?.including

rootTableName
} = handleIncluding(schemas, statementValues, schema, instructions?.including, table);
} = handleIncluding(schemas, statementParams, schema, instructions?.including, table);
if (rootTableSubQuery && rootTableName) {

@@ -1229,5 +1233,5 @@ table = rootTableName;

schema,
statementValues,
statementParams,
queryType,
writeStatements,
dependencyStatements,
{ with: instructions.with, to: instructions.to },

@@ -1243,3 +1247,3 @@ isJoining ? table : void 0

schema,
statementValues,
statementParams,
instructions?.with,

@@ -1254,3 +1258,3 @@ isJoining ? table : void 0

schema,
statementValues,
statementParams,
instructions?.for,

@@ -1283,3 +1287,3 @@ isJoining ? table : void 0

schema,
statementValues,
statementParams,
{

@@ -1313,10 +1317,13 @@ before: instructions.before,

}
if (["create", "set", "drop"].includes(queryType) && !options?.disableReturning) {
if (["create", "set", "drop"].includes(queryType) && returning) {
statement += "RETURNING * ";
}
const finalStatement = statement.trimEnd();
const mainStatement = {
statement: statement.trimEnd(),
params: statementParams || []
};
if (returning) mainStatement.returning = true;
return {
writeStatements,
readStatement: finalStatement,
values: statementValues || []
dependencies: dependencyStatements,
main: mainStatement
};

@@ -1326,8 +1333,19 @@ };

// src/index.ts
var compileQuery = (query, schemas, options) => {
const statementValues = options?.inlineValues ? null : [];
return compileQueryInput(query, schemas, statementValues);
var compileQueries = (queries, schemas, options) => {
const schemaList = addSystemSchemas(schemas);
const dependencyStatements = [];
const mainStatements = [];
for (const query of queries) {
const result = compileQueryInput(
query,
schemaList,
options?.inlineValues ? null : []
);
dependencyStatements.push(...result.dependencies);
mainStatements.push(result.main);
}
return [...dependencyStatements, ...mainStatements];
};
export {
compileQuery
compileQueries
};
{
"name": "@ronin/compiler",
"version": "0.4.0",
"version": "0.5.0",
"type": "module",

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

@@ -42,20 +42,26 @@ # RONIN Compiler

```typescript
import { compileQuery } from '@ronin/compiler';
import {
compileQueries,
const query = {
type Query,
type Schema,
type Statement
} from '@ronin/compiler';
const queries: Array<Query> = [{
get: {
accounts: null,
},
};
accounts: null
}
}];
const schemas = [
{
slug: 'account',
},
];
const schemas: Array<Schema> = [{
slug: 'account'
}];
const { writeStatements, readStatement } = compileQuery(query, schemas);
console.log(readStatement);
// SELECT * FROM "accounts" ORDER BY "ronin.createdAt" DESC LIMIT 101
const statements: Array<Statements> = compileQueries(queries, schemas);
// [{
// statement: 'SELECT * FROM "accounts" ORDER BY "ronin.createdAt" DESC LIMIT 101',
// params: [],
// returning: true,
// }]
```

@@ -68,3 +74,3 @@

```typescript
compileQuery(query, schemas, {
compileQueries(queries, schemas, {
// Instead of returning an array of values for every statement (which allows for

@@ -71,0 +77,0 @@ // preventing SQL injections), all values are inlined directly into the SQL strings.

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