uql-codemod 0.56.1 → 0.57.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/policyChecks.d.ts +9 -0
- package/dist/policyChecks.js +61 -0
- package/dist/transform.js +19 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,12 +37,13 @@ Only `--flag=value` is read, and anything unrecognised is an error. It exits `0`
|
|
|
37
37
|
- `col('cost')` to the ref it names, ``(product) => sql`${product.cost}` `` in a `computed` and `refs(Item).cost` in a statement, and `$lock: { wait: 'skip' }` to `{ $wait: 'skip' }`, or to `true` for `{}` and a `'block'` wait.
|
|
38
38
|
- `$exclude` to a `$select` leaving its fields out: `$exclude: { password: true }` to `$select: { password: false }`, at every depth of `$populate`.
|
|
39
39
|
- An aggregate's `$agg` to `$select`, and each field it or `$text`'s `$fields` names to a key: `{ $sum: { amount: true } }`.
|
|
40
|
-
- An upsert's conflict keys to a query, `upsertOne(User, { email: true }, row)` to `upsertOne(User, { $conflict: { email: true } }, row)`, and its update, `{ update: X }` options or a fourth argument, to the query's `$update: X`, in `upsertOne`, `upsertMany` and a trigger's `upsertInto`; `const { id } = await upsertOne(...)` to `const id`.
|
|
40
|
+
- An upsert's conflict keys to a query, `upsertOne(User, { email: true }, row)` to `upsertOne(User, { $conflict: { email: true } }, row)`, and its update, `{ update: X }` options or a fourth argument, to the query's `$update: X`, an empty one to `$update: false`, which keeps a found row as it did, in `upsertOne`, `upsertMany` and a trigger's `upsertInto`; `const { id } = await upsertOne(...)` to `const id`.
|
|
41
41
|
- Renamed exports, at the import and every use: `raw` to `sql`, `QueryWhereMap` to `QueryWhere`, `IndexColumnSchema` to `IndexEntry`, `IndexColumnModifiers` to `IndexEntryModifiers`, `RelationKeyMap` to `KeyMap`, `FilterCondition` to `FilterWhere`, `SqlMigrationModuleOptions` to `MigrationModuleOptions`, `buildSqlQuerierMigrationModule` to `buildMigrationModule`, `QueryDialect` to `SqlQueryDialect`, `EngineFeatures` to `DialectFeatures`, `KnownMigratorDialect` to `DialectName`, `QueryConflictPaths` to `QueryConflictKeys`, `D1Preparer`/`D1Database` to `D1Queryable`, and the removed driver classes to the one each extended: `PgDialect`/`NeonDialect`/`PgliteDialect` to `PostgresDialect`, `CrdbQuerier`/`NeonQuerier` to `PgQuerier`, `LibsqlQuerier`/`TursoQuerier` to `HranaQuerier`, `TursoLocalQuerier` to `SqliteQuerier` and `TursoDatabase` to `SqliteDatabase`, `MySql2Dialect` to `MySqlDialect`, `MongodbNativeDialect` to `MongoDialect`, `Sqlite3QuerierPool` to `SqliteQuerierPool`, the migration builder's `I`-prefixed types to the unprefixed name (`IColumnBuilder`/`IColumnFactory`, and `uql-orm/migrate`'s `ColumnBuilder`/`ColumnFactory`, to `TableColumnBuilder`/`TableColumnFactory`), moving the import to the entry that exports it; and `MongoQuerier`, `isMongoQuerier` and `MongoSchemaIntrospector` to `uql-orm/mongodb`.
|
|
42
42
|
- Imports off `uql-orm/maria`, `uql-orm/mongo` and `uql-orm/bunSql` to `uql-orm/mariadb`, `uql-orm/mongodb` and `uql-orm/bun-sql`, and off `uql-orm/type`, `uql-orm/dialect`, `uql-orm/entity`, `uql-orm/querier` and `uql-orm/namingStrategy` to `uql-orm`.
|
|
43
43
|
- A sole numeric key to the generated one it was: `f.number().id()` to `f.number().autoIncrement()`, `f.bigint().id()` to `f.bigint().autoIncrement()`, and `.autoIncrement()` on `f.column('int').id()`. `.autoIncrement(false)` is dropped, a key no longer needing it, and a key a relation shares stays as it is. A class's `id!` stays too, which an insert then has to name until it is `id?` or listed in `[filledKey]`. On MongoDB, take `.autoIncrement()` off a numeric key: nothing generates one there. A subclass's `.autoIncrement()` on a column that is no key, its key inherited, stays and has to go by hand.
|
|
44
44
|
- The `[idKey]?: '...'` brand, and its import, on a key not called `id`, `_id` or `uuid` and on every composite.
|
|
45
45
|
- A `security: true` filter to the policy replacing it, where it reads the context as data: `tenant: { where: (ctx) => (ctx?.tenantId != null ? { companyId: ctx.tenantId } : undefined), security: true }` in `@Entity`'s or `defineEntity`'s `filters` to `policies: [{ must: { companyId: $context.tenantId } }]`, keeping its `ops`, a `.filters({ ... })` section to `.policies([...])`, and one a `@Filter` declares once it is folded into `@Entity`.
|
|
46
|
+
- A policy's `check` to the `$new` side of its predicate: `{ allow: W, check: C }` to `{ allow: { $old: W, $new: C } }` (`{ $new: C }` where `W` is `{}`), wherever the policy literal is written, and `explainPolicies(...).where` and `.check` to `.$old` and `.$new`.
|
|
46
47
|
|
|
47
48
|
## What it leaves to you
|
|
48
49
|
|
|
@@ -69,6 +70,7 @@ Reported with what to do, never guessed:
|
|
|
69
70
|
- A `@Field`/`@Id` with no `type`, and a relation with no target: declare the column as a builder (`f.string()`) and pass the target first (`@ManyToOne(() => Target)`). A branded string id's column, `f.uuid()` or `f.string()`, is yours to pick.
|
|
70
71
|
- `Relation<T>` imported from `uql-orm`, which no longer exports it: write the property's type without it, and drop the import.
|
|
71
72
|
- A security filter it cannot read as data (a guard on another key, any other logic): declare it as a policy. A filter's `onMissing`, which no filter takes now: remove it.
|
|
73
|
+
- A destructuring of `explainPolicies`' answer, which names `$old` and `$new` now: rename what it reads.
|
|
72
74
|
|
|
73
75
|
## After it runs
|
|
74
76
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Each policy stating `check`, as the predicate it means: `{ allow: W, check: C }` reads the found row by `W` and the
|
|
3
|
+
* written one by `C`, so it is `{ allow: { $old: W, $new: C } }`, wherever the policy literal is written. Reads of
|
|
4
|
+
* `explainPolicies` go to their new sides; a destructuring of them is reported.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toSidedPolicies(fileName: string, text: string, locate: (at: number) => string): {
|
|
7
|
+
readonly text: string;
|
|
8
|
+
readonly unresolved: readonly string[];
|
|
9
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { formatFindings } from './context.js';
|
|
3
|
+
import { applyEdits, original, removeFromList, replaced } from './edits.js';
|
|
4
|
+
import { identifierText, propertyAssignment, propertyValue } from './syntax.js';
|
|
5
|
+
/** What `explainPolicies` answered each side as, and what it answers it as now. */
|
|
6
|
+
const EXPLAINED = { where: '$old', check: '$new' };
|
|
7
|
+
/**
|
|
8
|
+
* Each policy stating `check`, as the predicate it means: `{ allow: W, check: C }` reads the found row by `W` and the
|
|
9
|
+
* written one by `C`, so it is `{ allow: { $old: W, $new: C } }`, wherever the policy literal is written. Reads of
|
|
10
|
+
* `explainPolicies` go to their new sides; a destructuring of them is reported.
|
|
11
|
+
*/
|
|
12
|
+
export function toSidedPolicies(fileName, text, locate) {
|
|
13
|
+
if (!text.includes('uql-orm') || (!text.includes('check') && !text.includes('explainPolicies'))) {
|
|
14
|
+
return { text, unresolved: [] };
|
|
15
|
+
}
|
|
16
|
+
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true);
|
|
17
|
+
const edits = [];
|
|
18
|
+
const findings = [];
|
|
19
|
+
const visit = (node) => {
|
|
20
|
+
const sided = ts.isObjectLiteralExpression(node) ? sidedPolicy(node, source) : undefined;
|
|
21
|
+
if (sided) {
|
|
22
|
+
edits.push(...sided);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const renamed = ts.isPropertyAccessExpression(node) && explains(node.expression) && EXPLAINED[node.name.text];
|
|
26
|
+
if (renamed) {
|
|
27
|
+
edits.push(replaced(node.name, renamed));
|
|
28
|
+
}
|
|
29
|
+
if (ts.isVariableDeclaration(node) &&
|
|
30
|
+
ts.isObjectBindingPattern(node.name) &&
|
|
31
|
+
node.initializer &&
|
|
32
|
+
explains(node.initializer)) {
|
|
33
|
+
findings.push({
|
|
34
|
+
node,
|
|
35
|
+
message: "explainPolicies answers '$old' and '$new' now, where it answered 'where' and 'check': rename what you read",
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
ts.forEachChild(node, visit);
|
|
39
|
+
};
|
|
40
|
+
visit(source);
|
|
41
|
+
return { text: edits.length ? applyEdits(text, edits) : text, unresolved: formatFindings(findings, locate) };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The edits folding `policy`'s `check` into its predicate, the rest of it kept as written, or `undefined` for one
|
|
45
|
+
* stating none.
|
|
46
|
+
*/
|
|
47
|
+
function sidedPolicy(policy, source) {
|
|
48
|
+
const check = propertyAssignment(policy, 'check');
|
|
49
|
+
const found = propertyValue(policy, 'allow') ?? propertyValue(policy, 'must');
|
|
50
|
+
if (!check || !found) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const every = ts.isObjectLiteralExpression(found) && !found.properties.length;
|
|
54
|
+
const written = ['$new: ', original(check.initializer)];
|
|
55
|
+
const predicate = every ? ['{ ', ...written, ' }'] : ['{ $old: ', original(found), ', ', ...written, ' }'];
|
|
56
|
+
return [replaced(found, predicate), ...removeFromList(policy.properties, [check], source)];
|
|
57
|
+
}
|
|
58
|
+
/** Whether `node` calls `explainPolicies`. */
|
|
59
|
+
function explains(node) {
|
|
60
|
+
return ts.isCallExpression(node) && identifierText(node.expression) === 'explainPolicies';
|
|
61
|
+
}
|
package/dist/transform.js
CHANGED
|
@@ -14,6 +14,7 @@ import { clashingName, deadImportNames, foreignNames, LEGACY_SQL_TAG, reportRemo
|
|
|
14
14
|
import { rewriteBuilderIndexLengths, rewriteJsonIndexEntries } from './jsonIndex.js';
|
|
15
15
|
import { entityGetterTarget, fieldKeysEdits, isCallback, keyListEdits, mappedByEdits, memberAccess, paramFor, quoted, referencesEdits, } from './keyMaps.js';
|
|
16
16
|
import { toMigrationBuilders } from './migrationBuilders.js';
|
|
17
|
+
import { toSidedPolicies } from './policyChecks.js';
|
|
17
18
|
import { relationTargetFor } from './relationTarget.js';
|
|
18
19
|
import { toRelationTargets } from './relationTargets.js';
|
|
19
20
|
import { toPolicies } from './securityFilters.js';
|
|
@@ -468,9 +469,13 @@ function rewriteUpsertCall(call, ctx) {
|
|
|
468
469
|
text: [...upsertQuery(query, keys, update), ...(moved ? [{ start: query.getEnd(), end: payload.getEnd() }] : [])],
|
|
469
470
|
});
|
|
470
471
|
}
|
|
471
|
-
/**
|
|
472
|
+
/**
|
|
473
|
+
* The parts of an upsert's query naming `update` as its `$update`: the old conflict map wrapped, a literal extended, else
|
|
474
|
+
* spread. An empty update kept a found row, which `$update: false` says now: `{}` updates it, a touch.
|
|
475
|
+
*/
|
|
472
476
|
function upsertQuery(query, keys, update) {
|
|
473
|
-
const
|
|
477
|
+
const kept = update && objectLiteral(update)?.properties.length === 0;
|
|
478
|
+
const named = update ? [', $update: ', kept ? 'false' : original(update)] : [];
|
|
474
479
|
if (keys) {
|
|
475
480
|
return ['{ $conflict: ', original(query), ...named, ' }'];
|
|
476
481
|
}
|
|
@@ -753,14 +758,22 @@ export function transformFile(source, checker, options = { strictNullChecks: tru
|
|
|
753
758
|
const legacy = rewriteLegacy(source, checker, options, taken);
|
|
754
759
|
const located = (pass) => (at) => locationAt(source, pass.sourceOffset(at));
|
|
755
760
|
const builders = toBuilders(source.fileName, legacy.text, { locate: located(legacy), taken });
|
|
756
|
-
const
|
|
757
|
-
const
|
|
758
|
-
const
|
|
761
|
+
const locate = located(followed(legacy, builders));
|
|
762
|
+
const migrated = toMigrationBuilders(source.fileName, builders.text, locate);
|
|
763
|
+
const policies = toPolicies(source.fileName, migrated.text, locate);
|
|
764
|
+
const sided = toSidedPolicies(source.fileName, policies.text, locate);
|
|
765
|
+
const text = toRelationTargets(source.fileName, toGeneratedKeys(source.fileName, sided.text));
|
|
759
766
|
return {
|
|
760
767
|
fileName: source.fileName,
|
|
761
768
|
text,
|
|
762
769
|
changed: text !== source.getFullText(),
|
|
763
|
-
unresolved: [
|
|
770
|
+
unresolved: [
|
|
771
|
+
...legacy.unresolved,
|
|
772
|
+
...builders.unresolved,
|
|
773
|
+
...migrated.unresolved,
|
|
774
|
+
...policies.unresolved,
|
|
775
|
+
...sided.unresolved,
|
|
776
|
+
],
|
|
764
777
|
notes: legacy.notes,
|
|
765
778
|
};
|
|
766
779
|
}
|