Merge pull request #30 from martpie/master

Improved TS typings: make nedb methods return _id
pull/34/head
tex0l 2 years ago committed by GitHub
commit bb6bb8d645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 135
      index.d.ts
  2. 1052
      package-lock.json
  3. 4
      package.json
  4. 26
      typings-tests.ts

135
index.d.ts vendored

@ -5,15 +5,20 @@
// 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" />
import { EventEmitter } from 'events'; import { EventEmitter } from "events";
export default Nedb; export default Nedb;
declare class Nedb<G = 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,9 +41,12 @@ declare class Nedb<G = any> extends EventEmitter {
stopAutocompaction(): void; stopAutocompaction(): void;
getAllData<T extends G>(): T[]; getAllData<T extends Schema>(): Document<T>[];
ensureIndex(options: Nedb.EnsureIndexOptions, callback?: (err: Error | null) => void): void; ensureIndex(
options: Nedb.EnsureIndexOptions,
callback?: (err: Error | null) => void
): void;
ensureIndexAsync(options: Nedb.EnsureIndexOptions): Promise<void>; ensureIndexAsync(options: Nedb.EnsureIndexOptions): Promise<void>;
@ -46,47 +54,98 @@ declare class Nedb<G = any> extends EventEmitter {
removeIndexAsync(fieldName: string | string[]): Promise<void>; removeIndexAsync(fieldName: string | string[]): Promise<void>;
insert<T extends G>(newDoc: T, callback?: (err: Error | null, document: T) => void): void; insert<T extends Schema>(
insert<T extends G>(newDocs: T[], callback?: (err: Error | null, documents: T[]) => void): void; newDoc: T,
callback?: (err: Error | null, document: Document<T>) => void
): void;
insert<T extends Schema>(
newDocs: T[],
callback?: (err: Error | null, documents: Document<T>[]) => void
): void;
insertAsync<T extends G>(newDoc: T): Promise<T>; insertAsync<T extends Schema>(newDoc: T): Promise<Document<T>>;
insertAsync<T extends G>(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;
countAsync(query: any): Nedb.Cursor<number>; countAsync(query: any): Nedb.Cursor<number>;
find<T extends G>(query: any, projection: any, callback?: (err: Error | null, documents: T[]) => void): void; find<T extends Schema>(
find<T extends G>(query: any, projection?: any): Nedb.Cursor<T>; query: any,
find<T extends G>(query: any, callback: (err: Error | null, documents: T[]) => void): void; projection: any,
callback?: (err: Error | null, documents: Document<T>[]) => void
findAsync<T extends G>(query: any, projection?: any): Nedb.Cursor<T[]>; ): void;
find<T extends Schema>(
findOne<T extends G>(query: any, projection: any, callback: (err: Error | null, document: T) => void): void; query: any,
findOne<T extends G>(query: any, callback: (err: Error | null, document: T) => void): void; projection?: any
): Nedb.Cursor<T>;
findOneAsync<T extends G>(query: any, projection?: any): Nedb.Cursor<T>; find<T extends Schema>(
query: any,
update<T extends G>(query: any, updateQuery: any, options?: Nedb.UpdateOptions, callback?: (err: Error | null, numberOfUpdated: number, affectedDocuments: T | T[] | null, upsert: boolean | null) => void): void; callback: (err: Error | null, documents: Document<T>[]) => void
): void;
updateAsync<T extends G>(query: any, updateQuery: any, options?: Nedb.UpdateOptions): Promise<{numAffected: number, affectedDocuments: T|T[]|null, upsert: boolean}>;
findAsync<T extends Schema>(
remove(query: any, options: Nedb.RemoveOptions, callback?: (err: Error | null, n: number) => void): void; query: any,
projection?: any
): Nedb.Cursor<T[]>;
findOne<T extends Schema>(
query: any,
projection: any,
callback: (err: Error | null, document: Document<T>) => void
): void;
findOne<T extends Schema>(
query: any,
callback: (err: Error | null, document: Document<T>) => void
): void;
findOneAsync<T extends Schema>(
query: any,
projection?: any
): Nedb.Cursor<T>;
update<T extends Schema>(
query: any,
updateQuery: any,
options?: Nedb.UpdateOptions,
callback?: (
err: Error | null,
numberOfUpdated: number,
affectedDocuments: Document<T> | Document<T>[] | null,
upsert: boolean | null
) => void
): void;
updateAsync<T extends Schema>(
query: any,
updateQuery: any,
options?: Nedb.UpdateOptions
): Promise<{
numAffected: number;
affectedDocuments: Document<T> | Document<T>[] | null;
upsert: boolean;
}>;
remove(
query: any,
options: Nedb.RemoveOptions,
callback?: (err: Error | null, n: number) => void
): void;
remove(query: any, callback?: (err: Error | null, n: number) => void): void; remove(query: any, callback?: (err: Error | null, n: number) => void): void;
removeAsync(query: any, options: Nedb.RemoveOptions): Promise<number>; removeAsync(query: any, options: Nedb.RemoveOptions): Promise<number>;
addListener(event: 'compaction.done', listener: () => void): this; addListener(event: "compaction.done", listener: () => void): this;
on(event: 'compaction.done', listener: () => void): this; on(event: "compaction.done", listener: () => void): this;
once(event: 'compaction.done', listener: () => void): this; once(event: "compaction.done", listener: () => void): this;
prependListener(event: 'compaction.done', listener: () => void): this; prependListener(event: "compaction.done", listener: () => void): this;
prependOnceListener(event: 'compaction.done', listener: () => void): this; prependOnceListener(event: "compaction.done", listener: () => void): this;
removeListener(event: 'compaction.done', listener: () => void): this; removeListener(event: "compaction.done", listener: () => void): this;
off(event: 'compaction.done', listener: () => void): this; off(event: "compaction.done", listener: () => void): this;
listeners(event: 'compaction.done'): Array<() => void>; listeners(event: "compaction.done"): Array<() => void>;
rawListeners(event: 'compaction.done'): Array<() => void>; rawListeners(event: "compaction.done"): Array<() => void>;
listenerCount(type: 'compaction.done'): number; listenerCount(type: "compaction.done"): number;
} }
declare namespace Nedb { declare namespace Nedb {
@ -95,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 {
@ -113,7 +172,7 @@ declare namespace Nedb {
afterSerialization?(line: string): string; afterSerialization?(line: string): string;
corruptAlertThreshold?: number; corruptAlertThreshold?: number;
compareStrings?(a: string, b: string): number; compareStrings?(a: string, b: string): number;
modes?: {fileMode: number, dirMode: number}; modes?: { fileMode: number; dirMode: number };
testSerializationHooks?: boolean; testSerializationHooks?: boolean;
} }

1052
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -69,14 +69,14 @@
"path-browserify": "^1.0.1", "path-browserify": "^1.0.1",
"process": "^0.11.10", "process": "^0.11.10",
"react": "^18.2.0", "react": "^18.2.0",
"react-native": "^0.71.0", "react-native": "^0.71.2",
"semver": "^7.3.8", "semver": "^7.3.8",
"source-map-loader": "^4.0.1", "source-map-loader": "^4.0.1",
"standard": "^17.0.0", "standard": "^17.0.0",
"terser-webpack-plugin": "^5.3.6", "terser-webpack-plugin": "^5.3.6",
"timers-browserify": "^2.0.12", "timers-browserify": "^2.0.12",
"ts-jest": "^27.1.5", "ts-jest": "^27.1.5",
"typescript": "^4.9.4", "typescript": "^4.9.5",
"webpack": "^5.75.0", "webpack": "^5.75.0",
"webpack-cli": "^5.0.1", "webpack-cli": "^5.0.1",
"xvfb-maybe": "^0.2.1" "xvfb-maybe": "^0.2.1"

@ -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