flow: update `better-sqlite3` types for v7.0.0 (#1770)

Summary:
I first wrote these type definitions for v4.x.x. The library API has
changed since then. This patch updates the type definitions to match the
current API as of the v7.0.0 release:
<https://github.com/JoshuaWise/better-sqlite3/blob/v7.0.0/docs/api.md>

There are breaking changes, but none among functions that we use. On the
other hand, there are new features that will be useful to us.

Test Plan:
Running `yarn flow` still passes. There may be some errors among typing
of functions that we don’t actually use (particularly `aggregate`, which
is more complicated than the others). If so, we can cross those bridges
when we come to them.

wchargin-branch: flow-better-sqlite3-v7-api
This commit is contained in:
William Chargin 2020-04-26 13:08:51 -07:00 committed by GitHub
parent c18244bf19
commit 1f8925fe77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 26 deletions

View File

@ -13,15 +13,24 @@ declare class bettersqlite3$Database {
options?: bettersqlite3$Database$ConstructorOptions options?: bettersqlite3$Database$ConstructorOptions
): void; ): void;
prepare(source: string): bettersqlite3$Statement; prepare(source: string): bettersqlite3$Statement;
exec(source: string): this; transaction<R, A>(f: (...A) => R): (...A) => R;
transaction(sources: $ReadOnlyArray<string>): bettersqlite3$Transaction; pragma(pragma: string, options?: bettersqlite3$Database$PragmaOptions): any;
pragma(pragma: string, simplify?: boolean): any; backup(
checkpoint(databaseName?: string): this; destination: string,
register(fn: (...args: any[]) => any): void; options?: bettersqlite3$Database$BackupOptions
register( ): Promise<bettersqlite3$Database$BackupProgress>;
options: bettersqlite3$Database$RegisterOptions, function(name: string, fn: (...args: any[]) => any): this;
function(
name: string,
options: bettersqlite3$Database$FunctionOptions,
fn: (...args: any[]) => any fn: (...args: any[]) => any
): void; ): this;
aggregate<T>(
name: string,
options: bettersqlite3$Database$AggregateOptions<T>
): this;
loadExtension(path: strin, entryPoint?: string): this;
exec(source: string): this;
close(): this; close(): this;
defaultSafeIntegers(toggleState?: boolean): this; defaultSafeIntegers(toggleState?: boolean): this;
@ -29,18 +38,44 @@ declare class bettersqlite3$Database {
} }
declare type bettersqlite3$Database$ConstructorOptions = { declare type bettersqlite3$Database$ConstructorOptions = {
+memory?: boolean,
+readonly?: boolean, +readonly?: boolean,
+fileMustExist?: boolean +fileMustExist?: boolean,
+timeout?: number,
+verbose?: ?(sqlText: string) => void
}; };
declare type bettersqlite3$Database$RegisterOptions = { declare type bettersqlite3$Database$PragmaOptions = {
+name?: string, +simple?: boolean
};
declare type bettersqlite3$Database$BackupOptions = {
+attahced?: string,
+progress?: (bettersqlite3$Database$BackupProgress) => number
};
declare type bettersqlite3$Database$BackupProgress = {
+totalPages: number,
+remainingPages: number
};
declare type bettersqlite3$Database$FunctionOptions = {
+varargs?: boolean, +varargs?: boolean,
+deterministic?: boolean, +deterministic?: boolean,
+safeIntegers?: boolean +safeIntegers?: boolean
}; };
// The actual contract to `aggregate` is more complicated and dynamic
// than can be expressed with Flow types. This is a "best-effort,
// happy-path" type definition that is on the right track but will not
// catch all errors. Consult the `better-sqlite3` API docs for the
// source of truth.
declare type bettersqlite3$Database$AggregateOptions<T> = {
+step: (T, bettersqlite3$BoundValue) => T,
+inverse?: (T, bettersqlite3$BoundValue) => T,
+start?: T | (() => T),
+result?: (T) => bettersqlite3$BoundValue
};
// Functions that accept bound parameters take positional parameters, // Functions that accept bound parameters take positional parameters,
// named parameters (passed as an object), or a combination of the two. // named parameters (passed as an object), or a combination of the two.
// The named parameters can be placed anywhere in the argument list, but // The named parameters can be placed anywhere in the argument list, but
@ -72,25 +107,15 @@ declare class bettersqlite3$Statement {
all(...params: bettersqlite3$BoundParameter[]): any[]; all(...params: bettersqlite3$BoundParameter[]): any[];
iterate(...params: bettersqlite3$BoundParameter[]): Iterator<any>; iterate(...params: bettersqlite3$BoundParameter[]): Iterator<any>;
pluck(toggleState?: boolean): this; pluck(toggleState?: boolean): this;
expand(toggleState?: boolean): this;
raw(toggleState?: boolean): this;
columns(toggleState?: boolean): this;
bind(...params: bettersqlite3$BoundParameter[]): this; bind(...params: bettersqlite3$BoundParameter[]): this;
safeIntegers(toggleState?: boolean): this; safeIntegers(toggleState?: boolean): this;
} }
declare class bettersqlite3$Transaction {
+database: bettersqlite3$Database;
+source: string;
constructor(db: bettersqlite3$Database, sources: string[]): void;
run(...params: any[]): bettersqlite3$RunResult;
bind(...params: any[]): this;
safeIntegers(toggleState?: boolean): this;
}
declare interface bettersqlite3$RunResult { declare interface bettersqlite3$RunResult {
changes: number; changes: number;
// TODO: This is actually `Integer.IntLike` from npm/integer, but we
// don't have those typedefs. For now, `number` is a good
// approximation.
lastInsertRowid: number; lastInsertRowid: number;
} }
@ -107,7 +132,6 @@ declare module "better-sqlite3" {
declare export type BindingDictionary = bettersqlite3$BindingDictionary; declare export type BindingDictionary = bettersqlite3$BindingDictionary;
declare export type BoundParameter = bettersqlite3$BoundParameter; declare export type BoundParameter = bettersqlite3$BoundParameter;
declare export type Statement = bettersqlite3$Statement; declare export type Statement = bettersqlite3$Statement;
declare export type Transaction = bettersqlite3$Transaction;
declare export type RunResult = bettersqlite3$RunResult; declare export type RunResult = bettersqlite3$RunResult;
declare export type SqliteError = bettersqlite3$SqliteError; declare export type SqliteError = bettersqlite3$SqliteError;
declare module.exports: Class<bettersqlite3$Database>; declare module.exports: Class<bettersqlite3$Database>;