import Datastore from 'nedb' ; import fs from 'fs' ; import { LocalTag } from '~/src/types/localTag' ; export default class HashtagCache { private db : Datastore ; constructor ( path ) { this . db = new Datastore ( { filename : path , autoload : true , onload : ( err ) => { if ( err ) { fs . unlink ( path , err => { if ( err ) { console . error ( err ) ; } } ) ; } } } ) ; this . db . ensureIndex ( { fieldName : 'tagName' , unique : true , sparse : true } , err => { if ( err ) console . error ( err ) ; } ) ; } listTags ( ) { return new Promise ( ( resolve , reject ) => { this . db . find < LocalTag > ( { } , ( err , docs ) => { if ( err ) return reject ( err ) ; resolve ( docs ) ; } ) ; } ) ; } insertHashtag ( tag ) { return new Promise ( ( resolve , reject ) => { this . db . insert ( { tagName : tag } , ( err , doc ) => { if ( err ) return reject ( err ) ; resolve ( doc ) ; } ) ; } ) ; } }