Add typings for sqlstring

This commit is contained in:
Marvin Hagemeister 2017-07-11 20:23:04 +02:00
parent 737e62220e
commit f0228785a5
4 changed files with 63 additions and 0 deletions

9
types/sqlstring/index.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
// Type definitions for sqlstring 2.2
// Project: https://github.com/mysqljs/sqlstring
// Definitions by: Marvin Hagemeister <https://github.com/marvinhagemeister>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
export function format(sql: string, args: object | any[]): string;
export function escape(value: any): string;
export function escapeId(value: any, dotQualifier?: boolean): string;

View File

@ -0,0 +1,31 @@
import * as SqlString from "sqlstring";
// Code samples taken from:
// https://github.com/mysqljs/sqlstring/blob/master/README.md
const userId = "some user provided value";
const sql1 = "SELECT * FROM users WHERE id = " + SqlString.escape(userId);
const userId2 = 1;
const sql2 = SqlString.format("SELECT * FROM users WHERE id = ?", [userId2]);
const userId3 = 1;
const sql3 = SqlString.format(
"UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?",
["a", "b", "c", userId3],
);
const post = { id: 1, title: "Hello MySQL" };
const sql4 = SqlString.format("INSERT INTO posts SET ?", post);
const sorter = "date";
const sql5 =
"SELECT * FROM posts ORDER BY " + SqlString.escapeId("posts." + sorter);
const sorter2 = "date.2";
const sql6 =
"SELECT * FROM posts ORDER BY " + SqlString.escapeId(sorter2, true);
const userId4 = 1;
const inserts = ["users", "id", userId4];
const sql7 = SqlString.format("SELECT * FROM ?? WHERE ?? = ?", inserts);

View File

@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"sqlstring-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }