From 1e63d851f6e057a013c057e43406ca403ac7680c Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Fri, 19 Nov 2021 13:37:52 -0800 Subject: [PATCH 1/2] Update dex, sqlite, mysql, mongodb, postgres deps dex: update to the version that uses std@0.115.1, instead of the latest version of jspm-core, which could be dangerous when jspm-core pushes the latest braking code. https://github.com/aghussb/dex/pull/3 sqlite: update to v3.1.3, it contains braking changes so this PR fixes them too. mysql: minor upgrade to v2.10.1 (from v2.10.0) mongodb: update to v0.28.0, because the old version is not compatible with the latest deno. postgres: update to v0.14.2 and fix update postgres-connectors, because the old version is not compatible with the latest deno. The changes are tested on deno v1.16.2. --- .gitignore | 3 +- deps.ts | 22 ++++++-------- lib/connectors/mysql-connector.ts | 1 + lib/connectors/postgres-connector.ts | 3 +- lib/connectors/sqlite3-connector.ts | 45 +++++++++++----------------- tests/connection.ts | 2 +- tests/deps.ts | 2 +- 7 files changed, 33 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 964bc8a..2e4dcd6 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ design/*.sketch examples/ .deno_plugins .nova/* -tsconfig.json \ No newline at end of file +tsconfig.json +.env diff --git a/deps.ts b/deps.ts index f371c15..b717535 100644 --- a/deps.ts +++ b/deps.ts @@ -1,8 +1,7 @@ export * as ConsoleColor from "https://deno.land/x/colorlog@v1.0/mod.ts"; -// NOTE(eveningkid): this has not be versioned because the Github releases are not up-to-date. -// Only master is valid at the moment. Seems safe for now since there is no commits being added -export { default as SQLQueryBuilder } from "https://raw.githubusercontent.com/aghussb/dex/master/mod.ts"; +// NOTE: these changes fixes #303. +export { default as SQLQueryBuilder } from "https://raw.githubusercontent.com/Zhomart/dex/c452c40b365e73265a25c18cb7adf5d35c1dbb8b/mod-dyn.ts"; export { camelCase, snakeCase } from "https://deno.land/x/case@v2.1.0/mod.ts"; @@ -10,16 +9,13 @@ export { Client as MySQLClient, configLogger as configMySQLLogger, Connection as MySQLConnection, -} from "https://deno.land/x/mysql@v2.10.0/mod.ts"; -export type { LoggerConfig } from "https://deno.land/x/mysql@v2.10.0/mod.ts"; +} from "https://deno.land/x/mysql@v2.10.1/mod.ts"; +export type { LoggerConfig } from "https://deno.land/x/mysql@v2.10.1/mod.ts"; -export { Client as PostgresClient } from "https://deno.land/x/postgres@v0.11.2/mod.ts"; +export { Client as PostgresClient } from "https://deno.land/x/postgres@v0.14.2/mod.ts"; -export { DB as SQLiteClient } from "https://deno.land/x/sqlite@v2.3.1/mod.ts"; +export { DB as SQLiteClient } from "https://deno.land/x/sqlite@v3.1.3/mod.ts"; -// NOTE(eveningkid): upgrading to 0.24.0 would raise an issue asking for the --unstable flag. -// This would be asked to anyone using denodb, not only mongodb users. -// Should wait on a version that isn't using any unstable API -export { MongoClient as MongoDBClient, Bson } from "https://deno.land/x/mongo@v0.22.0/mod.ts"; -export type { ConnectOptions as MongoDBClientOptions } from "https://deno.land/x/mongo@v0.22.0/mod.ts"; -export type { Database as MongoDBDatabase } from "https://deno.land/x/mongo@v0.22.0/src/database.ts"; +export { MongoClient as MongoDBClient, Bson } from "https://deno.land/x/mongo@v0.28.0/mod.ts"; +export type { ConnectOptions as MongoDBClientOptions } from "https://deno.land/x/mongo@v0.28.0/mod.ts"; +export type { Database as MongoDBDatabase } from "https://deno.land/x/mongo@v0.28.0/src/database.ts"; diff --git a/lib/connectors/mysql-connector.ts b/lib/connectors/mysql-connector.ts index 504b16b..866716a 100644 --- a/lib/connectors/mysql-connector.ts +++ b/lib/connectors/mysql-connector.ts @@ -65,6 +65,7 @@ export class MySQLConnector implements Connector { async query( queryDescription: QueryDescription, client?: MySQLClient | MySQLConnection, + // deno-lint-ignore no-explicit-any ): Promise { await this._makeConnection(); diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index dc992c1..c1c838f 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -59,7 +59,7 @@ export class PostgresConnector implements Connector { await this._makeConnection(); try { - const [{ result }] = ( + const [result] = ( await this._client.queryObject("SELECT 1 + 1 as result") ).rows; return result === 2; @@ -68,6 +68,7 @@ export class PostgresConnector implements Connector { } } + // deno-lint-ignore no-explicit-any async query(queryDescription: QueryDescription): Promise { await this._makeConnection(); diff --git a/lib/connectors/sqlite3-connector.ts b/lib/connectors/sqlite3-connector.ts index 1ec8542..5dd4abc 100644 --- a/lib/connectors/sqlite3-connector.ts +++ b/lib/connectors/sqlite3-connector.ts @@ -48,27 +48,24 @@ export class SQLite3Connector implements Connector { } } + // deno-lint-ignore no-explicit-any query(queryDescription: QueryDescription): Promise { this._makeConnection(); const query = this._translator.translateToQuery(queryDescription); const subqueries = query.split(/;(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/); + // deno-lint-ignore require-await const results = subqueries.map(async (subquery, index) => { - const response = this._client.query(subquery + ";", []); + const preparedQuery = this._client.prepareQuery(subquery + ";"); + const response = preparedQuery.allEntries(); + preparedQuery.finalize(); if (index < subqueries.length - 1) { - response.return(); return []; } - const results = []; - let columns; - - try { - columns = response.columns(); - } catch { - // If there are no matching records, .columns will throw an error + if (response.length === 0) { if (queryDescription.type === "insert" && queryDescription.values) { return { affectedRows: this._client.changes, @@ -83,33 +80,25 @@ export class SQLite3Connector implements Connector { return { affectedRows: this._client.changes }; } - for (const row of response) { - const result: { [k: string]: FieldValue } = {}; - - let i = 0; - for (const column of row!) { - const columnName = columns[i].name; + return response.map(row => { + const result: Record = {}; + for (const [columnName, value] of Object.entries(row)) { if (columnName === "count(*)") { - result.count = column; + result.count = row[columnName] as FieldValue; } else if (columnName.startsWith("max(")) { - result.max = column; + result.max = value as FieldValue; } else if (columnName.startsWith("min(")) { - result.min = column; + result.min = value as FieldValue; } else if (columnName.startsWith("sum(")) { - result.sum = column; + result.sum = value as FieldValue; } else if (columnName.startsWith("avg(")) { - result.avg = column; + result.avg = value as FieldValue; } else { - result[columns[i].name] = column; + result[columnName] = value as FieldValue; } - - i++; } - - results.push(result); - } - - return results; + return result; + }); }); return results[results.length - 1]; diff --git a/tests/connection.ts b/tests/connection.ts index f6e473a..ba54c23 100644 --- a/tests/connection.ts +++ b/tests/connection.ts @@ -12,7 +12,7 @@ const defaultMySQLOptions = { }; const defaultSQLiteOptions = { - filepath: "test.db", + filepath: "test.sqlite", }; const getMySQLConnection = (options = {}, debug = true): Database => { diff --git a/tests/deps.ts b/tests/deps.ts index af241ea..b0012d2 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -1 +1 @@ -export { assertEquals } from "https://deno.land/std@0.56.0/testing/asserts.ts"; +export { assertEquals } from "https://deno.land/std@0.115.1/testing/asserts.ts"; From 2f740411b33122d835f7b2aee82c02b10ac14cb1 Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Fri, 10 Dec 2021 19:10:51 -0800 Subject: [PATCH 2/2] Fix sqlite promise issues and update dex to use latest std --- deps.ts | 11 ++++++----- lib/connectors/sqlite3-connector.ts | 7 +++---- tests/units/queries/sqlite/response.test.ts | 4 ++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/deps.ts b/deps.ts index b717535..dd99bc2 100644 --- a/deps.ts +++ b/deps.ts @@ -1,7 +1,8 @@ export * as ConsoleColor from "https://deno.land/x/colorlog@v1.0/mod.ts"; -// NOTE: these changes fixes #303. -export { default as SQLQueryBuilder } from "https://raw.githubusercontent.com/Zhomart/dex/c452c40b365e73265a25c18cb7adf5d35c1dbb8b/mod-dyn.ts"; +// NOTE: Migrate to the official https://github.com/aghussb/dex when it's updated to the +// latest deno version. +export { default as SQLQueryBuilder } from "https://raw.githubusercontent.com/Zhomart/dex/930253915093e1e08d48ec0409b4aee800d8bd0c/mod-dyn.ts"; export { camelCase, snakeCase } from "https://deno.land/x/case@v2.1.0/mod.ts"; @@ -16,6 +17,6 @@ export { Client as PostgresClient } from "https://deno.land/x/postgres@v0.14.2/m export { DB as SQLiteClient } from "https://deno.land/x/sqlite@v3.1.3/mod.ts"; -export { MongoClient as MongoDBClient, Bson } from "https://deno.land/x/mongo@v0.28.0/mod.ts"; -export type { ConnectOptions as MongoDBClientOptions } from "https://deno.land/x/mongo@v0.28.0/mod.ts"; -export type { Database as MongoDBDatabase } from "https://deno.land/x/mongo@v0.28.0/src/database.ts"; +export { MongoClient as MongoDBClient, Bson } from "https://deno.land/x/mongo@v0.28.1/mod.ts"; +export type { ConnectOptions as MongoDBClientOptions } from "https://deno.land/x/mongo@v0.28.1/mod.ts"; +export type { Database as MongoDBDatabase } from "https://deno.land/x/mongo@v0.28.1/src/database.ts"; diff --git a/lib/connectors/sqlite3-connector.ts b/lib/connectors/sqlite3-connector.ts index 5dd4abc..7b3e254 100644 --- a/lib/connectors/sqlite3-connector.ts +++ b/lib/connectors/sqlite3-connector.ts @@ -55,8 +55,7 @@ export class SQLite3Connector implements Connector { const query = this._translator.translateToQuery(queryDescription); const subqueries = query.split(/;(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/); - // deno-lint-ignore require-await - const results = subqueries.map(async (subquery, index) => { + const results = subqueries.map((subquery, index) => { const preparedQuery = this._client.prepareQuery(subquery + ";"); const response = preparedQuery.allEntries(); preparedQuery.finalize(); @@ -84,7 +83,7 @@ export class SQLite3Connector implements Connector { const result: Record = {}; for (const [columnName, value] of Object.entries(row)) { if (columnName === "count(*)") { - result.count = row[columnName] as FieldValue; + result.count = value as FieldValue; } else if (columnName.startsWith("max(")) { result.max = value as FieldValue; } else if (columnName.startsWith("min(")) { @@ -101,7 +100,7 @@ export class SQLite3Connector implements Connector { }); }); - return results[results.length - 1]; + return Promise.resolve(results[results.length - 1]); } async transaction(queries: () => Promise) { diff --git a/tests/units/queries/sqlite/response.test.ts b/tests/units/queries/sqlite/response.test.ts index 7238e2d..bd73253 100644 --- a/tests/units/queries/sqlite/response.test.ts +++ b/tests/units/queries/sqlite/response.test.ts @@ -84,6 +84,10 @@ Deno.test("SQLite: Response model", async () => { "Update many records response", ); + const articleCount = await Article.where({ title: "hola mundo!" }).count(); + + assertEquals(articleCount, 2, "Return article count"); + const deleteManyResponse = await Article.where({ title: "hola mundo!" }) .delete();