Add complete database schema and ORM models (#4)
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,14 +1,29 @@
|
||||
// Package models defines the data models used by the chat application.
|
||||
// All model structs embed Base, which provides database access for
|
||||
// relation-fetching methods directly on model instances.
|
||||
package models
|
||||
|
||||
import "database/sql"
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// DB is the interface that models use to query relations.
|
||||
// DB is the interface that models use to query the database.
|
||||
// This avoids a circular import with the db package.
|
||||
type DB interface {
|
||||
GetDB() *sql.DB
|
||||
}
|
||||
|
||||
// UserLookup provides user lookup by ID without circular imports.
|
||||
type UserLookup interface {
|
||||
GetUserByID(ctx context.Context, id string) (*User, error)
|
||||
}
|
||||
|
||||
// ChannelLookup provides channel lookup by ID without circular imports.
|
||||
type ChannelLookup interface {
|
||||
GetChannelByID(ctx context.Context, id string) (*Channel, error)
|
||||
}
|
||||
|
||||
// Base is embedded in all model structs to provide database access.
|
||||
type Base struct {
|
||||
db DB
|
||||
@@ -18,3 +33,26 @@ type Base struct {
|
||||
func (b *Base) SetDB(d DB) {
|
||||
b.db = d
|
||||
}
|
||||
|
||||
// GetDB returns the database interface for use in model methods.
|
||||
func (b *Base) GetDB() *sql.DB {
|
||||
return b.db.GetDB()
|
||||
}
|
||||
|
||||
// GetUserLookup returns the DB as a UserLookup if it implements the interface.
|
||||
func (b *Base) GetUserLookup() UserLookup {
|
||||
if ul, ok := b.db.(UserLookup); ok {
|
||||
return ul
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetChannelLookup returns the DB as a ChannelLookup if it implements the interface.
|
||||
func (b *Base) GetChannelLookup() ChannelLookup {
|
||||
if cl, ok := b.db.(ChannelLookup); ok {
|
||||
return cl
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user