From 5acee7a124a35c74a0197978a276d258649cb27e Mon Sep 17 00:00:00 2001 From: Pierre de la Martiniere Date: Sun, 25 Dec 2022 23:32:43 +0100 Subject: [PATCH] Make nedb methods return the generic provided + support _id --- index.d.ts | 50 +++++++++++++++++++++++++++++++----------------- typings-tests.ts | 28 ++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/index.d.ts b/index.d.ts index 00786d2..9c43a04 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,7 +5,8 @@ // Stefan Steinhart // Anthony Nichols // Alejandro Fernandez Haro -// TypeScript Version: 4.4 +// Pierre de la Martinière +// TypeScript Version: 4.9 /// @@ -13,7 +14,11 @@ import { EventEmitter } from "events"; export default Nedb; -declare class Nedb extends EventEmitter { +export type Document = Schema & { + _id: string; +}; + +declare class Nedb> extends EventEmitter { constructor(pathOrOptions?: string | Nedb.DataStoreOptions); persistence: Nedb.Persistence; @@ -36,7 +41,7 @@ declare class Nedb extends EventEmitter { stopAutocompaction(): void; - getAllData(): T[]; + getAllData(): Document[]; ensureIndex( options: Nedb.EnsureIndexOptions, @@ -51,15 +56,15 @@ declare class Nedb extends EventEmitter { insert( newDoc: T, - callback?: (err: Error | null, document: T) => void + callback?: (err: Error | null, document: Document) => void ): void; insert( newDocs: T[], - callback?: (err: Error | null, documents: T[]) => void + callback?: (err: Error | null, documents: Document[]) => void ): void; - insertAsync(newDoc: T): Promise; - insertAsync(newDocs: T[]): Promise; + insertAsync(newDoc: T): Promise>; + insertAsync(newDocs: T[]): Promise[]>; count(query: any, callback: (err: Error | null, n: number) => void): void; count(query: any): Nedb.CursorCount; @@ -69,27 +74,36 @@ declare class Nedb extends EventEmitter { find( query: any, projection: any, - callback?: (err: Error | null, documents: T[]) => void + callback?: (err: Error | null, documents: Document[]) => void ): void; - find(query: any, projection?: any): Nedb.Cursor; find( query: any, - callback: (err: Error | null, documents: T[]) => void + projection?: any + ): Nedb.Cursor; + find( + query: any, + callback: (err: Error | null, documents: Document[]) => void ): void; - findAsync(query: any, projection?: any): Nedb.Cursor; + findAsync( + query: any, + projection?: any + ): Nedb.Cursor; findOne( query: any, projection: any, - callback: (err: Error | null, document: T) => void + callback: (err: Error | null, document: Document) => void ): void; findOne( query: any, - callback: (err: Error | null, document: T) => void + callback: (err: Error | null, document: Document) => void ): void; - findOneAsync(query: any, projection?: any): Nedb.Cursor; + findOneAsync( + query: any, + projection?: any + ): Nedb.Cursor; update( query: any, @@ -98,7 +112,7 @@ declare class Nedb extends EventEmitter { callback?: ( err: Error | null, numberOfUpdated: number, - affectedDocuments: T | T[] | null, + affectedDocuments: Document | Document[] | null, upsert: boolean | null ) => void ): void; @@ -109,7 +123,7 @@ declare class Nedb extends EventEmitter { options?: Nedb.UpdateOptions ): Promise<{ numAffected: number; - affectedDocuments: T | T[] | null; + affectedDocuments: Document | Document[] | null; upsert: boolean; }>; @@ -140,8 +154,8 @@ declare namespace Nedb { skip(n: number): Cursor; limit(n: number): Cursor; projection(query: any): Cursor; - exec(callback: (err: Error | null, documents: T[]) => void): void; - execAsync(): Promise; + exec(callback: (err: Error | null, documents: Document[]) => void): void; + execAsync(): Promise>; } interface CursorCount { diff --git a/typings-tests.ts b/typings-tests.ts index 910c3f2..83d068f 100644 --- a/typings-tests.ts +++ b/typings-tests.ts @@ -3,7 +3,7 @@ * Modified my arantes555 on 19.10.2021. */ -import Datastore from './' +import Datastore, { Document } from './' import { mkdirSync } from 'fs' mkdirSync('./workspace/typings/', { recursive: true }) @@ -36,7 +36,18 @@ dbContainer.robots = new Datastore('path/to/robots.db') dbContainer.users.loadDatabase() dbContainer.robots.loadDatabase() -const doc: any = { +type Schema = { + hello: string, + n: number, + today: Date, + nedbIsAwesome: boolean, + notthere: null, + notToBeSaved: undefined, + fruits: string[]; + infos: { name: string } +} + +const doc: Schema = { hello: 'world', n: 5, today: new Date(), @@ -52,7 +63,7 @@ db.insert(doc, (err: Error | null, newDoc: any) => { // Callback is optional // newDoc has no key called notToBeSaved since its value was undefined }) -db.insert([{ a: 5 }, { a: 42 }], (err: Error | null, newdocs: any[]) => { +db.insert([{ a: 5 }, { a: 42 }], (err: Error | null, newdocs) => { // Two documents were inserted in the database // newDocs is an array with these documents, augmented with their _id }) @@ -386,3 +397,14 @@ db.off('compaction.done', () => {}) db.listeners('compaction.done') // $ExpectType (() => void)[] db.rawListeners('compaction.done') // $ExpectType (() => void)[] db.listenerCount('compaction.done') // $ExpectType number + +// Test Generics and types +const db2 = new Datastore({ filename: 'path/to/datafile' }) +db2.loadDatabase(); + +db2.findOne({ _id: 'id1' }, (err, doc) => { + doc._id; // added by nedb + doc.hello; // provided by user + // @ts-expect-error + doc.notExistingKey; // should fail +});