-
-
Save jaf7/5e398931e86322de5f7d10bb47906261 to your computer and use it in GitHub Desktop.
new-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dexie from 'dexie'; | |
import citiesJson from './cities.json'; | |
const delay = () => | |
new Promise(resolve => { | |
setTimeout(() => { | |
resolve(''); | |
}, 1000); | |
}); | |
export interface City { | |
id: number; | |
city: string; | |
state: string; | |
} | |
const db = new Dexie('MyAppDatabase'); | |
db.version(1).stores({ | |
cities: 'id, city, state', | |
}); | |
export const loadCities = async () => { | |
await db.open(); | |
await db.table('cities').clear(); | |
await delay(); | |
const cities = citiesJson.map((o, i) => ({ | |
id: i, | |
...o, | |
})) as City[]; | |
await db.table('cities').bulkAdd(cities); | |
}; | |
export const searchCities = async (starts: string) => { | |
const results = await db | |
.table('cities') | |
.where('city') | |
.startsWithIgnoreCase(starts) | |
.toArray(); | |
return results as City[]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment