Skip to content

Instantly share code, notes, and snippets.

@jaf7
Forked from larkintuckerllc/cities.ts
Created August 16, 2019 22:11
Show Gist options
  • Save jaf7/5e398931e86322de5f7d10bb47906261 to your computer and use it in GitHub Desktop.
Save jaf7/5e398931e86322de5f7d10bb47906261 to your computer and use it in GitHub Desktop.
new-1
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