3 Commits

Author SHA1 Message Date
3793e14f50 Add v prefix to all version tags 2025-01-11 00:53:45 +11:00
b194397645 Optimise query request write 2025-01-11 00:46:17 +11:00
858b7a95f3 Add streaming query testing code 2025-01-11 00:39:00 +11:00
6 changed files with 61 additions and 23 deletions

View File

@@ -14,9 +14,9 @@ The glue for TypeScript to PostgreSQL.
## Installation
```ts
import pglue from "https://git.lua.re/luaneko/pglue/raw/tag/0.1.1/mod.ts";
import pglue from "https://git.lua.re/luaneko/pglue/raw/tag/v0.1.3/mod.ts";
// ...or from github:
import pglue from "https://raw.githubusercontent.com/luaneko/pglue/refs/tags/0.1.1/mod.ts";
import pglue from "https://raw.githubusercontent.com/luaneko/pglue/refs/tags/v0.1.3/mod.ts";
```
## Documentation

View File

@@ -1,5 +1,5 @@
{
"name": "@luaneko/pglue",
"version": "0.1.1",
"version": "0.1.3",
"exports": "./mod.ts"
}

12
deno.lock generated
View File

@@ -460,11 +460,11 @@
"https://deno.land/x/postgresjs@v3.4.5/src/result.js": "001ff5e0c8d634674f483d07fbcd620a797e3101f842d6c20ca3ace936260465",
"https://deno.land/x/postgresjs@v3.4.5/src/subscribe.js": "9e4d0c3e573a6048e77ee2f15abbd5bcd17da9ca85a78c914553472c6d6c169b",
"https://deno.land/x/postgresjs@v3.4.5/src/types.js": "471f4a6c35412aa202a7c177c0a7e5a7c3bd225f01bbde67c947894c1b8bf6ed",
"https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/async.ts": "20bc54c7260c2d2cd27ffcca33b903dde57a3a3635386d8e0c6baca4b253ae4e",
"https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/bytes.ts": "5ffb12787dc3f9ef9680b6e2e4f5f9903783aa4c33b69e725b5df1d1c116bfe6",
"https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/events.ts": "28d395b8eea87f9bf7908a44b351d2d3c609ba7eab62bcecd0d43be8ee603438",
"https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/func.ts": "f1935f673365cd68939531d65ef18fe81b5d43dc795b03c34bb5ad821ab1c9ff",
"https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/jit.ts": "c1db7820de95c48521b057c7cdf9aa41f7eaba77462407c29d3932e7da252d53",
"https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/mod.ts": "95d8b15048a54cb82391825831f695b74e7c8b206317264a99c906ce25c63f13"
"https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/async.ts": "20bc54c7260c2d2cd27ffcca33b903dde57a3a3635386d8e0c6baca4b253ae4e",
"https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/bytes.ts": "5ffb12787dc3f9ef9680b6e2e4f5f9903783aa4c33b69e725b5df1d1c116bfe6",
"https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/events.ts": "28d395b8eea87f9bf7908a44b351d2d3c609ba7eab62bcecd0d43be8ee603438",
"https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/func.ts": "f1935f673365cd68939531d65ef18fe81b5d43dc795b03c34bb5ad821ab1c9ff",
"https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/jit.ts": "c1db7820de95c48521b057c7cdf9aa41f7eaba77462407c29d3932e7da252d53",
"https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/mod.ts": "95d8b15048a54cb82391825831f695b74e7c8b206317264a99c906ce25c63f13"
}
}

View File

@@ -1 +1 @@
export * from "https://git.lua.re/luaneko/lstd/raw/tag/0.2.0/mod.ts";
export * from "https://git.lua.re/luaneko/lstd/raw/tag/v0.2.0/mod.ts";

19
test.ts
View File

@@ -188,3 +188,22 @@ Deno.test(`transactions`, async () => {
);
});
});
Deno.test(`streaming`, async () => {
await using pg = await connect();
await using _tx = await pg.begin();
await pg.query`create table my_table (field text not null)`;
for (let i = 0; i < 100; i++) {
await pg.query`insert into my_table (field) values (${i})`;
}
let i = 0;
for await (const chunk of pg.query`select * from my_table`.chunked(10)) {
expect(chunk.length).toBe(10);
for (const row of chunk) expect(row.field).toBe(`${i++}`);
}
expect(i).toBe(100);
});

41
wire.ts
View File

@@ -579,6 +579,11 @@ export class Wire extends TypedEmitter<WireEvents> implements Disposable {
}
}
const msg_PD = object({ P: Parse, D: Describe });
const msg_BE = object({ B: Bind, E: Execute });
const msg_BEc = object({ B: Bind, E: Execute, c: CopyDone });
const msg_BEcC = object({ B: Bind, E: Execute, c: CopyDone, C: Close });
function wire_impl(
wire: Wire,
socket: Deno.Conn,
@@ -904,10 +909,11 @@ function wire_impl(
try {
const { name, query } = this;
return await pipeline(
async () => {
await write(Parse, { statement: name, query, param_types: [] });
await write(Describe, { which: "S", name });
},
() =>
write(msg_PD, {
P: { statement: name, query, param_types: [] },
D: { which: "S", name },
}),
async () => {
await read(ParseComplete);
const param_desc = await read(ParameterDescription);
@@ -1078,16 +1084,23 @@ function wire_impl(
try {
const { rows, tag } = await pipeline(
async () => {
await write(Bind, {
const B = {
portal,
statement: st.name,
param_formats: [],
param_values,
column_formats: [],
});
await write(Execute, { portal, row_limit: 0 });
};
const E = { portal, row_limit: 0 };
const C = { which: "P" as const, name: portal };
if (stdin !== null) {
await write(msg_BE, { B, E });
await write_copy_in(stdin);
await write(Close, { which: "P" as const, name: portal });
await write(Close, C);
} else {
return write(msg_BEcC, { B, E, c: {}, C });
}
},
async () => {
await read(BindComplete);
@@ -1131,15 +1144,21 @@ function wire_impl(
try {
let { done, rows, tag } = await pipeline(
async () => {
await write(Bind, {
const B = {
portal,
statement: st.name,
param_formats: [],
param_values,
column_formats: [],
});
await write(Execute, { portal, row_limit: chunk_size });
};
const E = { portal, row_limit: chunk_size };
if (stdin !== null) {
await write(msg_BE, { B, E });
await write_copy_in(stdin);
} else {
return write(msg_BEc, { B, E, c: {} });
}
},
async () => {
await read(BindComplete);