From 10b1b74d277582cbeb851e4933bdec9edda02ea0 Mon Sep 17 00:00:00 2001 From: Florian Keller Date: Thu, 30 May 2019 20:00:34 +0200 Subject: [PATCH] [sql.js] Fix config declaration and constructor (#35713) --- types/sql.js/index.d.ts | 87 ++---------------------------------- types/sql.js/module.d.ts | 85 +++++++++++++++++++++++++++++++++++ types/sql.js/sql.js-tests.ts | 32 +++++++------ types/sql.js/tsconfig.json | 6 ++- 4 files changed, 111 insertions(+), 99 deletions(-) create mode 100644 types/sql.js/module.d.ts diff --git a/types/sql.js/index.d.ts b/types/sql.js/index.d.ts index 82bbb53ad1..eb287cd197 100644 --- a/types/sql.js/index.d.ts +++ b/types/sql.js/index.d.ts @@ -3,89 +3,10 @@ // Definitions by: George Wu , // Florian Keller // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 +// TypeScript Version: 2.2 -/// +/// -export type ValueType = number | string | Uint8Array; -export type ParamsObject = Record; -export type ParamsCallback = (obj: ParamsObject) => void; - -export interface Config { - locateFile(fileName?: string): string; +declare module 'sql.js' { + export = initSqlJs; } - -export interface QueryResults { - columns: string[]; - values: ValueType[]; -} - -export class Database { - constructor(); - constructor(data: Buffer); - constructor(data: Uint8Array); - constructor(data: number[]); - - run(sql: string): Database; - run(sql: string, params: ParamsObject): Database; - run(sql: string, params: ValueType[]): Database; - - exec(sql: string): QueryResults[]; - - each(sql: string, callback: ParamsCallback, done: () => void): void; - each(sql: string, params: ParamsObject, callback: ParamsCallback, done: () => void): void; - each(sql: string, params: ValueType[], callback: ParamsCallback, done: () => void): void; - - prepare(sql: string): Statement; - prepare(sql: string, params: ParamsObject): Statement; - prepare(sql: string, params: ValueType[]): Statement; - - export(): Uint8Array; - - close(): void; - - getRowsModified(): number; - - create_function(name: string, func: Function): void; -} - -export class Statement { - bind(): boolean; - bind(values: ParamsObject): boolean; - bind(values: ValueType[]): boolean; - - step(): boolean; - - get(): ValueType[]; - get(params: ParamsObject): ValueType[]; - get(params: ValueType[]): ValueType[]; - - getColumnNames(): string[]; - - getAsObject(): ParamsObject; - getAsObject(params: ParamsObject): ParamsObject; - getAsObject(params: ValueType[]): ParamsObject; - - run(): void; - run(values: ParamsObject): void; - run(values: ValueType[]): void; - - reset(): void; - - freemem(): void; - - free(): boolean; -} - -declare interface sqlJSStatic { - Database: typeof Database; - Statement: typeof Statement; -} - -declare function initSqlJsStatic(config?: Config): Promise; - -declare global { - var initSqlJs: typeof initSqlJsStatic; -} - -export default initSqlJsStatic; diff --git a/types/sql.js/module.d.ts b/types/sql.js/module.d.ts new file mode 100644 index 0000000000..81c11489d2 --- /dev/null +++ b/types/sql.js/module.d.ts @@ -0,0 +1,85 @@ +/// +/// + +export namespace SqlJs { + type ValueType = number | string | Uint8Array; + type ParamsObject = Record; + type ParamsCallback = (obj: ParamsObject) => void; + type Config = Partial; + + interface QueryResults { + columns: string[]; + values: ValueType[]; + } + + class Database { + constructor(); + constructor(data?: Buffer | null); + constructor(data?: Uint8Array | null); + constructor(data?: number[] | null); + + run(sql: string): Database; + run(sql: string, params: ParamsObject): Database; + run(sql: string, params: ValueType[]): Database; + + exec(sql: string): QueryResults[]; + + each(sql: string, callback: ParamsCallback, done: () => void): void; + each(sql: string, params: ParamsObject, callback: ParamsCallback, done: () => void): void; + each(sql: string, params: ValueType[], callback: ParamsCallback, done: () => void): void; + + prepare(sql: string): Statement; + prepare(sql: string, params: ParamsObject): Statement; + prepare(sql: string, params: ValueType[]): Statement; + + export(): Uint8Array; + + close(): void; + + getRowsModified(): number; + + create_function(name: string, func: Function): void; + } + + class Statement { + bind(): boolean; + bind(values: ParamsObject): boolean; + bind(values: ValueType[]): boolean; + + step(): boolean; + + get(): ValueType[]; + get(params: ParamsObject): ValueType[]; + get(params: ValueType[]): ValueType[]; + + getColumnNames(): string[]; + + getAsObject(): ParamsObject; + getAsObject(params: ParamsObject): ParamsObject; + getAsObject(params: ValueType[]): ParamsObject; + + run(): void; + run(values: ParamsObject): void; + run(values: ValueType[]): void; + + reset(): void; + + freemem(): void; + + free(): boolean; + } + + export interface SqlJsStatic { + Database: typeof Database; + Statement: typeof Statement; + } + + interface InitSqlJsStatic extends Function { + (config?: Config): Promise; + readonly default: this; + } +} + +declare global { + let initSqlJs: SqlJs.InitSqlJsStatic; +} diff --git a/types/sql.js/sql.js-tests.ts b/types/sql.js/sql.js-tests.ts index a1ff0dbe9b..702180a3e5 100644 --- a/types/sql.js/sql.js-tests.ts +++ b/types/sql.js/sql.js-tests.ts @@ -1,26 +1,28 @@ import fs = require('fs'); -import initSqlJs from 'sql.js'; -var DB_PATH = 'data.db'; +import * as initSqlJs from 'sql.js'; +import initSqlJs2 from 'sql.js'; +import initSqlJs3 = require('sql.js'); + +const DB_PATH = 'data.db'; initSqlJs().then(SQL => { function createFile(path: string): void { - var fd = fs.openSync(path, 'a'); + const fd = fs.openSync(path, 'a'); fs.closeSync(fd); } // Open the database file. If it does not exist, create a blank database in memory. - var databaseData: Buffer; - databaseData = fs.existsSync(DB_PATH) ? fs.readFileSync(DB_PATH) : null; - var db = new SQL.Database(databaseData); + const databaseData = fs.existsSync(DB_PATH) ? fs.readFileSync(DB_PATH) : null; + const db = new SQL.Database(databaseData); // Create a new table 'test_table' in the database in memory. - var createTableStatement = + const createTableStatement = 'DROP TABLE IF EXISTS test_table;' + 'CREATE TABLE test_table (id INTEGER PRIMARY KEY, content TEXT);'; db.run(createTableStatement); // Insert 2 records for testing. - var insertRecordStatement = 'INSERT INTO test_table (id, content) VALUES (@id, @content);'; + const insertRecordStatement = 'INSERT INTO test_table (id, content) VALUES (@id, @content);'; db.run(insertRecordStatement, { '@id': 1, '@content': 'Content 1', @@ -43,16 +45,16 @@ initSqlJs().then(SQL => { } // A simple SELECT query. - var selectRecordStatement = 'SELECT * FROM test_table WHERE id = @id;'; - var selectStatementObject = db.prepare(selectRecordStatement); - var results = selectStatementObject.get({ + const selectRecordStatement = 'SELECT * FROM test_table WHERE id = @id;'; + const selectStatementObject = db.prepare(selectRecordStatement); + const results = selectStatementObject.get({ '@id': 1, }); console.log(results); selectStatementObject.free(); // Access the results one by one, asynchronously. - var selectRecordsStatement = 'SELECT * FROM test_table;'; + const selectRecordsStatement = 'SELECT * FROM test_table;'; db.each( selectRecordsStatement, (obj: { [columnName: string]: number | string | Uint8Array }): void => { @@ -69,7 +71,7 @@ initSqlJs().then(SQL => { if (!fs.existsSync(DB_PATH)) { createFile(DB_PATH); } - var exportedData = db.export(); + const exportedData = db.export(); fs.writeFileSync(DB_PATH, exportedData); // Finally, close the database connection and release the resources in memory. @@ -77,7 +79,7 @@ initSqlJs().then(SQL => { } // Create a database - var db2 = new SQL.Database(); + const db2 = new SQL.Database(); // You can also use javascript functions inside your SQL code // Create the js function you need @@ -88,4 +90,6 @@ initSqlJs().then(SQL => { db2.create_function('add_js', add); // Run a query in which the function is used db2.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world' + + new SQL.Database(null); }); diff --git a/types/sql.js/tsconfig.json b/types/sql.js/tsconfig.json index aa744160cb..aed9269531 100644 --- a/types/sql.js/tsconfig.json +++ b/types/sql.js/tsconfig.json @@ -2,7 +2,8 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6" + "es6", + "dom" ], "noImplicitAny": true, "noImplicitThis": true, @@ -18,6 +19,7 @@ }, "files": [ "index.d.ts", + "module.d.ts", "sql.js-tests.ts" ] -} \ No newline at end of file +}