Skip to content

Commit a6bc943

Browse files
committed
Add with statements and new neon import
1 parent 88964a9 commit a6bc943

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/content/docs/rls.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ export const authenticatedRole = pgRole('authenticated').existing();
303303
export const anonymousRole = pgRole('anonymous').existing();
304304

305305
export const authUid = (userIdColumn: AnyPgColumn) => sql`(select auth.user_id() = ${userIdColumn})`;
306+
307+
export const neonIdentitySchema = pgSchema('neon_identity');
308+
309+
export const usersSync = neonIdentitySchema.table('users_sync', {
310+
rawJson: jsonb('raw_json').notNull(),
311+
id: text().primaryKey().notNull(),
312+
name: text(),
313+
email: text(),
314+
createdAt: timestamp('created_at', { withTimezone: true, mode: 'string' }),
315+
deletedAt: timestamp('deleted_at', { withTimezone: true, mode: 'string' }),
316+
});
306317
```
307318

308319
For example, you can use the `Neon` predefined roles and functions like this:

src/content/docs/select.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,49 @@ select "id", "name", "age" from sq;
471471
```
472472
</Section>
473473

474+
You can also provide `insert`, `update` and `delete` statements inside `with`
475+
476+
<Section>
477+
```typescript copy
478+
const sq = db.$with('sq').as(
479+
db.insert(users).values({ name: 'John' }).returning(),
480+
);
481+
482+
const result = await db.with(sq).select().from(sq);
483+
```
484+
```sql
485+
with "sq" as (insert into "users" ("id", "name") values (default, 'John') returning "id", "name")
486+
select "id", "name" from "sq"
487+
```
488+
</Section>
489+
490+
<Section>
491+
```typescript copy
492+
const sq = db.$with('sq').as(
493+
db.update(users).set({ age: 25 }).where(eq(users.name, 'John')).returning(),
494+
);
495+
const result = await db.with(sq).select().from(sq);
496+
```
497+
```sql
498+
with "sq" as (update "users" set "age" = 25 where "users"."name" = 'John' returning "id", "name", "age")
499+
select "id", "name", "age" from "sq"
500+
```
501+
</Section>
502+
503+
<Section>
504+
```typescript copy
505+
const sq = db.$with('sq').as(
506+
db.delete(users).where(eq(users.name, 'John')).returning(),
507+
);
508+
509+
const result = await db.with(sq).select().from(sq);
510+
```
511+
```sql
512+
with "sq" as (delete from "users" where "users"."name" = $1 returning "id", "name", "age")
513+
select "id", "name", "age" from "sq"
514+
```
515+
</Section>
516+
474517
To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query,
475518
you need to add aliases to them:
476519
```typescript copy

0 commit comments

Comments
 (0)