mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
Added type definitions for sql.js.
This commit is contained in:
81
sql.js/sql.js-tests.ts
Normal file
81
sql.js/sql.js-tests.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
/// <reference path="sql.js.d.ts" />
|
||||
|
||||
import fs = require("fs");
|
||||
import SQL = require("sql.js");
|
||||
|
||||
var DB_PATH = "data.db";
|
||||
|
||||
function createFile(path: string): void {
|
||||
var 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);
|
||||
|
||||
// Create a new table 'test_table' in the database in memory.
|
||||
var 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);";
|
||||
db.run(insertRecordStatement, {
|
||||
"@id": 1,
|
||||
"@content": "Content 1"
|
||||
});
|
||||
db.run(insertRecordStatement, {
|
||||
"@id": 2,
|
||||
"@content": "Content 2"
|
||||
});
|
||||
|
||||
try {
|
||||
// This query will throw exception: primary key constraint failed.
|
||||
db.run(insertRecordStatement, {
|
||||
"@id": 1,
|
||||
"@content": "Content 3"
|
||||
});
|
||||
} catch (ex) {
|
||||
console.warn(ex);
|
||||
}
|
||||
|
||||
// A simple SELECT query.
|
||||
var selectRecordStatement =
|
||||
"SELECT * FROM test_table WHERE id = @id;"
|
||||
var selectStatementObject = db.prepare(selectRecordStatement);
|
||||
var results = selectStatementObject.get({
|
||||
"@id": 1
|
||||
});
|
||||
console.log(results);
|
||||
selectStatementObject.free();
|
||||
|
||||
// Access the results one by one, asynchronously.
|
||||
var selectRecordsStatement =
|
||||
"SELECT * FROM test_table;";
|
||||
db.each(
|
||||
selectRecordsStatement,
|
||||
(obj: SQL.SQLValueObject): void => {
|
||||
console.log(obj);
|
||||
},
|
||||
(): void => {
|
||||
console.info("Iteration done.");
|
||||
dbAccessDone();
|
||||
});
|
||||
|
||||
|
||||
function dbAccessDone(): void {
|
||||
// Save the database into SQLite version 3 format.
|
||||
if (!fs.existsSync(DB_PATH)) {
|
||||
createFile(DB_PATH);
|
||||
}
|
||||
var exportedData = db.export();
|
||||
fs.writeFileSync(DB_PATH, exportedData);
|
||||
|
||||
// Finally, close the database connection and release the resources in memory.
|
||||
db.close();
|
||||
}
|
||||
71
sql.js/sql.js.d.ts
vendored
Normal file
71
sql.js/sql.js.d.ts
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
|
||||
// Type definitions for sql.js (Sep. 6 2015 snapshot)
|
||||
// Project: https://github.com/kripken/sql.js
|
||||
// Definitions by: George Wu <https://github.com/Hozuki/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare module "sql.js" {
|
||||
|
||||
type SQLValue = number | string | Uint8Array;
|
||||
type KeyValueObject = { [key: string]: SQLValue };
|
||||
type SQLValueObject = { [columnName: string]: SQLValue };
|
||||
type DataRow = SQLValue[];
|
||||
|
||||
class Database {
|
||||
constructor(data: Buffer);
|
||||
constructor(data: Uint8Array);
|
||||
constructor(data: number[]);
|
||||
|
||||
run(sql: string): Database;
|
||||
run(sql: string, params: KeyValueObject): Database;
|
||||
run(sql: string, params: SQLValue[]): Database;
|
||||
|
||||
exec(sql: string): QueryResults[];
|
||||
|
||||
each(sql: string, callback: (obj: SQLValueObject) => void, done: () => void): void;
|
||||
each(sql: string, params: KeyValueObject, callback: (obj: SQLValueObject) => void, done: () => void): void;
|
||||
each(sql: string, params: SQLValue[], callback: (obj: SQLValueObject) => void, done: () => void): void;
|
||||
|
||||
prepare(sql: string): Statement;
|
||||
prepare(sql: string, params: KeyValueObject): Statement;
|
||||
prepare(sql: string, params: SQLValue[]): Statement;
|
||||
|
||||
export(): Uint8Array;
|
||||
|
||||
close(): void;
|
||||
}
|
||||
|
||||
class Statement {
|
||||
bind(): boolean;
|
||||
bind(values: KeyValueObject): boolean;
|
||||
bind(values: SQLValue[]): boolean;
|
||||
|
||||
step(): boolean;
|
||||
|
||||
get(): DataRow;
|
||||
get(params: KeyValueObject): DataRow;
|
||||
get(params: SQLValue[]): DataRow;
|
||||
|
||||
getColumnNames(): string[];
|
||||
|
||||
getAsObject(): SQLValueObject;
|
||||
getAsObject(params: KeyValueObject): SQLValueObject;
|
||||
getAsObject(params: SQLValue[]): SQLValueObject;
|
||||
|
||||
run(): void;
|
||||
run(values: KeyValueObject): void;
|
||||
run(values: SQLValue[]): void;
|
||||
|
||||
reset(): void;
|
||||
|
||||
freemem(): void;
|
||||
|
||||
free(): boolean;
|
||||
}
|
||||
|
||||
interface QueryResults {
|
||||
columns: string[];
|
||||
values: DataRow[];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user