@planetscale/database
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -62,5 +62,12 @@ export { format } from './sanitization.js'; | ||
constructor(config: Config); | ||
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>; | ||
execute(query: string, args?: object | any[]): Promise<ExecutedQuery>; | ||
connection(): Connection; | ||
} | ||
export declare type Transaction = Tx; | ||
declare class Tx { | ||
private conn; | ||
constructor(conn: Connection); | ||
execute(query: string, args?: object | any[]): Promise<ExecutedQuery>; | ||
} | ||
export declare class Connection { | ||
@@ -70,2 +77,3 @@ private config; | ||
constructor(config: Config); | ||
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>; | ||
refresh(): Promise<void>; | ||
@@ -72,0 +80,0 @@ execute(query: string, args?: any): Promise<ExecutedQuery>; |
@@ -18,2 +18,5 @@ import { format } from './sanitization.js'; | ||
} | ||
async transaction(fn) { | ||
return this.connection().transaction(fn); | ||
} | ||
async execute(query, args) { | ||
@@ -26,2 +29,10 @@ return this.connection().execute(query, args); | ||
} | ||
class Tx { | ||
constructor(conn) { | ||
this.conn = conn; | ||
} | ||
async execute(query, args) { | ||
return this.conn.execute(query, args); | ||
} | ||
} | ||
export class Connection { | ||
@@ -42,2 +53,16 @@ constructor(config) { | ||
} | ||
async transaction(fn) { | ||
const conn = new Connection(this.config); | ||
const tx = new Tx(conn); | ||
try { | ||
await tx.execute('BEGIN'); | ||
const res = await fn(tx); | ||
await tx.execute('COMMIT'); | ||
return res; | ||
} | ||
catch (err) { | ||
await tx.execute('ROLLBACK'); | ||
throw err; | ||
} | ||
} | ||
async refresh() { | ||
@@ -44,0 +69,0 @@ await this.createSession(); |
@@ -1,1 +0,1 @@ | ||
export declare const Version = "1.0.2"; | ||
export declare const Version = "1.1.0"; |
@@ -1,1 +0,1 @@ | ||
export const Version = '1.0.2'; | ||
export const Version = '1.1.0'; |
{ | ||
"name": "@planetscale/database", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A Fetch API-compatible PlanetScale database driver", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -59,2 +59,26 @@ # PlanetScale Serverless Driver for JavaScript | ||
### Transactions | ||
Use the `transaction` function to safely perform database transactions. If any unhandled errors are thrown during execution of the transaction, the transaction will be rolled back. | ||
The following example is based on [the Slotted Counter Pattern](https://planetscale.com/blog/the-slotted-counter-pattern). | ||
```ts | ||
import { connect } from '@planetscale/database' | ||
const config = { | ||
host: '<host>', | ||
username: '<user>', | ||
password: '<password>' | ||
} | ||
const conn = connect(config) | ||
const results = await conn.transaction(async (tx) => { | ||
const whenBranch = await tx.execute('INSERT INTO branches (database_id, name) VALUES (?, ?)', [42, "planetscale"]) | ||
const whenCounter = await tx.execute('INSERT INTO slotted_counters(record_type, record_id, slot, count) VALUES (?, ?, RAND() * 100, 1) ON DUPLICATE KEY UPDATE count = count + 1', ['branch_count', 42]) | ||
return [whenBranch, whenCounter] | ||
}) | ||
console.log(results) | ||
``` | ||
### Custom fetch function | ||
@@ -61,0 +85,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28870
366
166