typicode / lowdb

Simple and fast JSON database
MIT License
21.35k stars 918 forks source link

How to extend FileAsync in TypeScript (LowDB Version 1.0.0) #498

Closed shennan closed 3 years ago

shennan commented 3 years ago

I'm having trouble porting some old LowDB (version 1.0.0) scripts to TypeScript. Everything is working fine with the types from @types/lowdb until I try and extend FileAsync (in order to add business logic to storage and serialisation).

The error:

Base constructors must all have the same return type.ts(2510)

Screenshot 2021-08-09 at 18 16 24
import FileAsync from 'lowdb/adapters/FileAsync.js'

class GoogleCloudLowDBFileAsync extends FileAsync {

  protected cloud

  constructor (source, {cloudConnection, ...rest}) {

    super(source, rest)

    if (cloudConnection)
      this.cloud = cloudConnection

  }

  write (data) {

    const key = this.source.replace(/\.json$/, '')

    this.cloud.state({[key]: data})

    return super.write(data)

  }
}

export default GoogleCloudLowDBFileAsync

What am I doing wrong here? It doesn't seem to be anything between the class parenthesis because if I remove everything there then the error still remains. A simple extending of the class doesn't seem possible.

typicode commented 3 years ago

Sorry I don't known :/ Hope you've found a fix since then. LowDB 3.0 has been released and should have better TS support out of the box. You may want to give it a try.

shennan commented 2 years ago

I would love to use LowDB 3.0 but I think the licensing was blocking me. Has 3.0 moved to an MIT license now? I thought that it became a paid/sponsorship license before?