mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
BASE_DB_TIER=sql opened PostgreSQL and then spoke to it in SQLite: strftime, sqlite_master, json_extract, json_each, an INTEGER column holding an int64, an outer join with no ON, backticked identifiers, and a rowid that only one engine has. The tier picked a backend the queries could not reach. Every one of those now comes from hanzoai/orm/dialect, resolved once from the driver the data DB was opened with and reachable as app.Dialect(). Base holds no switch on the engine — it asks for the JSON accessors, the schema catalog, the generated instant and identifier, and the column types, and it asks whether a feature exists at all where one engine has nothing to offer: an insertion-order column, a write-ahead log to fold, a function that renders an instant. Empty means the caller falls back or refuses rather than emitting something the engine will reject. Some differences turned out not to need a dialect at all. An instant is stored as text in a fixed layout, so bucketing the log rollup to the hour is a substring rather than a calendar function, which every engine spells the same way and an index can be built on. Null-safe equality is IS NOT DISTINCT FROM. An int64 column is BIGINT. A join with nothing of its own to match still states a condition. Case-insensitive comparison stays COLLATE NOCASE, because the schema now asks the engine for the collation that name refers to. The proof is a Postgres 18 running the data plane: migrations apply, a collection created over /v1 lands as a table with text, numeric and jsonb columns and its declared index, and records insert, page, sort, filter across scalars, multi-value elements and JSON paths, update and delete. A unique index declared COLLATE NOCASE rejects the second spelling of a handle. Comparing a JSON sub-path to a number is refused there: the extraction yields text, the literal binds as a number, and the filter language does not say which the comparison meant. Compared as text it works.
274 lines
7.5 KiB
Go
274 lines
7.5 KiB
Go
package core
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/base/tools/dbutils"
|
|
"github.com/hanzoai/base/tools/list"
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
// ExpandFetchFunc defines the function that is used to fetch the expanded relation records.
|
|
type ExpandFetchFunc func(relCollection *Collection, relIds []string) ([]*Record, error)
|
|
|
|
// ExpandRecord expands the relations of a single Record model.
|
|
//
|
|
// If optFetchFunc is not set, then a default function will be used
|
|
// that returns all relation records.
|
|
//
|
|
// Returns a map with the failed expand parameters and their errors.
|
|
func (app *BaseApp) ExpandRecord(record *Record, expands []string, optFetchFunc ExpandFetchFunc) map[string]error {
|
|
return app.ExpandRecords([]*Record{record}, expands, optFetchFunc)
|
|
}
|
|
|
|
// ExpandRecords expands the relations of the provided Record models list.
|
|
//
|
|
// If optFetchFunc is not set, then a default function will be used
|
|
// that returns all relation records.
|
|
//
|
|
// Returns a map with the failed expand parameters and their errors.
|
|
func (app *BaseApp) ExpandRecords(records []*Record, expands []string, optFetchFunc ExpandFetchFunc) map[string]error {
|
|
normalized := normalizeExpands(expands)
|
|
|
|
failed := map[string]error{}
|
|
|
|
for _, expand := range normalized {
|
|
if err := app.expandRecords(records, expand, optFetchFunc, 1); err != nil {
|
|
failed[expand] = err
|
|
}
|
|
}
|
|
|
|
return failed
|
|
}
|
|
|
|
// Deprecated
|
|
var indirectExpandRegexOld = regexp.MustCompile(`^(\w+)\((\w+)\)$`)
|
|
|
|
var indirectExpandRegex = regexp.MustCompile(`^(\w+)_via_(\w+)$`)
|
|
|
|
// notes:
|
|
// - if fetchFunc is nil, dao.FindRecordsByIds will be used
|
|
// - all records are expected to be from the same collection
|
|
// - if maxNestedRels(6) is reached, the function returns nil ignoring the remaining expand path
|
|
func (app *BaseApp) expandRecords(records []*Record, expandPath string, fetchFunc ExpandFetchFunc, recursionLevel int) error {
|
|
if fetchFunc == nil {
|
|
// load a default fetchFunc
|
|
fetchFunc = func(relCollection *Collection, relIds []string) ([]*Record, error) {
|
|
return app.FindRecordsByIds(relCollection.Id, relIds)
|
|
}
|
|
}
|
|
|
|
if expandPath == "" || recursionLevel > maxNestedRels || len(records) == 0 {
|
|
return nil
|
|
}
|
|
|
|
mainCollection := records[0].Collection()
|
|
|
|
var relField *RelationField
|
|
var relCollection *Collection
|
|
|
|
parts := strings.SplitN(expandPath, ".", 2)
|
|
var matches []string
|
|
|
|
// legacy parenthesized syntax: collection(field) -> collection_via_field
|
|
if strings.Contains(parts[0], "(") {
|
|
matches = indirectExpandRegexOld.FindStringSubmatch(parts[0])
|
|
} else {
|
|
matches = indirectExpandRegex.FindStringSubmatch(parts[0])
|
|
}
|
|
|
|
if len(matches) == 3 {
|
|
indirectRel, _ := getCollectionByModelOrIdentifier(app, matches[1])
|
|
if indirectRel == nil {
|
|
return fmt.Errorf("couldn't find back-related collection %q", matches[1])
|
|
}
|
|
|
|
indirectRelField, _ := indirectRel.Fields.GetByName(matches[2]).(*RelationField)
|
|
if indirectRelField == nil || indirectRelField.CollectionId != mainCollection.Id {
|
|
return fmt.Errorf("couldn't find back-relation field %q in collection %q", matches[2], indirectRel.Name)
|
|
}
|
|
|
|
// add the related id(s) as a dynamic relation field value to
|
|
// allow further expand checks at later stage in a more unified manner
|
|
prepErr := func() error {
|
|
q := app.ConcurrentDB().Select("id").
|
|
From(indirectRel.Name).
|
|
Limit(1000) // the limit is arbitrary chosen and may change in the future
|
|
|
|
if indirectRelField.IsMultiple() {
|
|
q.AndWhere(dbx.Exists(dbx.NewExp(fmt.Sprintf(
|
|
"SELECT 1 FROM %s je WHERE je.value = {:id}",
|
|
dbutils.JSONEach(app.Dialect(), indirectRelField.Name),
|
|
))))
|
|
} else {
|
|
q.AndWhere(dbx.NewExp("[[" + indirectRelField.Name + "]] = {:id}"))
|
|
}
|
|
|
|
pq := q.Build().Prepare()
|
|
|
|
for _, record := range records {
|
|
var relIds []string
|
|
|
|
err := pq.Bind(dbx.Params{"id": record.Id}).Column(&relIds)
|
|
if err != nil {
|
|
return errors.Join(err, pq.Close())
|
|
}
|
|
|
|
if len(relIds) > 0 {
|
|
record.Set(parts[0], relIds)
|
|
}
|
|
}
|
|
|
|
return pq.Close()
|
|
}()
|
|
if prepErr != nil {
|
|
return prepErr
|
|
}
|
|
|
|
// indirect/back relation
|
|
relField = &RelationField{
|
|
Name: parts[0],
|
|
MaxSelect: 2147483647,
|
|
CollectionId: indirectRel.Id,
|
|
}
|
|
if _, ok := dbutils.FindSingleColumnUniqueIndex(indirectRel.Indexes, indirectRelField.GetName()); ok {
|
|
relField.MaxSelect = 1
|
|
}
|
|
relCollection = indirectRel
|
|
} else {
|
|
// direct relation
|
|
relField, _ = mainCollection.Fields.GetByName(parts[0]).(*RelationField)
|
|
if relField == nil {
|
|
return fmt.Errorf("couldn't find relation field %q in collection %q", parts[0], mainCollection.Name)
|
|
}
|
|
|
|
relCollection, _ = getCollectionByModelOrIdentifier(app, relField.CollectionId)
|
|
if relCollection == nil {
|
|
return fmt.Errorf("couldn't find related collection %q", relField.CollectionId)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
// extract the id of the relations to expand
|
|
relIds := make([]string, 0, len(records))
|
|
for _, record := range records {
|
|
relIds = append(relIds, record.GetStringSlice(relField.Name)...)
|
|
}
|
|
|
|
// fetch rels
|
|
rels, relsErr := fetchFunc(relCollection, relIds)
|
|
if relsErr != nil {
|
|
return relsErr
|
|
}
|
|
|
|
// expand nested fields
|
|
if len(parts) > 1 {
|
|
err := app.expandRecords(rels, parts[1], fetchFunc, recursionLevel+1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// reindex with the rel id
|
|
indexedRels := make(map[string]*Record, len(rels))
|
|
for _, rel := range rels {
|
|
indexedRels[rel.Id] = rel
|
|
}
|
|
|
|
for _, model := range records {
|
|
// init expand if not already
|
|
// (this is done to ensure that the "expand" key will be returned in the response even if empty)
|
|
if model.expand == nil {
|
|
model.SetExpand(nil)
|
|
}
|
|
|
|
relIds := model.GetStringSlice(relField.Name)
|
|
|
|
validRels := make([]*Record, 0, len(relIds))
|
|
for _, id := range relIds {
|
|
if rel, ok := indexedRels[id]; ok {
|
|
validRels = append(validRels, rel)
|
|
}
|
|
}
|
|
|
|
if len(validRels) == 0 {
|
|
continue // no valid relations
|
|
}
|
|
|
|
expandData := model.Expand()
|
|
|
|
// normalize access to the previously expanded rel records (if any)
|
|
var oldExpandedRels []*Record
|
|
switch v := expandData[relField.Name].(type) {
|
|
case nil:
|
|
// no old expands
|
|
case *Record:
|
|
oldExpandedRels = []*Record{v}
|
|
case []*Record:
|
|
oldExpandedRels = v
|
|
}
|
|
|
|
// merge expands
|
|
for _, oldExpandedRel := range oldExpandedRels {
|
|
// find a matching rel record
|
|
for _, rel := range validRels {
|
|
if rel.Id != oldExpandedRel.Id {
|
|
continue
|
|
}
|
|
|
|
rel.MergeExpand(oldExpandedRel.Expand())
|
|
}
|
|
}
|
|
|
|
// update the expanded data
|
|
if relField.IsMultiple() {
|
|
expandData[relField.Name] = validRels
|
|
} else {
|
|
expandData[relField.Name] = validRels[0]
|
|
}
|
|
|
|
model.SetExpand(expandData)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// normalizeExpands normalizes expand strings and merges self containing paths
|
|
// (eg. ["a.b.c", "a.b", " test ", " ", "test"] -> ["a.b.c", "test"]).
|
|
func normalizeExpands(paths []string) []string {
|
|
// normalize paths
|
|
normalized := make([]string, 0, len(paths))
|
|
for _, p := range paths {
|
|
p = strings.ReplaceAll(p, " ", "") // replace spaces
|
|
p = strings.Trim(p, ".") // trim incomplete paths
|
|
if p != "" {
|
|
normalized = append(normalized, p)
|
|
}
|
|
}
|
|
|
|
// merge containing paths
|
|
result := make([]string, 0, len(normalized))
|
|
for i, p1 := range normalized {
|
|
var skip bool
|
|
for j, p2 := range normalized {
|
|
if i == j {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(p2, p1+".") {
|
|
// skip because there is more detailed expand path
|
|
skip = true
|
|
break
|
|
}
|
|
}
|
|
if !skip {
|
|
result = append(result, p1)
|
|
}
|
|
}
|
|
|
|
return list.ToUniqueStringSlice(result)
|
|
}
|