staticbackendhq / core

Backend server API handling user mgmt, database, storage and real-time component
https://staticbackend.com
MIT License
682 stars 67 forks source link

Error when list/query for not created tables #46

Closed dstpierre closed 1 year ago

dstpierre commented 1 year ago

Describe the bug The creation of tables occur on the first insertion (call to CreateDocument function.

For this reason, code that tries to get records from a table that did not had a created document will return an error.

To Reproduce

  1. Start the CLI $ backend server
  2. Issue a ListDocument request
$ curl -H "SB-PUBLIC-KEY: dev-memory-pk" -X POST -d '{"email": "admin@dev.com", "password": "devpw1234"}' http://localhost:8099/login
$ curl -H "SB-PUBLIC-KEY: dev-memory-pk" -H "Authorization: Bearer session-token-you-got-above" http://localhost:8099/db/new-table-here

Error returned:

collection not found

Expected behavior

Might be better to return an empty slice instead of an error. The "standard" web CRUD flow is to create the "listing" page before users go to create entity.

Output of staticbackend -v

StaticBackend version v1.4.0-rc2-13-gb392f7a | 2022-08-23.05:33:15 (b392f7a)

Additional context

This should be the same behavior for all 3 database package: postgresql, mongo, and memory.

dstpierre commented 1 year ago

To add more context. I think we should detect the error on the database provider side and return the empty slice only for this error, not all error.

For instance, code taken from database/postgresql/base.go line:120:

if err = pg.DB.QueryRow(qry, auth.AccountID, auth.UserID).Scan(&result.Total); err != nil {
  return
}

We'd need to check for the specific error regarding table / relation does not exists and only in that case return the empty slice without error.

Same for mongo and memory database package.

rostikts commented 1 year ago

@dstpierre may I handle this issue?