Make nedb methods return the generic provided + support _id

pull/30/head
Pierre de la Martiniere 2 years ago committed by Timothée Rebours
parent a90b4bc814
commit 5acee7a124
  1. 50
      index.d.ts
  2. 28
      typings-tests.ts

50
index.d.ts vendored

@ -5,7 +5,8 @@
// Stefan Steinhart <https://github.com/reppners> // Stefan Steinhart <https://github.com/reppners>
// Anthony Nichols <https://github.com/anthonynichols> // Anthony Nichols <https://github.com/anthonynichols>
// Alejandro Fernandez Haro <https://github.com/afharo> // Alejandro Fernandez Haro <https://github.com/afharo>
// TypeScript Version: 4.4 // Pierre de la Martinière <https://github.com/martpie>
// TypeScript Version: 4.9
/// <reference types="node" /> /// <reference types="node" />
@ -13,7 +14,11 @@ import { EventEmitter } from "events";
export default Nedb; export default Nedb;
declare class Nedb<Schema = any> extends EventEmitter { export type Document<Schema> = Schema & {
_id: string;
};
declare class Nedb<Schema = Record<string, any>> extends EventEmitter {
constructor(pathOrOptions?: string | Nedb.DataStoreOptions); constructor(pathOrOptions?: string | Nedb.DataStoreOptions);
persistence: Nedb.Persistence; persistence: Nedb.Persistence;
@ -36,7 +41,7 @@ declare class Nedb<Schema = any> extends EventEmitter {
stopAutocompaction(): void; stopAutocompaction(): void;
getAllData<T extends Schema>(): T[]; getAllData<T extends Schema>(): Document<T>[];
ensureIndex( ensureIndex(
options: Nedb.EnsureIndexOptions, options: Nedb.EnsureIndexOptions,
@ -51,15 +56,15 @@ declare class Nedb<Schema = any> extends EventEmitter {
insert<T extends Schema>( insert<T extends Schema>(
newDoc: T, newDoc: T,
callback?: (err: Error | null, document: T) => void callback?: (err: Error | null, document: Document<T>) => void
): void; ): void;
insert<T extends Schema>( insert<T extends Schema>(
newDocs: T[], newDocs: T[],
callback?: (err: Error | null, documents: T[]) => void callback?: (err: Error | null, documents: Document<T>[]) => void
): void; ): void;
insertAsync<T extends Schema>(newDoc: T): Promise<T>; insertAsync<T extends Schema>(newDoc: T): Promise<Document<T>>;
insertAsync<T extends Schema>(newDocs: T[]): Promise<T[]>; insertAsync<T extends Schema>(newDocs: T[]): Promise<Document<T>[]>;
count(query: any, callback: (err: Error | null, n: number) => void): void; count(query: any, callback: (err: Error | null, n: number) => void): void;
count(query: any): Nedb.CursorCount; count(query: any): Nedb.CursorCount;
@ -69,27 +74,36 @@ declare class Nedb<Schema = any> extends EventEmitter {
find<T extends Schema>( find<T extends Schema>(
query: any, query: any,
projection: any, projection: any,
callback?: (err: Error | null, documents: T[]) => void callback?: (err: Error | null, documents: Document<T>[]) => void
): void; ): void;
find<T extends Schema>(query: any, projection?: any): Nedb.Cursor<T>;
find<T extends Schema>( find<T extends Schema>(
query: any, query: any,
callback: (err: Error | null, documents: T[]) => void projection?: any
): Nedb.Cursor<T>;
find<T extends Schema>(
query: any,
callback: (err: Error | null, documents: Document<T>[]) => void
): void; ): void;
findAsync<T extends Schema>(query: any, projection?: any): Nedb.Cursor<T[]>; findAsync<T extends Schema>(
query: any,
projection?: any
): Nedb.Cursor<T[]>;
findOne<T extends Schema>( findOne<T extends Schema>(
query: any, query: any,
projection: any, projection: any,
callback: (err: Error | null, document: T) => void callback: (err: Error | null, document: Document<T>) => void
): void; ): void;
findOne<T extends Schema>( findOne<T extends Schema>(
query: any, query: any,
callback: (err: Error | null, document: T) => void callback: (err: Error | null, document: Document<T>) => void
): void; ): void;
findOneAsync<T extends Schema>(query: any, projection?: any): Nedb.Cursor<T>; findOneAsync<T extends Schema>(
query: any,
projection?: any
): Nedb.Cursor<T>;
update<T extends Schema>( update<T extends Schema>(
query: any, query: any,
@ -98,7 +112,7 @@ declare class Nedb<Schema = any> extends EventEmitter {
callback?: ( callback?: (
err: Error | null, err: Error | null,
numberOfUpdated: number, numberOfUpdated: number,
affectedDocuments: T | T[] | null, affectedDocuments: Document<T> | Document<T>[] | null,
upsert: boolean | null upsert: boolean | null
) => void ) => void
): void; ): void;
@ -109,7 +123,7 @@ declare class Nedb<Schema = any> extends EventEmitter {
options?: Nedb.UpdateOptions options?: Nedb.UpdateOptions
): Promise<{ ): Promise<{
numAffected: number; numAffected: number;
affectedDocuments: T | T[] | null; affectedDocuments: Document<T> | Document<T>[] | null;
upsert: boolean; upsert: boolean;
}>; }>;
@ -140,8 +154,8 @@ declare namespace Nedb {
skip(n: number): Cursor<T>; skip(n: number): Cursor<T>;
limit(n: number): Cursor<T>; limit(n: number): Cursor<T>;
projection(query: any): Cursor<T>; projection(query: any): Cursor<T>;
exec(callback: (err: Error | null, documents: T[]) => void): void; exec(callback: (err: Error | null, documents: Document<T>[]) => void): void;
execAsync(): Promise<T>; execAsync(): Promise<Document<T>>;
} }
interface CursorCount { interface CursorCount {

@ -3,7 +3,7 @@
* Modified my arantes555 on 19.10.2021. * Modified my arantes555 on 19.10.2021.
*/ */
import Datastore from './' import Datastore, { Document } from './'
import { mkdirSync } from 'fs' import { mkdirSync } from 'fs'
mkdirSync('./workspace/typings/', { recursive: true }) mkdirSync('./workspace/typings/', { recursive: true })
@ -36,7 +36,18 @@ dbContainer.robots = new Datastore('path/to/robots.db')
dbContainer.users.loadDatabase() dbContainer.users.loadDatabase()
dbContainer.robots.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', hello: 'world',
n: 5, n: 5,
today: new Date(), 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 // 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 // Two documents were inserted in the database
// newDocs is an array with these documents, augmented with their _id // 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.listeners('compaction.done') // $ExpectType (() => void)[]
db.rawListeners('compaction.done') // $ExpectType (() => void)[] db.rawListeners('compaction.done') // $ExpectType (() => void)[]
db.listenerCount('compaction.done') // $ExpectType number db.listenerCount('compaction.done') // $ExpectType number
// Test Generics and types
const db2 = new Datastore<Schema>({ 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
});

Loading…
Cancel
Save