Skip to content

Commit

Permalink
Prisma full-text search support
Browse files Browse the repository at this point in the history
  • Loading branch information
maotora committed Aug 27, 2021
1 parent 7467e57 commit d92dfc5
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 38 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@nestjs/core": "^7.6.15",
"@nestjs/mapped-types": "^0.4.1",
"@nestjs/platform-express": "^7.6.15",
"@prisma/client": "^2.17.0",
"@prisma/client": "^2.30.0",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"config": "^3.3.6",
Expand All @@ -50,7 +50,7 @@
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"prisma": "^2.17.0",
"prisma": "^2.30.0",
"supertest": "^6.1.3",
"ts-jest": "^26.5.4",
"ts-loader": "^8.0.18",
Expand Down
39 changes: 39 additions & 0 deletions prisma/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}

datasource db {
Expand Down
13 changes: 11 additions & 2 deletions src/districts/districts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Get, Param, Controller } from '@nestjs/common';
import { DistrictsService } from "./districts.service"
import { DistrictSearchService, DistrictsService } from "./districts.service"
import { Districts as DistrictModel } from "@prisma/client"

@Controller('districts')
export class DistrictsController {
constructor(private readonly districtsService: DistrictsService){}
constructor(
private readonly districtsService: DistrictsService,
private readonly districtsSearchService: DistrictSearchService
){}

@Get()
async getAllDistricts(): Promise<DistrictModel[]> {
Expand All @@ -25,4 +28,10 @@ export class DistrictsController {
async getDistrictsByIdWithWards(@Param("id") id: string): Promise<DistrictModel> {
return this.districtsService.districtWithWards({districtCode: +id})
}

@Get("search/:searchText")
async searchDistricts(@Param('searchText') searchText) {
return this.districtsSearchService.search(searchText)
}

}
4 changes: 2 additions & 2 deletions src/districts/districts.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from "@nestjs/common"
import { DistrictsController } from "./districts.controller"
import { DistrictsService } from "./districts.service"
import { DistrictsService, DistrictSearchService } from "./districts.service"
import { PrismaService } from "../prisma.service"

@Module({
controllers: [DistrictsController],
providers: [PrismaService, DistrictsService]
providers: [PrismaService, DistrictsService, DistrictSearchService]
})
export class DistrictsModule {}
34 changes: 33 additions & 1 deletion src/districts/districts.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, NotFoundException, HttpException, HttpStatus } from '@nestjs/common';
import { PrismaService } from "../prisma.service"
import { Districts, Prisma } from "@prisma/client"

Expand Down Expand Up @@ -70,3 +70,35 @@ export class DistrictsService {
return res
}
}

@Injectable()
export class DistrictSearchService {
constructor(private prisma: PrismaService) {};

async search(
searchInput: String
): Promise<any | null> {

if(searchInput.length >= 3) {
const res = await this.prisma.districts.findMany({
where: {
districtName: {
search: `${searchInput}:*`
}
},
include: {
countries: true,
regions: true,
}
})

if(res.length <= 0) {
throw new NotFoundException(`No district found matching: ${searchInput}`)
}

return res
}

throw new HttpException(`Cannot search with less that 3 characters.`, HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
}
}
11 changes: 9 additions & 2 deletions src/places/places.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Get, Param, Controller } from '@nestjs/common';
import { Places as PlacesModel } from "@prisma/client"
import { PlacesService } from "./places.service"
import { PlacesService, PlacesSearchService } from "./places.service"

@Controller('places')
export class PlacesController {
constructor(private readonly placesService: PlacesService){}
constructor(
private readonly placesService: PlacesService,
private readonly placesSearchService: PlacesSearchService
){}

@Get()
async getPlaces() {
Expand All @@ -23,4 +26,8 @@ export class PlacesController {
return this.placesService.place({id: +id})
}

@Get("search/:searchText")
async searchPlaces(@Param('searchText') searchText) {
return this.placesSearchService.search(searchText)
}
}
4 changes: 2 additions & 2 deletions src/places/places.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from "@nestjs/common"
import { PlacesController } from "./places.controller"
import { PlacesService } from './places.service'
import { PlacesService, PlacesSearchService } from './places.service'
import { PrismaService } from "../prisma.service"

@Module({
controllers: [PlacesController],
providers: [PrismaService, PlacesService]
providers: [PrismaService, PlacesService, PlacesSearchService]
})
export class PlacesModule {}
45 changes: 44 additions & 1 deletion src/places/places.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, NotFoundException, HttpException, HttpStatus } from '@nestjs/common';
import { PrismaService } from "../prisma.service"
import { Places, Prisma } from "@prisma/client"

Expand Down Expand Up @@ -43,3 +43,46 @@ export class PlacesService {
}

}

@Injectable()
export class PlacesSearchService {
constructor(private prisma: PrismaService) {};

async search(
searchInput: String
): Promise<any | null> {

if(searchInput.length >= 3) {
const res = await this.prisma.places.findMany({
where: {
placeName: {
search: `${searchInput}:*`
}
},
include: {
ward: {
include: {
districts: {
include: {
regions: {
include: {
countries: true
}
}
}
}
}
}
}
})

if(res.length <= 0) {
throw new NotFoundException(`No street found matching: ${searchInput}`)
}

return res
}

throw new HttpException(`Cannot search with less that 3 characters.`, HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
}
}
13 changes: 11 additions & 2 deletions src/regions/regions.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Controller, Get, Param } from '@nestjs/common';
import { RegionsService } from "./regions.service"
import { RegionsSearchService, RegionsService } from "./regions.service"
import { Regions as RegionsModel } from "@prisma/client"

@Controller('regions')
export class RegionsController {
constructor (private readonly regionsSerice: RegionsService) {}
constructor (
private readonly regionsSerice: RegionsService,
private readonly regionsSearchService: RegionsSearchService
) {}

@Get()
getAllRegions(): Promise<RegionsModel[]> {
Expand All @@ -25,4 +28,10 @@ export class RegionsController {
async getRegionByName(@Param("regionName") regionName: string): Promise<RegionsModel[]> {
return this.regionsSerice.regions({where: {regionName}})
}

@Get("search/:searchText")
async searchRegions(@Param('searchText') searchText) {
return this.regionsSearchService.search(searchText)
}

}
4 changes: 2 additions & 2 deletions src/regions/regions.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from "@nestjs/common"
import { RegionsService } from "./regions.service"
import { RegionsSearchService, RegionsService } from "./regions.service"
import { RegionsController } from "./regions.controller"
import { PrismaService } from "../prisma.service"

@Module({
controllers: [RegionsController],
providers: [RegionsService, PrismaService]
providers: [RegionsService, RegionsSearchService, PrismaService]
})
export class RegionsModule {}
33 changes: 32 additions & 1 deletion src/regions/regions.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, NotFoundException, HttpException, HttpStatus } from '@nestjs/common';
import { PrismaService } from "../prisma.service"
import { Regions, Prisma } from "@prisma/client"

Expand Down Expand Up @@ -58,3 +58,34 @@ export class RegionsService {
return res
}
}

@Injectable()
export class RegionsSearchService {
constructor(private prisma: PrismaService) {};

async search(
searchInput: String
): Promise<any | null> {

if(searchInput.length >= 3) {
const res = await this.prisma.regions.findMany({
where: {
regionName: {
search: `${searchInput}:*`
}
},
include: {
countries: true
}
})

if(res.length <= 0) {
throw new NotFoundException(`No region found matching: ${searchInput}`)
}

return res
}

throw new HttpException(`Cannot search with less that 3 characters.`, HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
}
}
7 changes: 5 additions & 2 deletions src/search/search.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Controller, Body, Post } from '@nestjs/common';
import { Controller, Body, Post, Get, Param } from '@nestjs/common';
import { SearchService } from "./search.service"

@Controller('search')
export class SearchController {
constructor( private readonly searchService: SearchService ){}
constructor(
private readonly searchService: SearchService,
){}

@Post()
async getLocation(@Body() body) {
return this.searchService.search(body.searchText)
}

}
Loading

0 comments on commit d92dfc5

Please sign in to comment.