Adds types and tests for existing npm package level-ttl (#40931)

* feat: adds level-ttl types + tests

Signed-off-by: Carson Farmer <carson.farmer@gmail.com>

* fix: linter fixes

Signed-off-by: Carson Farmer <carson.farmer@gmail.com>
This commit is contained in:
Carson Farmer
2019-12-16 05:18:45 -08:00
committed by Orta
parent 736ae205a3
commit 5cce87d6b0
4 changed files with 168 additions and 0 deletions

64
types/level-ttl/index.d.ts vendored Normal file
View File

@@ -0,0 +1,64 @@
// Type definitions for level-ttl 3.1
// Project: https://github.com/level/level-ttl
// Definitions by: Carson Farmer <https://github.com/me>
// 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<K = any, V = any> extends LevelUp<AbstractLevelDOWN<K, V>, AbstractIterator<K, V>> {
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<K = any, V = any>(db: LevelUp<AbstractLevelDOWN<K, V>, AbstractIterator<K, V>>, opts?: ttl.LevelTTLOptions): ttl.LevelTTL<K, V>;
export = ttl;

View File

@@ -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');
});

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }