TokenDao: Typescript conversion
This is the first test of integrating typescript. I'm using the `export =' syntax to keep imports the same using `require', but note that the `new' keyword is now required for instantiation (whereas it was not with easejs).master
parent
bacefb0752
commit
5a1b84c271
1
dummy.ts
1
dummy.ts
|
@ -1 +0,0 @@
|
||||||
/** temporary file to prevent ts build from failing beacuse it can't find files **/
|
|
|
@ -241,7 +241,7 @@ function _initExportService( db, callback )
|
||||||
ExportService
|
ExportService
|
||||||
.use( TokenedService(
|
.use( TokenedService(
|
||||||
'c1import',
|
'c1import',
|
||||||
TokenDao( collection ),
|
new TokenDao( collection ),
|
||||||
function tokgen()
|
function tokgen()
|
||||||
{
|
{
|
||||||
var shasum = crypto.createHash( 'sha1' );
|
var shasum = crypto.createHash( 'sha1' );
|
||||||
|
|
|
@ -19,7 +19,20 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Class = require( 'easejs' ).Class;
|
/**
|
||||||
|
* Token status types
|
||||||
|
*/
|
||||||
|
type TokenType = 'ACTIVE' | 'DONE' | 'ACCEPTED' | 'DEAD';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token information
|
||||||
|
*/
|
||||||
|
interface TokenData
|
||||||
|
{
|
||||||
|
id: string,
|
||||||
|
status: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,23 +40,23 @@ var Class = require( 'easejs' ).Class;
|
||||||
*
|
*
|
||||||
* Note that this is tightly coupled with MongoDB.
|
* Note that this is tightly coupled with MongoDB.
|
||||||
*/
|
*/
|
||||||
module.exports = Class( 'TokenDao',
|
export = class TokenDao
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @type {MongoCollection} mongo database collection
|
* Mongo database collection
|
||||||
*/
|
*/
|
||||||
'private _collection': null,
|
private _collection?: MongoCollection;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize connection
|
* Initialize connection
|
||||||
*
|
*
|
||||||
* @param {MongoCollection} collection token Mongo collection
|
* @param token Mongo collection
|
||||||
*/
|
*/
|
||||||
'public __construct': function( collection )
|
constructor( collection: MongoCollection )
|
||||||
{
|
{
|
||||||
this._collection = collection;
|
this._collection = collection;
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,24 +65,30 @@ module.exports = Class( 'TokenDao',
|
||||||
* The token entry is entered in the token log, and then the current
|
* The token entry is entered in the token log, and then the current
|
||||||
* entry is updated to reflect the changes. The operation is atomic.
|
* entry is updated to reflect the changes. The operation is atomic.
|
||||||
*
|
*
|
||||||
* @param {number} quote_id unique quote identifier
|
* @param quote_id unique quote identifier
|
||||||
* @param {string} ns token namespace
|
* @param ns token namespace
|
||||||
* @param {string} token token value
|
* @param token token value
|
||||||
* @param {string} data token data, if any
|
* @param data token data, if any
|
||||||
* @param {string} status arbitrary token type
|
* @param status arbitrary token type
|
||||||
|
* @param callback with error or null (success)
|
||||||
*
|
*
|
||||||
* @param {function(*)} callback with error or null (success)
|
* @return self
|
||||||
*
|
|
||||||
* @return {TokenDao} self
|
|
||||||
*/
|
*/
|
||||||
'public updateToken': function( quote_id, ns, token, type, data, callback )
|
updateToken(
|
||||||
|
quote_id: number,
|
||||||
|
ns: string,
|
||||||
|
token: string,
|
||||||
|
type: TokenType,
|
||||||
|
data: string,
|
||||||
|
callback: ( err: Error|null ) => void,
|
||||||
|
): this
|
||||||
{
|
{
|
||||||
var token_data = {},
|
const token_data: any = {};
|
||||||
token_log = {},
|
const token_log: any = {};
|
||||||
root = this._genRoot( ns ) + '.',
|
const root = this._genRoot( ns ) + '.';
|
||||||
current_ts = Math.floor( ( new Date() ).getTime() / 1000 );
|
const current_ts = Math.floor( ( new Date() ).getTime() / 1000 );
|
||||||
|
|
||||||
var token_entry = {
|
const token_entry: any = {
|
||||||
type: type,
|
type: type,
|
||||||
timestamp: current_ts,
|
timestamp: current_ts,
|
||||||
};
|
};
|
||||||
|
@ -93,14 +112,14 @@ module.exports = Class( 'TokenDao',
|
||||||
},
|
},
|
||||||
{ upsert: true },
|
{ upsert: true },
|
||||||
|
|
||||||
function ( err, docs )
|
function ( err: Error|null )
|
||||||
{
|
{
|
||||||
callback( err || null );
|
callback( err || null );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -110,20 +129,22 @@ module.exports = Class( 'TokenDao',
|
||||||
* If a TOKEN_ID is provided, only that token will be queried; otherwise,
|
* If a TOKEN_ID is provided, only that token will be queried; otherwise,
|
||||||
* the most recently created token will be the subject of the query.
|
* the most recently created token will be the subject of the query.
|
||||||
*
|
*
|
||||||
* @param {number} quote_id quote identifier
|
* @param quote_id quote identifier
|
||||||
* @param {string} ns token namespace
|
* @param ns token namespace
|
||||||
* @param {string} token_id token identifier (unique to NS)
|
* @param token_id token identifier (unique to NS)
|
||||||
|
* @param callback
|
||||||
*
|
*
|
||||||
* @param {function(?Error,{{id: string, status: string}})} callback
|
* @return self
|
||||||
*
|
|
||||||
* @return {TokenDao} self
|
|
||||||
*/
|
*/
|
||||||
'public getToken': function( quote_id, ns, token_id, callback )
|
getToken(
|
||||||
|
quote_id: number,
|
||||||
|
ns: string,
|
||||||
|
token_id: string,
|
||||||
|
callback: ( err: Error|null, data: TokenData|null ) => void,
|
||||||
|
)
|
||||||
{
|
{
|
||||||
var _self = this;
|
const root = this._genRoot( ns ) + '.';
|
||||||
|
const fields: any = {};
|
||||||
var root = this._genRoot( ns ) + '.',
|
|
||||||
fields = {};
|
|
||||||
|
|
||||||
fields[ root + 'last' ] = 1;
|
fields[ root + 'last' ] = 1;
|
||||||
fields[ root + 'lastStatus' ] = 1;
|
fields[ root + 'lastStatus' ] = 1;
|
||||||
|
@ -137,7 +158,7 @@ module.exports = Class( 'TokenDao',
|
||||||
this._collection.findOne(
|
this._collection.findOne(
|
||||||
{ id: +quote_id },
|
{ id: +quote_id },
|
||||||
{ fields: fields },
|
{ fields: fields },
|
||||||
function( err, data )
|
( err: Error|null, data: any ) =>
|
||||||
{
|
{
|
||||||
if ( err )
|
if ( err )
|
||||||
{
|
{
|
||||||
|
@ -151,31 +172,32 @@ module.exports = Class( 'TokenDao',
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var exports = data.exports || {},
|
const exports = data.exports || {};
|
||||||
ns_data = exports[ ns ] || {};
|
const ns_data = exports[ ns ] || {};
|
||||||
|
|
||||||
callback(
|
callback(
|
||||||
null,
|
null,
|
||||||
( token_id )
|
( token_id )
|
||||||
? _self._getRequestedToken( token_id, ns_data )
|
? this._getRequestedToken( token_id, ns_data )
|
||||||
: _self._getLatestToken( ns_data )
|
: this._getLatestToken( ns_data )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve latest token data, or `null` if none
|
* Retrieve latest token data, or `null` if none
|
||||||
*
|
*
|
||||||
* @param {{last: string, lastStatus: string}} ns_data namespace data
|
* @param ns_data namespace data
|
||||||
*
|
*
|
||||||
* @return {?{{id: string, status: string}}} data of latest token in
|
* @return data of latest token in namespace
|
||||||
* namespace
|
|
||||||
*/
|
*/
|
||||||
'private _getLatestToken': function( ns_data )
|
private _getLatestToken(
|
||||||
|
ns_data: {last: string, lastStatus: string}
|
||||||
|
): TokenData|null
|
||||||
{
|
{
|
||||||
var last = ns_data.last;
|
var last = ns_data.last;
|
||||||
|
|
||||||
|
@ -188,22 +210,20 @@ module.exports = Class( 'TokenDao',
|
||||||
id: last,
|
id: last,
|
||||||
status: ns_data.lastStatus,
|
status: ns_data.lastStatus,
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve latest token data, or `null` if none
|
* Retrieve latest token data, or `null` if none
|
||||||
*
|
*
|
||||||
* @param {string} token_id token identifier for namespace associated
|
* @param token_id token identifier for namespace associated with NS_DATA
|
||||||
* with NS_DATA
|
* @param ns_data namespace data
|
||||||
*
|
*
|
||||||
* @param {{last: string, lastStatus: string}} ns_data namespace data
|
* @return data of requested token
|
||||||
*
|
|
||||||
* @return {?{{id: string, status: string}}} data of requested token
|
|
||||||
*/
|
*/
|
||||||
'private _getRequestedToken': function( token_id, ns_data )
|
private _getRequestedToken( token_id: string, ns_data: any ): TokenData|null
|
||||||
{
|
{
|
||||||
var reqtok = ns_data[ token_id ];
|
const reqtok = ns_data[ token_id ];
|
||||||
|
|
||||||
if ( !reqtok )
|
if ( !reqtok )
|
||||||
{
|
{
|
||||||
|
@ -214,20 +234,20 @@ module.exports = Class( 'TokenDao',
|
||||||
id: token_id,
|
id: token_id,
|
||||||
status: reqtok.status,
|
status: reqtok.status,
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine token root for the given namespace
|
* Determine token root for the given namespace
|
||||||
*
|
*
|
||||||
* @param {string} ns token namespace
|
* @param ns token namespace
|
||||||
*
|
*
|
||||||
* @return {string} token root for namespace NS
|
* @return token root for namespace NS
|
||||||
*/
|
*/
|
||||||
'private _genRoot': function( ns )
|
private _genRoot( ns: string ): string
|
||||||
{
|
{
|
||||||
// XXX: injectable
|
// XXX: injectable
|
||||||
return 'exports.' + ns;
|
return 'exports.' + ns;
|
||||||
},
|
}
|
||||||
} );
|
};
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
/**
|
||||||
|
* Type definitions for mongodb library
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010-2019 R-T Specialty, LLC.
|
||||||
|
*
|
||||||
|
* This file is part of the Liza Data Collection Framework.
|
||||||
|
*
|
||||||
|
* liza is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare module "mongodb";
|
||||||
|
|
||||||
|
/** TODO **/
|
||||||
|
type MongoCollection = any;
|
Loading…
Reference in New Issue