lines-db 0.15.0 → 0.18.1
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 +37 -0
- package/README.ja.md +72 -6
- package/README.md +69 -4
- package/bin/cli.mjs +123 -51
- package/dist/index.d.mts +10 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +66 -16
- package/dist/index.mjs.map +1 -1
- package/dist/internal.mjs +1 -2
- package/package.json +10 -8
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { a as ok, i as err, n as JsonlReader, o as toError, r as hashJsonlContent, s as unwrap, t as replaceFile } from "./replace-file-AWLIm7aN.mjs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { randomBytes } from "node:crypto";
|
|
4
|
-
import { access, chmod, mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { access, chmod, mkdir, readFile, readdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
5
5
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import { styleText } from "node:util";
|
|
8
|
+
import { z } from "zod";
|
|
8
9
|
//#region \0rolldown/runtime.js
|
|
9
10
|
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
10
11
|
//#endregion
|
|
@@ -2373,20 +2374,36 @@ async function fillFields(options) {
|
|
|
2373
2374
|
}
|
|
2374
2375
|
async function fillFieldsInternal(options) {
|
|
2375
2376
|
if (options.fields && options.fields.length === 0) throw new Error("No fields to fill. Name at least one field.");
|
|
2376
|
-
const
|
|
2377
|
-
|
|
2377
|
+
const paths = typeof options.path === "string" ? [options.path] : options.path;
|
|
2378
|
+
if (paths.length === 0) throw new Error("No paths to fill. Name at least one path.");
|
|
2379
|
+
const sources = /* @__PURE__ */ new Map();
|
|
2380
|
+
for (const path of paths) {
|
|
2381
|
+
const { dataDir, tableNames } = await resolveTables(path);
|
|
2382
|
+
for (const table of tableNames) {
|
|
2383
|
+
const file = join(dataDir, `${table}.jsonl`);
|
|
2384
|
+
const key = await realpath(file);
|
|
2385
|
+
if (!sources.has(key)) sources.set(key, {
|
|
2386
|
+
table,
|
|
2387
|
+
file,
|
|
2388
|
+
target: key,
|
|
2389
|
+
schemaDir: options.schemaDir ?? dataDir
|
|
2390
|
+
});
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2378
2393
|
const loadFiller = options.loadFiller ?? fillerFromValidationSchema;
|
|
2379
2394
|
const tablesWithoutSchema = [];
|
|
2380
2395
|
const tables = [];
|
|
2381
|
-
for (const table of
|
|
2396
|
+
for (const { table, file, target, schemaDir } of sources.values()) {
|
|
2382
2397
|
const schemaPath = await findSchemaFile(schemaDir, table);
|
|
2383
2398
|
if (!schemaPath) {
|
|
2384
|
-
tablesWithoutSchema.push(table);
|
|
2399
|
+
if (!tablesWithoutSchema.includes(table)) tablesWithoutSchema.push(table);
|
|
2385
2400
|
continue;
|
|
2386
2401
|
}
|
|
2387
2402
|
const schemaModule = await import(`${pathToFileURL(schemaPath).href}?t=${Date.now()}`);
|
|
2388
2403
|
tables.push({
|
|
2389
2404
|
table,
|
|
2405
|
+
file,
|
|
2406
|
+
target,
|
|
2390
2407
|
fields: options.fields ?? [primaryKeyOf(schemaModule)],
|
|
2391
2408
|
fill: loadFiller(schemaModule, {
|
|
2392
2409
|
tableName: table,
|
|
@@ -2398,9 +2415,8 @@ async function fillFieldsInternal(options) {
|
|
|
2398
2415
|
const unreadableLines = [];
|
|
2399
2416
|
const filled = [];
|
|
2400
2417
|
const writes = [];
|
|
2401
|
-
for (const { table, fields, fill } of tables) {
|
|
2402
|
-
const
|
|
2403
|
-
const read = await readFile(file, "utf-8");
|
|
2418
|
+
for (const { table, file, target, fields, fill } of tables) {
|
|
2419
|
+
const read = await readFile(target, "utf-8");
|
|
2404
2420
|
const bom = read.startsWith("") ? "" : "";
|
|
2405
2421
|
const lines = splitLines(read.slice(bom.length));
|
|
2406
2422
|
const written = /* @__PURE__ */ new Set();
|
|
@@ -2432,6 +2448,7 @@ async function fillFieldsInternal(options) {
|
|
|
2432
2448
|
if (count === 0) continue;
|
|
2433
2449
|
writes.push({
|
|
2434
2450
|
file,
|
|
2451
|
+
target,
|
|
2435
2452
|
read,
|
|
2436
2453
|
content: bom + lines.map((line) => `${line.text}${line.eol}`).join("")
|
|
2437
2454
|
});
|
|
@@ -2442,7 +2459,7 @@ async function fillFieldsInternal(options) {
|
|
|
2442
2459
|
count
|
|
2443
2460
|
});
|
|
2444
2461
|
}
|
|
2445
|
-
for (const { file, read } of writes) if (await readFile(
|
|
2462
|
+
for (const { file, target, read } of writes) if (await readFile(target, "utf-8") !== read) {
|
|
2446
2463
|
const error = /* @__PURE__ */ new Error(`JSONL file '${file}' was changed after it was read, so no file was filled. Run the fill again.`);
|
|
2447
2464
|
error.name = "JsonlConflictError";
|
|
2448
2465
|
error.file = file;
|
|
@@ -2454,7 +2471,7 @@ async function fillFieldsInternal(options) {
|
|
|
2454
2471
|
filled,
|
|
2455
2472
|
tablesWithoutSchema,
|
|
2456
2473
|
unreadableLines,
|
|
2457
|
-
unproducedFields:
|
|
2474
|
+
unproducedFields: sources.size > 0 ? named.filter((field) => !produced.has(field)) : []
|
|
2458
2475
|
};
|
|
2459
2476
|
}
|
|
2460
2477
|
/**
|
|
@@ -2464,21 +2481,21 @@ async function fillFieldsInternal(options) {
|
|
|
2464
2481
|
async function replaceAll(writes) {
|
|
2465
2482
|
const staged = writes.map((write) => ({
|
|
2466
2483
|
...write,
|
|
2467
|
-
temporary: `${write.
|
|
2484
|
+
temporary: `${write.target}.${randomBytes(6).toString("hex")}.tmp`
|
|
2468
2485
|
}));
|
|
2469
2486
|
const replaced = [];
|
|
2470
2487
|
try {
|
|
2471
|
-
for (const {
|
|
2488
|
+
for (const { target, temporary, content } of staged) {
|
|
2472
2489
|
await writeFile(temporary, content, "utf-8");
|
|
2473
|
-
await chmod(temporary, (await stat(
|
|
2490
|
+
await chmod(temporary, (await stat(target)).mode & 4095);
|
|
2474
2491
|
}
|
|
2475
2492
|
for (const write of staged) {
|
|
2476
|
-
await rename(write.temporary, write.
|
|
2493
|
+
await rename(write.temporary, write.target);
|
|
2477
2494
|
replaced.push(write);
|
|
2478
2495
|
}
|
|
2479
2496
|
} catch (error) {
|
|
2480
2497
|
const notRestored = [];
|
|
2481
|
-
for (const { file, read } of replaced) await writeFile(
|
|
2498
|
+
for (const { file, target, read } of replaced) await writeFile(target, read, "utf-8").catch(() => notRestored.push(file));
|
|
2482
2499
|
await Promise.all(staged.map(({ temporary }) => rm(temporary, { force: true })));
|
|
2483
2500
|
if (notRestored.length === 0) throw error;
|
|
2484
2501
|
throw new Error(`${toError(error).message}\nThese files could not be put back and stay filled: ${notRestored.join(", ")}`, { cause: error });
|
|
@@ -2791,6 +2808,39 @@ var ErrorFormatter = class {
|
|
|
2791
2808
|
}
|
|
2792
2809
|
};
|
|
2793
2810
|
//#endregion
|
|
2794
|
-
|
|
2811
|
+
//#region src/dataset-config.ts
|
|
2812
|
+
const directories = z.array(z.string().min(1)).min(1);
|
|
2813
|
+
const datasetConfigSchema = z.strictObject({
|
|
2814
|
+
schemaDir: z.string().min(1),
|
|
2815
|
+
base: directories,
|
|
2816
|
+
datasets: z.record(z.string().min(1), directories)
|
|
2817
|
+
});
|
|
2818
|
+
async function loadDatasetConfig(path = "lines-db.config.json", options = {}) {
|
|
2819
|
+
const configPath = resolve(path);
|
|
2820
|
+
let config;
|
|
2821
|
+
try {
|
|
2822
|
+
config = datasetConfigSchema.parse(JSON.parse(await readFile(configPath, "utf-8")));
|
|
2823
|
+
} catch (error) {
|
|
2824
|
+
throw new Error(`Failed to load dataset configuration ${configPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2825
|
+
}
|
|
2826
|
+
const selected = (options.datasets === "all" ? Object.keys(config.datasets) : options.datasets ?? []).flatMap((name) => {
|
|
2827
|
+
if (!Object.hasOwn(config.datasets, name)) throw new Error(`Unknown dataset '${name}'. Available datasets: ${Object.keys(config.datasets).join(", ") || "(none)"}`);
|
|
2828
|
+
return config.datasets[name];
|
|
2829
|
+
});
|
|
2830
|
+
const configDir = dirname(configPath);
|
|
2831
|
+
const schemaDir = resolve(configDir, config.schemaDir);
|
|
2832
|
+
try {
|
|
2833
|
+
if (!(await stat(schemaDir)).isDirectory()) throw new Error("Expected a directory");
|
|
2834
|
+
} catch (error) {
|
|
2835
|
+
throw new Error(`Invalid schemaDir ${schemaDir}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2836
|
+
}
|
|
2837
|
+
const dataDirs = await Promise.all([...config.base, ...selected].map((dir) => realpath(resolve(configDir, dir))));
|
|
2838
|
+
return {
|
|
2839
|
+
dataDir: [...new Set(dataDirs)],
|
|
2840
|
+
schemaDir
|
|
2841
|
+
};
|
|
2842
|
+
}
|
|
2843
|
+
//#endregion
|
|
2844
|
+
export { DirectoryScanner, ErrorFormatter, JsonlReader, JsonlWriter, LinesDB, RUNTIME, SCHEMA_EXTENSIONS, SchemaLoader, TypeGenerator, defineSchema, detectRuntime, ensureTableRowsValid, err, extractTableNameFromSchemaFile, fillFields, findSchemaFile, hasBackward, isSchemaFile, loadDatasetConfig, mergeFields, ok, rewriteExtensionForImport, unwrap };
|
|
2795
2845
|
|
|
2796
2846
|
//# sourceMappingURL=index.mjs.map
|