lines-db 0.19.0 → 0.19.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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @toiroakr/lines-db
2
2
 
3
+ ## 0.19.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 1a510a8: An inserted row's field the validation schema transforms is now written to the JSONL file as it was given, not as the schema made it. With a schema that turns the `code` `"1"` into `"ORD-1"` (a TailorDB `create` hook does this), `insert('orders', { id: 3, code: '1' })` used to write `"ORD-1"`, and the next load of the file turned it into `"ORD-ORD-1"`. It now writes `"1"`, as `update` already did, so the file holds the side a seed file holds and the schema applies its transformation once, on load. This applies to `insert` and `batchInsert` when `writeFilledValues` is `'primaryKey'` (the studio's default), except for fields named in `writeBackFields` and foreign-key columns a `CASCADE` or `SET NULL` action can change, and values a line cannot hold, such as a bigint, which are still written as the database has them; with `'all'` every value the schema gives is still written.
8
+
9
+ `findWithDefaults()` now also returns `input` for each row: the row as its line holds it, the values before the schema transformed them, without the fields the schema filled in. The studio's rows response carries it as `inputs`, and the studio can show each value as its input, its output (read-only), or both, the input marked `in` above the output marked `out`; the input is what a cell and the form edit.
10
+
11
+ Updating one field of a row no longer rewrites the other fields of its line with the values the schema made of them: with the line `{"id":1,"code":"a","note":"n"}`, changing only `note` used to write `"code":"ORD-a"`, which the schema turned into `"ORD-ORD-a"` on the next load. With `writeFilledValues: 'primaryKey'`, a field the line holds and nothing set now keeps the line's value; `'all'` still writes the value the schema gives.
12
+
13
+ A write-back finds the line of a row again by the primary key the schema makes of it, so a table whose schema transforms its primary key keeps the keys its lines hold, and a table without a primary key, and a row whose primary key was changed, find it by the position its line had when the file was read, kept up to date as rows are added and removed. The studio edits a column whose lines hold another type than the schema makes in its Input view, and every new row, as text when the lines hold strings and as JSON otherwise. A rowid change made by an ON UPDATE CASCADE, or by raw SQL, carries the row's recorded position with it, and a row with a recorded position keeps its line even when its key moves onto another row's old key.
14
+
15
+ A line's fields that the schema does not know, and the fields a migration `transform` set, now stay with their own row when a table's integer primary keys are not in order in the file: with the lines `{"id":3,"extra":"x3"}` and `{"id":1,"extra":"x1"}`, updating row 1 used to swap the two `extra` values.
16
+
17
+ A row whose key changes while a write-back of its table is in progress keeps its line, and the log of rowid changes works with the SQLite that Bun ships, which refuses a qualified table name in a trigger.
18
+
19
+ `update` now stores what the validation schema makes of each value it is given, as `insert` does: updating `code` to `"b"` used to leave `"b"` in the database, so the studio's Output view and `find()` showed `"b"` instead of `"ORD-b"` until the file was loaded again. The line still holds `"b"`.
20
+
21
+ ## 0.19.1
22
+
23
+ ### Patch Changes
24
+
25
+ - 4bfd4b7: Read the files a schema file imports anew when one of them changed, so an edit to a project file the schema imports, such as a table definition, is validated against instead of the version first loaded. Once the first read has recorded the imports, a file is evaluated again only when it or a file it imports changed, except after a read that failed, so reading the tables again many times does not grow memory with the number of files. A package from `node_modules`, and a file of another package, such as one linked from a workspace, stay one module. A runtime without synchronous module hooks still reads only the schema file itself again, and CommonJS schemas (`.schema.cts`) keep their first evaluation, as before.
26
+
27
+ `hasExternalChanges()` and `findExternalChanges()` now report an edit to, or the removal of, a project file the schemas imported, so the studio reloads when only a table definition changed. `listSchemaDependencies()` lists the project files a database's schemas imported. With synchronous module hooks, a relative import of a file that was not there when the tables were read is tracked too, so creating the file is reported as a change. Calling `initialize()` again in the same process before an earlier call has finished can leave `hasExternalChanges()` reporting no change for a file edited while the earlier call was reading; the studio reads the tables one reload at a time and is not affected. The studio watches the imported files too, so an open page is told as soon as one of them is edited or created, even when it is outside the watched directories; a file that is not there yet is watched through the directory that would hold it, or the nearest one that exists.
28
+
3
29
  ## 0.19.0
4
30
 
5
31
  ### Minor Changes
package/README.ja.md CHANGED
@@ -161,7 +161,7 @@ npx lines-db migrate ./data "(row) => row" --fields id
161
161
  npx lines-db migrate ./data/users.jsonl "(row) => row" --fields id
162
162
  ```
163
163
 
164
- 主キーがあれば、元の行との対応を主キーで判断します。主キーをこれから補う場合など、ファイルに主キーがなければ行の位置を使います。既存の行順は維持し、新しい行は末尾へ追加します。主キーがなく行数も変わっている場合は、対応を判断できないためエラーになります。
164
+ 主キーがあれば、元の行との対応を主キーで判断します。主キーをこれから補う場合など、ファイルに主キーがなければ行の位置を使います。既存の行順は維持し、新しい行は末尾へ追加します。主キーがない場合は、ファイルを読んだときの行の位置で対応を判断し、その位置は行の追加や削除に合わせて更新します。読み込み後に生SQLで行を変えた場合は位置が分からないため、主キーがなく行数も変わっていれば、対応を判断できずエラーになります。
165
165
 
166
166
  `--fields`は対象の全テーブルに適用します。指定したフィールドを持たないテーブルは書き戻さないため、列構成が異なるテーブルにも同じリストを使えます。どのテーブルにもないフィールド名を指定するとエラーになります。
167
167
 
@@ -473,7 +473,7 @@ if (!result.ok && result.error.name === 'JsonlConflictError') {
473
473
 
474
474
  内容の確認と書き込みの間はファイルをロックしません。その間に別のプロセスが保存した変更は上書きされます。一人でローカルファイルを編集する用途を想定しており、同時に書き込むプロセス間の排他制御は行いません。
475
475
 
476
- `hasExternalChanges()`は、最後に読み書きした後のJSONLファイルやスキーマファイルの変更・追加・削除を確認します。`findExternalChanges()`は該当ファイルの一覧を返します。長時間動くプロセスで、他のツールによる変更を検知して読み直すときに使えます。
476
+ `hasExternalChanges()`は、最後に読み書きした後のJSONLファイルやスキーマファイルの変更・追加・削除と、読み込み時にスキーマがimportしていたプロジェクト内ファイル(テーブル定義など)の変更・削除を確認します。`findExternalChanges()`は該当ファイルの一覧を返し、`listSchemaDependencies()`はスキーマがimportしていたプロジェクト内ファイルの一覧を返します。長時間動くプロセスで、他のツールによる変更を検知して読み直すときに使えます。同期のモジュールフックがあるときは、読み込み時に存在しなかった相対importの先も追跡し、そのファイルが作られると変更として報告します。最初の読み込みでimportが記録されたあとは、読み込みに失敗した場合を除き、そのファイルか、そのファイルがimportしているファイルが変わったときだけ、次の読み込みで評価し直されます。同期のモジュールフックがない実行環境では、スキーマファイル自身だけを読み直します。
477
477
 
478
478
  ```typescript
479
479
  if (unwrap(await db.hasExternalChanges())) {
@@ -506,9 +506,11 @@ unwrap(db.update('people', {}, { id: 1 }, { resetToDefault: ['age'] }));
506
506
 
507
507
  ```typescript
508
508
  unwrap(await db.findWithDefaults('people'));
509
- // [{ row: { id: 1, name: 'Alicia', age: 20 }, defaulted: ['age'] }]
509
+ // [{ row: { id: 1, name: 'Alicia', age: 20 }, defaulted: ['age'], input: { id: 1, name: 'Alicia' } }]
510
510
  ```
511
511
 
512
+ スキーマは、書いた値そのものを変換することもあります。TailorDBの`create`フックは、`code`の`"1"`を`"ORD-1"`にします。seedファイルにはプラットフォームがフックを自分で適用するため、変換前の値を持たせます。そのため`writeFilledValues: 'primaryKey'`では、書き戻しはファイルの行が持つ値と、`insert()`、`batchInsert()`、`update()`に渡した値を書き、変換後の値は書きません。データベース自体は、`insert()`と同じく`update()`の後も変換後の値を持ちます。`code`を`"2"`に更新すると、行は`"2"`を持ち、`find()`は`"ORD-2"`を返します。bigintのように行に書けない値(オブジェクトの中も含む)は、スキーマが作った値で書きます。例外は、`writeBackFields`や`sync(..., { fields })`で指定したフィールドと、`ON DELETE`/`ON UPDATE`の`CASCADE`や`SET NULL`で変わりうる外部キー列で、これらはデータベースの値(変換後の値)を書きます。`'all'`では変換後の値を書くため、変換を2回かけても結果が変わらないスキーマでない限り、読み直すと再び変換されます(`"ORD-ORD-1"`)。入力値を持たせるファイルには`'primaryKey'`を使います。行は、スキーマが作る主キーで探し直すため、主キーのあるテーブルは、スキーマが主キーを変換しても値を保ちます。ファイルを読んだときに各行の行が占めていた位置は、行の追加や削除に合わせて更新します。主キーのないテーブルの行と、主キーを変更した行は、この位置で行を探します。読み込んだ後に別のプロセスが変更したことを検知したファイルには書き込まないため、位置は正しいままです。ただし、その確認から書き込みまでの間の変更は検知できません。`findWithDefaults()`は、変換後の`row`と並べて、行がファイルで持つ値`input`を返します。
513
+
512
514
  `backward`でフィールド名を変える行や、SQLで追加して元の行と書き込み記録がない行は、明示的に指定されたフィールドを判断できないため行全体を書き戻します。SQLで行を変更したテーブルも全行を書き戻します。読み込み時の移行用`transform`で設定したフィールドは、明示的に指定された値として扱います。
513
515
 
514
516
  元の行にある未知のフィールドは、書き戻し後も元の位置に残します。Valibotの`v.object()`のように未知のキーを取り除くスキーマでは、その行は検証を通ります。未知のキーをエラーにするには`v.strictObject()`などを使ってください。
@@ -529,7 +531,7 @@ unwrap(await db.sync('users', { fields: ['id'] }));
529
531
  const db = LinesDB.create({ dataDir: './data', writeBackFields: ['id'] });
530
532
  ```
531
533
 
532
- 元の行との対応には主キーを使い、ファイルにまだ主キーがなければ行の位置を使います。行順は維持し、新しい行は末尾に追加します。指定したフィールドはnullも含めてデータベースの値を書き込みます。行を対応付けられない場合は、`sync()`が失敗したResultを返します。
534
+ 元の行との対応には主キーを使い、ファイルにまだ主キーがなければ、ファイルを読んだときの行の位置を使います。行順は維持し、新しい行は末尾に追加します。指定したフィールドはnullも含めてデータベースの値を書き込みます。読み込み後に生SQLで行を変え、ファイルに主キーがなく、行数も変わっているなど、行を対応付けられない場合は、`sync()`が失敗したResultを返します。
533
535
 
534
536
  新しいフィールドはスキーマの順に挿入します。たとえば`id`を先頭に定義していれば、後から補った`id`も行の先頭に入ります。既存のキーは移動せず、スキーマにないキーは宣言済みのキーの後ろに残します。
535
537
 
package/README.md CHANGED
@@ -160,7 +160,7 @@ A normal migration writes whole rows back to the file, including schema defaults
160
160
  npx lines-db migrate ./data/users.jsonl "(row) => row" --fields id
161
161
  ```
162
162
 
163
- Rows are matched by primary key, or by position when the file has no primary key yet, such as when filling in that key. Existing row order is preserved, and new rows are appended. If the file has no primary key and the row count has changed, the migration fails because the rows cannot be matched.
163
+ Rows are matched by primary key, or by position when the file has no primary key yet, such as when filling in that key. Existing row order is preserved, and new rows are appended. Without a primary key, a row is matched by the position its line had when the file was read, kept up to date as rows are added and removed. If raw SQL changed the rows since, that position is not known, and when the file has no primary key and the row count has changed, the migration fails because the rows cannot be matched.
164
164
 
165
165
  `--fields` applies to all target tables. Tables without the named fields are not written back, so one list can cover tables with different columns. A field absent from every table causes an error.
166
166
 
@@ -474,7 +474,7 @@ With `sync()` or automatic sync outside a transaction, database changes remain a
474
474
 
475
475
  Files are not locked between checking their contents and writing. Changes saved by another process during that interval are overwritten. This is intended for one person editing local files; it does not coordinate concurrent writers.
476
476
 
477
- `hasExternalChanges()` detects changes, additions, and deletions of JSONL and schema files since the last read or write. `findExternalChanges()` lists the affected files. Use them to detect edits made by other tools and reload data in a long-running process.
477
+ `hasExternalChanges()` detects changes, additions, and deletions of JSONL and schema files since the last read or write, and changes and deletions of the project files the schemas imported (such as a table definition) when they were read. `findExternalChanges()` lists the affected files, and `listSchemaDependencies()` lists the project files the schemas imported. Use them to detect edits made by other tools and reload data in a long-running process. With synchronous module hooks, a relative import of a file that was not there when the tables were read is tracked too, so creating the file is reported as a change. Once the first read has recorded the imports, a file is evaluated again on the next read only when it or a file it imports changed, except after a read that failed; a runtime without synchronous module hooks reads only the schema file itself again.
478
478
 
479
479
  ```typescript
480
480
  if (unwrap(await db.hasExternalChanges())) {
@@ -507,9 +507,11 @@ unwrap(db.update('people', {}, { id: 1 }, { resetToDefault: ['age'] }));
507
507
 
508
508
  ```typescript
509
509
  unwrap(await db.findWithDefaults('people'));
510
- // [{ row: { id: 1, name: 'Alicia', age: 20 }, defaulted: ['age'] }]
510
+ // [{ row: { id: 1, name: 'Alicia', age: 20 }, defaulted: ['age'], input: { id: 1, name: 'Alicia' } }]
511
511
  ```
512
512
 
513
+ A schema can also transform a value you wrote: a TailorDB `create` hook turns the `code` `"1"` into `"ORD-1"`. A seed file holds the value before that transformation, because the platform applies the hook itself, so with `writeFilledValues: 'primaryKey'` a write-back keeps the value a line holds, and writes the value an `insert()`, `batchInsert()` or `update()` was given, not the transformed one. The database itself holds the transformed value, after an `update()` as after an `insert()`: updating `code` to `"2"` makes `find()` return `"ORD-2"` while the line holds `"2"`. A value a line cannot hold, such as a bigint (also inside an object), is written as the schema made it. The exceptions are the fields named in `writeBackFields` or `sync(..., { fields })` and the foreign-key columns an `ON DELETE` / `ON UPDATE` `CASCADE` or `SET NULL` action can change: those are written from the database, as the transformed value. With `'all'` the transformed value is written, and reading the file back transforms it a second time (`"ORD-ORD-1"`) unless the transformation gives the same result when applied again. Use `'primaryKey'` for files that hold input values. A line is found again by the primary key the schema makes of it, so a table with a primary key keeps its values even when the schema transforms the key. The position each row's line had when the file was read is kept up to date as rows are added and removed: it finds the line of a table without a primary key, and of a row whose primary key was changed. A file another process is seen to have changed since it was read is not written over, so these positions stay correct; a change made between that check and the write is not caught. `findWithDefaults()` returns each row's `input`, the row as its line holds it, beside the transformed `row`.
514
+
513
515
  Rows whose `backward` transformation renames fields, or rows added by SQL with no original row or write record, are written in full because explicitly supplied fields cannot be identified. A table changed through SQL also has all rows written back. Fields set by a migration `transform` on load count as explicitly supplied.
514
516
 
515
517
  Unknown fields from the original row retain their position on write-back. Schemas that strip unknown keys, such as Valibot's `v.object()`, allow those rows to pass validation. Use `v.strictObject()` or another schema that rejects unknown keys to report them as errors.
@@ -530,7 +532,7 @@ Set `writeBackFields` in the config to apply the rule to every sync, including a
530
532
  const db = LinesDB.create({ dataDir: './data', writeBackFields: ['id'] });
531
533
  ```
532
534
 
533
- Rows are matched by primary key, or by position when the file has no primary key yet. Row order is preserved, and new rows are appended. Selected fields use the database value, including null. If rows cannot be matched, `sync()` returns a failed Result.
535
+ Rows are matched by primary key, or by the position each row's line had when the file was read when the file has no primary key yet. Row order is preserved, and new rows are appended. Selected fields use the database value, including null. If rows cannot be matched - raw SQL changed them since the file was read, the file has no primary key, and the row count changed - `sync()` returns a failed Result.
534
536
 
535
537
  New fields are inserted in schema order. For example, an `id` declared first is inserted at the beginning of the row when filled later. Existing keys do not move, and unknown keys stay after declared keys.
536
538