@ronin/compiler
Advanced tools
Comparing version 0.8.8 to 0.9.0
@@ -148,3 +148,4 @@ // src/utils/helpers.ts | ||
const symbol = getSymbol(value); | ||
let conditionValue = value; | ||
const syntax = WITH_CONDITIONS[options.condition || "being"](value); | ||
let conditionValue = syntax[1]; | ||
if (symbol) { | ||
@@ -163,7 +164,7 @@ if (symbol?.type === "expression") { | ||
} else if (collectStatementValue) { | ||
conditionValue = prepareStatementValue(statementParams, value); | ||
conditionValue = prepareStatementValue(statementParams, conditionValue); | ||
} | ||
if (options.type === "fields") return conditionSelector; | ||
if (options.type === "values") return conditionValue; | ||
return `${conditionSelector} ${WITH_CONDITIONS[options.condition || "being"](conditionValue, value)}`; | ||
return `${conditionSelector} ${syntax[0]} ${conditionValue}`; | ||
}; | ||
@@ -298,14 +299,14 @@ var composeConditions = (models, model, statementParams, instructionName, value, options) => { | ||
var WITH_CONDITIONS = { | ||
being: (value, baseValue) => `${getMatcher(baseValue, false)} ${value}`, | ||
notBeing: (value, baseValue) => `${getMatcher(baseValue, true)} ${value}`, | ||
startingWith: (value) => `LIKE ${value}%`, | ||
notStartingWith: (value) => `NOT LIKE ${value}%`, | ||
endingWith: (value) => `LIKE %${value}`, | ||
notEndingWith: (value) => `NOT LIKE %${value}`, | ||
containing: (value) => `LIKE %${value}%`, | ||
notContaining: (value) => `NOT LIKE %${value}%`, | ||
greaterThan: (value) => `> ${value}`, | ||
greaterOrEqual: (value) => `>= ${value}`, | ||
lessThan: (value) => `< ${value}`, | ||
lessOrEqual: (value) => `<= ${value}` | ||
being: (value) => [getMatcher(value, false), value], | ||
notBeing: (value) => [getMatcher(value, true), value], | ||
startingWith: (value) => ["LIKE", `${value}%`], | ||
notStartingWith: (value) => ["NOT LIKE", `${value}%`], | ||
endingWith: (value) => ["LIKE", `%${value}`], | ||
notEndingWith: (value) => ["NOT LIKE", `%${value}`], | ||
containing: (value) => ["LIKE", `%${value}%`], | ||
notContaining: (value) => ["NOT LIKE", `%${value}%`], | ||
greaterThan: (value) => [">", value], | ||
greaterOrEqual: (value) => [">=", value], | ||
lessThan: (value) => ["<", value], | ||
lessOrEqual: (value) => ["<=", value] | ||
}; | ||
@@ -1356,31 +1357,71 @@ var handleWith = (models, model, statementParams, instruction, parentModel) => { | ||
// src/index.ts | ||
var compileQueries = (queries, models, options) => { | ||
const modelList = addSystemModels(models).map((model) => { | ||
return addDefaultModelFields(model, true); | ||
}); | ||
const modelListWithPresets = modelList.map((model) => { | ||
return addDefaultModelPresets(modelList, model); | ||
}); | ||
const dependencyStatements = []; | ||
const mainStatements = []; | ||
for (const query of queries) { | ||
const statementValues = options?.inlineParams ? null : []; | ||
const transformedQuery = transformMetaQuery( | ||
modelListWithPresets, | ||
dependencyStatements, | ||
statementValues, | ||
query | ||
); | ||
const result = compileQueryInput( | ||
transformedQuery, | ||
modelListWithPresets, | ||
statementValues | ||
); | ||
dependencyStatements.push(...result.dependencies); | ||
mainStatements.push(result.main); | ||
var Transaction = class { | ||
statements; | ||
models = []; | ||
queries; | ||
constructor(queries, options) { | ||
const models = options?.models || []; | ||
this.statements = this.compileQueries(queries, models, options); | ||
this.queries = queries; | ||
} | ||
return [...dependencyStatements, ...mainStatements]; | ||
/** | ||
* Composes SQL statements for the provided RONIN queries. | ||
* | ||
* @param queries - The RONIN queries for which SQL statements should be composed. | ||
* @param models - A list of models. | ||
* @param options - Additional options to adjust the behavior of the statement generation. | ||
* | ||
* @returns The composed SQL statements. | ||
*/ | ||
compileQueries = (queries, models, options) => { | ||
const modelList = addSystemModels(models).map((model) => { | ||
return addDefaultModelFields(model, true); | ||
}); | ||
const modelListWithPresets = modelList.map((model) => { | ||
return addDefaultModelPresets(modelList, model); | ||
}); | ||
const dependencyStatements = []; | ||
const mainStatements = []; | ||
for (const query of queries) { | ||
const statementValues = options?.inlineParams ? null : []; | ||
const transformedQuery = transformMetaQuery( | ||
modelListWithPresets, | ||
dependencyStatements, | ||
statementValues, | ||
query | ||
); | ||
const result = compileQueryInput( | ||
transformedQuery, | ||
modelListWithPresets, | ||
statementValues | ||
); | ||
dependencyStatements.push(...result.dependencies); | ||
mainStatements.push(result.main); | ||
} | ||
this.models = modelListWithPresets; | ||
return [...dependencyStatements, ...mainStatements]; | ||
}; | ||
formatRecord(model, record) { | ||
const formattedRecord = { ...record }; | ||
for (const key in record) { | ||
const { field } = getFieldFromModel(model, key, "to"); | ||
if (field.type === "json") { | ||
formattedRecord[key] = JSON.parse(record[key]); | ||
continue; | ||
} | ||
formattedRecord[key] = record[key]; | ||
} | ||
return expand(formattedRecord); | ||
} | ||
prepareResults(results) { | ||
return results.map((result, index) => { | ||
const query = this.queries.at(-index); | ||
const { queryModel } = splitQuery(query); | ||
const model = getModelBySlug(this.models, queryModel); | ||
return { record: this.formatRecord(model, result[0]) }; | ||
}); | ||
} | ||
}; | ||
export { | ||
compileQueries | ||
Transaction | ||
}; |
{ | ||
"name": "@ronin/compiler", | ||
"version": "0.8.8", | ||
"version": "0.9.0", | ||
"type": "module", | ||
@@ -36,2 +36,3 @@ "description": "Compiles RONIN queries to SQL statements.", | ||
"@biomejs/biome": "1.9.2", | ||
"@ronin/engine": "0.0.2", | ||
"@types/bun": "1.1.10", | ||
@@ -38,0 +39,0 @@ "@types/title": "3.4.3", |
@@ -42,21 +42,14 @@ # RONIN Compiler | ||
```typescript | ||
import { | ||
compileQueries, | ||
import { Transaction } from '@ronin/compiler'; | ||
type Query, | ||
type Model, | ||
type Statement | ||
} from '@ronin/compiler'; | ||
const queries: Array<Query> = [{ | ||
get: { | ||
accounts: null | ||
const transaction = new Transaction([ | ||
{ | ||
create: { model: { slug: 'account' } } | ||
}, | ||
{ | ||
get: { accounts: null } | ||
} | ||
}]; | ||
]); | ||
const models: Array<Model> = [{ | ||
slug: 'account' | ||
}]; | ||
const statements: Array<Statement> = compileQueries(queries, models); | ||
transaction.statements; | ||
// [{ | ||
@@ -69,2 +62,30 @@ // statement: 'SELECT * FROM "accounts"', | ||
Once the RONIN queries have been compiled down to SQL statements, the statements can be | ||
executed and their results can be formatted by the compiler as well: | ||
```typescript | ||
// `rows` are provided by the database engine | ||
const results: Array<Result> = transaction.prepareResults(rows); | ||
``` | ||
#### Types | ||
In total, the following types are being exported: | ||
```typescript | ||
import type { | ||
Query, | ||
Model, | ||
ModelField, | ||
ModelIndex, | ||
ModelTrigger, | ||
ModelPreset, | ||
Statement, | ||
Result | ||
} from '@ronin/compiler'; | ||
``` | ||
#### Options | ||
@@ -75,3 +96,8 @@ | ||
```typescript | ||
compileQueries(queries, models, { | ||
new Transaction(queries, { | ||
// A list of models that already existing inside the database. | ||
models: [ | ||
{ slug: 'account' } | ||
], | ||
// Instead of returning an array of parameters for every statement (which allows for | ||
@@ -94,2 +120,12 @@ // preventing SQL injections), all parameters are inlined directly into the SQL strings. | ||
### Mental Model | ||
The interface of creating new `Transaction` instances (thereby creating new transactions) was chosen in order to define the smallest workload unit that the compiler can operate on. | ||
Just like for the database, a transaction for the compiler defines an [atomic operation](https://www.sqlite.org/lang_transaction.html) in which a list of queries can be executed serially, and where each query can rely on the changes made by a previous one. In order to facilitate this, a programmatic interface that clarifies the accumulation of in-memory state is required (class instances). | ||
For example, if one query creates a new model, every query after it within the same transaction must be able to interact with the records of that model, or update the model itself, without roundtrips to the database, thereby requiring the accumulation of state while the transaction is being compiled. | ||
Furthermore, since every database transaction causes a [lock](https://www.sqlite.org/lockingv3.html), the database is inherently not locked between the execution of multiple transactions, which could cause the compilation inputs (e.g. models) of a `Transaction` to no longer be up-to-date. If the inputs have changed, a `new Transaction` should therefore be created. | ||
### Running Tests | ||
@@ -96,0 +132,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
445832
7428
140
7