From d1f72cf5b3a0c114f33f5628acf39ec8596a3df6 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 1 Oct 2019 11:53:23 -0400 Subject: [PATCH] MongoTokenDao, TokenStore: Simplify constructors This is a feature that I originally wished I could introduce into easejs. I enjoyed it in Scala. Cuts down on a lot of boilerplate. * src/server/token/MongoTokenDao.ts (constructor): Remove fields and replace with inline definitions. * src/server/token/TokenStore.ts (constructor): Likewise. --- src/server/token/MongoTokenDao.ts | 43 +++++++------------------------ src/server/token/TokenStore.ts | 37 +++++++------------------- 2 files changed, 19 insertions(+), 61 deletions(-) diff --git a/src/server/token/MongoTokenDao.ts b/src/server/token/MongoTokenDao.ts index 00d2ad6..ee90d7a 100644 --- a/src/server/token/MongoTokenDao.ts +++ b/src/server/token/MongoTokenDao.ts @@ -43,41 +43,18 @@ import { context } from "../../error/ContextError"; */ export class MongoTokenDao implements TokenDao { - /** - * Mongo database collection - */ - private readonly _collection: MongoCollection; - - /** - * Field storing token data, relative to document root - */ - private readonly _rootField: string; - - /** - * Retrieve a Unix timestamp - * - * This is used for timestampping token updates. - */ - private readonly _getTimestamp: () => UnixTimestamp; - - /** * Initialize connection * - * @param collection Mongo collection - * @param root_field topmost field in mongo document - * @param date_ctor Date constructor + * @param _collection Mongo collection + * @param _root_field topmost field in mongo document + * @param _date_ctor Date constructor */ constructor( - collection: MongoCollection, - root_field: string, - getTimestamp: () => UnixTimestamp, - ) - { - this._collection = collection; - this._rootField = root_field; - this._getTimestamp = getTimestamp; - } + private readonly _collection: MongoCollection, + private readonly _root_field: string, + private readonly _getTimestamp: () => UnixTimestamp, + ) {} /** @@ -150,7 +127,7 @@ export class MongoTokenDao implements TokenDao } const prev_result = - prev_data[ this._rootField ] || {}; + prev_data[ this._root_field ] || {}; const prev_ns = prev_result[ ns ]; @@ -281,7 +258,7 @@ export class MongoTokenDao implements TokenDao return; } - const field = data[ this._rootField ] + const field = data[ this._root_field ] || {}; const ns_data = field[ ns ]; @@ -410,7 +387,7 @@ export class MongoTokenDao implements TokenDao private _genRoot( ns: TokenNamespace ): string { // XXX: injectable - return this._rootField + '.' + ns; + return this._root_field + '.' + ns; } }; diff --git a/src/server/token/TokenStore.ts b/src/server/token/TokenStore.ts index a886024..23ff313 100644 --- a/src/server/token/TokenStore.ts +++ b/src/server/token/TokenStore.ts @@ -88,39 +88,20 @@ import { DocumentId } from "../../document/Document"; */ export class TokenStore { - /** Data access layer for underlying token data */ - private readonly _dao: TokenDao; - - /** Identifier of document to which store is constrained */ - private readonly _doc_id: DocumentId; - - /** Token namespace used for grouping per document */ - private readonly _token_ns: TokenNamespace; - - /** Token id generator (nullary, nondeterministic) */ - private readonly _idgen: () => TokenId; - - /** * Initialize store * - * @param dao data access layer - * @param doc_id constrain store to given document id - * @param token_ns token namespace - * @param idgen token id generator + * @param _dao data access layer + * @param _doc_id constrain store to given document id + * @param _token_ns token namespace + * @param _idgen token id generator */ constructor( - dao: TokenDao, - doc_id: DocumentId, - token_ns: TokenNamespace, - idgen: () => TokenId - ) - { - this._dao = dao; - this._doc_id = doc_id; - this._token_ns = token_ns; - this._idgen = idgen; - } + private readonly _dao: TokenDao, + private readonly _doc_id: DocumentId, + private readonly _token_ns: TokenNamespace, + private readonly _idgen: () => TokenId + ) {} /**