@otwdesign/db-core 1.4.1 → 1.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.
- package/README.md +38 -0
- package/dist/backup/api.js +2 -33
- package/dist/backup/api.js.map +1 -1
- package/dist/body.d.ts +17 -0
- package/dist/body.js +88 -0
- package/dist/body.js.map +1 -0
- package/dist/browser.d.ts +1 -0
- package/dist/drivers/mysql-connection.d.ts +101 -0
- package/dist/drivers/mysql-connection.js +204 -0
- package/dist/drivers/mysql-connection.js.map +1 -0
- package/dist/drivers/mysql-import.d.ts +22 -0
- package/dist/drivers/mysql-import.js +688 -0
- package/dist/drivers/mysql-import.js.map +1 -0
- package/dist/drivers/mysql.d.ts +11 -1
- package/dist/drivers/mysql.js +87 -97
- package/dist/drivers/mysql.js.map +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js.map +1 -1
- package/dist/handler.d.ts +31 -0
- package/dist/handler.js +93 -9
- package/dist/handler.js.map +1 -1
- package/dist/import/api.d.ts +65 -0
- package/dist/import/api.js +1282 -0
- package/dist/import/api.js.map +1 -0
- package/dist/import/audit.d.ts +47 -0
- package/dist/import/audit.js +434 -0
- package/dist/import/audit.js.map +1 -0
- package/dist/import/charsets.d.ts +25 -0
- package/dist/import/charsets.js +39 -0
- package/dist/import/charsets.js.map +1 -0
- package/dist/import/frames.d.ts +26 -0
- package/dist/import/frames.js +158 -0
- package/dist/import/frames.js.map +1 -0
- package/dist/import/grants.d.ts +26 -0
- package/dist/import/grants.js +122 -0
- package/dist/import/grants.js.map +1 -0
- package/dist/import/protocol.d.ts +462 -0
- package/dist/import/protocol.js +72 -0
- package/dist/import/protocol.js.map +1 -0
- package/dist/import/shared.d.ts +3 -0
- package/dist/import/shared.js +9 -0
- package/dist/import/shared.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/types.d.ts +308 -2
- package/package.json +7 -2
- package/src/backup/api.ts +2 -33
- package/src/body.ts +94 -0
- package/src/browser.ts +24 -0
- package/src/drivers/mysql-connection.ts +282 -0
- package/src/drivers/mysql-import.ts +769 -0
- package/src/drivers/mysql.ts +96 -107
- package/src/errors.ts +2 -0
- package/src/handler.ts +126 -8
- package/src/import/api.ts +1530 -0
- package/src/import/audit.ts +467 -0
- package/src/import/charsets.ts +46 -0
- package/src/import/frames.ts +191 -0
- package/src/import/grants.ts +117 -0
- package/src/import/protocol.ts +540 -0
- package/src/import/shared.ts +8 -0
- package/src/index.ts +24 -0
- package/src/types.ts +319 -2
package/src/types.ts
CHANGED
|
@@ -52,6 +52,11 @@ export interface Capabilities {
|
|
|
52
52
|
* them with `session()`.
|
|
53
53
|
*/
|
|
54
54
|
backup?: boolean
|
|
55
|
+
/**
|
|
56
|
+
* SQL files imported a statement at a time, as the `mysql` client runs them: MySQL and MariaDB.
|
|
57
|
+
* The API sets it: a driver offers them with `importer()`.
|
|
58
|
+
*/
|
|
59
|
+
import?: boolean
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
/** What a schema-tree entry is: SQLite and MySQL have tables and views, PostgreSQL the rest too. */
|
|
@@ -317,6 +322,16 @@ export interface Driver {
|
|
|
317
322
|
* database in the host's own process) doesn't.
|
|
318
323
|
*/
|
|
319
324
|
readonly pgUrl?: string
|
|
325
|
+
/**
|
|
326
|
+
* A connection of its own for one import of an SQL file, open until its `end()`: what the file
|
|
327
|
+
* sets, locks and leaves uncommitted stays on it from one statement to the next. MySQL and
|
|
328
|
+
* MariaDB.
|
|
329
|
+
*
|
|
330
|
+
* `idleMs` is the longest the import may sit unused between two statements, in milliseconds
|
|
331
|
+
* (5 minutes, given none): the server is asked to keep the connection that long and a minute
|
|
332
|
+
* more, where its own setting would drop it sooner.
|
|
333
|
+
*/
|
|
334
|
+
importer?(options?: { idleMs?: number }): Promise<Importer>
|
|
320
335
|
close(): Promise<void>
|
|
321
336
|
}
|
|
322
337
|
|
|
@@ -339,6 +354,211 @@ export interface Session {
|
|
|
339
354
|
inTransaction?(): boolean
|
|
340
355
|
}
|
|
341
356
|
|
|
357
|
+
/**
|
|
358
|
+
* One import's connection. It is the file's alone: no other request runs on it, and it never goes
|
|
359
|
+
* back to a pool. It can't run two statements in one query, whatever the connection's URL says,
|
|
360
|
+
* and it proved that to itself when it opened.
|
|
361
|
+
*
|
|
362
|
+
* A kill names the connection, not a statement: it stops whatever runs there when it arrives. So
|
|
363
|
+
* while one is on its way, from `stop()` or from a statement's own time limit, nothing else is
|
|
364
|
+
* sent on the connection: `use()`, `run()` and `state()` reject, and `end()` waits for the kill
|
|
365
|
+
* to be answered before its rollback. A kill the server takes after the statement has already
|
|
366
|
+
* ended stops nothing later.
|
|
367
|
+
*/
|
|
368
|
+
export interface Importer {
|
|
369
|
+
/** The server it reached, and its session as it opened. */
|
|
370
|
+
readonly target: ImportTarget
|
|
371
|
+
/**
|
|
372
|
+
* Selects `database`. The answer's `database` is what the server then says is selected, to
|
|
373
|
+
* compare with the name the person typed.
|
|
374
|
+
*/
|
|
375
|
+
use(database: string): Promise<ImportSession>
|
|
376
|
+
/**
|
|
377
|
+
* One statement of the file, sent as written and as one query: never split, never prepared (a
|
|
378
|
+
* `?` or a `:name` is the file's own), and as UTF-8 whatever character set the file has set.
|
|
379
|
+
* It answers when the server has: with what the statement did, or with the error the server
|
|
380
|
+
* refused it with, which is an answer and not a rejection. Past `timeoutMs` the statement is
|
|
381
|
+
* stopped as `stop()` stops it, and answers as refused.
|
|
382
|
+
*
|
|
383
|
+
* A statement longer than the server takes in one packet (its `max_allowed_packet`), or than
|
|
384
|
+
* 16,777,208 bytes, is answered as refused too, with errno 1153, and is not sent: sent, it
|
|
385
|
+
* would cost the connection. So is a statement the server would read otherwise than the
|
|
386
|
+
* browser cut it, with errno 1235: one with text beyond ASCII while the file has the session
|
|
387
|
+
* read statements in gbk, big5, sjis or another set of several bytes a character but UTF-8's,
|
|
388
|
+
* and any statement at all under swe7 or a set the import doesn't know. Both errors say
|
|
389
|
+
* `unsent`.
|
|
390
|
+
*
|
|
391
|
+
* It answers only once every kill sent while it ran has been answered too, its own time
|
|
392
|
+
* limit's included: a statement that ends just as it is stopped answers as it ended, and by
|
|
393
|
+
* then no kill is left on its way to land on the next statement.
|
|
394
|
+
*
|
|
395
|
+
* Whatever the server answered for the statement is what `run()` answers, even when the
|
|
396
|
+
* connection is gone by then: a statement that ran is answered for as run, with `lost` in the
|
|
397
|
+
* place of the session it could no longer tell (see `ImportRanLast`). Count that statement.
|
|
398
|
+
*
|
|
399
|
+
* It rejects only when the server gave the statement no answer: the connection went under it
|
|
400
|
+
* or before it (a ConnectionError, and then whether it ran is in doubt, unless `end()` says
|
|
401
|
+
* otherwise), a statement is already running on the connection, a kill is on its way, or the
|
|
402
|
+
* import has ended.
|
|
403
|
+
*/
|
|
404
|
+
run(sql: string, options: { timeoutMs: number }): Promise<ImportResult>
|
|
405
|
+
/**
|
|
406
|
+
* Asks the server what state the session is in, with a statement that does nothing. For after
|
|
407
|
+
* a refused statement: an error says nothing of whether the transaction it ran in is still
|
|
408
|
+
* open, and a deadlock or a lock timeout has rolled it back.
|
|
409
|
+
*/
|
|
410
|
+
state(): Promise<ImportSession>
|
|
411
|
+
/**
|
|
412
|
+
* Stops the statement that is running, with KILL QUERY from a connection of its own, and
|
|
413
|
+
* answers when the server has taken the kill: the statement's own `run()` then answers, as
|
|
414
|
+
* refused unless it had already ended. With nothing running it sends nothing, and a second
|
|
415
|
+
* call while one is on its way waits for that one.
|
|
416
|
+
*/
|
|
417
|
+
stop(): Promise<void>
|
|
418
|
+
/**
|
|
419
|
+
* Rolls back a transaction left open, as the `mysql` client does at the end of a file, and
|
|
420
|
+
* closes the connection. If a statement is still running, one `stop()` could not end, the
|
|
421
|
+
* connection is cut under it instead. After it, nothing runs; a second call answers as the
|
|
422
|
+
* first did.
|
|
423
|
+
*
|
|
424
|
+
* It can take long: a statement that runs is stopped first, with ten seconds for the kill's
|
|
425
|
+
* answer and then two minutes for the statement's own, and the rollback has two minutes.
|
|
426
|
+
*/
|
|
427
|
+
end(): Promise<ImportClosed>
|
|
428
|
+
/**
|
|
429
|
+
* `listener` is told, once, when the connection is gone other than by `end()`: the server
|
|
430
|
+
* dropped it, or the network did. Straight away if it already is.
|
|
431
|
+
*
|
|
432
|
+
* It is told before a `run()` that was waiting answers. That `run()` still answers, or
|
|
433
|
+
* rejects, and what it answers still counts: a statement the server had answered for is
|
|
434
|
+
* answered for as it ran, with the same error as its `lost`.
|
|
435
|
+
*/
|
|
436
|
+
onLost(listener: (error: Error) => void): void
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** The server an import's connection reached. */
|
|
440
|
+
export interface ImportTarget {
|
|
441
|
+
/** `MySQL` or `MariaDB`. */
|
|
442
|
+
product: string
|
|
443
|
+
/** Such as `8.4.3` or `11.4.2`. */
|
|
444
|
+
version: string
|
|
445
|
+
/**
|
|
446
|
+
* The connection's id on the server, CONNECTION_ID(): what PROCESSLIST shows and KILL names.
|
|
447
|
+
* After a restart, a new import can wait until this one has gone from the server.
|
|
448
|
+
*/
|
|
449
|
+
thread: number
|
|
450
|
+
/** The session's sql_mode as it opened, whole. */
|
|
451
|
+
sqlMode: string
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* An import's session after a statement, as the server reports it with the statement's result:
|
|
456
|
+
* never worked out from the statements' text.
|
|
457
|
+
*/
|
|
458
|
+
export interface ImportSession {
|
|
459
|
+
/** A transaction is open. */
|
|
460
|
+
inTransaction: boolean
|
|
461
|
+
/** sql_mode has NO_BACKSLASH_ESCAPES: a backslash in a string is a backslash. */
|
|
462
|
+
noBackslashEscapes: boolean
|
|
463
|
+
/** sql_mode has ANSI_QUOTES: double quotes hold a name, not a string. */
|
|
464
|
+
ansiQuotes: boolean
|
|
465
|
+
/** The database selected, which a `USE` in the file changes; null with none. */
|
|
466
|
+
database: string | null
|
|
467
|
+
/**
|
|
468
|
+
* The character set the session reads statements in (character_set_client), by its name in
|
|
469
|
+
* small letters, which a `SET NAMES` in the file changes.
|
|
470
|
+
*/
|
|
471
|
+
characterSet: string
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* What one statement of an import did, or the error the server refused it with. Tell them apart
|
|
476
|
+
* by `error`, and then by `lost`.
|
|
477
|
+
*/
|
|
478
|
+
export type ImportResult = ImportRan | ImportRanLast | ImportRefused
|
|
479
|
+
|
|
480
|
+
/** What a statement that ran did, as the server answered for it. */
|
|
481
|
+
export interface ImportDone {
|
|
482
|
+
/** Rows it changed, as the server counts them; 0 for a statement that changes none. */
|
|
483
|
+
affected: number
|
|
484
|
+
/**
|
|
485
|
+
* How many warnings the server raised. Always 0 for a statement that returned rows: mysql2
|
|
486
|
+
* does not pass on the count that ends them.
|
|
487
|
+
*/
|
|
488
|
+
warnings: number
|
|
489
|
+
/**
|
|
490
|
+
* Rows it returned, counted and dropped as they arrived: an import shows no results. A
|
|
491
|
+
* statement that is nothing on this server, such as a versioned comment for another (errno
|
|
492
|
+
* 1065), ran with nothing changed and nothing returned.
|
|
493
|
+
*/
|
|
494
|
+
rows: number
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** A statement that ran, and the session after it. */
|
|
498
|
+
export interface ImportRan extends ImportDone, ImportSession {
|
|
499
|
+
error?: undefined
|
|
500
|
+
lost?: undefined
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* A statement that ran, and the last one this connection will: the connection went after the
|
|
505
|
+
* server had answered for it, before it could say what its session then was. The statement is
|
|
506
|
+
* done, as much as any that ran, and counts; nothing after it can be split or sent.
|
|
507
|
+
*
|
|
508
|
+
* Whether it is kept is the server's to say, as after any lost connection: outside a
|
|
509
|
+
* transaction it is, and inside one it went with the transaction.
|
|
510
|
+
*/
|
|
511
|
+
export interface ImportRanLast extends ImportDone {
|
|
512
|
+
error?: undefined
|
|
513
|
+
/** Why the connection is gone: the error `onLost` listeners were told, and later calls reject with. */
|
|
514
|
+
lost: Error
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** A statement the server refused, or one that was stopped. `state()` tells the session after it. */
|
|
518
|
+
export interface ImportRefused {
|
|
519
|
+
error: ImportError
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** The server's error, as the `mysql` client prints it: `ERROR 1062 (23000): Duplicate entry`. */
|
|
523
|
+
export interface ImportError {
|
|
524
|
+
/** The server's error number, such as 1062; 1317 for a statement that was stopped. */
|
|
525
|
+
errno: number
|
|
526
|
+
/** Its SQLSTATE, such as `23000`. */
|
|
527
|
+
code: string
|
|
528
|
+
message: string
|
|
529
|
+
/**
|
|
530
|
+
* The import refused the statement itself and never sent it, with the server's number for
|
|
531
|
+
* such a refusal (1153, too long; 1235, a character set that would read it otherwise): the
|
|
532
|
+
* server changed nothing. Absent on every error the server gave.
|
|
533
|
+
*/
|
|
534
|
+
unsent?: true
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** How an import's connection closed. */
|
|
538
|
+
export interface ImportClosed {
|
|
539
|
+
/**
|
|
540
|
+
* What became of a transaction the file left open, as far as the server said:
|
|
541
|
+
* - `none`: none was open when the server was asked, at the end.
|
|
542
|
+
* - `rolled-back`: one was open, and the server took the rollback.
|
|
543
|
+
* - `abandoned`: the connection went, or was closed, with no rollback taken: it was lost
|
|
544
|
+
* before the server could be asked, or the server refused, as it does inside an XA
|
|
545
|
+
* transaction. Whether one was still open by then is not known here. A server undoes what
|
|
546
|
+
* is open on a connection that goes, with one exception: an XA transaction already
|
|
547
|
+
* prepared stays, locks and all, until an `XA COMMIT` or `XA ROLLBACK` names it (`XA
|
|
548
|
+
* RECOVER` lists those).
|
|
549
|
+
*
|
|
550
|
+
* MySQL takes a transaction off its connection at `XA PREPARE` (8.0.29 on, unless
|
|
551
|
+
* `xa_detach_on_prepare` is off): from then on none is open here, and it is the server's.
|
|
552
|
+
*/
|
|
553
|
+
transaction: 'none' | 'rolled-back' | 'abandoned'
|
|
554
|
+
/**
|
|
555
|
+
* A statement of the file's was still unanswered when the connection went, or was cut under
|
|
556
|
+
* it: whether that statement finished is not known. False when every statement sent was
|
|
557
|
+
* answered, a last one `run()` answered for with `lost` included.
|
|
558
|
+
*/
|
|
559
|
+
inDoubt: boolean
|
|
560
|
+
}
|
|
561
|
+
|
|
342
562
|
/** Who is asking, as the host's `authorize()` says. */
|
|
343
563
|
export interface Viewer {
|
|
344
564
|
/** Recorded in the audit log, such as a user id or an email. */
|
|
@@ -357,11 +577,108 @@ export interface AuditEvent {
|
|
|
357
577
|
connection: string
|
|
358
578
|
/** The database it ran on, when it wasn't the connection's own. */
|
|
359
579
|
database?: string
|
|
360
|
-
action:
|
|
361
|
-
|
|
580
|
+
action:
|
|
581
|
+
'query' | 'rows' | 'count' | 'apply' | 'export' | 'cancel' | 'backup' | 'restore' | 'import'
|
|
582
|
+
/**
|
|
583
|
+
* The SQL run, or for `rows`, the table read. For `import`, a batch's statements, each ended
|
|
584
|
+
* with `;` and a new line, logged before the first is sent. The other events of an import say
|
|
585
|
+
* in words what they are about.
|
|
586
|
+
*
|
|
587
|
+
* An import's statements are each whole, a `CREATE USER` with its password included. One kind
|
|
588
|
+
* is shortened, to keep a dump's rows out of the log: an INSERT or REPLACE of more than 1,024
|
|
589
|
+
* characters, not counting white space around it and between its first words, that is nothing
|
|
590
|
+
* but rows of plain values. It is logged as its start,
|
|
591
|
+
* `… [its length in bytes, sha256 its SHA-256] …`, and its end:
|
|
592
|
+
* - its start is 256 characters from its first word, with the words up to the table's name
|
|
593
|
+
* one space apart, whatever white space the file has before and between them, so it always
|
|
594
|
+
* says which table;
|
|
595
|
+
* - its end is the last 256 characters before any white space it ends on.
|
|
596
|
+
*
|
|
597
|
+
* Nothing but rows means: after the table's name, the columns' names in brackets or none,
|
|
598
|
+
* `VALUES`, and rows of numbers, strings in single quotes, `NULL`, `TRUE`, `FALSE`, `DEFAULT`
|
|
599
|
+
* and hex or bit values, as mysqldump writes them, to the statement's last character. An
|
|
600
|
+
* INSERT or REPLACE with anything else in it is whole: a `SELECT`, a function, a variable or
|
|
601
|
+
* another column in a row, `SET`, `ON DUPLICATE KEY UPDATE`, `RETURNING`, a comment anywhere.
|
|
602
|
+
* So what is left out of the log is data the file holds, never something the statement read
|
|
603
|
+
* from the server. It is whole, too:
|
|
604
|
+
* - when the table's name can't be read, or doesn't end within what is logged of the start;
|
|
605
|
+
* - when a string or a name in backticks could end elsewhere in a character set the file
|
|
606
|
+
* chose (`SET NAMES gbk`): a backtick, or backslashes before a quote, right after a
|
|
607
|
+
* character beyond ASCII. A table or a column named so has all its rows in the log.
|
|
608
|
+
*/
|
|
362
609
|
statement: string
|
|
363
610
|
durationMs: number
|
|
364
611
|
/** Rows returned or changed. */
|
|
365
612
|
rows?: number
|
|
366
613
|
error?: string
|
|
614
|
+
/** For `import`: which import, and which part of it. */
|
|
615
|
+
import?: ImportAudit
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* What an audit event says of an import. Every event of one import has its `thread`, and the
|
|
620
|
+
* event's `database` is the one the import started in.
|
|
621
|
+
*/
|
|
622
|
+
export type ImportAudit = ImportAuditStart | ImportAuditBatch | ImportAuditEnd
|
|
623
|
+
|
|
624
|
+
/** An import was opened: its connection is made, and nothing of the file has run. */
|
|
625
|
+
export interface ImportAuditStart {
|
|
626
|
+
step: 'start'
|
|
627
|
+
/** The import's connection on the server, CONNECTION_ID(). */
|
|
628
|
+
thread: number
|
|
629
|
+
/** The file as the browser said it is: the server never sees it whole. */
|
|
630
|
+
file: {
|
|
631
|
+
name: string
|
|
632
|
+
/** In bytes, as chosen: a `.sql.gz`'s compressed size. */
|
|
633
|
+
size: number
|
|
634
|
+
/** How many statements the browser's check counted. */
|
|
635
|
+
statements: number
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* A batch of an import. It is logged twice: before its first statement is sent, with its
|
|
641
|
+
* statements as the event's `statement`, and when it ends, with `ended`, `done` and the rows it
|
|
642
|
+
* changed.
|
|
643
|
+
*/
|
|
644
|
+
export interface ImportAuditBatch {
|
|
645
|
+
step: 'batch'
|
|
646
|
+
thread: number
|
|
647
|
+
/** The number of the batch's first statement in the file, counting from 1. */
|
|
648
|
+
start: number
|
|
649
|
+
/** How many statements the batch holds. */
|
|
650
|
+
count: number
|
|
651
|
+
/**
|
|
652
|
+
* The number of a refused statement the batch leaves out, when the person chose to skip it:
|
|
653
|
+
* it is in none of the batch's statements, and counts as done with those before it.
|
|
654
|
+
*/
|
|
655
|
+
skip?: number
|
|
656
|
+
/**
|
|
657
|
+
* Absent before the batch runs. Then how it ended: `done`, nothing in it failed; `failed`, a
|
|
658
|
+
* statement was refused (the event's `error`); `lost`, the connection went; `stopped`, the
|
|
659
|
+
* import was ended while it ran.
|
|
660
|
+
*/
|
|
661
|
+
ended?: 'done' | 'failed' | 'lost' | 'stopped'
|
|
662
|
+
/** With `ended`: the number of the last statement done, which the next batch starts after. */
|
|
663
|
+
done?: number
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** An import was closed, and its connection with it. */
|
|
667
|
+
export interface ImportAuditEnd {
|
|
668
|
+
step: 'end'
|
|
669
|
+
thread: number
|
|
670
|
+
/**
|
|
671
|
+
* Why: `asked`, by import-end; `failed`, a statement was refused and the import could not go
|
|
672
|
+
* on after it; `pause`, nobody chose in time after a refused statement; `idle`, no request
|
|
673
|
+
* came for it in time; `unheard`, the request that opened it had gone before it could be
|
|
674
|
+
* answered, so nobody could have named it; `closed`, the host closed the tool; `lost`, the
|
|
675
|
+
* connection went.
|
|
676
|
+
*/
|
|
677
|
+
why: 'asked' | 'failed' | 'pause' | 'idle' | 'unheard' | 'closed' | 'lost'
|
|
678
|
+
/** The number of the last statement done. */
|
|
679
|
+
done: number
|
|
680
|
+
/** A transaction the file left open was rolled back. */
|
|
681
|
+
rolledBack: boolean
|
|
682
|
+
/** The statement that was running and would not stop: whether it finished is not known. */
|
|
683
|
+
doubt?: number
|
|
367
684
|
}
|