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
7 changes: 7 additions & 0 deletions src/demo/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe("demo.ts examples", async () => {
"Example 1"?: { species: string }[];
"Example 2"?: { key: string; isNum: boolean }[];
"Example 3"?: { personName: string; petSpecies: string; petAge: number }[];
"Example 4"?: { result: number }[];
} = {};

const spyConsole = vi
Expand All @@ -21,6 +22,7 @@ describe("demo.ts examples", async () => {
"Example 1": example1,
"Example 2": example2,
"Example 3": example3,
"Example 4": example4,
} = calls;

it("example1: grouping and aggregation on pets", async () => {
Expand Down Expand Up @@ -63,4 +65,9 @@ describe("demo.ts examples", async () => {
{ personName: "Charlie", petSpecies: "dog", petAge: 10 },
]);
});

it("example4: math coverage", async () => {
expect(Array.isArray(example4)).toBe(true);
expect(example4).toEqual([{ result: 1.669026835867367 }]);
});
});
26 changes: 24 additions & 2 deletions src/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tg = await typegres({ type: "pglite" });
const pets = values(
{ species: Text.new("cat"), age: Float8.new(2), id: Int8.new(1n) },
{ species: Text.new("dog"), age: Float8.new(3), id: Int8.new(2n) },
{ species: Text.new("dog"), age: Float8.new(10), id: Int8.new(3n) },
{ species: Text.new("dog"), age: Float8.new(10), id: Int8.new(3n) }
);

const example1 = await pets
Expand Down Expand Up @@ -41,7 +41,7 @@ console.log("Example 2", example2);
const people = values(
{ id: Int8.new(1n), name: Text.new("Alice"), petId: Int8.new(1n) },
{ id: Int8.new(2n), name: Text.new("Bob"), petId: Int8.new(2n) },
{ id: Int8.new(3n), name: Text.new("Charlie"), petId: Int8.new(3n) },
{ id: Int8.new(3n), name: Text.new("Charlie"), petId: Int8.new(3n) }
);

const example3 = await people
Expand All @@ -55,3 +55,25 @@ const example3 = await people
.execute(tg);

console.log("Example 3", example3);

// Example 4: Math coverage (https://x.com/dshukertjr/status/1948730454535798799)
import { sin, cos, exp, ln, tan, abs, sqrt } from "typegres";
const homework = await values({
x: Float8.new(1.5),
y: Float8.new(0.7),
})
.select(({ x, y }) => ({
result: sin(x)
["+"](cos(y))
["/"](ln(x.power(2)["+"](1))["+"](1))
.power(2)
["+"](
exp(Float8.new(0)["-"](y.power(2)))["*"](
sqrt(abs(tan(x["*"](y)))["+"](1))
)
),
}))
.debug()
.execute(tg);

console.log("Example 4", homework);
8 changes: 8 additions & 0 deletions src/gen/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ type FunctionsFile = {
};

const keepFunction = (name: string, definitionns: FunctionDefinition[]) => {
const falsePositives = [
// ends with `in`:
"sin",
];
if (falsePositives.includes(name)) {
return true;
}

return (
// Skip functions that start with _send/recv/in/out
!/_?(?:send|recv|in|out)$/.test(name) &&
Expand Down
57 changes: 48 additions & 9 deletions src/gen/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,70 @@ import * as Types from "../types";
import { database } from "../query/db";

export const db = database({
kysely_migration_lock: {
id: Types.Text<1>,
is_locked: Types.Int4<1>,
},
update_test_users: {
id: Types.Int4<1>,
active: Types.Int4<0>,
name: Types.Text<1>,
email: Types.Text<1>,
role: Types.Text<0>,
},
kysely_migration: {
name: Types.Text<1>,
timestamp: Types.Text<1>,
},
example_3d_array: {
data: Types.Array<0, Types.Int4<0 | 1>>,
id: Types.Int4<1>,
data_1d: Types.Array<0, Types.Int4<0 | 1>>,
},
users: {
name: Types.Text<1>,
email: Types.Text<1>,
role: Types.Text<0>,
id: Types.Int4<1>,
active: Types.Int4<0>,
},
person: {
firstName: Types.Text<1>,
lastName: Types.Text<0>,
gender: Types.Text<1>,
id: Types.Int4<1>,
createdAt: Types.Timestamp<1>,
},
kysely_migration_lock: {
id: Types.Text<1>,
is_locked: Types.Int4<1>,
},
pet: {
name: Types.Text<1>,
ownerId: Types.Int4<1>,
species: Types.Text<1>,
age: Types.Int4<1>,
id: Types.Int4<1>,
},
kysely_migration: {
update_test_posts: {
title: Types.Text<1>,
content: Types.Text<0>,
user_id: Types.Int4<1>,
id: Types.Int4<1>,
published: Types.Int4<0>,
},
test_users: {
id: Types.Int4<1>,
balance: Types.Int4<0>,
name: Types.Text<1>,
timestamp: Types.Text<1>,
},
example_3d_array: {
data: Types.Array<0, Types.Int4<0 | 1>>,
posts: {
title: Types.Text<1>,
content: Types.Text<0>,
user_id: Types.Int4<1>,
id: Types.Int4<1>,
published: Types.Int4<0>,
},
comments: {
post_id: Types.Int4<1>,
user_id: Types.Int4<1>,
content: Types.Text<1>,
id: Types.Int4<1>,
data_1d: Types.Array<0, Types.Int4<0 | 1>>,
},
});