### Install Drizzle ORM v1 RC Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Installs the beta versions of drizzle-orm and drizzle-kit. Use this command to begin the upgrade process. ```bash npm i drizzle-orm@beta drizzle-kit@beta -D ``` -------------------------------- ### Drizzle ORM v1 Database Instance Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Example of creating a database instance with Drizzle ORM v1, specifying the schema and mode. ```typescript const db = drizzle(url, { schema, mode: "planetscale" }); ``` -------------------------------- ### Drizzle ORM v2 Database Instance Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Example of creating a database instance with Drizzle ORM v2. The `mode` option is removed, and `relations` are passed directly. ```typescript import { relations } from './relations'; const db = drizzle(url, { relations }); ``` -------------------------------- ### Drizzle ORM v1 Relations Definition Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Example of defining relations for a single table using the older v1 syntax. Note the import path for `relations`. ```typescript import { relations } from "drizzle-orm/_relations"; // Note: moved import export const usersRelation = relations(users, ({ one, many }) => ({ posts: many(posts), })); ``` -------------------------------- ### Custom BigInt Type with JSON Handling (v2) Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Define custom data types for specific database columns, including custom logic for JSON serialization and deserialization. This example shows a custom `bigint` type. ```typescript const customBigint = customType<{ data: bigint; driverData: string; jsonData: string; }>({ dataType: () => 'bigint', fromJson: (value) => BigInt(value), // New: JSON -> JS forJsonSelect: (identifier, sql) => sql`${identifier}::text`, // New: SQL for JSON }); ``` -------------------------------- ### Run Drizzle Kit Migration Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Migrates your existing migrations folder to the new v1 format. This command cleans up old files and organizes SQL files and snapshots. ```bash npx drizzle-kit up ``` -------------------------------- ### Migrate One-to-Many Relations from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Shows the v2 syntax for one-to-many relations, simplifying the definition compared to v1. ```typescript export const usersRelation = relations(users, ({ many }) => ({ posts: many(posts), })); export const postsRelation = relations(posts, ({ one }) => ({ author: one(users, { fields: [posts.authorId], references: [users.id], }), })); ``` ```typescript export const relations = defineRelations(schema, (r) => ({ users: { posts: r.many.posts({ from: r.users.id, to: r.posts.authorId, }), }, posts: { author: r.one.users({ from: r.posts.authorId, to: r.users.id, }), }, })); ``` -------------------------------- ### Generate Relations Schema with drizzle-kit pull Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Use `drizzle-kit pull` to automatically generate the `drizzle/relations.ts` file with v2 syntax. Remember to update the import path to your schema. ```bash npx drizzle-kit pull ``` ```typescript import * as schema from './schema'; // Update path to your schema ``` -------------------------------- ### Drizzle ORM v1 Query Syntax Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Demonstrates the function-based syntax for `where` and `orderBy` clauses in Drizzle ORM v1 queries. ```typescript await db.query.users.findMany({ where: (users, { eq }) => eq(users.id, 1), orderBy: (users, { asc }) => [asc(users.id)], }); ``` -------------------------------- ### Migrate Many-to-Many Relations from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Illustrates the v2 syntax for many-to-many relations, which uses a `through` clause for clarity. ```typescript export const usersRelations = relations(users, ({ many }) => ({ usersToGroups: many(usersToGroups), })); export const groupsRelations = relations(groups, ({ many }) => ({ usersToGroups: many(usersToGroups), })); export const usersToGroupsRelations = relations(usersToGroups, ({ one }) => ({ group: one(groups, { fields: [usersToGroups.groupId], references: [groups.id] }), user: one(users, { fields: [usersToGroups.userId], references: [users.id] }), })); ``` ```typescript export const relations = defineRelations(schema, (r) => ({ users: { groups: r.many.groups({ from: r.users.id.through(r.usersToGroups.userId), to: r.groups.id.through(r.usersToGroups.groupId), }), }, groups: { participants: r.many.users({ from: r.groups.id.through(r.usersToGroups.groupId), to: r.users.id.through(r.usersToGroups.userId), }), }, })); ``` -------------------------------- ### Migrate One-to-One Relations from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Demonstrates the syntax change for defining one-to-one relations. v2 uses `defineRelations` and a more concise mapping. ```typescript export const usersRelation = relations(users, ({ one }) => ({ profile: one(profiles, { fields: [users.id], references: [profiles.userId], }), })); ``` ```typescript export const relations = defineRelations(schema, (r) => ({ users: { profile: r.one.profiles({ from: r.users.id, to: r.profiles.userId, }), }, })); ``` -------------------------------- ### Drizzle ORM v2 Query Syntax Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Shows the new object-based syntax for `where` and `orderBy` clauses in Drizzle ORM v2 queries, offering a more concise approach. ```typescript await db.query.users.findMany({ where: { id: 1 }, orderBy: { id: "asc" }, }); ``` -------------------------------- ### Filter Operators in v2 `where` Statement Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Lists common filter operators available in v2, showing their object syntax and corresponding SQL. ```typescript | Operator | Example | SQL | |----------|---------|-----| | `eq` (default) | `{ id: 1 }` | `id = 1` | | `ne` | `{ id: { ne: 1 } }` | `id != 1` | | `gt` | `{ age: { gt: 18 } }` | `age > 18` | | `gte` | `{ age: { gte: 18 } }` | `age >= 18` | | `lt` | `{ age: { lt: 65 } }` | `age < 65` | | `lte` | `{ age: { lte: 65 } }` | `age <= 65` | | `like` | `{ name: { like: "J%" } }` | `name LIKE 'J%'` | | `ilike` | `{ name: { ilike: "j%" } }` | `name ILIKE 'j%'` | | `inArray` | `{ id: { inArray: [1, 2, 3] } }` | `id IN (1, 2, 3)` | | `notInArray` | `{ id: { notInArray: [1, 2] } }` | `id NOT IN (1, 2)` | | `isNull` | `{ deletedAt: { isNull: true } }` | `deleted_at IS NULL` | | `isNotNull` | `{ name: { isNotNull: true } }` | `name IS NOT NULL` | ``` -------------------------------- ### Split Relation Definitions (v2) Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Organize relation definitions into multiple parts using `defineRelationsPart` and merge them when creating the Drizzle instance. This improves code modularity for large schemas. ```typescript import { defineRelations, defineRelationsPart } from 'drizzle-orm'; export const relations = defineRelations(schema, (r) => ({ users: { /* ... */ }, })); export const part = defineRelationsPart(schema, (r) => ({ posts: { /* ... */ }, })); // Merge when creating db const db = drizzle(url, { relations: { ...relations, ...part } }); ``` -------------------------------- ### Migrate Self-Referential Relations from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Shows the v2 syntax for self-referential relations, using `alias` instead of `relationName`. ```typescript export const usersRelation = relations(users, ({ one }) => ({ invitee: one(users, { fields: [users.invitedBy], references: [users.id], relationName: "inviter", }), })); ``` ```typescript export const relations = defineRelations(schema, (r) => ({ users: { invitee: r.one.users({ from: r.users.invitedBy, to: r.users.id, alias: "inviter", // renamed from relationName }), }, })); ``` -------------------------------- ### Paginate Related Objects with Offset (v2) Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Apply limit and offset to both the main query and nested relations. This is useful for deep pagination within related data structures. ```typescript await db.query.posts.findMany({ limit: 5, offset: 2, with: { comments: { offset: 3, limit: 3, }, }, }); ``` -------------------------------- ### Define Optional Relations (v2) Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Mark a relation as optional using `optional: false` to enforce its presence at the type level. This is useful for ensuring required foreign keys. ```typescript r.one.users({ from: r.posts.authorId, to: r.users.id, optional: false, // Makes author required at type level }) ``` -------------------------------- ### Drizzle ORM v2 Advanced Filtering Operators Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Demonstrates the use of object-based filters in Drizzle ORM v2, including `AND`, `OR`, `NOT`, and `RAW` for complex query conditions. ```typescript await db.query.users.findMany({ where: { AND: [ { OR: [{ name: { like: "John%" } }, { name: { ilike: "jane%" } }] }, { age: { gt: 18 } }, { NOT: { status: "banned" } }, { RAW: (t) => sql`${t.createdAt} > NOW() - INTERVAL '30 days'` }, ], }, }); ``` -------------------------------- ### Migrate `orderBy` Statement from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md The v2 `orderBy` statement uses a simpler object syntax for specifying sort order. ```typescript orderBy: (users, { asc, desc }) => [asc(users.name), desc(users.createdAt)] ``` ```typescript orderBy: { name: "asc", createdAt: "desc" } ``` -------------------------------- ### Migrate `where` Statement from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md The v2 `where` statement uses a more declarative object-based syntax, simplifying complex conditions. ```typescript db.query.users.findMany({ where: (users, { eq, and, gt }) => and(eq(users.name, "John"), gt(users.age, 18)), }); ``` ```typescript db.query.users.findMany({ where: { name: "John", age: { gt: 18 }, }, }); ``` -------------------------------- ### Define Predefined Filters in Relations (v2) Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Use predefined filters within relation definitions to easily access subsets of related data. This requires defining relations using `defineRelations`. ```typescript export const relations = defineRelations(schema, (r) => ({ groups: { verifiedUsers: r.many.users({ from: r.groups.id.through(r.usersToGroups.groupId), to: r.users.id.through(r.usersToGroups.userId), where: { verified: true }, }), }, })); ``` -------------------------------- ### Drizzle ORM v2 Many-to-Many with `through` Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Illustrates the new `through` feature in Drizzle ORM v2 for defining many-to-many relations directly, simplifying complex relationships. ```typescript export const relations = defineRelations(schema, (r) => ({ users: { groups: r.many.groups({ from: r.users.id.through(r.usersToGroups.userId), to: r.groups.id.through(r.usersToGroups.groupId), }), }, })); ``` -------------------------------- ### Many-to-Many Queries Migration from v1 to v2 Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md v2 simplifies many-to-many queries by directly accessing the related table (`groups`) without needing to explicitly include the junction table (`usersToGroups`) in the `with` clause. ```typescript const response = await db.query.users.findMany({ with: { usersToGroups: { columns: {}, with: { group: true }, }, }, }); // Result: { usersToGroups: [{ group: {...} }] } ``` ```typescript const response = await db.query.users.findMany({ with: { groups: true }, }); // Result: { groups: [...] } - No junction table in response ``` -------------------------------- ### Filter Users by Related Posts Content (v2) Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md Use this to find records in one table based on conditions in a related table. Ensure the relation is defined in your schema. ```typescript await db.query.users.findMany({ where: { posts: { content: { like: "%drizzle%" }, }, }, }); ``` -------------------------------- ### Drizzle ORM v2 Relations Definition Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/SKILL.md Defines relations for all tables within a single `defineRelations` call using the new v2 syntax. This consolidates relation definitions. ```typescript import { defineRelations } from "drizzle-orm"; import * as schema from "./schema"; export const relations = defineRelations(schema, (r) => ({ users: { posts: r.many.posts({ from: r.users.id, to: r.posts.authorId, }), }, })); ``` -------------------------------- ### Complex Filters in v2 `where` Statement Source: https://github.com/habibium/agent-skills/blob/main/skills/drizzle-v1/references/relational-queries-v2.md v2 supports complex filtering logic like AND, OR, NOT, and nested conditions directly within the `where` object. RAW SQL filters are also supported. ```typescript // AND (implicit - same object level) where: { name: "John", age: 18 } // OR where: { OR: [{ name: "John" }, { name: "Jane" }] } // NOT where: { NOT: { status: "banned" } } // Nested AND/OR where: { AND: [ { OR: [{ name: "John" }, { name: "Jane" }] }, { age: { gte: 18 } }, ], } // RAW SQL where: { RAW: (table) => sql`${table.age} BETWEEN 25 AND 35` } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.