Skip to content

Commit

Permalink
refactor: move store to options
Browse files Browse the repository at this point in the history
its optional like the other options

also use ttl store instead when no store is supplied

BREAKING CHANGE: constructor changed
  • Loading branch information
EdJoPaTo committed Jun 20, 2020
1 parent 09d639c commit ce30843
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Cache} from '@edjopato/datastore';
import {Cache, TtlKeyValueInMemory} from '@edjopato/datastore';
import {EntitySimplified} from 'wikidata-sdk-got/dist/source/wikibase-sdk-types';
import {getEntitiesSimplified} from 'wikidata-sdk-got';
import {isEntityId} from 'wikibase-types';
Expand Down Expand Up @@ -45,6 +45,12 @@ export interface Options {
*/
readonly logQueriedEntityIds?: boolean;

/**
* Store object which keeps the entities. Can be used to store more persistent than in memory.
* In order to always use up to date wikidata entities use a store with ttl support.
*/
readonly store?: Store<EntitySimplified>;

/**
* User Agent which is used to query the items
*/
Expand All @@ -61,12 +67,13 @@ export class TelegrafWikibase {
private readonly _contextKey: string;

constructor(
store: Store<EntitySimplified> = new Map(),
options: Options = {}
) {
this._defaultLanguageCode = 'en';
this._contextKey = options.contextKey ?? 'wb';

const store: Store<EntitySimplified> = options.store ?? new TtlKeyValueInMemory();

this._entityCache = new Cache({bulkQuery: async ids => {
if (options.logQueriedEntityIds) {
console.log('TelegrafWikibase getEntities', ids.length, ids);
Expand Down
2 changes: 1 addition & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function macro(
return next();
});

const twb = new TelegrafWikibase(entityStore, options);
const twb = new TelegrafWikibase({store: entityStore, ...options});
twb.addResourceKeys({human: 'Q5', earth: 'Q2', cat: 'Q146'});

bot.use(twb.middleware());
Expand Down

0 comments on commit ce30843

Please sign in to comment.