1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-23 14:07:13 +02:00

added support for specifying @collection.* aliases

This commit is contained in:
Gani Georgiev 2023-12-03 10:57:49 +02:00
parent d3713a9d7c
commit 6416328c3b
5 changed files with 26 additions and 14 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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+$`,
},
}

View File

@ -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]]"},