lines-db 0.19.1 → 0.19.3
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 +26 -0
- package/README.ja.md +21 -3
- package/README.md +21 -3
- package/bin/cli.mjs +394 -102
- package/dist/index.d.mts +57 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +332 -70
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +1 -1
- package/dist/internal.mjs +1 -1
- package/dist/{schema-module-Tb40gPyF.d.mts → schema-module-C_GKc1zV.d.mts} +3 -1
- package/dist/{schema-module-Tb40gPyF.d.mts.map → schema-module-C_GKc1zV.d.mts.map} +1 -1
- package/dist/{schema-module-D4j10w8_.mjs → schema-module-CdiS2Jq_.mjs} +41 -14
- package/dist/schema-module-CdiS2Jq_.mjs.map +1 -0
- package/package.json +2 -2
- package/dist/schema-module-D4j10w8_.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @toiroakr/lines-db
|
|
2
2
|
|
|
3
|
+
## 0.19.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- dbe99ef: `jsonlRecordLines(content)` is now exported. It applies the rule `JsonlReader` and `lines-db validate` read a JSONL file with (drop one leading byte order mark and each trailing carriage return, skip lines that are blank after trimming, count physical lines) and returns each row's trimmed `text` with its 1-based `line`, so a caller that reports errors by line no longer needs its own copy of the rule.
|
|
8
|
+
|
|
9
|
+
A row with a character around it that JSON does not allow as whitespace, such as a non-breaking space (`U+00A0`) or a second leading byte order mark, used to fail with `Failed to parse JSON line`. It is now trimmed and loaded, with a warning: `JsonlReader.readSnapshot` returns it in the new `warnings` of its result as `<file>:<line> has U+00A0 around its row, which JSON does not allow`, and `validate` and `initialize` add it to their `warnings` with the table name, which `lines-db validate` prints. Spaces and tabs around a row, which JSON allows, are still trimmed without a warning. A line that is only whitespace, including a non-breaking space, is still skipped without one. `fillFields` (`lines-db fill`) reads such a row the same way, instead of reporting it as a line that is not a JSON object: a row that gains a value is written back trimmed, and a row that gains nothing is left as it was.
|
|
10
|
+
|
|
11
|
+
## 0.19.2
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 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.
|
|
16
|
+
|
|
17
|
+
`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.
|
|
18
|
+
|
|
19
|
+
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.
|
|
20
|
+
|
|
21
|
+
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.
|
|
22
|
+
|
|
23
|
+
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.
|
|
24
|
+
|
|
25
|
+
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.
|
|
26
|
+
|
|
27
|
+
`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"`.
|
|
28
|
+
|
|
3
29
|
## 0.19.1
|
|
4
30
|
|
|
5
31
|
### Patch 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
|
|
|
@@ -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
|
-
|
|
534
|
+
元の行との対応には主キーを使い、ファイルにまだ主キーがなければ、ファイルを読んだときの行の位置を使います。行順は維持し、新しい行は末尾に追加します。指定したフィールドはnullも含めてデータベースの値を書き込みます。読み込み後に生SQLで行を変え、ファイルに主キーがなく、行数も変わっているなど、行を対応付けられない場合は、`sync()`が失敗したResultを返します。
|
|
533
535
|
|
|
534
536
|
新しいフィールドはスキーマの順に挿入します。たとえば`id`を先頭に定義していれば、後から補った`id`も行の先頭に入ります。既存のキーは移動せず、スキーマにないキーは宣言済みのキーの後ろに残します。
|
|
535
537
|
|
|
@@ -553,6 +555,22 @@ await JsonlWriter.write('./data/users.jsonl', filled);
|
|
|
553
555
|
|
|
554
556
|
対象外のフィールドは検証しません。その値がスキーマに違反していても、指定したフィールドを更新できます。計算後の行にないフィールドは元の値を残します。計算結果のキー順がスキーマと異なる場合は、`keyOrder`で新しいキーの位置を指定できます。
|
|
555
557
|
|
|
558
|
+
### 行の読み取り規則
|
|
559
|
+
|
|
560
|
+
`JsonlReader`と`lines-db validate`は、JSONLファイルを1つの規則で読みます。先頭のBOMを1つと各行末のCRを落とし、trimして空になる行は飛ばし、行番号は物理行番号です(飛ばした行も数えます)。各行の行番号を自分で使いたいとき(エラーを行番号つきで報告する場合など)は、`jsonlRecordLines`でファイルの内容にこの規則を適用できます。
|
|
561
|
+
|
|
562
|
+
```typescript
|
|
563
|
+
import { jsonlRecordLines } from 'lines-db';
|
|
564
|
+
|
|
565
|
+
jsonlRecordLines('\n{"a":1}\n {"a":2}\n');
|
|
566
|
+
// [
|
|
567
|
+
// { text: '{"a":1}', line: 2 },
|
|
568
|
+
// { text: '{"a":2}', line: 3, warning: 'has U+00A0 around its row, which JSON does not allow' },
|
|
569
|
+
// ]
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
行の前後にある半角スペースとタブは、JSONが空白として許すので、警告なしでtrimします。NBSP(`U+00A0`)や2つ目のBOMのように、JSONが空白として許さない文字もtrimするので、その行は読み込めます。`JsonlReader.readSnapshot`は、そうした行の`warning`をファイルと行番号つきで`warnings`に返し、`validate`と`initialize`は、`Table 'users': ./data/users.jsonl:3 has U+00A0 around its row, which JSON does not allow`の形で`warnings`に加えます。
|
|
573
|
+
|
|
556
574
|
## 設定
|
|
557
575
|
|
|
558
576
|
```typescript
|
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
|
|
|
@@ -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
|
|
|
@@ -554,6 +556,22 @@ await JsonlWriter.write('./data/users.jsonl', filled);
|
|
|
554
556
|
|
|
555
557
|
Fields outside the selection are not validated. Even if their values would fail the schema, selected fields can still be updated. Fields absent from the computed row keep their original values. If computed key order differs from schema order, use `keyOrder` to set the position of new keys.
|
|
556
558
|
|
|
559
|
+
### Read the lines that hold rows
|
|
560
|
+
|
|
561
|
+
`JsonlReader` and `lines-db validate` read a JSONL file with one rule: one leading byte order mark and each line's trailing carriage return are dropped, lines that are blank after trimming are skipped, and a row's line is its physical line, so skipped lines still count. `jsonlRecordLines` applies that rule to a file's content when you need each row's line yourself, such as to report errors by line.
|
|
562
|
+
|
|
563
|
+
```typescript
|
|
564
|
+
import { jsonlRecordLines } from 'lines-db';
|
|
565
|
+
|
|
566
|
+
jsonlRecordLines('\n{"a":1}\n {"a":2}\n');
|
|
567
|
+
// [
|
|
568
|
+
// { text: '{"a":1}', line: 2 },
|
|
569
|
+
// { text: '{"a":2}', line: 3, warning: 'has U+00A0 around its row, which JSON does not allow' },
|
|
570
|
+
// ]
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
Spaces and tabs around a row are trimmed without a warning, as JSON allows them. A character JSON does not allow as whitespace, such as a non-breaking space (`U+00A0`) or a second byte order mark, is trimmed too, so the row still loads. `JsonlReader.readSnapshot` returns each such row's `warning` in `warnings` with its file and line, and `validate` and `initialize` add it to their `warnings` as `Table 'users': ./data/users.jsonl:3 has U+00A0 around its row, which JSON does not allow`.
|
|
574
|
+
|
|
557
575
|
## Configuration
|
|
558
576
|
|
|
559
577
|
```typescript
|