json-fs-query 3.3.1 → 3.5.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.
Files changed (145) hide show
  1. package/README.md +104 -2
  2. package/browser/jsonFsQuery.js +810 -3845
  3. package/browser/jsonFsQuery.min.js +1 -1
  4. package/dist/cli/chooseDatabase.d.ts +13 -0
  5. package/dist/cli/chooseDatabase.js +37 -0
  6. package/dist/cli/chooseDatabase.min.js +1 -0
  7. package/dist/cli/formatCsv.d.ts +9 -0
  8. package/dist/cli/formatCsv.js +53 -0
  9. package/dist/cli/formatCsv.min.js +1 -0
  10. package/dist/cli/formatResult.d.ts +30 -0
  11. package/dist/cli/formatResult.js +139 -0
  12. package/dist/cli/formatResult.min.js +1 -0
  13. package/dist/cli/index.d.ts +7 -0
  14. package/dist/cli/index.js +52 -0
  15. package/dist/cli/index.min.js +2 -0
  16. package/dist/cli/interactive.d.ts +15 -0
  17. package/dist/cli/interactive.js +257 -0
  18. package/dist/cli/interactive.min.js +1 -0
  19. package/dist/cli/output.d.ts +31 -0
  20. package/dist/cli/output.js +64 -0
  21. package/dist/cli/output.min.js +1 -0
  22. package/dist/cli/runDirect.d.ts +11 -0
  23. package/dist/cli/runDirect.js +28 -0
  24. package/dist/cli/runDirect.min.js +1 -0
  25. package/dist/data/introspect/alter.js +2 -10
  26. package/dist/data/introspect/alter.min.js +1 -1
  27. package/dist/data/introspect/create.js +15 -4
  28. package/dist/data/introspect/create.min.js +1 -1
  29. package/dist/data/introspect/definitions/key.js +0 -2
  30. package/dist/data/introspect/definitions/key.min.js +1 -1
  31. package/dist/data/introspect/definitions/record.js +0 -2
  32. package/dist/data/introspect/definitions/record.min.js +1 -1
  33. package/dist/data/introspect/describe.js +2 -0
  34. package/dist/data/introspect/describe.min.js +1 -1
  35. package/dist/data/introspect/drop.js +3 -4
  36. package/dist/data/introspect/drop.min.js +1 -1
  37. package/dist/data/introspect/formatDatabase.js +0 -2
  38. package/dist/data/introspect/formatDatabase.min.js +1 -1
  39. package/dist/data/introspect/index.js +0 -4
  40. package/dist/data/introspect/index.min.js +1 -1
  41. package/dist/data/introspect/packDatabase.js +0 -19
  42. package/dist/data/introspect/packDatabase.min.js +1 -1
  43. package/dist/data/queries/createRecord.js +0 -2
  44. package/dist/data/queries/createRecord.min.js +1 -1
  45. package/dist/data/queries/databaseStatements.js +0 -3
  46. package/dist/data/queries/databaseStatements.min.js +1 -1
  47. package/dist/data/queries/deleteEntity.js +0 -20
  48. package/dist/data/queries/deleteEntity.min.js +1 -1
  49. package/dist/data/queries/dropRecord.js +0 -5
  50. package/dist/data/queries/dropRecord.min.js +1 -1
  51. package/dist/data/queries/groupBy.js +0 -16
  52. package/dist/data/queries/groupBy.min.js +1 -1
  53. package/dist/data/queries/insertEntity.js +0 -35
  54. package/dist/data/queries/insertEntity.min.js +1 -1
  55. package/dist/data/queries/joinEntity.js +0 -15
  56. package/dist/data/queries/joinEntity.min.js +1 -1
  57. package/dist/data/queries/parser.js +0 -2
  58. package/dist/data/queries/parser.min.js +1 -1
  59. package/dist/data/queries/selectFields.js +0 -3
  60. package/dist/data/queries/selectFields.min.js +1 -1
  61. package/dist/data/queries/showRecords.js +0 -2
  62. package/dist/data/queries/showRecords.min.js +1 -1
  63. package/dist/data/queries/updateEntity.js +0 -4
  64. package/dist/data/queries/updateEntity.min.js +1 -1
  65. package/dist/data/queries/where.js +0 -4
  66. package/dist/data/queries/where.min.js +1 -1
  67. package/dist/data/storage/databases.js +1 -2
  68. package/dist/data/storage/databases.min.js +1 -1
  69. package/dist/data/storage/listFiles.js +0 -2
  70. package/dist/data/storage/listFiles.min.js +1 -1
  71. package/dist/data/storage/readCache.js +0 -13
  72. package/dist/data/storage/readCache.min.js +1 -1
  73. package/dist/data/storage/readJson.js +2 -2
  74. package/dist/data/storage/readJson.min.js +1 -1
  75. package/dist/data/storage/storagePath.js +0 -1
  76. package/dist/data/storage/storagePath.min.js +1 -1
  77. package/dist/data/storage/writeSet.js +0 -32
  78. package/dist/data/storage/writeSet.min.js +1 -1
  79. package/dist/data/utilities/assertValidName.d.ts +14 -0
  80. package/dist/data/utilities/assertValidName.js +34 -0
  81. package/dist/data/utilities/assertValidName.min.js +1 -0
  82. package/dist/data/utilities/computeKeyValue.js +0 -2
  83. package/dist/data/utilities/computeKeyValue.min.js +1 -1
  84. package/dist/data/utilities/conditionEntities.js +0 -20
  85. package/dist/data/utilities/conditionEntities.min.js +1 -1
  86. package/dist/data/utilities/conditionTree.js +0 -3
  87. package/dist/data/utilities/conditionTree.min.js +1 -1
  88. package/dist/data/utilities/config.d.ts +11 -0
  89. package/dist/data/utilities/config.js +15 -1
  90. package/dist/data/utilities/config.min.js +1 -1
  91. package/dist/data/utilities/databaseLock.js +0 -14
  92. package/dist/data/utilities/databaseLock.min.js +1 -1
  93. package/dist/data/utilities/deregisterRecord.js +0 -2
  94. package/dist/data/utilities/deregisterRecord.min.js +1 -1
  95. package/dist/data/utilities/fieldType.js +0 -2
  96. package/dist/data/utilities/fieldType.min.js +1 -1
  97. package/dist/data/utilities/getConditionIndexedList.js +0 -20
  98. package/dist/data/utilities/getConditionIndexedList.min.js +1 -1
  99. package/dist/data/utilities/indexBuffer.js +0 -33
  100. package/dist/data/utilities/indexBuffer.min.js +1 -1
  101. package/dist/data/utilities/indexHas.js +0 -3
  102. package/dist/data/utilities/indexHas.min.js +1 -1
  103. package/dist/data/utilities/joinBases.js +0 -33
  104. package/dist/data/utilities/joinBases.min.js +1 -1
  105. package/dist/data/utilities/keyIndexFiles.js +0 -19
  106. package/dist/data/utilities/keyIndexFiles.min.js +1 -1
  107. package/dist/data/utilities/knownFields.js +0 -21
  108. package/dist/data/utilities/knownFields.min.js +1 -1
  109. package/dist/data/utilities/mergeJoin.js +0 -2
  110. package/dist/data/utilities/mergeJoin.min.js +1 -1
  111. package/dist/data/utilities/packedTable.js +0 -3
  112. package/dist/data/utilities/packedTable.min.js +1 -1
  113. package/dist/data/utilities/parsers/findConditions.js +0 -2
  114. package/dist/data/utilities/parsers/findConditions.min.js +1 -1
  115. package/dist/data/utilities/parsers/findGroupBy.js +0 -2
  116. package/dist/data/utilities/parsers/findGroupBy.min.js +1 -1
  117. package/dist/data/utilities/parsers/maskStrings.js +0 -3
  118. package/dist/data/utilities/parsers/maskStrings.min.js +1 -1
  119. package/dist/data/utilities/parsers/parseCreate.js +0 -21
  120. package/dist/data/utilities/parsers/parseCreate.min.js +1 -1
  121. package/dist/data/utilities/parsers/splitClauses.js +0 -16
  122. package/dist/data/utilities/parsers/splitClauses.min.js +1 -1
  123. package/dist/data/utilities/parsers/splitQueries.js +0 -3
  124. package/dist/data/utilities/parsers/splitQueries.min.js +1 -1
  125. package/dist/data/utilities/reconcileJoin.js +0 -4
  126. package/dist/data/utilities/reconcileJoin.min.js +1 -1
  127. package/dist/data/utilities/removeFromIndex.js +0 -3
  128. package/dist/data/utilities/removeFromIndex.min.js +1 -1
  129. package/dist/data/utilities/requireNode.js +0 -1
  130. package/dist/data/utilities/requireNode.min.js +1 -1
  131. package/dist/data/utilities/retrieveRecord.js +0 -1
  132. package/dist/data/utilities/retrieveRecord.min.js +1 -1
  133. package/dist/data/utilities/retrieveRecords.js +0 -16
  134. package/dist/data/utilities/retrieveRecords.min.js +1 -1
  135. package/dist/data/utilities/rowLookup.js +0 -15
  136. package/dist/data/utilities/rowLookup.min.js +1 -1
  137. package/dist/data/utilities/runQuery.js +0 -16
  138. package/dist/data/utilities/runQuery.min.js +1 -1
  139. package/dist/data/utilities/semiJoin.js +0 -19
  140. package/dist/data/utilities/semiJoin.min.js +1 -1
  141. package/dist/data/utilities/updateIndex.js +0 -2
  142. package/dist/data/utilities/updateIndex.min.js +1 -1
  143. package/dist/index.js +0 -4
  144. package/dist/index.min.js +1 -1
  145. package/package.json +9 -3
package/README.md CHANGED
@@ -95,6 +95,9 @@ test that calls `checkPacked()` (see Packed tables) catches a forgotten `packDat
95
95
  npm install json-fs-query
96
96
  ```
97
97
 
98
+ It comes with a command line, `jfsq` (also `json-fs-query`): `npx jfsq` opens a prompt on the `database/` folder (see
99
+ [Command line](#command-line-jfsq)).
100
+
98
101
  ## Usage
99
102
 
100
103
  ```js
@@ -517,7 +520,7 @@ new fields. The `alter()` function takes the same changes as objects, e.g.
517
520
 
518
521
  ### Reading a query over several lines: `queryStatus()`
519
522
 
520
- For a prompt which reads a query line by line (like the CLI), `queryStatus()` says whether the text so far is ready to
523
+ For a prompt which reads a query line by line (like the [command line](#command-line-jfsq)'s), `queryStatus()` says whether the text so far is ready to
521
524
  run, and if not, what it is waiting for: `'` or `"` (a string), `/*` (a comment), `[` or `(` (a bracket), or `;`.
522
525
  Join the lines with `'\n'`, since a `--` comment ends at the end of its line.
523
526
 
@@ -613,7 +616,8 @@ configure({
613
616
  format: 'compact', // how files are written: 'compact' or 'pretty' (default 'compact')
614
617
  packedReads: undefined, // use packed files (see packDatabase); default true in a browser, false in Node
615
618
  databasesPath: undefined, // a folder holding several databases (see Several databases)
616
- database: undefined // the one of them in use
619
+ database: undefined, // the one of them in use
620
+ fetchOptions: undefined // passed to every fetch() in a browser, such as { cache: 'no-cache' }
617
621
  })
618
622
  ```
619
623
 
@@ -624,6 +628,11 @@ Reads fetch several entry files at once, `readConcurrency` at a time, and keep t
624
628
  file is one request: 16 suits HTTP/2 static hosts (such as GitHub Pages), while over HTTP/1.1 a browser only opens about
625
629
  6 connections per host anyway. `readConcurrency: 1` reads one file at a time, as every read did before this setting.
626
630
 
631
+ `fetchOptions` is given to every `fetch()` a browser read makes, as its second argument. By default reads use the
632
+ browser's normal HTTP cache, which suits data that only changes when the site is deployed. When something writes the
633
+ files while the page is in use (a local server, say), `{ cache: 'no-cache' }` makes the browser check every file with the
634
+ server before using its cached copy: an unchanged file comes back as a short "not modified", a changed one is read again.
635
+
627
636
  ### Several databases: `databasesPath`, `use`
628
637
 
629
638
  One program can keep several databases (say, real data and a test copy) in one folder and switch between them:
@@ -673,6 +682,99 @@ is changed. Node only, like every write.
673
682
  file turns compact the next time it changes, and diffs mix both. Either run `formatDatabase('compact')` once and commit
674
683
  the result on its own, or set `format: 'pretty'` to keep writing indented files.
675
684
 
685
+ ## Command line: `jfsq`
686
+
687
+ `jfsq` (or `json-fs-query`) runs queries from a terminal or a script: a prompt for trying things, `-q` for one query,
688
+ or a file of queries piped in to set a database up.
689
+
690
+ ```shell
691
+ npx jfsq # the prompt, on database/ in the current folder
692
+ npx jfsq -q "read workouts where reps > 8" # run it, print the result, exit
693
+ npx jfsq -d site/database < seed.txt # run every query in a file
694
+ ```
695
+
696
+ | Option | |
697
+ | --- | --- |
698
+ | `-q, --query <query>` | run this query (or several, separated by `;`), print the results and exit |
699
+ | `-d, --database <path>` | the database folder (default `database/`); with `--databases`, the name of a database in that folder |
700
+ | `--databases <folder>` | a folder holding several databases (see [Several databases](#several-databases-databasespath-use)) |
701
+ | `-o, --output <format>` | how results print: `table` (the default), `json`, `csv` or `dump` (see Output) |
702
+ | `--null <text>` | how a table shows `null` (default `NULL`) |
703
+ | `--missing <text>` | how a table shows a missing field (default `—`) |
704
+ | `-v, --version` | print the version |
705
+ | `-h, --help` | list the options, with examples |
706
+
707
+ ### The prompt
708
+
709
+ Without `-q`, `jfsq` opens a prompt. A query runs once it ends with a `;` outside any string, bracket or comment, so a
710
+ query can take several lines; the continuation prompt shows what it is still waiting for (`'>` for an open string,
711
+ `(>` for a bracket, `;>` for the end), the way psql does (see [`queryStatus()`](#reading-a-query-over-several-lines-querystatus)).
712
+ An error is printed and the prompt carries on. Ctrl+C clears the query being typed; on an empty prompt, Ctrl+C or
713
+ `/exit` quits.
714
+
715
+ ```text
716
+ JSON-Query> read workouts
717
+ ;> where reps > 8;
718
+ +--------------------------------------+------+------------+
719
+ | _id | reps | date |
720
+ +--------------------------------------+------+------------+
721
+ | 9f3c1e2a-5b7d-4e8f-a1c2-3d4e5f6a7b8c | 10 | 2024-06-02 |
722
+ +--------------------------------------+------+------------+
723
+ (1 row)
724
+ ```
725
+
726
+ With `--databases`, the prompt names the database in use (`real> `) and follows a `use` in a query.
727
+
728
+ Commands start with a `/` and only count at the start of a query (they're never sent to the query parser, and a query
729
+ can still start with a `/* comment */`). Without a value, each says what it is set to:
730
+
731
+ | Command | |
732
+ | --- | --- |
733
+ | `/output <format>` | how the next results print: `table`, `json`, `csv` or `dump` |
734
+ | `/null <text>` | how a table shows `null` |
735
+ | `/missing <text>` | how a table shows a missing field |
736
+ | `/exit` | quit |
737
+
738
+ ### Output
739
+
740
+ - **table** (the default): a table per result, with a row count. Each column is a field, in the order they first
741
+ appear. A field a row doesn't have shows `—`, `null` shows `NULL` and the empty string `''`, so the three read
742
+ differently without colour (on a terminal they are also dimmed; never with `NO_COLOR`, or when the output isn't a
743
+ terminal). A table that shows `—` or `NULL` has a one-line legend under it. A grouped read is a heading and a table
744
+ per group (`date = 2024-06-02 (3 rows)`, `date is missing (1 row)`), nested groups indented under theirs. A result
745
+ that isn't rows (`describe`) prints as JSON.
746
+ - **json**: each result as JSON, one value per query (`jq` reads several as a stream): `jfsq -o json -q "read
747
+ workouts" | jq '.[].reps'`.
748
+ - **csv**: a header line, then a line per row; a field is quoted when it holds a comma, a quote, a line break or space
749
+ at either end. `null` and a missing field are empty, the empty string is `""` (as psql's CSV output does). A grouped
750
+ read is its rows: `jfsq -o csv -q "read workouts" > workouts.csv`.
751
+ - **dump** can be chosen, but isn't available yet: it refuses each query (before running it). It is meant to print
752
+ the queries that would rebuild a record (its `create`, then an `insert` per row) for you to run or edit.
753
+
754
+ ### Scripts and seed files
755
+
756
+ Queries piped in (`jfsq < seed.txt`, or `generate-queries | jfsq`) run the same way as at the prompt, but quietly:
757
+ only results (on stdout) and errors (on stderr), no prompts. Every query runs even after one fails, and the last one
758
+ runs without its closing `;`. Commands such as `/output json` work in the file too.
759
+
760
+ ```sql
761
+ -- seed.txt
762
+ create if not exists workouts (_id string auto, reps int, date string, primary key (_id));
763
+ insert workouts values (reps = 10, date = '2024-06-02'), (reps = 12, date = '2024-06-05');
764
+ ```
765
+
766
+ `if not exists` makes the `create` safe to run twice; the `insert` isn't (running the file again adds the rows again).
767
+
768
+ The exit code is 0 when everything worked and 1 when anything failed: a query, a bad option, or a database that
769
+ doesn't exist (then nothing runs). The same goes for `-q`, so a script can check it:
770
+
771
+ ```shell
772
+ jfsq -d site/database < seed.txt || echo "seeding failed"
773
+ ```
774
+
775
+ The command line is a separate process: a server and `jfsq` writing to the same folder at the same time aren't
776
+ protected from each other (see [Several calls at once](#several-calls-at-once)).
777
+
676
778
  ## Several calls at once
677
779
 
678
780
  Calls to `query()`, `create()`, `drop()`, `describe()`, `formatDatabase()`, `packDatabase()` and `checkPacked()` on