levelup: Add declarations for custom encodings.

Levelup `keyEncoding`/`valueEncoding` options also accept an object that
implements a custom encoding, add declarations to enable it.
This commit is contained in:
Thiago de Arruda 2016-11-21 20:13:00 -03:00
parent 508c535dfb
commit 69bdfc5daa
2 changed files with 47 additions and 11 deletions

30
levelup/index.d.ts vendored
View File

@ -1,14 +1,26 @@
// Type definitions for LevelUp
// Project: https://github.com/rvagg/node-levelup
// Project: https://github.com/Level/levelup
// Definitions by: Bret Little <https://github.com/blittle>
// Definitions by: Thiago de Arruda <https://github.com/tarruda>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
interface CustomEncoding {
encode(val: any): Buffer| string;
decode(val: Buffer | string): any;
buffer: boolean;
type: string;
}
type Encoding = string | CustomEncoding;
interface Batch {
type: string;
key: any;
value?: any;
keyEncoding?: string;
valueEncoding?: string;
keyEncoding?: Encoding;
valueEncoding?: Encoding;
}
interface LevelUp {
open(callback ?: (error : any) => any): void;
@ -17,12 +29,12 @@ interface LevelUp {
put(key: any, value: any, options?: { sync?: boolean }, callback ?: (error: any) => any): void;
get(key: any, callback ?: (error: any, value: any) => any): void;
get(key: any, options ?: { keyEncoding?: string; fillCache?: boolean }, callback ?: (error: any, value: any) => any): void;
get(key: any, options ?: { keyEncoding?: Encoding; fillCache?: boolean }, callback ?: (error: any, value: any) => any): void;
del(key: any, callback ?: (error: any) => any): void;
del(key: any, options ?: { keyEncoding?: string; sync?: boolean }, callback ?: (error: any) => any): void;
del(key: any, options ?: { keyEncoding?: Encoding; sync?: boolean }, callback ?: (error: any) => any): void;
batch(array: Batch[], options?: { keyEncoding?: string; valueEncoding?: string; sync?: boolean }, callback?: (error?: any)=>any): void;
batch(array: Batch[], options?: { keyEncoding?: Encoding; valueEncoding?: Encoding; sync?: boolean }, callback?: (error?: any)=>any): void;
batch(array: Batch[], callback?: (error?: any)=>any): void;
batch():LevelUpChain;
isOpen():boolean;
@ -39,7 +51,7 @@ interface LevelUpChain {
put(key: any, value: any): LevelUpChain;
put(key: any, value: any, options?: { sync?: boolean }): LevelUpChain;
del(key: any): LevelUpChain;
del(key: any, options ?: { keyEncoding?: string; sync?: boolean }): LevelUpChain;
del(key: any, options ?: { keyEncoding?: Encoding; sync?: boolean }): LevelUpChain;
clear(): LevelUpChain;
write(callback?: (error?: any)=>any) : LevelUpChain;
}
@ -49,8 +61,8 @@ interface levelupOptions {
errorIfExists?: boolean;
compression?: boolean;
cacheSize?: number;
keyEncoding?: string;
valueEncoding?: string;
keyEncoding?: Encoding;
valueEncoding?: Encoding;
db?: string
}

View File

@ -1,7 +1,31 @@
import levelup = require("levelup");
interface BufferEncoding {
encode(val: any): Buffer;
decode(val: Buffer): any;
buffer: boolean;
type: string;
}
interface StringEncoding {
encode(val: any): string;
decode(val: string): any;
buffer: boolean;
type: string;
}
declare const bufferEncoding: BufferEncoding;
declare const stringEncoding: StringEncoding;
var db1 = levelup("db1", {
keyEncoding: bufferEncoding,
valueEncoding: bufferEncoding
});
var db2 = levelup("db2", {
keyEncoding: stringEncoding,
valueEncoding: stringEncoding
});
var db = levelup('./mydb')
db.open();
db.close();