@ronin/compiler
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -1,2 +0,2 @@ | ||
// src/utils/index.ts | ||
// src/utils/helpers.ts | ||
import { init as cuid } from "@paralleldrive/cuid2"; | ||
@@ -36,3 +36,3 @@ var RONIN_SCHEMA_SYMBOLS = { | ||
var SPLIT_REGEX = /(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|[\s.\-_]+/; | ||
var generateRecordId = (prefix) => `${prefix || "rec"}_${cuid({ length: 16 })()}`; | ||
var generateRecordId = (prefix) => `${prefix}_${cuid({ length: 16 })()}`; | ||
var capitalize = (str) => { | ||
@@ -100,4 +100,5 @@ if (!str || str.length === 0) return ""; | ||
// src/utils/statement.ts | ||
var prepareStatementValue = (statementValues, value, bindNull = false) => { | ||
if (!bindNull && value === null) return "NULL"; | ||
var prepareStatementValue = (statementValues, value) => { | ||
if (value === null) return "NULL"; | ||
if (!statementValues) return JSON.stringify(value); | ||
let formattedValue = value; | ||
@@ -127,3 +128,3 @@ if (Array.isArray(value) || isObject(value)) { | ||
schemas, | ||
{ statementValues } | ||
statementValues | ||
).readStatement})`; | ||
@@ -145,7 +146,7 @@ } else if (typeof value === "string" && value.startsWith(RONIN_SCHEMA_SYMBOLS.FIELD)) { | ||
if (collectStatementValue) { | ||
const preparedValue = prepareStatementValue(statementValues, value, false); | ||
const preparedValue = prepareStatementValue(statementValues, value); | ||
conditionValue = `IIF(${conditionSelector} IS NULL, ${preparedValue}, json_patch(${conditionSelector}, ${preparedValue}))`; | ||
} | ||
} else if (collectStatementValue) { | ||
conditionValue = prepareStatementValue(statementValues, value, false); | ||
conditionValue = prepareStatementValue(statementValues, value); | ||
} | ||
@@ -257,6 +258,6 @@ if (options.type === "fields") return conditionSelector; | ||
for (const oldKey of Object.keys(newNestedInstructions)) { | ||
if (oldKey !== "titleIdentifier" && oldKey !== "slugIdentifier") continue; | ||
const identifierName = oldKey === "titleIdentifier" ? "title" : "slug"; | ||
if (oldKey !== "nameIdentifier" && oldKey !== "slugIdentifier") continue; | ||
const identifierName = oldKey === "nameIdentifier" ? "name" : "slug"; | ||
const value = newNestedInstructions[oldKey]; | ||
const newKey = identifiers?.[identifierName] || "id"; | ||
const newKey = identifiers[identifierName]; | ||
newNestedInstructions[newKey] = value; | ||
@@ -403,2 +404,6 @@ delete newNestedInstructions[oldKey]; | ||
pluralSlug: "schemas", | ||
identifiers: { | ||
name: "name", | ||
slug: "slug" | ||
}, | ||
fields: [ | ||
@@ -412,3 +417,3 @@ ...SYSTEM_FIELDS, | ||
{ slug: "identifiers", type: "group" }, | ||
{ slug: "identifiers.title", type: "string" }, | ||
{ slug: "identifiers.name", type: "string" }, | ||
{ slug: "identifiers.slug", type: "string" }, | ||
@@ -425,2 +430,6 @@ { slug: "fields", type: "json" }, | ||
pluralSlug: "fields", | ||
identifiers: { | ||
name: "name", | ||
slug: "slug" | ||
}, | ||
fields: [ | ||
@@ -454,2 +463,6 @@ ...SYSTEM_FIELDS, | ||
pluralSlug: "indexes", | ||
identifiers: { | ||
name: "slug", | ||
slug: "slug" | ||
}, | ||
fields: [ | ||
@@ -473,2 +486,6 @@ ...SYSTEM_FIELDS, | ||
pluralSlug: "triggers", | ||
identifiers: { | ||
name: "slug", | ||
slug: "slug" | ||
}, | ||
fields: [ | ||
@@ -494,2 +511,6 @@ ...SYSTEM_FIELDS, | ||
copiedSchema.pluralName = slugToName(copiedSchema.pluralSlug); | ||
if (!copiedSchema.idPrefix) copiedSchema.idPrefix = copiedSchema.slug.slice(0, 3); | ||
if (!copiedSchema.identifiers) copiedSchema.identifiers = {}; | ||
if (!copiedSchema.identifiers.name) copiedSchema.identifiers.name = "id"; | ||
if (!copiedSchema.identifiers.slug) copiedSchema.identifiers.slug = "id"; | ||
return copiedSchema; | ||
@@ -510,2 +531,6 @@ }; | ||
slug: fieldSlug, | ||
identifiers: { | ||
name: "id", | ||
slug: "id" | ||
}, | ||
fields: [ | ||
@@ -592,5 +617,10 @@ { | ||
var addSchemaQueries = (schemas, statementValues, queryDetails, writeStatements) => { | ||
const { queryType, querySchema, queryInstructions } = queryDetails; | ||
if (!["create", "set", "drop"].includes(queryType)) return; | ||
if (!SYSTEM_SCHEMA_SLUGS.includes(querySchema)) return; | ||
const { | ||
queryType, | ||
querySchema, | ||
queryInstructions: queryInstructionsRaw | ||
} = queryDetails; | ||
const queryInstructions = queryInstructionsRaw; | ||
if (!["create", "set", "drop"].includes(queryType)) return queryInstructions; | ||
if (!SYSTEM_SCHEMA_SLUGS.includes(querySchema)) return queryInstructions; | ||
const instructionName = mappedInstructions[queryType]; | ||
@@ -659,3 +689,3 @@ const instructionList = queryInstructions[instructionName]; | ||
writeStatements.push(statement2); | ||
return; | ||
return queryInstructions; | ||
} | ||
@@ -686,4 +716,3 @@ if (kind === "triggers") { | ||
const effectStatements = effectQueries.map((effectQuery) => { | ||
return compileQueryInput(effectQuery, schemas, { | ||
statementValues, | ||
return compileQueryInput(effectQuery, schemas, statementValues, { | ||
disableReturning: true | ||
@@ -698,7 +727,11 @@ }).readStatement; | ||
writeStatements.push(statement2); | ||
return; | ||
return queryInstructions; | ||
} | ||
let statement = `${tableAction} TABLE "${tableName}"`; | ||
if (kind === "schemas") { | ||
const fields = [...SYSTEM_FIELDS]; | ||
const providedFields = instructionList?.fields || []; | ||
const fields = [...SYSTEM_FIELDS, ...providedFields]; | ||
if (queryType === "create" || queryType === "set") { | ||
queryInstructions.to = prepareSchema(queryInstructions.to); | ||
} | ||
if (queryType === "create") { | ||
@@ -708,5 +741,5 @@ const columns = fields.map(getFieldStatement).filter(Boolean); | ||
} else if (queryType === "set") { | ||
const newSlug = queryInstructions.to?.slug; | ||
const newSlug = queryInstructions.to?.pluralSlug; | ||
if (newSlug) { | ||
const newTable = convertToSnakeCase(pluralize(newSlug)); | ||
const newTable = convertToSnakeCase(newSlug); | ||
statement += ` RENAME TO "${newTable}"`; | ||
@@ -716,3 +749,3 @@ } | ||
writeStatements.push(statement); | ||
return; | ||
return queryInstructions; | ||
} | ||
@@ -739,2 +772,3 @@ if (kind === "fields") { | ||
} | ||
return queryInstructions; | ||
}; | ||
@@ -904,3 +938,3 @@ var slugToName = (slug) => { | ||
schemas, | ||
{ statementValues } | ||
statementValues | ||
); | ||
@@ -1032,5 +1066,3 @@ relatedTableSelector = `(${subSelect.readStatement})`; | ||
} | ||
return compileQueryInput(subQuery, schemas, { | ||
statementValues | ||
}).readStatement; | ||
return compileQueryInput(subQuery, schemas, statementValues).readStatement; | ||
} | ||
@@ -1058,3 +1090,4 @@ Object.assign(toInstruction, defaultFields); | ||
schemas, | ||
{ statementValues, disableReturning: true } | ||
statementValues, | ||
{ disableReturning: true } | ||
); | ||
@@ -1108,4 +1141,4 @@ return readStatement; | ||
// src/index.ts | ||
var compileQueryInput = (query, defaultSchemas, options) => { | ||
// src/utils/index.ts | ||
var compileQueryInput = (query, defaultSchemas, statementValues, options) => { | ||
const parsedQuery = splitQuery(query); | ||
@@ -1118,5 +1151,9 @@ const { queryType, querySchema, queryInstructions } = parsedQuery; | ||
let table = getTableForSchema(schema); | ||
const statementValues = options?.statementValues || []; | ||
const writeStatements = []; | ||
addSchemaQueries(schemas, statementValues, parsedQuery, writeStatements); | ||
instructions = addSchemaQueries( | ||
schemas, | ||
statementValues, | ||
{ queryType, querySchema, queryInstructions: instructions }, | ||
writeStatements | ||
); | ||
const columns = handleSelecting(schema, statementValues, { | ||
@@ -1261,7 +1298,13 @@ selecting: instructions?.selecting, | ||
readStatement: finalStatement, | ||
values: statementValues | ||
values: statementValues || [] | ||
}; | ||
}; | ||
// src/index.ts | ||
var compileQuery = (query, schemas, options) => { | ||
const statementValues = options?.inlineValues ? null : []; | ||
return compileQueryInput(query, schemas, statementValues); | ||
}; | ||
export { | ||
compileQueryInput | ||
compileQuery | ||
}; |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Compiles RONIN queries to SQL statements.", |
@@ -42,3 +42,3 @@ # RONIN Compiler | ||
```typescript | ||
import { compileQueryInput } from '@ronin/compiler'; | ||
import { compileQuery } from '@ronin/compiler'; | ||
@@ -57,3 +57,3 @@ const query = { | ||
const { writeStatements, readStatement } = compileQueryInput(query, schemas); | ||
const { writeStatements, readStatement } = compileQuery(query, schemas); | ||
@@ -64,2 +64,17 @@ console.log(readStatement); | ||
#### Options | ||
To fine-tune the behavior of the compiler, you can pass the following options: | ||
```typescript | ||
compileQuery(query, schemas, { | ||
// Instead of returning an array of values for every statement (which allows for | ||
// preventing SQL injections), all values are inlined directly into the SQL strings. | ||
// This option should only be used if the generated SQL will be manually verified. | ||
inlineValues: true | ||
}); | ||
``` | ||
#### Transpilation | ||
In order to be compatible with a wide range of projects, the source code of the `compiler` repo needs to be compiled (transpiled) whenever you make changes to it. To automate this, you can keep this command running in your terminal: | ||
@@ -66,0 +81,0 @@ |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1279134
20268
98
0
157
157