@dgmip/firestore-searchable-index
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

Firestore Searchable Indexc

Create searchable indices in firestore from a string or string[]

Installation

npm i --save @dgmip/firestore-searchable-index

Use

import * as searchableIndex from "@dgmip/firestore-searchable-index";

mySearchableIndex = searchableIndex.firestoreSearchableIndex;

// create and index on a string
const index = searchableIndex.create("Ben Wall");
// create and index on an array
const index = searchableIndex.create(["Ben Wall", "Peter Williams"]);

Example search using @angular/fire

offset = new Subject<string>() // your search term subject
results$: Observable<any[]>

contructor() {
  this.results$ = this.search()
}

search() {
  return this.offset.pipe(
    filter(val => !!val), // filter empty strings
    switchMap(offset => {
      return this.afs.collection('names', ref =>
        ref.orderBy(`searchableIndex.${ offset }`)
      )
        .snapshotChanges().pipe(
          map(actions => {
            const results = []
            actions.forEach(action => {
              const id = action.payload.doc.id
              const data = action.payload.doc.data()
              results.push({ id, data })
            })
            return results
          })
        )
    })
  )
}

// fire off a search for 'ben'
fireSearch() {
  this.offset.next('ben')
}

Package Sidebar

Install

npm i @dgmip/firestore-searchable-index

Weekly Downloads

7

Version

1.0.9

License

ISC

Unpacked Size

27.7 kB

Total Files

12

Last publish

Collaborators

  • dgmip