From 6416328c3b6c9f9260c59a253d764150cd979351 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sun, 3 Dec 2023 10:57:49 +0200 Subject: [PATCH] added support for specifying `@collection.*` aliases --- CHANGELOG.md | 2 ++ core/base.go | 16 ++++++++-------- resolvers/record_field_resolve_runner.go | 12 ++++++++++-- resolvers/record_field_resolver.go | 2 +- resolvers/record_field_resolver_test.go | 8 +++++--- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b7616e..0e6dce2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ - Added support for comments in the API rules and filter expressions. +- Added support for specifying a collection alias in `@collection.someCollection:alias.*`. + ## v0.20.0-rc3 diff --git a/core/base.go b/core/base.go index c55edcd5..f23cb181 100644 --- a/core/base.go +++ b/core/base.go @@ -1055,14 +1055,14 @@ func (app *BaseApp) initDataDB() error { nonconcurrentDB.DB().SetConnMaxIdleTime(3 * time.Minute) // @todo benchmark whether it will have an impact if always enabled as TRACE log - // nonconcurrentDB.QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) { - // color.HiBlack("[%.2fms] %v\n", float64(t.Milliseconds()), sql) - // } - // concurrentDB.QueryLogFunc = nonconcurrentDB.QueryLogFunc - // nonconcurrentDB.ExecLogFunc = func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error) { - // color.HiBlack("[%.2fms] %v\n", float64(t.Milliseconds()), sql) - // } - // concurrentDB.ExecLogFunc = nonconcurrentDB.ExecLogFunc + // nonconcurrentDB.QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) { + // color.HiBlack("[%.2fms] %v\n", float64(t.Milliseconds()), sql) + // } + // concurrentDB.QueryLogFunc = nonconcurrentDB.QueryLogFunc + // nonconcurrentDB.ExecLogFunc = func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error) { + // color.HiBlack("[%.2fms] %v\n", float64(t.Milliseconds()), sql) + // } + // concurrentDB.ExecLogFunc = nonconcurrentDB.ExecLogFunc app.dao = app.createDaoWithHooks(concurrentDB, nonconcurrentDB) diff --git a/resolvers/record_field_resolve_runner.go b/resolvers/record_field_resolve_runner.go index d3bede86..a5365fd6 100644 --- a/resolvers/record_field_resolve_runner.go +++ b/resolvers/record_field_resolve_runner.go @@ -141,13 +141,21 @@ func (r *runner) processCollectionField() (*search.ResolverResult, error) { return nil, fmt.Errorf("invalid @collection field path in %q", r.fieldName) } - collection, err := r.resolver.loadCollection(r.activeProps[1]) + // nameOrId or nameOrId:alias + collectionParts := strings.SplitN(r.activeProps[1], ":", 2) + + collection, err := r.resolver.loadCollection(collectionParts[0]) if err != nil { return nil, fmt.Errorf("failed to load collection %q from field path %q", r.activeProps[1], r.fieldName) } r.activeCollectionName = collection.Name - r.activeTableAlias = inflector.Columnify("__collection_" + r.activeCollectionName) + + if len(collectionParts) == 2 && collectionParts[1] != "" { + r.activeTableAlias = inflector.Columnify("__collection_alias_" + collectionParts[1]) + } else { + r.activeTableAlias = inflector.Columnify("__collection_" + r.activeCollectionName) + } r.withMultiMatch = true diff --git a/resolvers/record_field_resolver.go b/resolvers/record_field_resolver.go index 51411eab..d5eb3ff7 100644 --- a/resolvers/record_field_resolver.go +++ b/resolvers/record_field_resolver.go @@ -94,7 +94,7 @@ func NewRecordFieldResolver( `^\@request\.data\.[\w\.\:]*\w+$`, `^\@request\.query\.[\w\.\:]*\w+$`, `^\@request\.headers\.\w+$`, - `^\@collection\.\w+\.[\w\.\:]*\w+$`, + `^\@collection\.\w+(\:\w+)?\.[\w\.\:]*\w+$`, }, } diff --git a/resolvers/record_field_resolver_test.go b/resolvers/record_field_resolver_test.go index be28996c..91f6d91b 100644 --- a/resolvers/record_field_resolver_test.go +++ b/resolvers/record_field_resolver_test.go @@ -155,9 +155,9 @@ func TestRecordFieldResolverUpdateQuery(t *testing.T) { { "@collection join (opt/any operators)", "demo4", - "@collection.demo1.text ?> true || @collection.demo2.active ?> true || @collection.demo1.file_one ?> true", + "@collection.demo1.text ?> true || @collection.demo2.active ?> true || @collection.demo1:demo1_alias.file_one ?> true", false, - "SELECT DISTINCT `demo4`.* FROM `demo4` LEFT JOIN `demo1` `__collection_demo1` LEFT JOIN `demo2` `__collection_demo2` WHERE ([[__collection_demo1.text]] > 1 OR [[__collection_demo2.active]] > 1 OR [[__collection_demo1.file_one]] > 1)", + "SELECT DISTINCT `demo4`.* FROM `demo4` LEFT JOIN `demo1` `__collection_demo1` LEFT JOIN `demo2` `__collection_demo2` LEFT JOIN `demo1` `__collection_alias_demo1_alias` WHERE ([[__collection_demo1.text]] > 1 OR [[__collection_demo2.active]] > 1 OR [[__collection_alias_demo1_alias.file_one]] > 1)", }, { "@collection join (multi-match operators)", @@ -391,8 +391,10 @@ func TestRecordFieldResolverResolveSchemaFields(t *testing.T) { {"@collection.unknown", true, ""}, {"@collection.demo2", true, ""}, {"@collection.demo2.", true, ""}, + {"@collection.demo2:someAlias", true, ""}, + {"@collection.demo2:someAlias.", true, ""}, {"@collection.demo2.title", false, "[[__collection_demo2.title]]"}, - {"@collection.demo4.title", false, "[[__collection_demo4.title]]"}, + {"@collection.demo2:someAlias.title", false, "[[__collection_alias_someAlias.title]]"}, {"@collection.demo4.id", false, "[[__collection_demo4.id]]"}, {"@collection.demo4.created", false, "[[__collection_demo4.created]]"}, {"@collection.demo4.updated", false, "[[__collection_demo4.updated]]"},