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.master
parent
c8589a1c57
commit
d1f72cf5b3
|
@ -43,41 +43,18 @@ import { context } from "../../error/ContextError";
|
||||||
*/
|
*/
|
||||||
export class MongoTokenDao implements TokenDao
|
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
|
* Initialize connection
|
||||||
*
|
*
|
||||||
* @param collection Mongo collection
|
* @param _collection Mongo collection
|
||||||
* @param root_field topmost field in mongo document
|
* @param _root_field topmost field in mongo document
|
||||||
* @param date_ctor Date constructor
|
* @param _date_ctor Date constructor
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
collection: MongoCollection,
|
private readonly _collection: MongoCollection,
|
||||||
root_field: string,
|
private readonly _root_field: string,
|
||||||
getTimestamp: () => UnixTimestamp,
|
private readonly _getTimestamp: () => UnixTimestamp,
|
||||||
)
|
) {}
|
||||||
{
|
|
||||||
this._collection = collection;
|
|
||||||
this._rootField = root_field;
|
|
||||||
this._getTimestamp = getTimestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -150,7 +127,7 @@ export class MongoTokenDao implements TokenDao
|
||||||
}
|
}
|
||||||
|
|
||||||
const prev_result = <TokenNamespaceResults>
|
const prev_result = <TokenNamespaceResults>
|
||||||
prev_data[ this._rootField ] || {};
|
prev_data[ this._root_field ] || {};
|
||||||
|
|
||||||
const prev_ns = prev_result[ ns ];
|
const prev_ns = prev_result[ ns ];
|
||||||
|
|
||||||
|
@ -281,7 +258,7 @@ export class MongoTokenDao implements TokenDao
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const field = <TokenNamespaceResults>data[ this._rootField ]
|
const field = <TokenNamespaceResults>data[ this._root_field ]
|
||||||
|| {};
|
|| {};
|
||||||
|
|
||||||
const ns_data = field[ ns ];
|
const ns_data = field[ ns ];
|
||||||
|
@ -410,7 +387,7 @@ export class MongoTokenDao implements TokenDao
|
||||||
private _genRoot( ns: TokenNamespace ): string
|
private _genRoot( ns: TokenNamespace ): string
|
||||||
{
|
{
|
||||||
// XXX: injectable
|
// XXX: injectable
|
||||||
return this._rootField + '.' + ns;
|
return this._root_field + '.' + ns;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -88,39 +88,20 @@ import { DocumentId } from "../../document/Document";
|
||||||
*/
|
*/
|
||||||
export class TokenStore
|
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
|
* Initialize store
|
||||||
*
|
*
|
||||||
* @param dao data access layer
|
* @param _dao data access layer
|
||||||
* @param doc_id constrain store to given document id
|
* @param _doc_id constrain store to given document id
|
||||||
* @param token_ns token namespace
|
* @param _token_ns token namespace
|
||||||
* @param idgen token id generator
|
* @param _idgen token id generator
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
dao: TokenDao,
|
private readonly _dao: TokenDao,
|
||||||
doc_id: DocumentId,
|
private readonly _doc_id: DocumentId,
|
||||||
token_ns: TokenNamespace,
|
private readonly _token_ns: TokenNamespace,
|
||||||
idgen: () => TokenId
|
private readonly _idgen: () => TokenId
|
||||||
)
|
) {}
|
||||||
{
|
|
||||||
this._dao = dao;
|
|
||||||
this._doc_id = doc_id;
|
|
||||||
this._token_ns = token_ns;
|
|
||||||
this._idgen = idgen;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue