@ronin/compiler 0.8.8 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +54 -18
- package/dist/index.d.ts +42 -13
- package/dist/index.js +82 -41
- package/package.json +2 -1
package/README.md
CHANGED
@@ -39,25 +39,18 @@ You will just need to make sure that, once you [create a pull request](https://d
|
|
39
39
|
The programmatic API of the RONIN compiler looks like this:
|
40
40
|
|
41
41
|
```typescript
|
42
|
-
import {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
const queries: Array<Query> = [{
|
51
|
-
get: {
|
52
|
-
accounts: null
|
42
|
+
import { Transaction } from '@ronin/compiler';
|
43
|
+
|
44
|
+
const transaction = new Transaction([
|
45
|
+
{
|
46
|
+
create: { model: { slug: 'account' } }
|
47
|
+
},
|
48
|
+
{
|
49
|
+
get: { accounts: null }
|
53
50
|
}
|
54
|
-
|
51
|
+
]);
|
55
52
|
|
56
|
-
|
57
|
-
slug: 'account'
|
58
|
-
}];
|
59
|
-
|
60
|
-
const statements: Array<Statement> = compileQueries(queries, models);
|
53
|
+
transaction.statements;
|
61
54
|
// [{
|
62
55
|
// statement: 'SELECT * FROM "accounts"',
|
63
56
|
// params: [],
|
@@ -65,12 +58,45 @@ const statements: Array<Statement> = compileQueries(queries, models);
|
|
65
58
|
// }]
|
66
59
|
```
|
67
60
|
|
61
|
+
Once the RONIN queries have been compiled down to SQL statements, the statements can be
|
62
|
+
executed and their results can be formatted by the compiler as well:
|
63
|
+
|
64
|
+
```typescript
|
65
|
+
// `rows` are provided by the database engine
|
66
|
+
|
67
|
+
const results: Array<Result> = transaction.prepareResults(rows);
|
68
|
+
```
|
69
|
+
|
70
|
+
#### Types
|
71
|
+
|
72
|
+
In total, the following types are being exported:
|
73
|
+
|
74
|
+
```typescript
|
75
|
+
import type {
|
76
|
+
Query,
|
77
|
+
|
78
|
+
Model,
|
79
|
+
ModelField,
|
80
|
+
ModelIndex,
|
81
|
+
ModelTrigger,
|
82
|
+
ModelPreset,
|
83
|
+
|
84
|
+
Statement,
|
85
|
+
Result
|
86
|
+
} from '@ronin/compiler';
|
87
|
+
```
|
88
|
+
|
68
89
|
#### Options
|
69
90
|
|
70
91
|
To fine-tune the behavior of the compiler, you can pass the following options:
|
71
92
|
|
72
93
|
```typescript
|
73
|
-
|
94
|
+
new Transaction(queries, {
|
95
|
+
// A list of models that already existing inside the database.
|
96
|
+
models: [
|
97
|
+
{ slug: 'account' }
|
98
|
+
],
|
99
|
+
|
74
100
|
// Instead of returning an array of parameters for every statement (which allows for
|
75
101
|
// preventing SQL injections), all parameters are inlined directly into the SQL strings.
|
76
102
|
// This option should only be used if the generated SQL will be manually verified.
|
@@ -88,6 +114,16 @@ bun run dev
|
|
88
114
|
|
89
115
|
Whenever you make a change to the source code, it will then automatically be transpiled again.
|
90
116
|
|
117
|
+
### Mental Model
|
118
|
+
|
119
|
+
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.
|
120
|
+
|
121
|
+
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).
|
122
|
+
|
123
|
+
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.
|
124
|
+
|
125
|
+
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.
|
126
|
+
|
91
127
|
### Running Tests
|
92
128
|
|
93
129
|
The RONIN compiler has 100% test coverage, which means that every single line of code is tested automatically, to ensure that any change to the source code doesn't cause a regression.
|
package/dist/index.d.ts
CHANGED
@@ -5972,17 +5972,46 @@ type PublicModel<T extends Array<ModelField> = Array<ModelField>> = Omit<Partial
|
|
5972
5972
|
identifiers?: Partial<Model['identifiers']>;
|
5973
5973
|
};
|
5974
5974
|
|
5975
|
-
|
5976
|
-
|
5977
|
-
|
5978
|
-
|
5979
|
-
|
5980
|
-
|
5981
|
-
|
5982
|
-
|
5983
|
-
|
5984
|
-
|
5985
|
-
|
5986
|
-
|
5975
|
+
type Row = Record<string, unknown>;
|
5976
|
+
type NativeRecord = Record<string, unknown> & {
|
5977
|
+
id: string;
|
5978
|
+
ronin: {
|
5979
|
+
createdAt: Date;
|
5980
|
+
updatedAt: Date;
|
5981
|
+
};
|
5982
|
+
};
|
5983
|
+
type SingleRecordResult = {
|
5984
|
+
record: NativeRecord | null;
|
5985
|
+
};
|
5986
|
+
type MultipleRecordResult = {
|
5987
|
+
records: Array<NativeRecord>;
|
5988
|
+
moreAfter?: string;
|
5989
|
+
moreBefore?: string;
|
5990
|
+
};
|
5991
|
+
type AmountResult = {
|
5992
|
+
amount: number;
|
5993
|
+
};
|
5994
|
+
type Result = SingleRecordResult | MultipleRecordResult | AmountResult;
|
5995
|
+
|
5996
|
+
declare class Transaction {
|
5997
|
+
statements: Array<Statement>;
|
5998
|
+
models: Array<Model>;
|
5999
|
+
private queries;
|
6000
|
+
constructor(queries: Array<Query>, options?: Parameters<typeof this.compileQueries>[2] & {
|
6001
|
+
models?: Array<PublicModel>;
|
6002
|
+
});
|
6003
|
+
/**
|
6004
|
+
* Composes SQL statements for the provided RONIN queries.
|
6005
|
+
*
|
6006
|
+
* @param queries - The RONIN queries for which SQL statements should be composed.
|
6007
|
+
* @param models - A list of models.
|
6008
|
+
* @param options - Additional options to adjust the behavior of the statement generation.
|
6009
|
+
*
|
6010
|
+
* @returns The composed SQL statements.
|
6011
|
+
*/
|
6012
|
+
private compileQueries;
|
6013
|
+
private formatRecord;
|
6014
|
+
prepareResults(results: Array<Array<Row>>): Array<Result>;
|
6015
|
+
}
|
5987
6016
|
|
5988
|
-
export { type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, type Query, type Statement,
|
6017
|
+
export { type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, type Query, type Result, type Statement, Transaction };
|
package/dist/index.js
CHANGED
@@ -145,7 +145,8 @@ var composeFieldValues = (models, model, statementParams, instructionName, value
|
|
145
145
|
);
|
146
146
|
const collectStatementValue = options.type !== "fields";
|
147
147
|
const symbol = getSymbol(value);
|
148
|
-
|
148
|
+
const syntax = WITH_CONDITIONS[options.condition || "being"](value);
|
149
|
+
let conditionValue = syntax[1];
|
149
150
|
if (symbol) {
|
150
151
|
if (symbol?.type === "expression") {
|
151
152
|
conditionValue = parseFieldExpression(
|
@@ -159,11 +160,11 @@ var composeFieldValues = (models, model, statementParams, instructionName, value
|
|
159
160
|
conditionValue = `(${compileQueryInput(symbol.value, models, statementParams).main.statement})`;
|
160
161
|
}
|
161
162
|
} else if (collectStatementValue) {
|
162
|
-
conditionValue = prepareStatementValue(statementParams,
|
163
|
+
conditionValue = prepareStatementValue(statementParams, conditionValue);
|
163
164
|
}
|
164
165
|
if (options.type === "fields") return conditionSelector;
|
165
166
|
if (options.type === "values") return conditionValue;
|
166
|
-
return `${conditionSelector} ${
|
167
|
+
return `${conditionSelector} ${syntax[0]} ${conditionValue}`;
|
167
168
|
};
|
168
169
|
var composeConditions = (models, model, statementParams, instructionName, value, options) => {
|
169
170
|
const isNested = isObject(value) && Object.keys(value).length > 0;
|
@@ -293,18 +294,18 @@ var getMatcher = (value, negative) => {
|
|
293
294
|
return "=";
|
294
295
|
};
|
295
296
|
var WITH_CONDITIONS = {
|
296
|
-
being: (value
|
297
|
-
notBeing: (value
|
298
|
-
startingWith: (value) =>
|
299
|
-
notStartingWith: (value) =>
|
300
|
-
endingWith: (value) =>
|
301
|
-
notEndingWith: (value) =>
|
302
|
-
containing: (value) =>
|
303
|
-
notContaining: (value) =>
|
304
|
-
greaterThan: (value) =>
|
305
|
-
greaterOrEqual: (value) =>
|
306
|
-
lessThan: (value) =>
|
307
|
-
lessOrEqual: (value) =>
|
297
|
+
being: (value) => [getMatcher(value, false), value],
|
298
|
+
notBeing: (value) => [getMatcher(value, true), value],
|
299
|
+
startingWith: (value) => ["LIKE", `${value}%`],
|
300
|
+
notStartingWith: (value) => ["NOT LIKE", `${value}%`],
|
301
|
+
endingWith: (value) => ["LIKE", `%${value}`],
|
302
|
+
notEndingWith: (value) => ["NOT LIKE", `%${value}`],
|
303
|
+
containing: (value) => ["LIKE", `%${value}%`],
|
304
|
+
notContaining: (value) => ["NOT LIKE", `%${value}%`],
|
305
|
+
greaterThan: (value) => [">", value],
|
306
|
+
greaterOrEqual: (value) => [">=", value],
|
307
|
+
lessThan: (value) => ["<", value],
|
308
|
+
lessOrEqual: (value) => ["<=", value]
|
308
309
|
};
|
309
310
|
var handleWith = (models, model, statementParams, instruction, parentModel) => {
|
310
311
|
const subStatement = composeConditions(
|
@@ -1350,33 +1351,73 @@ var compileQueryInput = (query, models, statementParams, options) => {
|
|
1350
1351
|
};
|
1351
1352
|
|
1352
1353
|
// src/index.ts
|
1353
|
-
var
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
);
|
1375
|
-
|
1376
|
-
|
1354
|
+
var Transaction = class {
|
1355
|
+
statements;
|
1356
|
+
models = [];
|
1357
|
+
queries;
|
1358
|
+
constructor(queries, options) {
|
1359
|
+
const models = options?.models || [];
|
1360
|
+
this.statements = this.compileQueries(queries, models, options);
|
1361
|
+
this.queries = queries;
|
1362
|
+
}
|
1363
|
+
/**
|
1364
|
+
* Composes SQL statements for the provided RONIN queries.
|
1365
|
+
*
|
1366
|
+
* @param queries - The RONIN queries for which SQL statements should be composed.
|
1367
|
+
* @param models - A list of models.
|
1368
|
+
* @param options - Additional options to adjust the behavior of the statement generation.
|
1369
|
+
*
|
1370
|
+
* @returns The composed SQL statements.
|
1371
|
+
*/
|
1372
|
+
compileQueries = (queries, models, options) => {
|
1373
|
+
const modelList = addSystemModels(models).map((model) => {
|
1374
|
+
return addDefaultModelFields(model, true);
|
1375
|
+
});
|
1376
|
+
const modelListWithPresets = modelList.map((model) => {
|
1377
|
+
return addDefaultModelPresets(modelList, model);
|
1378
|
+
});
|
1379
|
+
const dependencyStatements = [];
|
1380
|
+
const mainStatements = [];
|
1381
|
+
for (const query of queries) {
|
1382
|
+
const statementValues = options?.inlineParams ? null : [];
|
1383
|
+
const transformedQuery = transformMetaQuery(
|
1384
|
+
modelListWithPresets,
|
1385
|
+
dependencyStatements,
|
1386
|
+
statementValues,
|
1387
|
+
query
|
1388
|
+
);
|
1389
|
+
const result = compileQueryInput(
|
1390
|
+
transformedQuery,
|
1391
|
+
modelListWithPresets,
|
1392
|
+
statementValues
|
1393
|
+
);
|
1394
|
+
dependencyStatements.push(...result.dependencies);
|
1395
|
+
mainStatements.push(result.main);
|
1396
|
+
}
|
1397
|
+
this.models = modelListWithPresets;
|
1398
|
+
return [...dependencyStatements, ...mainStatements];
|
1399
|
+
};
|
1400
|
+
formatRecord(model, record) {
|
1401
|
+
const formattedRecord = { ...record };
|
1402
|
+
for (const key in record) {
|
1403
|
+
const { field } = getFieldFromModel(model, key, "to");
|
1404
|
+
if (field.type === "json") {
|
1405
|
+
formattedRecord[key] = JSON.parse(record[key]);
|
1406
|
+
continue;
|
1407
|
+
}
|
1408
|
+
formattedRecord[key] = record[key];
|
1409
|
+
}
|
1410
|
+
return expand(formattedRecord);
|
1411
|
+
}
|
1412
|
+
prepareResults(results) {
|
1413
|
+
return results.map((result, index) => {
|
1414
|
+
const query = this.queries.at(-index);
|
1415
|
+
const { queryModel } = splitQuery(query);
|
1416
|
+
const model = getModelBySlug(this.models, queryModel);
|
1417
|
+
return { record: this.formatRecord(model, result[0]) };
|
1418
|
+
});
|
1377
1419
|
}
|
1378
|
-
return [...dependencyStatements, ...mainStatements];
|
1379
1420
|
};
|
1380
1421
|
export {
|
1381
|
-
|
1422
|
+
Transaction
|
1382
1423
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ronin/compiler",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.9.0",
|
4
4
|
"type": "module",
|
5
5
|
"description": "Compiles RONIN queries to SQL statements.",
|
6
6
|
"publishConfig": {
|
@@ -33,6 +33,7 @@
|
|
33
33
|
},
|
34
34
|
"devDependencies": {
|
35
35
|
"@biomejs/biome": "1.9.2",
|
36
|
+
"@ronin/engine": "0.0.2",
|
36
37
|
"@types/bun": "1.1.10",
|
37
38
|
"@types/title": "3.4.3",
|
38
39
|
"tsup": "8.3.0",
|