[sql.js] Fix config declaration and constructor (#35713)

This commit is contained in:
Florian Keller
2019-05-30 11:00:34 -07:00
committed by Sheetal Nandi
parent b6ff9c2306
commit 10b1b74d27
4 changed files with 111 additions and 99 deletions
+4 -83
View File
@@ -3,89 +3,10 @@
// Definitions by: George Wu <https://github.com/Hozuki>,
// Florian Keller <https://github.com/ffflorian>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
// TypeScript Version: 2.2
/// <reference types="node" />
/// <reference path="./module.d.ts" />
export type ValueType = number | string | Uint8Array;
export type ParamsObject = Record<string, ValueType>;
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<sqlJSStatic>;
declare global {
var initSqlJs: typeof initSqlJsStatic;
}
export default initSqlJsStatic;
+85
View File
@@ -0,0 +1,85 @@
/// <reference types="node" />
/// <reference types="emscripten" />
export namespace SqlJs {
type ValueType = number | string | Uint8Array;
type ParamsObject = Record<string, ValueType>;
type ParamsCallback = (obj: ParamsObject) => void;
type Config = Partial<typeof Module>;
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<SqlJsStatic>;
readonly default: this;
}
}
declare global {
let initSqlJs: SqlJs.InitSqlJsStatic;
}
+18 -14
View File
@@ -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);
});
+4 -2
View File
@@ -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"
]
}
}