uql-codemod 0.56.1 → 0.56.2
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 +2 -0
- package/dist/policyChecks.d.ts +9 -0
- package/dist/policyChecks.js +61 -0
- package/dist/transform.js +13 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,6 +43,7 @@ Only `--flag=value` is read, and anything unrecognised is an error. It exits `0`
|
|
|
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';
|
|
@@ -753,14 +754,22 @@ export function transformFile(source, checker, options = { strictNullChecks: tru
|
|
|
753
754
|
const legacy = rewriteLegacy(source, checker, options, taken);
|
|
754
755
|
const located = (pass) => (at) => locationAt(source, pass.sourceOffset(at));
|
|
755
756
|
const builders = toBuilders(source.fileName, legacy.text, { locate: located(legacy), taken });
|
|
756
|
-
const
|
|
757
|
-
const
|
|
758
|
-
const
|
|
757
|
+
const locate = located(followed(legacy, builders));
|
|
758
|
+
const migrated = toMigrationBuilders(source.fileName, builders.text, locate);
|
|
759
|
+
const policies = toPolicies(source.fileName, migrated.text, locate);
|
|
760
|
+
const sided = toSidedPolicies(source.fileName, policies.text, locate);
|
|
761
|
+
const text = toRelationTargets(source.fileName, toGeneratedKeys(source.fileName, sided.text));
|
|
759
762
|
return {
|
|
760
763
|
fileName: source.fileName,
|
|
761
764
|
text,
|
|
762
765
|
changed: text !== source.getFullText(),
|
|
763
|
-
unresolved: [
|
|
766
|
+
unresolved: [
|
|
767
|
+
...legacy.unresolved,
|
|
768
|
+
...builders.unresolved,
|
|
769
|
+
...migrated.unresolved,
|
|
770
|
+
...policies.unresolved,
|
|
771
|
+
...sided.unresolved,
|
|
772
|
+
],
|
|
764
773
|
notes: legacy.notes,
|
|
765
774
|
};
|
|
766
775
|
}
|