Skip to content

Commit

Permalink
Merge pull request #3 from kiranojhanp/0.0.10
Browse files Browse the repository at this point in the history
0.0.10
  • Loading branch information
kiranojhanp authored Jan 7, 2025
2 parents 6be6c8f + 450f2bd commit 35aeedd
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 74 deletions.
73 changes: 54 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ npm install pgbuddy
import postgres from "postgres";
import { PgBuddy } from "pgbuddy";

// Define your table types
// Define table type
interface User {
id: number;
name: string;
email: string;
role: string;
created_at: Date;
}

// Initialize
const sql = postgres({
host: env.DB_HOST,
port: env.DB_PORT,
Expand All @@ -60,9 +60,41 @@ const sql = postgres({
});

const db = new PgBuddy(sql);

// Create a type-safe table context
const userTable = db.table<User>("users");

// Example operations
async function examples() {
// Select with search and pagination
const users = await userTable.select({
select: ["id", "name", "email"],
search: { columns: ["name", "email"], query: "john" },
take: 10,
skip: 0,
orderBy: [
{ column: "name", direction: "ASC" },
{ column: "email", direction: "DESC" }
]
});

// Insert with returning values
const newUser = await userTable.insert({
data: { name: "John", email: "[email protected]" },
select: ["id", "name"]
});

// Update with conditions
const updated = await userTable.update({
data: { role: "admin" },
where: { id: 1 },
select: ["id", "name", "role"]
});

// Delete with conditions
const deleted = await userTable.delete({
where: { role: "guest" },
select: ["id", "name"]
});
}
```

### Select Queries
Expand All @@ -71,27 +103,30 @@ const userTable = db.table<User>("users");

```typescript
const users = await userTable.select({
columns: ["id", "name", "email"], // TypeScript will ensure these columns exist
select: ["id", "name", "email"], // TypeScript will ensure these columns exist
});
```

#### Search & Pagination

```typescript
const users = await userTable.select({
columns: ["id", "name", "email"],
select: ["id", "name", "email"],
search: { columns: ["name", "email"], query: "john" },
page: 2,
pageSize: 5,
skip: 0,
take: 10,
});
```

#### Sorting

```typescript
const users = await userTable.select({
columns: ["id", "name"],
orderBy: "name ASC", // Type-safe column names
select: ["id", "name"],
orderBy: [
{ column: "id", direction: "ASC" },
{ column: "name", direction: "DESC" }
]
});
```

Expand All @@ -102,7 +137,7 @@ const users = await userTable.select({
```typescript
const user = await userTable.insert({
data: { name: "John", email: "[email protected]" }, // TypeScript validates the shape
returning: ["id", "name", "email"],
select: ["id", "name", "email"],
});
```

Expand All @@ -124,8 +159,8 @@ const users = await userTable.insert({
```typescript
const updated = await userTable.update({
data: { name: "Updated Name" },
conditions: { id: 1 }, // Type-safe conditions
returning: ["id", "name", "email"],
where: { id: 1 }, // Type-safe conditions
select: ["id", "name", "email"],
});
```

Expand All @@ -134,8 +169,8 @@ const updated = await userTable.update({
```typescript
const deactivated = await userTable.update({
data: { active: false },
conditions: { role: "guest" },
returning: ["id", "name"],
where: { role: "guest" },
select: ["id", "name"],
});
```

Expand All @@ -145,17 +180,17 @@ const deactivated = await userTable.update({

```typescript
const deleted = await userTable.delete({
conditions: { id: 1 },
returning: ["id", "name"],
where: { id: 1 },
select: ["id", "name"],
});
```

#### Delete Multiple Records

```typescript
const deleted = await userTable.delete({
conditions: { status: "inactive" },
returning: ["id", "name"],
where: { status: "inactive" },
select: ["id", "name"],
});
```

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pgbuddy",
"version": "0.0.9",
"version": "0.0.10",
"description": "A lightweight set of helper functions for Postgres.js to simplify simple CRUD operations.",
"files": [
"dist/*"
Expand Down Expand Up @@ -46,4 +46,4 @@
"peerDependencies": {
"postgres": "^3.4.5"
}
}
}
Loading

0 comments on commit 35aeedd

Please sign in to comment.