🤖 Merge PR #65788 Add types definitions for xk6-sql by @leandrodotec

* Add types definitions for xk6-sql

Adding definitions for k6/x/sql plugin: https://github.com/grafana/xk6-sql

* remove file

* adding "-browser" suffix for non npm packages.

* more guess work

* fixing identations

* Update types/xk6-sql-browser/index.d.ts

Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com>

* Fixing "npm-naming": false and re-running prettier

---------

Co-authored-by: Leandro Gomes <leandro.gomes@bpsoftware.net>
Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com>
This commit is contained in:
Leandro
2023-06-19 13:26:45 +12:00
committed by GitHub
parent 0ae8800830
commit 0e1dc03c3d
4 changed files with 169 additions and 0 deletions

108
types/xk6-sql-browser/index.d.ts vendored Normal file
View File

@@ -0,0 +1,108 @@
// Type definitions for non-npm package xk6-sql 0.1
// Project: https://github.com/grafana/xk6-sql
// Definitions by: Leandro Gomes <https://github.com/leandrodotec>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* xk6-sql: k6 extension that allows connection to RDBMSs: mysql, postgres, sqlite3, sqlserver
* https://github.com/grafana/xk6-sql
*/
declare module 'k6/x/sql' {
namespace sql {
/**
* Opens a database connection.
* @param type - the type of database (mysql, postgres, sqlite3, sqlserver)
* @param connectionString - the connection string to connect to the database
* @returns Connection to the database.
* @example
* const db = sql.open("sqlserver", "Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;")
*/
function open(
type: 'mysql' | 'postgres' | 'sqlite3' | 'sqlserver',
connectionString: string,
): DatabaseConnection;
/**
* Executes the provided query string against the database, while
* providing results as a slice of KeyValue instance(s) if available.
* @param db - Connection to database
* @param query - SQL query
* @param args - query parameters
* @returns Array with query results
* @example
* const db = sql.open("sqlserver", "Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;")
* const results = sql.query(db, "SELECT Colour FROM Shapes WHERE Name=@p1 AND Type=@p2", "circle", "round")
* for (const row of results) {
* console.log(`key: ${row.key}, value: ${row.value}`);
* }
*/
function query(db: DatabaseConnection, query: string, ...args: Array<string | number | boolean>): any[];
interface DatabaseConnection {
/**
* Executes the provided query string against the database.
* @param query - SQL query
* @example
* const db = sql.open("sqlserver", "Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;")
* db.exec("INSERT INTO Shapes(Name, Type, Colour) VALUES{'circle', 'round', 'red')}")
*/
exec(query: string): void;
/**
* Closes the database connection
*/
close(): void;
}
}
export default sql;
}
declare module 'xk6-sql' {
namespace sql {
/**
* Opens a database connection.
* @param type - the type of database (mysql, postgres, sqlite3, sqlserver)
* @param connectionString - the connection string to connect to the database
* @returns Connection to the database.
* @example
* const db = sql.open("sqlserver", "Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;")
*/
function open(
type: 'mysql' | 'postgres' | 'sqlite3' | 'sqlserver',
connectionString: string,
): DatabaseConnection;
/**
* Executes the provided query string against the database, while
* providing results as a slice of KeyValue instance(s) if available.
* @param db - Connection to database
* @param query - SQL query
* @param args - query parameters
* @returns Array with query results
* @example
* const db = sql.open("sqlserver", "Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;")
* const results = sql.query(db, "SELECT Colour FROM Shapes WHERE Name=@p1 AND Type=@p2", "circle", "round")
* for (const row of results) {
* console.log(`key: ${row.key}, value: ${row.value}`);
* }
*/
function query(db: DatabaseConnection, query: string, ...args: Array<string | number | boolean>): any[];
interface DatabaseConnection {
/**
* Executes the provided query string against the database.
* @param query - SQL query
* @example
* const db = sql.open("sqlserver", "Server=127.0.0.1;Database=myDB;User Id=myUser;Password=myPassword;")
* db.exec("INSERT INTO Shapes(Name, Type, Colour) VALUES{'circle', 'round', 'red')}")
*/
exec(query: string): void;
/**
* Closes the database connection
*/
close(): void;
}
}
export default sql;
}

View File

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

View File

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

View File

@@ -0,0 +1,35 @@
import sql from 'k6/x/sql';
// @ts-expect-error
sql.open();
// @ts-expect-error
sql.open('not-supported', '123');
// @ts-expect-error
sql.open('sqlite3');
// $ExpectType DatabaseConnection
sql.open('sqlite3', 'my.db');
const db = sql.open('sqlite3', 'my.db');
// @ts-expect-error
db.exec();
db.exec('some statements');
// @ts-expect-error
db.close(1);
db.close();
// @ts-expect-error
sql.query();
// @ts-expect-error
sql.query('not-connection-object', 'select statement');
// @ts-expect-error
sql.query(db);
sql.query(db, 'select statement');
sql.query(db, 'select statement', 'param1');
sql.query(db, 'select statement', 'param1', 'param2');
sql.query(db, 'select statement', 'param1', 'param2', 'param3');