List ID of records from a collection

This commit is contained in:
Łukasz Wróblewski 2020-06-17 06:22:46 +02:00
parent 4116320640
commit c32ca9162c
1 changed files with 30 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/jcelliott/lumber"
@ -216,6 +217,35 @@ func readAll(files []os.FileInfo, dir string) ([][]byte, error) {
return records, nil
}
// List ID of records from a collection; this is returned as a slice of strings.
func (d *Driver) List(collection string) ([]string, error) {
// ensure there is a collection to read
if collection == "" {
return nil, ErrMissingCollection
}
//
dir := filepath.Join(d.dir, collection)
// check to see if collection (directory) exists
if _, err := stat(dir); err != nil {
return nil, err
}
// the IDs of collection
var recordsIDs []string
for _, file := range files {
name := file.Name()
if strings.HasSuffix(name, ".json") {
recordsIDs = append(recordsIDs, name[:len(name)-5])
}
}
return recordsIDs, nil
}
// Delete locks that database and then attempts to remove the collection/resource
// specified by [path]
func (d *Driver) Delete(collection, resource string) error {