diff --git a/cf/src/query.js b/cf/src/query.js index 848f3b88..0d44a15c 100644 --- a/cf/src/query.js +++ b/cf/src/query.js @@ -37,13 +37,12 @@ export class Query extends Promise { } get origin() { - return this.handler.debug + return (this.handler.debug ? this[originError].stack - : this.tagged - ? originStackCache.has(this.strings) - ? originStackCache.get(this.strings) - : originStackCache.set(this.strings, this[originError].stack).get(this.strings) - : '' + : this.tagged && originStackCache.has(this.strings) + ? originStackCache.get(this.strings) + : originStackCache.set(this.strings, this[originError].stack).get(this.strings) + ) || '' } static get [Symbol.species]() { diff --git a/cjs/src/query.js b/cjs/src/query.js index 7246c5f3..45327f2f 100644 --- a/cjs/src/query.js +++ b/cjs/src/query.js @@ -37,13 +37,12 @@ const Query = module.exports.Query = class Query extends Promise { } get origin() { - return this.handler.debug + return (this.handler.debug ? this[originError].stack - : this.tagged - ? originStackCache.has(this.strings) - ? originStackCache.get(this.strings) - : originStackCache.set(this.strings, this[originError].stack).get(this.strings) - : '' + : this.tagged && originStackCache.has(this.strings) + ? originStackCache.get(this.strings) + : originStackCache.set(this.strings, this[originError].stack).get(this.strings) + ) || '' } static get [Symbol.species]() { diff --git a/deno/README.md b/deno/README.md index 4c6d0fc8..19fd0993 100644 --- a/deno/README.md +++ b/deno/README.md @@ -1056,6 +1056,34 @@ const sql = postgres({ }) ``` +### Cloudflare Workers support + +Postgres.js has built-in support for the [TCP socket API](https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/) in Cloudflare Workers, which is [on-track](https://github.com/wintercg/proposal-sockets-api) to be standardized and adopted in Node.js and other JavaScript runtimes, such as Deno. + +You can use Postgres.js directly in a Worker, or to benefit from connection pooling and query caching, via the [Hyperdrive](https://developers.cloudflare.com/hyperdrive/learning/connect-to-postgres/#driver-examples) service available to Workers by passing the Hyperdrive `connectionString` when creating a new `postgres` client as follows: + +```ts +// Requires Postgres.js 3.4.0 or later +import postgres from 'postgres' + +interface Env { + HYPERDRIVE: Hyperdrive; +} + +export default async fetch(req: Request, env: Env, ctx: ExecutionContext) { + // The Postgres.js library accepts a connection string directly + const sql = postgres(env.HYPERDRIVE.connectionString) + const results = await sql`SELECT * FROM users LIMIT 10` + return Response.json(results) +} +``` + +In `wrangler.toml` you will need to enable `node_compat` to allow Postgres.js to operate in the Workers environment: + +```toml +node_compat = true # required for database drivers to function +``` + ### Auto fetching of array types Postgres.js will automatically fetch table/array-type information when it first connects to a database. diff --git a/deno/src/query.js b/deno/src/query.js index 848f3b88..0d44a15c 100644 --- a/deno/src/query.js +++ b/deno/src/query.js @@ -37,13 +37,12 @@ export class Query extends Promise { } get origin() { - return this.handler.debug + return (this.handler.debug ? this[originError].stack - : this.tagged - ? originStackCache.has(this.strings) - ? originStackCache.get(this.strings) - : originStackCache.set(this.strings, this[originError].stack).get(this.strings) - : '' + : this.tagged && originStackCache.has(this.strings) + ? originStackCache.get(this.strings) + : originStackCache.set(this.strings, this[originError].stack).get(this.strings) + ) || '' } static get [Symbol.species]() { diff --git a/deno/types/index.d.ts b/deno/types/index.d.ts index 0fb74e03..215d5b62 100644 --- a/deno/types/index.d.ts +++ b/deno/types/index.d.ts @@ -179,9 +179,17 @@ type Rest = T extends TemplateStringsArray ? never : // force fallback to the tagged template function overload T extends string ? readonly string[] : T extends readonly any[][] ? readonly [] : - T extends readonly (object & infer R)[] ? readonly (Keys & keyof R)[] : + T extends readonly (object & infer R)[] ? ( + readonly (Keys & keyof R)[] // sql(data, "prop", "prop2") syntax + | + [readonly (Keys & keyof R)[]] // sql(data, ["prop", "prop2"]) syntax + ) : T extends readonly any[] ? readonly [] : - T extends object ? readonly (Keys & keyof T)[] : + T extends object ? ( + readonly (Keys & keyof T)[] // sql(data, "prop", "prop2") syntax + | + [readonly (Keys & keyof T)[]] // sql(data, ["prop", "prop2"]) syntax + ) : any type Return =