@guiseek/ngx-fire-search
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

NgxFireSearch

The goal is very simple! Improve searches on firestore using structure so it can understand and answer queries by strings. Transforming texts into object/map data structure. Pull requests are welcome.

Install

npm

npm install @guiseek/ngx-fire-search --save

yarn

yarn add @guiseek/ngx-fire-search

Example use:

//song.model

import { FirestoreSearch, SearchIndex } from '@guiseek/ngx-fire-search';

export class SongModel extends FirestoreSearch {
  title: string
  artist: string
  album: string
  search?: {
    [key: string]: boolean
  }
  constructor(
    title: string,
    artist: string,
    album: string
  ) {
    super()
    this.title = title
    this.artist = artist
    this.album = album
    return this
  }
  public getSearchIndexes?(): SearchIndex {
    this.search = this.createSearchIndexes(
      `${this.title} ${this.artist} ${this.album}`
    )
    return this
  }
}
// song.service
import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Song } from '../models/song.model';

@Injectable({ providedIn: 'root' })
export class SongService {
  private path = 'songs';
  constructor(
    private afs: AngularFirestore
  ) { }

  getAll() {
    return this.afs.collection<Song>(this.path)
      .valueChanges({ idField: 'id' })
  }
  create({title, artist, album}: Song) {
    const song = new Song(title, artist, album).createIndex()
    return this.afs.collection<Song>(this.path)
      .add(Object.assign({}, song))
  }
  search(value: string): Observable<Song[]> {
    return this.afs.collection<Song>(this.path, ref => {
      
      return ref.where(`search.${value}`, '==', true)

      // Bad way
      // return ref.orderBy('title').startAt(value).endAt(value + '\uf8ff')

    }).valueChanges().pipe(
      catchError(_ => {
        return of(null)
      })
    )
  }
}

TODO

  • [ ] Improv object map
  • [ ] Pure function

This library was generated with Angular CLI version 8.0.1.

Code scaffolding

Run ng generate component component-name --project ngx-fire-search to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module --project ngx-fire-search.

Note: Don't forget to add --project ngx-fire-search or else it will be added to the default project in your angular.json file.

Build

Run ng build ngx-fire-search to build the project. The build artifacts will be stored in the dist/ directory.

Publishing

After building your library with ng build ngx-fire-search, go to the dist folder cd dist/ngx-fire-search and run npm publish.

Running unit tests

Run ng test ngx-fire-search to execute the unit tests via Karma.

Further help

To get more help on the Angular CLI use ng help or go check out the Angular CLI README.

Readme

Keywords

none

Package Sidebar

Install

npm i @guiseek/ngx-fire-search

Weekly Downloads

0

Version

0.0.4

License

none

Unpacked Size

38.2 kB

Total Files

29

Last publish

Collaborators

  • guiseek