LokiJS
LokiJS copied to clipboard
Can you add typescript support?
Can you add typescript support?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Out of curiosity, are there things @types/lokijs isn't covering? The only item I've really come across so far is the $exists
operator which isn't exposed on the top level. It might be helpful to have a list of types that need fixing.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lokijs/index.d.ts
Based on the type file history, it seems very receptive to PRs as well.
Unfortunately, the typing definition provided by @types missed some detail about many classes, marked a lot of things as any
which make the definition less useful, and most important, not included the typing for all adapters.
Based on the roadmap I don't think a TS migration is planned. We can either add our own .d.ts
or PR against @types/lokijs
. My suggestion is we improve the external types when we bump against rough edges since those already have tests in place and cover a lot of Loki's API surface area.
A quick list of changes for those who are really good at typings:
- address
any
usage. Where possible,any
should becomeunknown
-
LokiOps
should probably become a genericLokiOps<E = unknown>
, withLokiQuery
passing this type information in. Just typing the LokiOps will remove a lot ofany
. here is how Mongo types their filter operations
-
- add the
$exists
operator ref - add missing adapters and adapter methods
-
LokiFsStructuredAdapter
-
LokiIndexedAdapter
-
LokiPartitioningAdapter
official docs do not mention a lot of methods in the types
-
Additional Context:
- A ts fork of Loki was made (last updated 2020 and appears abandoned). Meanwhile, the Nozbe team did several IndexedDB improvements in 2021 and techfort sees to be merging PRs again.
- There are some Loki alternatives out there with TS support, but all seem to end up in a pile of abandoned software
- As a last resort, if you need a fully typed in-memory DB with query support, I'd recommend either typicode/lowdb or a combination of fake-indexeddb & Dexie. Not to be dismissive of the typing problems, but specifically because typing something as complex as Mongo / Mango style filtering operations is really difficult to add after-the-fact.
I spent 6 days fully reviewing most of Loki's code (~6000 lines), and finally came to the conclusion that perhaps we have to do some house-keeping work to modernize the project and improve the code quality.
In addition, the mongo-like query is not the most tough one, writing typing definition for the Transform API and the lens implantation ( getIn
and deepProperty
) is even more desperate, I've did a lot of experiment about these API.
Maybe I should write a complete code review report but it's too late today, I think it will happen after I finished reading the last part of the source code.
BTW, actually there is another fork called loki-collections, but it is much different from original Loki.js