Skip to content

Commit

Permalink
Allow query on collection group (#10)
Browse files Browse the repository at this point in the history
* Allow query on collection group

Fix #8

* Fix tests

* Update README.md

* Fix tests
  • Loading branch information
pgollangi authored Jan 5, 2023
1 parent 3d884d4 commit 1cb7959
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Read the [documentation](https://pgollangi.github.io/FireQL/) for more informati
Some cool `SELECT` queries that are possible with `FireQL`:
```sql
select * from users
select * from `[contacts]` // To query collection group. enclose subcollect name in square brackets.
select *, id as user_id from users
select id, email as email_address, `address.city` AS city from `users`
select * from users order by 'address.city' desc limit 10
Expand Down
11 changes: 9 additions & 2 deletions pkg/select/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ func (sel *SelectStatement) Execute() (*util.QueryResult, error) {
defer fireClient.Close()

qCollectionName = strings.Trim(qCollectionName, "`")
fCollection := fireClient.Collection(qCollectionName)
fQuery := fCollection.Query

var fQuery firestore.Query
if strings.HasPrefix(qCollectionName, "[") && strings.HasSuffix(qCollectionName, "]") {
groupName := strings.TrimPrefix(qCollectionName, "[")
groupName = strings.TrimSuffix(groupName, "]")
fQuery = fireClient.CollectionGroup(groupName).Query
} else {
fQuery = fireClient.Collection(qCollectionName).Query
}

fQuery, selectedFields, err := sel.selectFields(fQuery, sQuery)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var selectTests = []TestExpect{
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
query: "select * from `users`",
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
query: "select id as uid, * from users",
columns: []string{"uid", "id", "email", "username", "address", "name"},
Expand Down Expand Up @@ -97,7 +102,7 @@ func newFirestoreTestClient(ctx context.Context) *firestore.Client {

func TestMain(m *testing.M) {
// command to start firestore emulator
cmd := exec.Command("gcloud", "beta", "emulators", "firestore", "start", "--host-port=localhost")
cmd := exec.Command("gcloud", "beta", "emulators", "firestore", "start", "--host-port=localhost:8765")

// this makes it killable
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
Expand Down Expand Up @@ -151,19 +156,14 @@ func TestMain(m *testing.M) {
wg.Done()
}

// and capturing the FIRESTORE_EMULATOR_HOST value to set
pos := strings.Index(d, FirestoreEmulatorHost+"=")
if pos > 0 {
host := d[pos+len(FirestoreEmulatorHost)+1 : len(d)-1]
os.Setenv(FirestoreEmulatorHost, host)
}
}
}
}()

// wait until the running message has been received
wg.Wait()

os.Setenv(FirestoreEmulatorHost, "localhost:8765")
ctx := context.Background()
users := newFirestoreTestClient(ctx).Collection("users")

Expand Down

0 comments on commit 1cb7959

Please sign in to comment.