-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(packages/drizzledb-pg): drizzle relational queries (#40)
* setup drizzle client to accept relations by passing in schemas * definie relations in drizzle schema files * add new findBy query using relational queries
- Loading branch information
1 parent
8c3a578
commit f1c58a7
Showing
4 changed files
with
55 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { relations } from 'drizzle-orm' | ||
import { pgSchema, text, timestamp, uuid } from 'drizzle-orm/pg-core' | ||
|
||
import { citext } from './custom-types.js' | ||
|
||
export const authSchema = pgSchema('auth') | ||
|
||
export const users = authSchema.table('users', { | ||
id: uuid('id').primaryKey().defaultRandom(), | ||
username: citext('username').notNull().unique(), | ||
hashedPassword: text('hashed_password').notNull(), | ||
}) | ||
|
||
export const sessions = authSchema.table('sessions', { | ||
id: text('id').primaryKey(), | ||
userId: uuid('user_id') | ||
.notNull() | ||
.references(() => users.id), | ||
expiresAt: timestamp('expires_at', { | ||
withTimezone: true, | ||
mode: 'date', | ||
}).notNull(), | ||
}) | ||
|
||
export const usersRelations = relations(users, ({ many }) => ({ | ||
sessions: many(sessions), | ||
})) | ||
|
||
export const sessionsRelations = relations(sessions, ({ one }) => ({ | ||
user: one(users, { | ||
fields: [sessions.userId], | ||
references: [users.id], | ||
}), | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1 @@ | ||
import { pgSchema, text, timestamp, uuid } from 'drizzle-orm/pg-core' | ||
|
||
import { citext } from './custom-types.js' | ||
|
||
export { count } from 'drizzle-orm' | ||
|
||
export const authSchema = pgSchema('auth') | ||
|
||
export const users = authSchema.table('users', { | ||
id: uuid('id').primaryKey().defaultRandom(), | ||
username: citext('username').notNull().unique(), | ||
hashedPassword: text('hashed_password').notNull(), | ||
}) | ||
|
||
export const sessions = authSchema.table('sessions', { | ||
id: text('id').primaryKey(), | ||
userId: uuid('user_id') | ||
.notNull() | ||
.references(() => users.id), | ||
expiresAt: timestamp('expires_at', { | ||
withTimezone: true, | ||
mode: 'date', | ||
}).notNull(), | ||
}) | ||
export * from './auth-schema.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters