diff --git a/types/level-ttl/index.d.ts b/types/level-ttl/index.d.ts new file mode 100644 index 0000000000..5cb2fd9ba2 --- /dev/null +++ b/types/level-ttl/index.d.ts @@ -0,0 +1,64 @@ +// Type definitions for level-ttl 3.1 +// Project: https://github.com/level/level-ttl +// Definitions by: Carson Farmer +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { LevelUp } from 'levelup'; +import { AbstractLevelDOWN, AbstractIterator, ErrorCallback } from 'abstract-leveldown'; + +declare namespace ttl { + interface LevelTTLOptions { + /** + * A string for prefixing the modified database methods (i.e., put, del, batch, ttl, stop). + */ + methodPrefix?: string; + /** + * Character for separating sublevel prefixes from user keys and each other. Should be outside the character (or byte) range of user keys. + * @default '' + */ + separator?: string; + /** + * Character specifying the key namespace for ttl values. + * @default 'ttl' + */ + namespace?: string; + /** + * Character specifying the key namespace for expiration values. + * @default 'x' + */ + expiryNamespace?: string; + /** + * A number specifying the frequency of internal scans for checking for expired keys. + * @default 10000 + */ + checkFrequency?: number; + /** + * A number specifying the default time-to-live for new or updated values. + * This can be overridden by explicitly setting the ttl value. + * @default 0 + */ + defaultTTL?: number; + /** + * A custom storage database for the meta data. + * If it's set, that storage will contain all the ttl meta data. + * A use case for this would be to avoid mixing data and meta data in the same keyspace, since if it's not set, all data will be sharing the same keyspace. + */ + sub?: LevelUp; + } + + interface LevelTTL extends LevelUp, AbstractIterator> { + ttl(key: K, ttl: number, callback?: ErrorCallback): void; + stop(callback?: ErrorCallback): void; + } +} + +/** + * Augment levelup to handle a new 'ttl' option on put() and batch() that specifies + * the number of milliseconds an entry should remain in the data store. + * After the TTL, the entry will be automatically cleared for you. + * @param db + * @param opts + * @see {@link https://github.com/level/level-ttl#usage level-ttl Usage} + */ +declare function ttl(db: LevelUp, AbstractIterator>, opts?: ttl.LevelTTLOptions): ttl.LevelTTL; +export = ttl; diff --git a/types/level-ttl/level-ttl-tests.ts b/types/level-ttl/level-ttl-tests.ts new file mode 100644 index 0000000000..9462181f95 --- /dev/null +++ b/types/level-ttl/level-ttl-tests.ts @@ -0,0 +1,79 @@ +import levelup from 'levelup'; +import { AbstractLevelDOWN } from 'abstract-leveldown'; +import ttl, { LevelTTL, LevelTTLOptions } from 'level-ttl'; + +const db = levelup(new AbstractLevelDOWN('here')); + +const opts: LevelTTLOptions = { defaultTTL: 1000, checkFrequency: 100 }; +const example: LevelTTL = ttl(db, opts); + +example.open(); +example.close(); +example.open(error => { }); + +example.close(error => { }); + +example.stop(); +example.stop(error => { }); + +example.put('key', { ttl: 500 }); +example.put('key', {}, error => { }); +example.put('key', { ttl: 500 }, { sync: true }, error => { }); + +example.put('hello', 'world', () => { +}); + +example.get('key', { keyEncoding: 'json' }, (error, val) => { }); +example.get('key', { fillCache: true }, (error, val) => { }); +example.get('key', (error, val) => { }); + +example.ttl('key', 500); +example.ttl('key', 500, err => { }); + +example.del('key'); +example.del('key', error => { }); +example.del('key', { keyEncoding: 'json' }, error => { }); +example.del('key', { sync: true }, error => { }); + +example.batch( + [ + { + type: 'put', + key: [1, 2, 3], + value: { some: 'json' }, + }, + ], + { ttl: 500 }, + (error: Error | undefined) => { }, +); + +example + .batch() // Not supported by level-ttl + .del('father') + .put('name', 'Yuri Irsenovich Kim') + .put('dob', '16 February 1941') + .put('spouse', 'Kim Young-sook') + .put('occupation', 'Clown') + .write(() => { + console.log('Done!'); + }); + +// $ExpectType boolean +example.isOpen(); +// $ExpectType boolean +example.isClosed(); + +example + .createReadStream() + .on('data', (data: any) => { + console.log(data.key, '=', data.value); + }) + .on('error', (err: any) => { + console.log('Oh my!', err); + }) + .on('close', () => { + console.log('Stream closed'); + }) + .on('end', () => { + console.log('Stream closed'); + }); diff --git a/types/level-ttl/tsconfig.json b/types/level-ttl/tsconfig.json new file mode 100644 index 0000000000..f39737058f --- /dev/null +++ b/types/level-ttl/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "esModuleInterop": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "level-ttl-tests.ts" + ] +} diff --git a/types/level-ttl/tslint.json b/types/level-ttl/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/level-ttl/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }