From 46eedd55558700b7abf2f260aa0949c16c358d48 Mon Sep 17 00:00:00 2001 From: Daniel Byrne Date: Mon, 17 Sep 2018 15:53:16 -0700 Subject: [PATCH] adds typings for the 'RocksDB' package --- types/rocksdb/index.d.ts | 89 ++++++++++++++++++++++++++++++++++ types/rocksdb/rocksdb-tests.ts | 19 ++++++++ types/rocksdb/tsconfig.json | 23 +++++++++ types/rocksdb/tslint.json | 1 + 4 files changed, 132 insertions(+) create mode 100644 types/rocksdb/index.d.ts create mode 100644 types/rocksdb/rocksdb-tests.ts create mode 100644 types/rocksdb/tsconfig.json create mode 100644 types/rocksdb/tslint.json diff --git a/types/rocksdb/index.d.ts b/types/rocksdb/index.d.ts new file mode 100644 index 0000000000..1ba938271d --- /dev/null +++ b/types/rocksdb/index.d.ts @@ -0,0 +1,89 @@ +// Type definitions for rocksdb 3.0 +// Project: https://github.com/Level/rocksdb +// Definitions by: Meirion Hughes +// Daniel Byrne +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +import { + AbstractLevelDOWN, + AbstractIteratorOptions, + AbstractIterator, + AbstractOpenOptions, + AbstractGetOptions, + ErrorCallback, + ErrorValueCallback, + AbstractChainedBatch, + AbstractBatch, + AbstractOptions +} from 'abstract-leveldown'; + +interface RocksDB extends AbstractLevelDOWN { + open(cb: ErrorCallback): void; + open(options: RocksDB.OpenOptions, cb: ErrorCallback): void; + + get(key: RocksDB.Bytes, cb: ErrorValueCallback): void; + get(key: RocksDB.Bytes, options: RocksDB.GetOptions, cb: ErrorValueCallback): void; + + put(key: RocksDB.Bytes, value: RocksDB.Bytes, cb: ErrorCallback): void; + put(key: RocksDB.Bytes, value: RocksDB.Bytes, options: RocksDB.PutOptions, cb: ErrorCallback): void; + + del(key: RocksDB.Bytes, cb: ErrorCallback): void; + del(key: RocksDB.Bytes, options: RocksDB.DelOptions, cb: ErrorCallback): void; + + batch(): AbstractChainedBatch; + batch(array: AbstractBatch[], cb: ErrorCallback): AbstractChainedBatch; + batch(array: AbstractBatch[], options: RocksDB.BatchOptions, cb: ErrorCallback): AbstractChainedBatch; + + approximateSize(start: RocksDB.Bytes, end: RocksDB.Bytes, cb: RocksDB.ErrorSizeCallback): void; + compactRange(start: RocksDB.Bytes, end: RocksDB.Bytes, cb: ErrorCallback): void; + getProperty(property: string): string; + destroy(location: string, cb: ErrorCallback): void; + repair(location: string, cb: ErrorCallback): void; + iterator(options?: RocksDB.IteratorOptions): RocksDB.Iterator; +} + +declare namespace RocksDB { + type Bytes = string | Buffer; + type ErrorSizeCallback = (err: Error | undefined, size: number) => void; + // tslint:disable-next-line:no-empty-interface + interface OpenOptions extends AbstractOpenOptions {} + + interface GetOptions extends AbstractGetOptions { + fillCache?: boolean; + } + + interface PutOptions extends AbstractOptions { + sync?: boolean; + } + + interface DelOptions extends AbstractOptions { + sync?: boolean; + } + + interface BatchOptions extends AbstractOptions { + sync?: boolean; + } + + interface IteratorOptions extends AbstractIteratorOptions { + fillCache?: boolean; + } + + interface Iterator extends AbstractIterator { + seek(key: Bytes): void; + binding: any; + cache: any; + finished: any; + fastFuture: any; + } + + interface Constructor { + new(location: string): RocksDB; + (location: string): RocksDB; + } +} + +declare const RocksDB: RocksDB.Constructor; +export default RocksDB; diff --git a/types/rocksdb/rocksdb-tests.ts b/types/rocksdb/rocksdb-tests.ts new file mode 100644 index 0000000000..6703b4b387 --- /dev/null +++ b/types/rocksdb/rocksdb-tests.ts @@ -0,0 +1,19 @@ +import RocksDB from 'rocksdb'; + +// can use 'new', or not. +const a = new RocksDB("./tmp/rocksdb"); +const b = RocksDB("./tmp/rocksdb"); + +const down = new RocksDB("./tmp/rocksdb"); + +down.open(() => { + down.put("key", "value", (err?) => { }); + down.put(Buffer.from([1]), "value", { something: true }, (err?) => { }); + + down.get("key", (err?) => { }); + down.get(Buffer.from([1]), { something: true }, (err: Error | undefined, value: any) => { }); + + down.close(() => { + // do nothing + }); +}); diff --git a/types/rocksdb/tsconfig.json b/types/rocksdb/tsconfig.json new file mode 100644 index 0000000000..268e499637 --- /dev/null +++ b/types/rocksdb/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "rocksdb-tests.ts" + ] +} \ No newline at end of file diff --git a/types/rocksdb/tslint.json b/types/rocksdb/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/rocksdb/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }