uql-codemod 0.56.2 → 0.58.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 +2 -1
- package/dist/generatedKeys.js +6 -9
- package/dist/presentFields.d.ts +2 -0
- package/dist/presentFields.js +34 -0
- package/dist/syntax.d.ts +4 -0
- package/dist/syntax.js +10 -0
- package/dist/transform.js +9 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,10 +37,11 @@ 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
|
+
- `?` to `!` on a `@Field` whose column nothing fills, which `@Field` refuses as optional: `@Field(f.string()) title?: string` to `title!: string`. A nullable, defaulted, generated, `eager(false)` or version column keeps its `?`, as does a text key MongoDB mints.
|
|
44
45
|
- The `[idKey]?: '...'` brand, and its import, on a key not called `id`, `_id` or `uuid` and on every composite.
|
|
45
46
|
- 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
47
|
- 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`.
|
package/dist/generatedKeys.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { applyEdits } from './edits.js';
|
|
3
|
-
import { chainOf } from './syntax.js';
|
|
3
|
+
import { chainOf, fieldColumn, isNumericColumn } from './syntax.js';
|
|
4
4
|
/** The columns a class's `@Field`s declare, by property. */
|
|
5
5
|
function classColumns(node) {
|
|
6
|
-
return node.members
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
: []));
|
|
6
|
+
return node.members.filter(ts.isPropertyDeclaration).flatMap((member) => {
|
|
7
|
+
const column = fieldColumn(member);
|
|
8
|
+
return column ? [{ name: member.name.getText(), column }] : [];
|
|
9
|
+
});
|
|
11
10
|
}
|
|
12
11
|
/** The columns a `fields` object declares, by property. */
|
|
13
12
|
function fieldColumns(node) {
|
|
@@ -38,9 +37,7 @@ function stateKey(keys, scope) {
|
|
|
38
37
|
const [{ name: key, column }] = ids;
|
|
39
38
|
const { root, methods } = chainOf(column);
|
|
40
39
|
const names = methods.map(({ name }) => name);
|
|
41
|
-
const
|
|
42
|
-
const numeric = ['number', 'bigint'].includes(type.name) ||
|
|
43
|
-
(type.name === 'column' && /\b(?:tiny|small|medium|big)?(?:int\d?|integer|serial)\b/i.test(type.call.getText()));
|
|
40
|
+
const numeric = isNumericColumn(methods);
|
|
44
41
|
const shared = new RegExp(`references:\\s*\\(?\\w+\\)?\\s*=>\\s*\\w+\\.${key}\\b`).test(scope.getText());
|
|
45
42
|
const filled = ['references', 'onInsert', 'default'].some((method) => names.includes(method));
|
|
46
43
|
const declared = methods.some(({ name, call }) => name === 'autoIncrement' && call.arguments.length);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { applyEdits } from './edits.js';
|
|
3
|
+
import { chainOf, fieldColumn, hasModifier, isNumericColumn } from './syntax.js';
|
|
4
|
+
/** Methods after which an insert may leave a column out: something fills it, `null` included. */
|
|
5
|
+
const FILLED = ['default', 'onInsert', 'autoIncrement', 'tenant', 'version', 'nullable'];
|
|
6
|
+
/** Whether an insert has to name a builder's column: nothing fills it, a read loads it, and it is no text key MongoDB mints. */
|
|
7
|
+
function insertNames(column) {
|
|
8
|
+
const { root, methods } = chainOf(column);
|
|
9
|
+
const has = (name) => methods.some((method) => method.name === name);
|
|
10
|
+
const lazy = methods.some(({ name, call }) => name === 'eager' && call.arguments[0]?.getText() === 'false');
|
|
11
|
+
const mintedKey = has('id') && !has('references') && !isNumericColumn(methods);
|
|
12
|
+
return root.getText() === 'f' && !FILLED.some(has) && !lazy && !mintedKey;
|
|
13
|
+
}
|
|
14
|
+
/** Turns `?` into `!` on each `@Field` an insert cannot leave out, which `@Field` refuses as optional. */
|
|
15
|
+
export function toPresentFields(fileName, text) {
|
|
16
|
+
if (!text.includes('@Field'))
|
|
17
|
+
return text;
|
|
18
|
+
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true);
|
|
19
|
+
const edits = [];
|
|
20
|
+
const visit = (node) => {
|
|
21
|
+
if (ts.isPropertyDeclaration(node) &&
|
|
22
|
+
node.questionToken &&
|
|
23
|
+
node.type &&
|
|
24
|
+
!hasModifier(node, ts.SyntaxKind.DeclareKeyword)) {
|
|
25
|
+
const column = fieldColumn(node);
|
|
26
|
+
if (column && insertNames(column)) {
|
|
27
|
+
edits.push({ start: node.questionToken.getStart(), end: node.questionToken.getEnd(), text: '!' });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
ts.forEachChild(node, visit);
|
|
31
|
+
};
|
|
32
|
+
visit(source);
|
|
33
|
+
return applyEdits(text, edits);
|
|
34
|
+
}
|
package/dist/syntax.d.ts
CHANGED
|
@@ -48,6 +48,8 @@ export declare function chainOf(node: ts.Expression): {
|
|
|
48
48
|
readonly root: ts.Expression;
|
|
49
49
|
readonly methods: readonly ChainMethod[];
|
|
50
50
|
};
|
|
51
|
+
/** Whether a column chain, `f.number()...`, holds a number: its type's method, or a `column()` naming an integer type. */
|
|
52
|
+
export declare function isNumericColumn([type]: readonly ChainMethod[]): boolean;
|
|
51
53
|
export declare function hasModifier(node: ts.Node, kind: ts.ModifierSyntaxKind): boolean;
|
|
52
54
|
/** `preferred`, or it numbered past every name `nodes` mention, so a parameter named so shadows nothing they read. */
|
|
53
55
|
export declare function freeName(preferred: string, nodes: readonly ts.Node[]): string;
|
|
@@ -72,6 +74,8 @@ export declare function indentUnit(source: ts.SourceFile): string;
|
|
|
72
74
|
export declare function decoratorsOf(node: ts.Node): readonly ts.Decorator[];
|
|
73
75
|
/** `Field` for `@Field` and `@Field(...)`. */
|
|
74
76
|
export declare function decoratorName(node: ts.Decorator): string | undefined;
|
|
77
|
+
/** The column `@Field(column)` declares on `member`. */
|
|
78
|
+
export declare function fieldColumn(member: ts.PropertyDeclaration): ts.Expression | undefined;
|
|
75
79
|
/** The decorators declaring a field: `@Field`, and `@Id` for the key the class names. */
|
|
76
80
|
export declare const FIELD_DECORATORS: ReadonlySet<string | undefined>;
|
|
77
81
|
export declare const RELATION_DECORATORS: ReadonlySet<string | undefined>;
|
package/dist/syntax.js
CHANGED
|
@@ -83,6 +83,11 @@ export function chainOf(node) {
|
|
|
83
83
|
}
|
|
84
84
|
return { root, methods };
|
|
85
85
|
}
|
|
86
|
+
/** Whether a column chain, `f.number()...`, holds a number: its type's method, or a `column()` naming an integer type. */
|
|
87
|
+
export function isNumericColumn([type]) {
|
|
88
|
+
return (['number', 'bigint'].includes(type.name) ||
|
|
89
|
+
(type.name === 'column' && /\b(?:tiny|small|medium|big)?(?:int\d?|integer|serial)\b/i.test(type.call.getText())));
|
|
90
|
+
}
|
|
86
91
|
export function hasModifier(node, kind) {
|
|
87
92
|
return ts.canHaveModifiers(node) && !!ts.getModifiers(node)?.some((modifier) => modifier.kind === kind);
|
|
88
93
|
}
|
|
@@ -181,6 +186,11 @@ export function decoratorName(node) {
|
|
|
181
186
|
const { expression } = node;
|
|
182
187
|
return identifierText(ts.isCallExpression(expression) ? expression.expression : expression);
|
|
183
188
|
}
|
|
189
|
+
/** The column `@Field(column)` declares on `member`. */
|
|
190
|
+
export function fieldColumn(member) {
|
|
191
|
+
const field = decoratorsOf(member).find((decorator) => decoratorName(decorator) === 'Field');
|
|
192
|
+
return field && ts.isCallExpression(field.expression) ? field.expression.arguments[0] : undefined;
|
|
193
|
+
}
|
|
184
194
|
/** The decorators declaring a field: `@Field`, and `@Id` for the key the class names. */
|
|
185
195
|
export const FIELD_DECORATORS = new Set(['Field', 'Id']);
|
|
186
196
|
export const RELATION_DECORATORS = new Set([
|
package/dist/transform.js
CHANGED
|
@@ -15,6 +15,7 @@ import { rewriteBuilderIndexLengths, rewriteJsonIndexEntries } from './jsonIndex
|
|
|
15
15
|
import { entityGetterTarget, fieldKeysEdits, isCallback, keyListEdits, mappedByEdits, memberAccess, paramFor, quoted, referencesEdits, } from './keyMaps.js';
|
|
16
16
|
import { toMigrationBuilders } from './migrationBuilders.js';
|
|
17
17
|
import { toSidedPolicies } from './policyChecks.js';
|
|
18
|
+
import { toPresentFields } from './presentFields.js';
|
|
18
19
|
import { relationTargetFor } from './relationTarget.js';
|
|
19
20
|
import { toRelationTargets } from './relationTargets.js';
|
|
20
21
|
import { toPolicies } from './securityFilters.js';
|
|
@@ -469,9 +470,13 @@ function rewriteUpsertCall(call, ctx) {
|
|
|
469
470
|
text: [...upsertQuery(query, keys, update), ...(moved ? [{ start: query.getEnd(), end: payload.getEnd() }] : [])],
|
|
470
471
|
});
|
|
471
472
|
}
|
|
472
|
-
/**
|
|
473
|
+
/**
|
|
474
|
+
* The parts of an upsert's query naming `update` as its `$update`: the old conflict map wrapped, a literal extended, else
|
|
475
|
+
* spread. An empty update kept a found row, which `$update: false` says now: `{}` updates it, a touch.
|
|
476
|
+
*/
|
|
473
477
|
function upsertQuery(query, keys, update) {
|
|
474
|
-
const
|
|
478
|
+
const kept = update && objectLiteral(update)?.properties.length === 0;
|
|
479
|
+
const named = update ? [', $update: ', kept ? 'false' : original(update)] : [];
|
|
475
480
|
if (keys) {
|
|
476
481
|
return ['{ $conflict: ', original(query), ...named, ' }'];
|
|
477
482
|
}
|
|
@@ -758,7 +763,8 @@ export function transformFile(source, checker, options = { strictNullChecks: tru
|
|
|
758
763
|
const migrated = toMigrationBuilders(source.fileName, builders.text, locate);
|
|
759
764
|
const policies = toPolicies(source.fileName, migrated.text, locate);
|
|
760
765
|
const sided = toSidedPolicies(source.fileName, policies.text, locate);
|
|
761
|
-
const
|
|
766
|
+
const keyed = toGeneratedKeys(source.fileName, sided.text);
|
|
767
|
+
const text = toRelationTargets(source.fileName, toPresentFields(source.fileName, keyed));
|
|
762
768
|
return {
|
|
763
769
|
fileName: source.fileName,
|
|
764
770
|
text,
|