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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The PostgreSQL implementation of `@antelopejs/interface-database` and `@antelope

## Versioning

- Both implemented interfaces stay capped below the next minor (`interface-database` `>=0.3.3 <0.4.0`, `interface-database-catalog` `>=0.0.3 <0.1.0`), because a new minor can add required handlers. `pnpm lint` enforces it with `antelopejs-check-interface-ranges`.
- Both implemented interfaces stay capped below the next minor (`interface-database` `>=0.4.0 <0.5.0`, `interface-database-catalog` `>=0.0.3 <0.1.0`), because a new minor can add required handlers. `pnpm lint` enforces it with `antelopejs-check-interface-ranges`.
- When a dependency range changes, update the compatibility table in `README.md` too.
- A release that needs action from users gets an `### Upgrade notes` section in `CHANGELOG.md`, above the generated entries.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ This repository documents what is specific to the PostgreSQL backend:
| Package | Supported | Tested with |
| ---------------------------------------- | ---------------- | ----------- |
| `@antelopejs/interface-core` | `>=0.1.1 <1.0.0` | `0.4.0` |
| `@antelopejs/interface-database` | `>=0.3.3 <0.4.0` | `0.3.3` |
| `@antelopejs/interface-database` | `>=0.4.0 <0.5.0` | `0.4.0` |
| `@antelopejs/interface-database-catalog` | `>=0.0.3 <0.1.0` | `0.0.3` |

<!-- antelopejs-docs-compat:end -->
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"dependencies": {
"@antelopejs/interface-core": ">=0.1.1 <1.0.0",
"@antelopejs/interface-database": ">=0.3.3 <0.4.0",
"@antelopejs/interface-database": ">=0.4.0 <0.5.0",
"@antelopejs/interface-database-catalog": ">=0.0.3 <0.1.0",
"pg": "^8.22.0"
},
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions src/test-external/native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ describe("an external key that its column cannot hold", () => {
["order_lines", '["x", "1"]'],
]) {
it(`gets nothing for ${key} on ${table}, rather than failing the request`, async () => {
expect(await Table(table).get(key)).to.equal(undefined);
expect(await Table(table).get(key).run()).to.equal(undefined);
});
}

it("filters nothing for an enum label the type does not have", async () => {
const total = await Table("accounts")
.filter((row) => row.key("status").eq("archived"))
.count();
.count()
.run();
expect(total).to.equal(0);
});
});
Expand Down Expand Up @@ -117,7 +118,8 @@ describe("numbers read from an external relation's exact types", () => {
it("sums a numeric column exactly", async () => {
const total = await Table("accounts")
.filter((row) => row.key("active").eq(true))
.sum("revenue");
.sum("revenue")
.run();
const [expected] = await Native<{ total: number }>(
"SELECT sum(revenue)::double precision AS total FROM sales.accounts WHERE active",
);
Expand All @@ -136,7 +138,8 @@ describe("numbers read from an external relation's exact types", () => {
it(`keeps the meaning of '${operator} 5.5' on an integer column`, async () => {
const total = await Table("accounts")
.filter((row) => row.key("score")[method](5.5))
.count();
.count()
.run();
expect(total).to.equal(
await NativeCount(
`SELECT count(*) FROM sales.accounts WHERE score ${operator} 5.5`,
Expand All @@ -162,10 +165,12 @@ describe("inequality against an external column's own type", () => {
it(`answers ${field} ne ${JSON.stringify(value)} as the complement of eq, keeping NULL rows`, async () => {
const unequal = await Table("flags")
.filter((row) => row.key(field).ne(value))
.count();
.count()
.run();
const equal = await Table("flags")
.filter((row) => row.key(field).eq(value))
.count();
.count()
.run();
expect(unequal).to.equal(expected);
expect(unequal + equal).to.equal(2);
});
Expand Down
62 changes: 41 additions & 21 deletions src/test-external/reads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe("listing an external relation as a TableView does", () => {
.filter((row) => row.key("status").eq("active"))
.filter((row) => row.key("name").match("(?i)acct 1"))
.slice(0, 5)
.pluck("_internal", "_id", "name", "status");
.pluck("_internal", "_id", "name", "status")
.run();
const expected = await Native<Row>(
`SELECT id::text AS "_id", name, status::text AS status FROM sales.accounts
WHERE status = 'active' AND name ~* 'acct 1' ORDER BY name DESC LIMIT 5`,
Expand All @@ -55,7 +56,8 @@ describe("listing an external relation as a TableView does", () => {
const total = await Accounts()
.filter((row) => row.key("status").eq("active"))
.filter((row) => row.key("name").match("(?i)acct 1"))
.count();
.count()
.run();
expect(total).to.equal(
await NativeCount(
"SELECT count(*) FROM sales.accounts WHERE status = 'active' AND name ~* 'acct 1'",
Expand All @@ -67,7 +69,8 @@ describe("listing an external relation as a TableView does", () => {
const total = await Accounts()
.filter((row) => row.key("score").gt(97))
.filter((row) => row.key("revenue").ge(267000))
.count();
.count()
.run();
expect(total).to.equal(
await NativeCount(
"SELECT count(*) FROM sales.accounts WHERE score > 97 AND revenue >= 267000",
Expand All @@ -91,7 +94,8 @@ describe("listing an external relation as a TableView does", () => {
.ge(Day(day))
.and(row.key(column).lt(NextDay(day))),
)
.count();
.count()
.run();
expect(total).to.equal(
await NativeCount(
`SELECT count(*) FROM sales.${table}
Expand All @@ -106,7 +110,8 @@ describe("listing an external relation as a TableView does", () => {
it("searches a date column through its ISO rendering, as a TableView search does", async () => {
const total = await Accounts()
.filter((row) => row.key("created_at").match("(?i)2026-01-02T0"))
.count();
.count()
.run();
expect(total).to.equal(10);
});

Expand All @@ -118,7 +123,8 @@ describe("listing an external relation as a TableView does", () => {
.match("(?i)CHURN")
.or(row.key("external_ref").match("(?i)^0")),
)
.count();
.count()
.run();
expect(total).to.equal(
await NativeCount(
"SELECT count(*) FROM sales.accounts WHERE status::text ~* 'churn' OR external_ref::text ~* '^0'",
Expand All @@ -135,7 +141,8 @@ describe("reading an external row by key", () => {
it("gets a row by its bigint key as a string _id, with exact bigints and true instants", async () => {
const row = await Accounts()
.get("42")
.pluck("_id", "id", "big_counter", "signed_on", "last_seen");
.pluck("_id", "id", "big_counter", "signed_on", "last_seen")
.run();
expect(row).to.deep.equal({
_id: "42",
id: "42",
Expand All @@ -149,7 +156,10 @@ describe("reading an external row by key", () => {
const original = process.env.TZ;
process.env.TZ = "America/New_York";
try {
const row = await Accounts().get("42").pluck("signed_on", "last_seen");
const row = await Accounts()
.get("42")
.pluck("signed_on", "last_seen")
.run();
expect(row).to.deep.equal({
signed_on: new Date("2026-02-12T00:00:00.000Z"),
last_seen: new Date("2026-01-01T00:42:00.000Z"),
Expand All @@ -160,7 +170,7 @@ describe("reading an external row by key", () => {
});

it("reads a whole row in row mode with _id first and every declared field", async () => {
const row = await Accounts().get("7");
const row = await Accounts().get("7").run();
expect(Object.keys(row)[0]).to.equal("_id");
expect(row).to.include({
_id: "7",
Expand All @@ -176,7 +186,7 @@ describe("reading an external row by key", () => {
const [known] = await Native<{ id: string }>(
"SELECT contact_id::text AS id FROM sales.contacts ORDER BY first_name LIMIT 1",
);
const rows = await Contacts().getAll([known.id, "not-a-uuid"], "_id");
const rows = await Contacts().getAll([known.id, "not-a-uuid"], "_id").run();
expect(rows.map((row) => row._id)).to.deep.equal([known.id]);
});
});
Expand All @@ -191,7 +201,8 @@ describe("reading across external relations", () => {
.orderBy("first_name", "asc")
.slice(0, 3)
.lookup(Accounts().pluck("_internal", "_id", "name"), "account_id", "_id")
.pluck("first_name", "account_id");
.pluck("first_name", "account_id")
.run();
const expected = await Native<Row>(
`SELECT c.first_name, jsonb_build_object('_id', a.id::text, 'name', a.name) AS account_id
FROM sales.contacts c JOIN sales.accounts a ON a.id = c.account_id
Expand All @@ -216,7 +227,8 @@ describe("reading across external relations", () => {
}))
.group("bucket", (rows, group) => ({ x: group, y: rows.count() }))
.orderBy("x", "asc")
.slice(0, 3);
.slice(0, 3)
.run();
const expected = await Native<{ x: string; y: string }>(
`SELECT to_char(created_at AT TIME ZONE 'UTC' AT TIME ZONE '${PARIS}', 'YYYYMMDD') AS x, count(*) AS y
FROM sales.contacts GROUP BY 1 ORDER BY 1 LIMIT 3`,
Expand All @@ -227,11 +239,12 @@ describe("reading across external relations", () => {
});

it("reads an undeclared key, such as the _internal data-api plucks, as absent", async () => {
const row = await Accounts().get("1").pluck("_internal", "name");
const row = await Accounts().get("1").pluck("_internal", "name").run();
expect(row).to.deep.equal({ name: "Acct 1" });
const total = await Accounts()
.filter((row) => row.key("nickname").eq("x"))
.count();
.count()
.run();
expect(total).to.equal(0);
});
});
Expand All @@ -246,28 +259,35 @@ describe("the _id of relations without a single-column primary key", () => {
.instance()
.table("order_lines")
.orderBy("_id")
.slice(0, 1);
.slice(0, 1)
.run();
expect(first._id).to.equal('["0", "1"]');
const again = await sales
.instance()
.table("order_lines")
.get(String(first._id));
.get(String(first._id))
.run();
expect(again).to.deep.equal(first);
});

it("answers _id from the unique index of a table without a primary key", async () => {
const row = await sales.instance().table("events").orderBy("at").nth(0);
const row = await sales
.instance()
.table("events")
.orderBy("at")
.nth(0)
.run();
if (row === null) throw new Error("the relation answered no row");
expect(row._id).to.equal(row.event_ref);
});

for (const relation of ["raw_log", "big_accounts", "daily_signups"]) {
it(`counts ${relation}, which has no key, and its rows answer no _id`, async () => {
const table = sales.instance().table(relation);
expect(await table.count()).to.equal(
expect(await table.count().run()).to.equal(
await NativeCount(`SELECT count(*) FROM sales.${relation}`),
);
const [row] = await table.slice(0, 1);
const [row] = await table.slice(0, 1).run();
expect(row).to.not.have.property("_id");
});
}
Expand All @@ -280,14 +300,14 @@ describe("instances over an external relation", () => {

it("reads every row through CROSS_INSTANCE, since an external relation has no instances", async () => {
expect(
await sales.instance(CROSS_INSTANCE).table("accounts").count(),
await sales.instance(CROSS_INSTANCE).table("accounts").count().run(),
).to.equal(20000);
});

it("refuses a named instance rather than answer rows nobody scoped to it", async () => {
let refusal: Error | undefined;
try {
await sales.instance("tenant-1").table("accounts").count();
await sales.instance("tenant-1").table("accounts").count().run();
} catch (err) {
if (!(err instanceof Error)) throw err;
refusal = err;
Expand Down
16 changes: 10 additions & 6 deletions src/test-external/refusals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("what an external relation refuses", () => {
const expected = stage === "atomicMutation" ? stage : "may not write to";
it(`refuses ${stage} on a relation the connected role may not write, naming it, and writes nothing`, async () => {
const message = await Refusal(async () => {
await write();
await write().run();
});
expect(message).to.contain(expected).and.to.contain('"sales"."accounts"');
});
Expand All @@ -91,18 +91,18 @@ describe("what an external relation refuses", () => {
it("refuses creating and destroying an instance", async () => {
expect(
await Refusal(async () => {
await sales.createInstance("t1");
await sales.createInstance("t1").run();
}),
).to.contain("createInstance");
expect(
await Refusal(async () => {
await sales.destroyInstance("t1");
await sales.destroyInstance("t1").run();
}),
).to.contain("destroyInstance");
});

it("lists no instances", async () => {
expect(await sales.listInstances()).to.deep.equal([]);
expect(await sales.listInstances().run()).to.deep.equal([]);
});

for (const { description, read } of [
Expand All @@ -121,14 +121,18 @@ describe("what an external relation refuses", () => {
]) {
it(`refuses ${description} on a relation with no key, naming it`, async () => {
const message = await Refusal(async () => {
await read();
await read().run();
});
expect(message).to.contain('"sales"."raw_log"').and.to.contain("no _id");
});
}

it("still reads a keyless relation by a field", async () => {
const rows = await sales.instance().table("raw_log").getAll(["r1"], "msg");
const rows = await sales
.instance()
.table("raw_log")
.getAll(["r1"], "msg")
.run();
expect(rows).to.have.length(1);
});
});
Loading
Loading