Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ design/*.sketch
examples/
.deno_plugins
.nova/*
tsconfig.json
tsconfig.json
.env
23 changes: 10 additions & 13 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
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: 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";

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.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";
1 change: 1 addition & 0 deletions lib/connectors/mysql-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class MySQLConnector implements Connector {
async query(
queryDescription: QueryDescription,
client?: MySQLClient | MySQLConnection,
// deno-lint-ignore no-explicit-any
): Promise<any | any[]> {
await this._makeConnection();

Expand Down
3 changes: 2 additions & 1 deletion lib/connectors/postgres-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -68,6 +68,7 @@ export class PostgresConnector implements Connector {
}
}

// deno-lint-ignore no-explicit-any
async query(queryDescription: QueryDescription): Promise<any | any[]> {
await this._makeConnection();

Expand Down
48 changes: 18 additions & 30 deletions lib/connectors/sqlite3-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,23 @@ export class SQLite3Connector implements Connector {
}
}

// deno-lint-ignore no-explicit-any
query(queryDescription: QueryDescription): Promise<any | any[]> {
this._makeConnection();

const query = this._translator.translateToQuery(queryDescription);
const subqueries = query.split(/;(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/);

const results = subqueries.map(async (subquery, index) => {
const response = this._client.query(subquery + ";", []);
const results = subqueries.map((subquery, index) => {
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,
Expand All @@ -83,36 +79,28 @@ 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<string, FieldValue> = {};
for (const [columnName, value] of Object.entries(row)) {
if (columnName === "count(*)") {
result.count = column;
result.count = value 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];
return Promise.resolve(results[results.length - 1]);
}

async transaction(queries: () => Promise<void>) {
Expand Down
2 changes: 1 addition & 1 deletion tests/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultMySQLOptions = {
};

const defaultSQLiteOptions = {
filepath: "test.db",
filepath: "test.sqlite",
};

const getMySQLConnection = (options = {}, debug = true): Database => {
Expand Down
2 changes: 1 addition & 1 deletion tests/deps.ts
Original file line number Diff line number Diff line change
@@ -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";
4 changes: 4 additions & 0 deletions tests/units/queries/sqlite/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down