I find it fascinating that I can use this constant even before declaring it.
The code below is functioning perfectly:
import { relations } from 'drizzle-orm'
import { index, integer, pgTable, serial, uniqueIndex, varchar } from 'drizzle-orm/pg-core'
// users
export const usersTable = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name'),
email: varchar('email'),
}, (table) => ({
nameIndex: index('users_name_index').on(table.name),
emailIndex: uniqueIndex('users_email_index').on(table.email),
}))
export const usersRelations = relations(usersTable, ({ many }) => ({
pets: many(petsTable),
}))
// pets
export const petsTable = pgTable('pets', {
id: serial('id').primaryKey(),
userId: integer('user_id'),
name: varchar('name'),
}, (table) => ({
nameIndex: index('pets_name_index').on(table.name),
}))
export const petsRelations = relations(petsTable, ({ one }) => ({
user: one(usersTable, {
fields: [petsTable.userId],
references: [usersTable.id],
}),
}))
It's interesting to note that I'm using petsTable
before its declaration. Does this mean that exported constants are accessible throughout the module? Or is this a feature specific to the Drizzle API?