Skip to content

Commit

Permalink
Add typings for npm-module spatialite (DefinitelyTyped#14094)
Browse files Browse the repository at this point in the history
* Add typings for spatialite

* Fix lints

* Enhance for strictNullChecks option

* Running npm run new-package
  • Loading branch information
atd-schubert authored and mhegazy committed Jan 18, 2017
1 parent a5530fc commit 0471d55
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spatialite/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Type definitions for spatialite 0.0
// Project: https://github.com/zhm/node-spatialite
// Definitions by: Arne Schubert <https://github.com/atd-schubert/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

export {
OPEN_READONLY,
OPEN_READWRITE,
OPEN_CREATE,
cached,
RunResult,
Statement,
verbose } from 'sqlite3';
import {
Database as OriginalDatabase
} from 'sqlite3';

export class Database extends OriginalDatabase {
spatialite(cb: (err: Error) => void): void;
}

118 changes: 118 additions & 0 deletions spatialite/spatialite-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

import spatialite = require("spatialite");

function applySpatialFunctions() {
console.log("");
var spatialDb: spatialite.Database = new spatialite.Database('spatialite', () => {
spatialDb.spatialite((err) => {
if (err) {
console.error(err);
}
});
});
}


// Following tests taken from the sqlite3 typings

spatialite.verbose();

// This line is enhanced to fulfill the `strictNullChecks` option
var db: spatialite.Database = new spatialite.Database('chain.sqlite3', () => {});
// var db: spatialite.Database

function createDb() {
console.log("createDb chain");
db = new spatialite.Database('chain.sqlite3', createTable);
}

function createTable() {
console.log("createTable lorem");
db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)", insertRows);
}

function insertRows() {
console.log("insertRows Ipsum i");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");

for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}

stmt.finalize(readAllRows);
}

function readAllRows() {
console.log("readAllRows lorem");
db.all("SELECT rowid AS id, info FROM lorem", function(err, rows) {
rows.forEach(function (row) {
console.log(row.id + ": " + row.info);
});
readSomeRows();
});
}

function readSomeRows() {
console.log("readAllRows lorem");
db.each("SELECT rowid AS id, info FROM lorem WHERE rowid < ? ", 5, function(err, row) {
console.log(row.id + ": " + row.info);
}, closeDb);
}

function closeDb() {
console.log("closeDb");
db.close();
}

function runChainExample() {
createDb();
}

runChainExample();

db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");

var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();

db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});

db.serialize(function() {
// These two queries will run sequentially.
db.run("CREATE TABLE foo (num)");
db.run("INSERT INTO foo VALUES (?)", 1, function(err) {
// These queries will run in parallel and the second query will probably
// fail because the table might not exist yet.
db.run("CREATE TABLE bar (num)");
db.run("INSERT INTO bar VALUES (?)", 1);
});
});

// Directly in the function arguments.
db.run("UPDATE tbl SET name = ? WHERE id = ?", "bar", 2);

// As an array.
db.run("UPDATE tbl SET name = ? WHERE id = ?", [ "bar", 2 ]);

// As an object with named parameters.
db.run("UPDATE tbl SET name = $name WHERE id = $id", {
$id: 2,
$name: "bar"
});
db.run("UPDATE tbl SET name = $name WHERE id = $id", { $id: 2, $name: "bar" },
function(err) { }
);

db.run("UPDATE tbl SET name = ?5 WHERE id = ?", {
1: 2,
5: "bar"
});

db.close();
20 changes: 20 additions & 0 deletions spatialite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"spatialite-tests.ts"
]
}
1 change: 1 addition & 0 deletions spatialite/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../tslint.json" }

0 comments on commit 0471d55

Please sign in to comment.