forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typings for npm-module spatialite (DefinitelyTyped#14094)
* Add typings for spatialite * Fix lints * Enhance for strictNullChecks option * Running npm run new-package
- Loading branch information
1 parent
a5530fc
commit 0471d55
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "extends": "../tslint.json" } |