An atproto PDS written in Go
1package db 2 3import ( 4 "sync" 5 6 "gorm.io/gorm" 7 "gorm.io/gorm/clause" 8) 9 10type DB struct { 11 cli *gorm.DB 12 mu sync.Mutex 13} 14 15func NewDB(cli *gorm.DB) *DB { 16 return &DB{ 17 cli: cli, 18 mu: sync.Mutex{}, 19 } 20} 21 22func (db *DB) Create(value any, clauses []clause.Expression) *gorm.DB { 23 db.mu.Lock() 24 defer db.mu.Unlock() 25 return db.cli.Clauses(clauses...).Create(value) 26} 27 28func (db *DB) Exec(sql string, clauses []clause.Expression, values ...any) *gorm.DB { 29 db.mu.Lock() 30 defer db.mu.Unlock() 31 return db.cli.Clauses(clauses...).Exec(sql, values...) 32} 33 34func (db *DB) Raw(sql string, clauses []clause.Expression, values ...any) *gorm.DB { 35 return db.cli.Clauses(clauses...).Raw(sql, values...) 36} 37 38func (db *DB) AutoMigrate(models ...any) error { 39 return db.cli.AutoMigrate(models...) 40} 41 42func (db *DB) Delete(value any, clauses []clause.Expression) *gorm.DB { 43 db.mu.Lock() 44 defer db.mu.Unlock() 45 return db.cli.Clauses(clauses...).Delete(value) 46} 47 48func (db *DB) First(dest any, conds ...any) *gorm.DB { 49 return db.cli.First(dest, conds...) 50} 51 52// TODO: this isn't actually good. we can commit even if the db is locked here. this is probably okay for the time being, but need to figure 53// out a better solution. right now we only do this whenever we're importing a repo though so i'm mostly not worried, but it's still bad. 54// e.g. when we do apply writes we should also be using a transcation but we don't right now 55func (db *DB) BeginDangerously() *gorm.DB { 56 return db.cli.Begin() 57} 58 59func (db *DB) Lock() { 60 db.mu.Lock() 61} 62 63func (db *DB) Unlock() { 64 db.mu.Unlock() 65}