-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:databaseDriverAdaptations - Postgres|Sqlite
- Loading branch information
Showing
20 changed files
with
1,252 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ mysql.log | |
package-lock.json | ||
.vscode | ||
node_modules | ||
test.db | ||
*.code-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
Client, | ||
PostgresError, | ||
log, | ||
QueryResult, | ||
QueryConfig, | ||
} from "./postgres_deps.ts"; | ||
|
||
/** Transaction processor */ | ||
export interface TransactionProcessor<T> { | ||
(connection: PostgresClient): Promise<T>; | ||
} | ||
|
||
export class PostgresClient { | ||
protected client: Client; | ||
|
||
constructor(config: object) { | ||
this.client = new Client(config); | ||
} | ||
|
||
async connect(): Promise<void> { | ||
return this.client.connect(); | ||
} | ||
|
||
// TODO: can we use more specific type for args? | ||
async query(text: string): Promise<QueryResult> { | ||
return this.client.query(text); | ||
} | ||
|
||
// TODO: can we use more specific type for args? | ||
async execute(text: string): Promise<QueryResult> { | ||
return this.client.query(text); | ||
} | ||
|
||
async multiQuery(queries: QueryConfig[]): Promise<QueryResult[]> { | ||
const result: QueryResult[] = []; | ||
|
||
for (const query of queries) { | ||
result.push(await this.client.query(query)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
async end(): Promise<void> { | ||
await this.client.end(); | ||
} | ||
|
||
/** | ||
* Use a connection for transaction processor | ||
* | ||
* @param fn transation processor | ||
*/ | ||
async useConnection<T>(fn: (conn: PostgresClient) => Promise<T>) { | ||
if (!this.client) { | ||
throw new Error("Unconnected"); | ||
} | ||
try { | ||
const result = await fn(this); | ||
return result; | ||
} catch (error) { | ||
throw new PostgresError( | ||
{ severity: "high", code: "TA", message: "transactions" }, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Execute a transaction process, and the transaction successfully | ||
* returns the return value of the transaction process | ||
* @param processor transation processor | ||
*/ | ||
async transaction<T>(processor: TransactionProcessor<T>): Promise<T> { | ||
return await this.useConnection(async (connection) => { | ||
try { | ||
await connection.query("BEGIN"); | ||
const result = await processor(connection); | ||
await connection.query("COMMIT"); | ||
return result; | ||
} catch (error) { | ||
log.info(`ROLLBACK: ${error.message}`); | ||
await connection.query("ROLLBACK"); | ||
throw new PostgresError( | ||
{ severity: "high", code: "TA", message: "transactions" }, | ||
); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { DB } from "https://deno.land/x/sqlite/mod.ts"; | ||
import { log } from "./sqlite_deps.ts"; | ||
import SqliteError from "https://deno.land/x/sqlite/src/error.ts"; | ||
import { Rows } from "./sqlite_deps.ts"; | ||
|
||
/** Transaction processor */ | ||
export interface TransactionProcessor<T> { | ||
(connection: SqliteClient): Promise<T>; | ||
} | ||
|
||
export class SqliteClient { | ||
protected db: DB; | ||
constructor(path: string) { | ||
this.db = new DB(path); | ||
} | ||
|
||
query(sql: string): Rows { | ||
return this.db.query(sql); | ||
} | ||
|
||
execute(sql: string): Rows { | ||
return this.db.query(sql); | ||
} | ||
|
||
/** | ||
* DB.close | ||
* | ||
* Close database handle. This must be called if | ||
* DB is no longer used, to avoid leaking file | ||
* resources. | ||
* | ||
* If force is specified, any on-going transactions | ||
* will be closed. | ||
*/ | ||
close(force: boolean = false) { | ||
this.db.close(force); | ||
} | ||
|
||
/** | ||
* DB.lastInsertRowId | ||
* | ||
* Get last inserted row id. This corresponds to | ||
* the SQLite function `sqlite3_last_insert_rowid`. | ||
* | ||
* By default, it will return 0 if there is no row | ||
* inserted yet. | ||
*/ | ||
get lastInsertRowId(): number { | ||
return this.db.lastInsertRowId; | ||
} | ||
|
||
/** | ||
* DB.changes | ||
* | ||
* Return the number of rows modified, inserted or | ||
* deleted by the most recently completed query. | ||
* This corresponds to the SQLite function | ||
* `sqlite3_changes`. | ||
*/ | ||
get changes(): number { | ||
return this.db.changes; | ||
} | ||
|
||
/** | ||
* DB.totalChanges | ||
* | ||
* Return the number of rows modified, inserted or | ||
* deleted since the database was opened. | ||
* This corresponds to the SQLite function | ||
* `sqlite3_total_changes`. | ||
*/ | ||
get totalChanges(): number { | ||
return this.db.totalChanges; | ||
} | ||
|
||
/** | ||
* Use a connection for transaction processor | ||
* | ||
* @param fn transation processor | ||
*/ | ||
async useConnection<T>(fn: (conn: this) => Promise<T>) { | ||
if (!this.db) { | ||
throw new Error("Unconnected"); | ||
} | ||
try { | ||
const result = await fn(this); | ||
return result; | ||
} catch (error) { | ||
throw new SqliteError("connection", 2); | ||
} | ||
} | ||
|
||
/** | ||
* Execute a transaction process, and the transaction successfully | ||
* returns the return value of the transaction process | ||
* @param processor transation processor | ||
*/ | ||
async transaction<T>(processor: TransactionProcessor<T>): Promise<T> { | ||
return await this.useConnection(async (connection) => { | ||
try { | ||
await connection.query("BEGIN"); | ||
const result = await processor(connection); | ||
await connection.query("COMMIT"); | ||
return result; | ||
} catch (error) { | ||
log.info(`ROLLBACK: ${error.message}`); | ||
await connection.query("ROLLBACK"); | ||
throw new SqliteError("transaction", 1); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export { | ||
Pool, | ||
PostgresError, | ||
Client, | ||
} from "https://deno.land/x/postgres/mod.ts"; | ||
|
||
export { | ||
Connection, | ||
} from "https://deno.land/x/postgres/connection.ts"; | ||
|
||
export { | ||
Query as QueryPostgres, | ||
QueryConfig, | ||
QueryResult, | ||
} from "https://deno.land/x/postgres/query.ts"; | ||
|
||
export { | ||
ConnectionOptions, | ||
createParams, | ||
} from "https://deno.land/x/postgres/connection_params.ts"; | ||
|
||
export { | ||
log, | ||
} from "https://deno.land/x/mysql/src/logger.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export { | ||
getStr, | ||
setStr, | ||
setArr, | ||
} from "https://deno.land/x/sqlite/src/wasm.ts"; | ||
|
||
export { Status, Values } from "https://deno.land/x/sqlite/src/constants.ts"; | ||
|
||
export { | ||
Rows, | ||
Empty, | ||
} from "https://deno.land/x/sqlite/src/rows.ts"; | ||
|
||
export { | ||
log, | ||
} from "https://deno.land/x/mysql/src/logger.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Base { | ||
type: string; // MySQl|Postgres|Sqlite | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Base } from "./base.ts"; | ||
import { ClientConfig, Client } from "../../deps.ts"; | ||
|
||
export interface MysqlConfig extends Base { | ||
clientConfig: ClientConfig; | ||
client?: Client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Base } from "./base.ts"; | ||
import { PostgresClient } from "../../PostgresClient.ts"; | ||
|
||
export interface PostgresConfig extends Base { | ||
clientConfig: object; | ||
client?: PostgresClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Base } from "./base.ts"; | ||
import { SqliteClient } from "../../SqliteClient.ts"; | ||
|
||
export interface SqliteConfig extends Base { | ||
clientConfig: object; | ||
client?: SqliteClient; | ||
} |
Oops, something went wrong.