Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Search Across All Fields of a Collection #117

@vikikamath

Description

@vikikamath

Currently following forms of HTTP GET Requests are supported:

GET api/heroes/42       // the character with id=42
GET api/heroes?name=^j  // 'j' is a regex; returns heroes whose name starting with 'j' or 'J'

To mock a Search Component, it would be preferable to find across any fields, in a given Collection. Something like:

GET api/heroes?search=^j  // 'j' is a regex; returns heroes seen in **any** of the fields starting with 'j' or 'J'

A solution might be to provide a function that takes a collection input, an [ optional ] searchKey and returns a modified collection that contains an additional array property searchKey . This searchKey array property will contain all keywords pertinent to that record :

// 
function searchAnything( collection: heroes[] , searchKey = "search") {
  return  heroes.map( hero => {
    hero[ searchKey ] = // parse keywords from *hero* record

    return hero;
 } );
}

For example to make Heroes search anything, usage would look like :

import { InMemoryDbService } from 'angular-in-memory-web-api';

export class InMemHeroService implements InMemoryDbService {
  createDb() {
    let heroes = [
      { id: '1', name: 'Batman', movies: ['The Dark Knight'] },
      { id: '2', name: 'Bond', movies: ['Casino Royale']  }
    ];
   
   let villians = [
    { id: '1', name: 'Joker', movie: ['The Dark Knight'] }
   ];
    return { searchAnything( heroes ), villians };
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions