angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 231 forks source link

Search Across All Fields of a Collection #117

Open vikikamath opened 7 years ago

vikikamath commented 7 years ago

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 };
  }
}
BenRacicot commented 6 years ago

Great question! I'm here to ask about advanced searches also. I was thinking that we should be able to: 1: send a regex to the root in order to check for matches in any collection. (general matching) 2: send a regex to an endpoint (omitting property) in order to check for matches within any properties in that collection. 3: send a regex to search two (or more) collections for matches.

This could mock backend search and be much more fun! Any ideas how to accomplish this?