firstly 0.10.3 → 0.10.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # firstly
2
2
 
3
+ ## 0.10.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#375](https://github.com/jycouet/firstly/pull/375) [`b0aea3e`](https://github.com/jycouet/firstly/commit/b0aea3eef9d31339a7446e855f0dbc0c8690177f) Thanks [@jycouet](https://github.com/jycouet)! - sqlAdmin drift: `relationIndexes` skips relations keyed on a computed field (`sqlExpression` / `serverExpression`), which have no column to index and produced invalid SQL.
8
+
3
9
  ## 0.10.3
4
10
 
5
11
  ### Patch Changes
@@ -25,6 +25,8 @@ export type RelIndexPlan = {
25
25
  * (e.g. an `a` index on an `(a, b)` PK table).
26
26
  */
27
27
  export declare function planRelationIndexes(desired: RelIndexDesired[], existing: Map<string, ExistingIndex[]>): RelIndexPlan[];
28
+ /** FK columns of each entity's `toOne` relations - the indexes we'd like to exist. */
29
+ export declare function relationIndexTargets(entities: ClassType<unknown>[]): Promise<RelIndexDesired[]>;
28
30
  /**
29
31
  * Find - and optionally create - indexes for `toOne` relation FK columns that
30
32
  * aren't already covered by an existing index. Default is a dry run: returns the
@@ -33,18 +33,8 @@ export function planRelationIndexes(desired, existing) {
33
33
  }
34
34
  return plans;
35
35
  }
36
- /**
37
- * Find - and optionally create - indexes for `toOne` relation FK columns that
38
- * aren't already covered by an existing index. Default is a dry run: returns the
39
- * plan, changes nothing. Pass `{ apply: true }` to create the missing ones.
40
- *
41
- * Postgres does NOT auto-index FK columns, and `toMany` relations are indexed
42
- * from their owning `toOne` side, so only `toOne` is considered. Creates use
43
- * `CREATE INDEX IF NOT EXISTS` (idempotent), so they join the BackendMethod's
44
- * transaction - which is also why they can't be `CONCURRENTLY`.
45
- */
46
- export async function createRelationIndexes(db, entities, opts) {
47
- // Desired FK indexes from each entity's toOne relations.
36
+ /** FK columns of each entity's `toOne` relations - the indexes we'd like to exist. */
37
+ export async function relationIndexTargets(entities) {
48
38
  const desired = [];
49
39
  for (const ent of entities) {
50
40
  const meta = repo(ent).metadata;
@@ -59,9 +49,30 @@ export async function createRelationIndexes(db, entities, opts) {
59
49
  const localKeys = Object.values(fi.getFields().fields); // FK columns on THIS entity
60
50
  if (localKeys.length === 0)
61
51
  continue;
52
+ // A computed key (sqlExpression / serverExpression) has no column to index.
53
+ const computed = localKeys.some((k) => {
54
+ const o = meta.fields.find(k)?.options;
55
+ return !!(o?.sqlExpression || o?.serverExpression);
56
+ });
57
+ if (computed)
58
+ continue;
62
59
  desired.push({ table, columns: localKeys.map((k) => stripIdent(names.$dbNameOf(k))) });
63
60
  }
64
61
  }
62
+ return desired;
63
+ }
64
+ /**
65
+ * Find - and optionally create - indexes for `toOne` relation FK columns that
66
+ * aren't already covered by an existing index. Default is a dry run: returns the
67
+ * plan, changes nothing. Pass `{ apply: true }` to create the missing ones.
68
+ *
69
+ * Postgres does NOT auto-index FK columns, and `toMany` relations are indexed
70
+ * from their owning `toOne` side, so only `toOne` is considered. Creates use
71
+ * `CREATE INDEX IF NOT EXISTS` (idempotent), so they join the BackendMethod's
72
+ * transaction - which is also why they can't be `CONCURRENTLY`.
73
+ */
74
+ export async function createRelationIndexes(db, entities, opts) {
75
+ const desired = await relationIndexTargets(entities);
65
76
  // Every existing index (incl. PK), columns in order, per table.
66
77
  const res = await db.createCommand().execute(`
67
78
  SELECT c.relname AS table_name, i.relname AS index_name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "firstly",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "type": "module",
5
5
  "description": "Firstly, an opinionated Remult setup!",
6
6
  "bin": {