@jinshuju/cli 0.31.0 → 0.33.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.
package/README.md CHANGED
@@ -305,7 +305,8 @@ than a prompt nobody is there to answer.
305
305
  ## Searching across containers
306
306
 
307
307
  ```bash
308
- jinshuju entry search "Acme Corp" # every form and table you can read, up to 10
308
+ jinshuju entry search "Acme Corp" # every form you can read, however many; see unsearched_count
309
+ jinshuju entry search "Acme Corp" --next <next> # carry on one that stopped short
309
310
  jinshuju entry search 13800138000 --form Kp7mQ2 --form Vn4xR8
310
311
  jinshuju entry search repair --scope-filter 'entries_count gt 100' # picks which forms to search, not which rows
311
312
 
@@ -699,6 +700,7 @@ jinshuju artifact share set Xy7Qm2PaLk9W --audience private # the link still
699
700
 
700
701
  ```bash
701
702
  jinshuju form list --name signup --name survey # several keywords match any, not one joined phrase
703
+ jinshuju form list --folder Fd2xK8 --all # only the forms filed in that folder
702
704
  jinshuju entry list --form Kp7mQ2 --labels # carry the field label with each value, saving a form read
703
705
  jinshuju table move Vn4xR8 --folder Nf7mDC # a table only goes into a kind=table folder
704
706
  jinshuju table create --json @t.json --with-default-entries # seed a few blank rows, as the web UI does
@@ -7,6 +7,29 @@ import { bodyChoices, call, choices } from '../spec.js';
7
7
  import { isRecord } from '../values.js';
8
8
  import { basename } from 'node:path';
9
9
  import { upload } from './upload.js';
10
+ /**
11
+ * How many calls one `entry search` makes before handing the rest back. Each
12
+ * call searches for as long as the API allows, so this bounds the command's own
13
+ * time; a search still unfinished answers its next for the caller to go on.
14
+ */
15
+ const SEARCH_ROUNDS = 6;
16
+ /**
17
+ * A search over every form in reach comes back a part at a time: each answer
18
+ * names the forms it covered and, when time ran out, a cursor to the rest. The
19
+ * parts are put together into one answer, so "absent" still means "searched,
20
+ * no match" for every form the cursor walked past.
21
+ */
22
+ async function searchAll(client, query) {
23
+ let page = (await client.request(call('searchEntriesAcrossForms', { query })));
24
+ const forms = [...(page.forms ?? [])];
25
+ let total = page.total ?? 0;
26
+ for (let round = 1; round < SEARCH_ROUNDS && page.next; round += 1) {
27
+ page = (await client.request(call('searchEntriesAcrossForms', { query: { ...query, next: page.next } })));
28
+ forms.push(...(page.forms ?? []));
29
+ total += page.total ?? 0;
30
+ }
31
+ return { ...page, total, forms };
32
+ }
10
33
  /** The api_codes a --fields named, for `call` to spell as the parameter says: comma-joined, as the spec has it. */
11
34
  function list(value) {
12
35
  const values = value;
@@ -421,8 +444,10 @@ export const ENTRY = [
421
444
  'never returned. Read what you need afterwards with `entry get` or `entry list --keyword`. ' +
422
445
  'A form that matched nothing is left out, so a token you named and cannot find was searched ' +
423
446
  'and matched nothing. A form that could not be searched is listed with a reason instead — ' +
424
- '"not searched" is not "no match". Name up to 10 containers, or name none and let ' +
425
- '--scope-filter describe them; the call is refused rather than truncated when more than 10 match.',
447
+ '"not searched" is not "no match". Name up to 10 containers, or name none and every form in ' +
448
+ 'reach is searched, narrowed by --scope-filter: the API searches as many as its time allows and ' +
449
+ 'this keeps calling it until all are done. If it still stops short, unsearched_count says how ' +
450
+ 'many forms were not searched (they may hold matches) and --next <next> carries on from there.',
426
451
  args: [{ name: 'keyword', required: true, description: 'The text to search for' }],
427
452
  options: [
428
453
  ...CONTAINER_LIST_OPTIONS,
@@ -432,28 +457,33 @@ export const ENTRY = [
432
457
  repeatable: true,
433
458
  placeholder: "'<field> <op> [value]'",
434
459
  description: 'Which forms to search, not which entries match. Takes form_name, created_at, ' +
435
- 'last_entry_created_at, entries_count. Empty forms are skipped unless you say otherwise'
460
+ 'last_entry_created_at, entries_count, folder_token. Empty forms are skipped unless you say otherwise'
461
+ },
462
+ {
463
+ name: '--next',
464
+ type: 'string',
465
+ placeholder: '<next>',
466
+ description: 'Carry on a search that stopped short, from the next it answered'
436
467
  },
437
468
  MINE_OPTION
438
469
  ],
439
- request: (input) => {
470
+ run: async (input, client) => {
440
471
  const { tokens } = namedContainers(input.options);
441
472
  if (input.options.mine) {
442
- refuseWithMine(input, ['table', 'scope_filter']);
443
- return call('searchMySubmittedEntries', {
444
- query: {
445
- keyword: input.args.keyword,
446
- form_tokens: tokens.length > 0 ? tokens : undefined
447
- }
448
- });
473
+ refuseWithMine(input, ['table', 'scope_filter', 'next']);
474
+ return client.request(call('searchMySubmittedEntries', {
475
+ query: { keyword: input.args.keyword, form_tokens: tokens.length > 0 ? tokens : undefined }
476
+ }));
477
+ }
478
+ if (tokens.length > 0 && input.options.next !== undefined) {
479
+ throw new UsageError('--next carries on a search over every form in reach; it does not take --form or --table');
449
480
  }
450
481
  const scope = input.options.scope_filter ?? [];
451
- return call('searchEntriesAcrossForms', {
452
- query: {
453
- keyword: input.args.keyword,
454
- form_tokens: tokens.length > 0 ? tokens : undefined,
455
- filters: scope.length > 0 ? JSON.stringify(scope.map(parseFilter)) : undefined
456
- }
482
+ return searchAll(client, {
483
+ keyword: input.args.keyword,
484
+ form_tokens: tokens.length > 0 ? tokens : undefined,
485
+ filters: scope.length > 0 ? JSON.stringify(scope.map(parseFilter)) : undefined,
486
+ next: input.options.next
457
487
  });
458
488
  },
459
489
  examples: [
@@ -1,5 +1,5 @@
1
1
  import { FILTER_OPTION, FILTERS_OPTION, JSON_OPTION, MINE_OPTION, PAGINATION_OPTIONS, SORT_OPTION, UsageError } from '../options.js';
2
- import { FOLDER_OPTION, LISTING, PREVIEW_OPTION, YES_OPTION, confirmed, filters, given, keywords, overriding, paging, payload, refuseWithMine, requiredOption, sort, trash } from './shared.js';
2
+ import { FOLDER_FILTER_OPTION, FOLDER_OPTION, LISTING, PREVIEW_OPTION, YES_OPTION, confirmed, filters, given, keywords, overriding, paging, payload, refuseWithMine, requiredOption, sort, trash } from './shared.js';
3
3
  import { validateContainerPayload } from '../payload.js';
4
4
  import { bodyChoices, bodyKeys, call } from '../spec.js';
5
5
  import { readableSize, upload, uploadedId, uploadFormImage } from './upload.js';
@@ -442,7 +442,8 @@ export const FORM = [
442
442
  {
443
443
  path: ['form', 'list'],
444
444
  summary: 'List forms',
445
- description: 'Filters act on the form itself: form_name, created_at, last_entry_created_at, entries_count.',
445
+ description: 'Filters act on the form itself: form_name, created_at, last_entry_created_at, entries_count, ' +
446
+ 'folder_token (or --folder).',
446
447
  options: [
447
448
  {
448
449
  name: '--name',
@@ -452,6 +453,7 @@ export const FORM = [
452
453
  description: 'Match forms whose name contains the keyword, repeatable'
453
454
  },
454
455
  { name: '--with-transactions', type: 'boolean', description: "Carry each payment form's collected totals" },
456
+ FOLDER_FILTER_OPTION,
455
457
  MINE_OPTION,
456
458
  FILTER_OPTION,
457
459
  FILTERS_OPTION,
@@ -460,7 +462,7 @@ export const FORM = [
460
462
  ],
461
463
  request: (input) => {
462
464
  if (input.options.mine) {
463
- refuseWithMine(input, ['name', 'with_transactions', 'filter', 'filters', 'sort']);
465
+ refuseWithMine(input, ['name', 'with_transactions', 'folder', 'filter', 'filters', 'sort']);
464
466
  return call('listMyFilledForms', { query: paging(input) });
465
467
  }
466
468
  return call('listForms', {
@@ -477,7 +479,8 @@ export const FORM = [
477
479
  examples: [
478
480
  'jinshuju form list --name 报名',
479
481
  'jinshuju form list --sort entries_count:desc --limit 10',
480
- 'jinshuju form list --mine'
482
+ 'jinshuju form list --mine',
483
+ 'jinshuju form list --folder Fd2xK8 --all'
481
484
  ]
482
485
  },
483
486
  {
@@ -126,11 +126,21 @@ export const COMMANDS = [
126
126
  ...ARTIFACT
127
127
  ];
128
128
  /**
129
- * Words that name the same verb. Some resources delete and others remove, after
130
- * what the API calls it, and nobody remembers which; either one reaches the
131
- * command, and the one written out still wins where both exist.
129
+ * Words that name the same verb. Some resources delete and others remove, some
130
+ * edit and others update, after what the API calls it, and nobody remembers
131
+ * which; any word of a group reaches the command, and the one written out still
132
+ * wins where both exist. No resource has two of one group, and every update or
133
+ * edit changes only what it names, so a synonym never lands on a command that
134
+ * does more than the word said.
132
135
  */
133
- const SAME_VERB = { delete: 'remove', remove: 'delete' };
136
+ const VERB_GROUPS = [
137
+ ['delete', 'remove'],
138
+ ['create', 'add'],
139
+ ['update', 'edit'],
140
+ ['get', 'show'],
141
+ ['list', 'ls']
142
+ ];
143
+ const SAME_VERB = new Map(VERB_GROUPS.flatMap((group) => group.map((word) => [word, group.filter((other) => other !== word)])));
134
144
  /** The command whose path the words begin with, longest match first; a spelled-out match beats a synonym. */
135
145
  export function findCommand(words, commands = COMMANDS) {
136
146
  let best;
@@ -138,7 +148,7 @@ export function findCommand(words, commands = COMMANDS) {
138
148
  for (const command of commands) {
139
149
  if (command.path.length > words.length)
140
150
  continue;
141
- const parts = command.path.map((part, index) => words[index] === part ? 'exact' : SAME_VERB[words[index]] === part ? 'synonym' : 'miss');
151
+ const parts = command.path.map((part, index) => words[index] === part ? 'exact' : SAME_VERB.get(words[index])?.includes(part) ? 'synonym' : 'miss');
142
152
  if (parts.includes('miss'))
143
153
  continue;
144
154
  const exact = !parts.includes('synonym');
@@ -13,6 +13,8 @@ export declare function onContainer(input: CommandInput, form: OperationId, tabl
13
13
  /** The conditions themselves. A read sends them as a query string, a write as a body. */
14
14
  export declare function filterConditions(input: CommandInput): unknown;
15
15
  export declare function filters(input: CommandInput): string | undefined;
16
+ /** Only what is filed in these folders, as the caller sees them. */
17
+ export declare const FOLDER_FILTER_OPTION: OptionSpec;
16
18
  export declare function sort(input: CommandInput, key: 'api_code' | 'field'): string | undefined;
17
19
  export declare function paging(input: CommandInput): {
18
20
  limit?: number;
@@ -59,8 +59,21 @@ function withOperator(condition) {
59
59
  }
60
60
  export function filters(input) {
61
61
  const conditions = filterConditions(input);
62
- return conditions === undefined ? undefined : JSON.stringify(conditions);
62
+ const folders = input.options.folder ?? [];
63
+ if (folders.length === 0)
64
+ return conditions === undefined ? undefined : JSON.stringify(conditions);
65
+ // A folder is one more condition on the listing, AND-ed with the rest.
66
+ const rest = conditions === undefined ? [] : [conditions].flat();
67
+ return JSON.stringify([...rest, { field: 'folder_token', operator: 'any_in', value: folders }]);
63
68
  }
69
+ /** Only what is filed in these folders, as the caller sees them. */
70
+ export const FOLDER_FILTER_OPTION = {
71
+ name: '--folder',
72
+ type: 'string',
73
+ repeatable: true,
74
+ placeholder: '<folder>',
75
+ description: 'Only what is filed in this folder (its folder_token), repeatable'
76
+ };
64
77
  export function sort(input, key) {
65
78
  const rules = input.options.sort ?? [];
66
79
  if (rules.length === 0)
@@ -1,6 +1,6 @@
1
1
  import { FILTER_OPTION, FILTERS_OPTION, JSON_OPTION, PAGINATION_OPTIONS, SORT_OPTION } from '../options.js';
2
2
  import { bodyChoices, call } from '../spec.js';
3
- import { FOLDER_OPTION, LISTING, PREVIEW_OPTION, YES_OPTION, filters, keywords, overriding, paging, payload, sort, trash } from './shared.js';
3
+ import { FOLDER_FILTER_OPTION, FOLDER_OPTION, LISTING, PREVIEW_OPTION, YES_OPTION, filters, keywords, overriding, paging, payload, sort, trash } from './shared.js';
4
4
  import { validateContainerPayload } from '../payload.js';
5
5
  /**
6
6
  * The field types a table column may be, which is a third of what a form takes.
@@ -20,6 +20,7 @@ export const TABLE = [
20
20
  placeholder: '<kw>',
21
21
  description: 'Match tables whose name contains the keyword'
22
22
  },
23
+ FOLDER_FILTER_OPTION,
23
24
  FILTER_OPTION,
24
25
  FILTERS_OPTION,
25
26
  SORT_OPTION,
@@ -1791,7 +1791,8 @@ export const OPERATIONS = {
1791
1791
  in: 'query',
1792
1792
  content: 'application/json',
1793
1793
  schema: { type: 'array', items: { $ref: 'ScopeCondition' } }
1794
- }
1794
+ },
1795
+ { name: 'next', in: 'query', schema: { type: 'string' } }
1795
1796
  ]
1796
1797
  },
1797
1798
  searchMySubmittedEntries: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jinshuju/cli",
3
- "version": "0.31.0",
3
+ "version": "0.33.0",
4
4
  "description": "Command line interface for Jinshuju Open API v1",
5
5
  "homepage": "https://github.com/jinshuju/cli#readme",
6
6
  "bugs": {