This creates an interface for TokenStore. The main motivation of this right
now is testing, since I'm punting on figuring out a mock framework right
now (due to time constraints).
* src/server/token/TokenStore.ts: Rename file.
* src/server/token/store/PersistentTokenStore.ts: Rename from TokenStore.
* test/server/token/TokenStoreTest.ts: Rename file.
* test/server/token/store/PersistentTokenStoreTest.ts: Rename from TokenStoreTest.
This is much more useful information than the last modified. For example:
- Token A is created. It becomes the last modified.
- Token B is created. It becomes the last modified.
- Token A completes. Mismatch. It becomes the last modified.
- Token B completes. Mismatch. It becomes the last modified.
So in this case, we're unable to use the flag to determine whether we should
ignore the token. But if we instead us the new flag to see what token was
last _created_, the problem is solved.
This should have been obvious the first time around.
* src/server/token/MongoTokenDao.ts (updateToken): Query
`lastState'. Return its value. Update its value.
(getToken): Query lastState. Return its value.
* src/server/token/Token.ts (Token)[last_state]: New field.
* src/server/token/TokenDao.ts (TokenQueryResult, TokenNamespaceResults):
Use type instead of interface.
(TokenStateHistory): New type.
(TokenNamespaceData)[lastState]: New optional field.
(TokenData)[prev_state]: New field.
* src/server/token/TokenStore.ts: Return previous state data for each
method.
* test/server/token/MongoTokenDaoTest.ts: Add last_state.
* test/server/token/TokenStoreTest.ts: Likewise.
The primary use case for this is currently the DataAPI, and the quote id is
only available at the highest level of the server, before dapis are
processed.
In any case, the TokenStore was already described in terms of a combination
of document id; namespace; and root field; so it makes sense for doc id to
be part of the constructor.
If a more generic TokenStore is needed in the future, we could go back to
the previous API and wrap it in another class, like a partially applied
function (e.g. `DocumentTokenStore`).
* src/server/token/TokenStore.ts: Move doc_id out of arguments and into the
constructor.
* test/server/token/TokenStoreTest.ts: Update accordingly.
Does not yet support token updates.
* src/server/token/Token.ts (TokenStateDeadable, TokenStateDoneable,
TokenStateAcceptable, Token): New types.
* src/server/token/TokenStore.ts: New class.
* test/server/token/TokenStoreTest.ts: New test case.
This causes the DAO to return the state of the document prior to the
database operation (in the case of a retrieval, the previous state is the
same as the current state). This will allow determining whether other
tokens were modified since a previous request.
* src/server/token/MongoTokenDao.ts: Stop using TokenType in favor of new
TokenState.
(updateToken): Query for and return previous state. Use findAndModify
instead of update.
(getToken): Return previous state. Minor changes to account for new
TokenQueryResult types (null=>undefined).
* src/server/token/Token.ts: Add comments for existing Token{Id,Namespace}
nominal types.
(TokenState): New string enum.
* src/server/token/TokenDao.ts: Import new TokenState.
(TokenDao)[updateToken]: Use it.
(TokenType): Remove.
(TokenQueryResult, TokenNamespaceResults, TokenNamespaceData):
null=>undefined for unavailable value. null was wrong.
(TokenStatus): Token{Type=>State}.
(TokenData)[prev_state, prev_status]: New fields.
* test/server/token/MongoTokenDaoTest.ts: Update tests accordingly.
Since some of these data are generated within TokenDao (e.g. the timestamp),
the caller cannot infer all values.
* src/server/token/MongoTokenDao.ts (updateToken): Return
Promise<{void=>TokenData}>.
* src/server/token/TokenDao.ts (TokenDao)[#updateToken]: Update interface
accordingly.
* test/server/token/MongoTokenDaoTest.ts: Update test accordingly.
Rather than replying with null, which complicates using the returned promise
efficiently, we'll respond with a unique error that allows us to distinguish
between a database failure and a missing token.
These are more traditional errors, but we're moving toward structured
logging, so I want error objects that provide more context. I'll explore
that a bit more in next commit. Unfortunately, the untypedness of Promise
rejections make for a less than desirable situation here. Async/await is
not yet an option since we're still compiling to ES5 (have to support
IE11), and TS compiles async/await into generators for environments that
don't support them, which also are not available in ES5.
* src/server/service/TokenedService.js (_getQuoteToken): Remove null check,
since this situation can no longer occur.
* src/server/token/MongoTokenDao.ts (getToken): Remove null from return type
union; reject with `UnknownTokenError' instead.
* src/server/token/TokenDao.ts: Modify interface accordingly.
* src/server/token/UnknownTokenError.ts: New class.
* test/server/token/MongoTokenDaoTest.ts: Modify tests accordingly. Add
missing test for latest token namespace missing.
TokenDao has been renamed to MongoTokenDao. While it's good for this to
have its own interface anyway, the immediate motivation was for unit tests:
I started playing with mocking with TypeScript and researching some
libraries, but I don't have time to research enough to commit to any of them
at the moment. Interfaces remove the need to mock at all.
This also stops using `export default' in favor of just importing by
name. Using defaults only saves us a few characters, and it makes for
awkward syntax in various cases (e.g. with multiple exports). But I'm still
new to TS, so who knows if I'll be flip-flopping on this decision in the
future. If we kept to our normal 1:1 file:definition convention, it
wouldn't cause problems, but based on the types I've had to define so far,
that'd cause way too much bloat and boilerplate.
* src/server/daemon/controller.js: No long import `default'. Use
`MongoTokenDao'.
* src/server/token/TokenedService.js: Stop checking type (since TS
interfaces do not result in compiler output, easejs cannot validate
against them.)
* src/server/token/MongoTokenDao.ts: Rename from `TokenDao.ts'.
* src/server/token/TokenDao.ts: Rename from `TokenQueryResult.ts'.
(TokenDao): New interface.
* src/server/token/TokenQueryResult.ts: Rename to `TokenDao.ts'.
* test/server/token/MongoTokenDaoTest.ts: Rename from `TokenDaoTest.ts'.
* src/document/Document.ts: New file.
* src/server/token/TokenDao.ts: quote=>document and use DocumentId.
* test/server/token/TokenDaoTest.ts: Likewise.
This beings an experiment with nominal typing using what the TS community
calls "branding". The lack of nominal types was one of my biggest
disappointments with TS, so this should really help to mitigate bugs
resulting from misappropriation of data.
* src/server/token/Token.ts: New file.
* src/server/token/TokenDao.ts: Use Token{Id,Namespace}.
* src/server/token/TokenQueryResult.ts: Likewise.
* src/types/misc.d.ts: Introduce NominalType and UnixTimestamp.
* test/server/token/TokenDaoTest.ts: Use nominal types.
* src/server/daemon/controller.js (getUnixTimestamp): New method. Not
ideal, but better than where it was.
(_initExportService): Pass to TokenDao constructor.
* src/server/token/TokenDao.ts (_getTimestamp): New field.
(constructor)[get_timestamp]: New param.
(updateToken): Use it.
* test/server/token/TokenDaoTest.ts: Provide stub timestamp function.
Our coding standards are to create a separate file for classes and
interfaces, but this is a bit different: this "interface" is more like a
struct, and it's used to define the return type of a method of this
class. Since it's inherently coupled, I'm keeping it in the same file.
The idea is that the caller will provide its own abstraction rather than
continuing to export this one. Note that Typescript does support
re-exporting symbols if need be.
* src/server/daemon/controller.js: Adjust import of TokenDao (TS compiles
default modules as `default').
* src/server/service/TokenedService.js: Adjust import of TokenDao.
* src/server/token/TokenDao.ts: Export TokenDao as `default'. Export
TokenData.
* test/server/token/TokenDaoTest.ts: Adjust import of TokenDao. Import
TokenData.
This makes minimal changes to TokenedService, even though there is obvious
refactoring that can be done to reduce duplication, because the class is
currently untested.
* src/server/service/TokenedService.js (_getQuoteToken, generateToken,
killToken, acceptToken, completeToken): Expect promise.
* src/server/token/TokenDao.ts (updateToken, getToken): Remove callback
param, return Promise.
* test/server/token/TokenDaoTest.ts: Use promises.
This tests the existing state of TokenDao before additional modifications
are made. This commit also further refines the types introduced in a
previous commit.
This is also the first test written in Typescript.
* package.json.in (devDependencies): Add node, chai, and mocha types.
* src/server/token/TokenDao.ts (updateToken): `data` accepts null (as it
should). Do not conditionall add data to object (it doesn't matter for
later retrieval). Note nondeterminism with date. More concise syntax
for object fields.
* src/server/token/TokenQueryResult.ts: Make all fields readonly.
(TokenStatus): Date is no longer optional (see above mention).
* src/types/mongodb.d.ts: Remove generics (erroneously added).
(Collection)[update]: Remove 3-argument declaration (see comment).
* test/server/token/TokenDaoTest.ts: New test case.