Skip to content

Commit

Permalink
better-sqlite3: update for version 7
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Aug 13, 2021
1 parent e35e062 commit 2434d01
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
23 changes: 10 additions & 13 deletions types/better-sqlite3/better-sqlite3-tests.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import Sqlite = require('better-sqlite3');

const integer = Sqlite.Integer(1);
const err = new Sqlite.SqliteError('ok', 'ok');
const result: Sqlite.RunResult = { changes: 1, lastInsertRowid: 1 };
const options: Sqlite.Options = { fileMustExist: true, memory: true, readonly: true };
const options: Sqlite.Options = { fileMustExist: true, readonly: true };
const registrationOptions: Sqlite.RegistrationOptions = {
deterministic: true,
safeIntegers: true,
varargs: true
};

let db: Sqlite.Database = Sqlite('.');
db = new Sqlite('.', { memory: true, verbose: () => {} });
let db: Sqlite.Database = Sqlite(':memory:');
db = new Sqlite(':memory:', { verbose: () => {} });
db.exec('CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL);');
db.exec('INSERT INTO test(name) VALUES("name");');
db.pragma('data_version', { simple: true });
db.checkpoint();
db.checkpoint('main');
db.table('vtable', {
columns: ['name'],
*rows() {
Expand Down Expand Up @@ -71,17 +68,17 @@ for (col of stmt.columns()) {
col.column;
col.type;
}
type BindParameters = [string, number];
const stmtWithBindTyped = db.prepare<BindParameters>('SELECT * FROM test WHERE name == ? and age = ?');
stmtWithBindTyped.run('alice', 20);
type BindParameters = [string, number, bigint];
const stmtWithBindTyped = db.prepare<BindParameters>('SELECT * FROM test WHERE name == ? and age = ? and id == ?');
stmtWithBindTyped.run('alice', 20, 1234n);
// note the following is technically legal according to the docs, but we do not have a way to express both typed args
// and variable tuples in typescript. If you want to group tuples, you must specify it that way in the prepare function
// https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#binding-parameters
// $ExpectError
stmtWithBindTyped.run(['alice', 20]);
interface NamedBindParameters { name: string; age: number; }
const stmtWithNamedBind = db.prepare<NamedBindParameters>('INSERT INTO test VALUES (@name, @age)');
stmtWithNamedBind.run({ name: 'bob', age: 20 });
stmtWithBindTyped.run(['alice', 20, 1234n]);
interface NamedBindParameters { name: string; age: number; id: bigint; }
const stmtWithNamedBind = db.prepare<NamedBindParameters>('INSERT INTO test VALUES (@name, @age, @id)');
stmtWithNamedBind.run({ name: 'bob', age: 20, id: 1234n });

const trans: Sqlite.Transaction = db.transaction((param) => stmt.all(param));
trans('name');
Expand Down
14 changes: 5 additions & 9 deletions types/better-sqlite3/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Type definitions for better-sqlite3 5.4
// Type definitions for better-sqlite3 7.4
// Project: http://github.com/JoshuaWise/better-sqlite3
// Definitions by: Ben Davies <https://github.com/Morfent>
// Mathew Rumsey <https://github.com/matrumz>
// Santiago Aguilar <https://github.com/sant123>
// Alessandro Vergani <https://github.com/loghorn>
// Andrew Kaiser <https://github.com/andykais>
// Mark Stewart <https://github.com/mrkstwrt>
// Evan Hahn <https://github.com/EvanHahn>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0

import Integer = require("integer");
// TypeScript Version: 4.3

type VariableArgFunction = (...params: any[]) => any;
type ArgumentTypes<F extends VariableArgFunction> = F extends (...args: infer A) => any
Expand Down Expand Up @@ -71,7 +70,6 @@ declare namespace BetterSqlite3 {
transaction<F extends VariableArgFunction>(fn: F): Transaction<F>;
exec(source: string): this;
pragma(source: string, options?: Database.PragmaOptions): any;
checkpoint(databaseName?: string): this;
function(name: string, cb: (...params: any[]) => any): this;
function(name: string, options: Database.RegistrationOptions, cb: (...params: any[]) => any): this;
aggregate(name: string, options: Database.AggregateOptions): this;
Expand All @@ -80,14 +78,14 @@ declare namespace BetterSqlite3 {
defaultSafeIntegers(toggleState?: boolean): this;
backup(destinationFile: string, options?: Database.BackupOptions): Promise<Database.BackupMetadata>;
table(name: string, options: VirtualTableOptions): this;
unsafeMode(unsafe?: boolean): this;
}

interface DatabaseConstructor {
new(filename: string, options?: Database.Options): Database;
(filename: string, options?: Database.Options): Database;
prototype: Database;

Integer: typeof Integer;
SqliteError: typeof SqliteError;
}
}
Expand All @@ -102,11 +100,10 @@ declare class SqliteError implements Error {
declare namespace Database {
interface RunResult {
changes: number;
lastInsertRowid: Integer.IntLike;
lastInsertRowid: number | bigint;
}

interface Options {
memory?: boolean | undefined;
readonly?: boolean | undefined;
fileMustExist?: boolean | undefined;
timeout?: number | undefined;
Expand Down Expand Up @@ -138,7 +135,6 @@ declare namespace Database {
progress: (info: BackupMetadata) => number;
}

type Integer = typeof Integer;
type SqliteError = typeof SqliteError;
type Statement<BindParameters extends any[] | {} = any[]> = BindParameters extends any[]
? BetterSqlite3.Statement<BindParameters>
Expand Down
4 changes: 2 additions & 2 deletions types/better-sqlite3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"target": "es2020",
"lib": [
"es6"
"es2020"
],
"noImplicitAny": true,
"noImplicitThis": true,
Expand Down

0 comments on commit 2434d01

Please sign in to comment.