feat: add Only

This commit is contained in:
moein 2025-05-05 14:16:22 +03:30
parent 8c4e8e2d2a
commit 4386e52264
No known key found for this signature in database
GPG Key ID: 5D04361D895D9E7E

View File

@ -159,6 +159,34 @@ func (db *DB) Last(dest interface{}, conds ...interface{}) (tx *DB) {
return tx.callbacks.Query().Execute(tx) return tx.callbacks.Query().Execute(tx)
} }
// Only finds the single record and asserts that it was the only record that matched the query
func (db *DB) Only(dest interface{}, conds ...interface{}) (tx *DB) {
baseTx := db.Session(&Session{NewDB: true})
if len(conds) > 0 {
if exprs := baseTx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
baseTx.Statement.AddClause(clause.Where{Exprs: exprs})
}
}
findTx := baseTx.Limit(1).Find(dest)
if findTx.Error != nil {
return findTx
}
// Check total matches (with fresh session)
var count int64
countTx := baseTx.Session(&Session{}).Count(&count)
if countTx.Error != nil {
return countTx
}
if count > 1 {
findTx.AddError(fmt.Errorf("expected exactly one record, but found %d", count))
}
return findTx
}
// Find finds all records matching given conditions conds // Find finds all records matching given conditions conds
func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) { func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()