drizzle-toolbelt
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "drizzle-toolbelt", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Set of tools for drizzle-orm.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -29,4 +29,6 @@ # Drizzle Toolbelt | ||
Takes first element from array or throws if no results. | ||
Takes first element from array or throws if length is zero. | ||
**With default error** | ||
```ts | ||
@@ -37,12 +39,26 @@ import { db } from "./db"; | ||
const firstUserOrThrowDefault = await db.select().from(users) | ||
.then(takeFirstOrThrow); | ||
const firstUserOrThrowDefault = await db | ||
.select() | ||
.from(users) | ||
// throw inline | ||
.then(takeFirstOrThrow()); | ||
const firstUserOrThrow = await db.select().from(users) | ||
.then(takeFirstOrThrow("No users found")); | ||
// or | ||
const firstUserOrThrowCustom = await db.select().from(users) | ||
.then(takeFirstOrThrow(new UnauthorizedError("You cannot view this page."))); | ||
``` | ||
// prepare errors | ||
const takeFirstOrError = takeFirstOrThrow(); | ||
const takeFirstOrUnauthorized = takeFirstOrThrow(new UnauthorizedError("You cannot view this page.")); | ||
// and pass it | ||
const firstUserOrThrow = await db | ||
.select() | ||
.from(users) | ||
.then(takeFirstOrError); | ||
const firstUserOrUnauthorized = await db | ||
.select() | ||
.from(users) | ||
.then(takeFirstOrUnauthorized); | ||
``` | ||
### `aggregate` | ||
@@ -58,4 +74,3 @@ | ||
const usersPosts = await db.select({ | ||
const usersWithPosts = await db.select({ | ||
id: users.id, | ||
@@ -78,9 +93,11 @@ post: posts | ||
id: number; | ||
posts: {id: number, …otherPostProperties}[]; | ||
// `post` was removed | ||
// `posts` was added as an array of `post` | ||
posts: { id: number, …otherPostProperties }[]; | ||
} | ||
``` | ||
### `aggregateRows` | ||
#### Data-first | ||
Data-first version of `aggregate` to be used stand-alone. | ||
You may choose to pass rows in the arguments to call `aggregate` in a stand-alone way. | ||
@@ -90,11 +107,19 @@ ```ts | ||
import { users } from "./schema"; | ||
import { aggregateRows } from "drizzle-toolbelt"; | ||
import { aggregate } from "drizzle-toolbelt"; | ||
const usersRows = await db.select({ | ||
id: users.id, | ||
post: posts | ||
}).from(users) | ||
.leftJoin(posts.id, eq(posts.author_id, users.id)) | ||
const usersCount = await db.select().from(users) | ||
.then(aggregateRows("count")); | ||
const usersWithPosts = aggregateRows({rows, pkey: 'id', fields: { posts: 'post.id' }}); | ||
``` | ||
## Contributing | ||
This project is open to contributions. Feel free to open an issue or a pull request. | ||
## License | ||
MIT |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
150477
8
152
120
0