diff --git a/types/sqlstring/index.d.ts b/types/sqlstring/index.d.ts new file mode 100644 index 0000000000..bf774824e7 --- /dev/null +++ b/types/sqlstring/index.d.ts @@ -0,0 +1,9 @@ +// Type definitions for sqlstring 2.2 +// Project: https://github.com/mysqljs/sqlstring +// Definitions by: Marvin Hagemeister +// 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; diff --git a/types/sqlstring/sqlstring-tests.ts b/types/sqlstring/sqlstring-tests.ts new file mode 100644 index 0000000000..6d2ef471c7 --- /dev/null +++ b/types/sqlstring/sqlstring-tests.ts @@ -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); diff --git a/types/sqlstring/tsconfig.json b/types/sqlstring/tsconfig.json new file mode 100644 index 0000000000..8af214b3f7 --- /dev/null +++ b/types/sqlstring/tsconfig.json @@ -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" + ] +} diff --git a/types/sqlstring/tslint.json b/types/sqlstring/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sqlstring/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }