From d1a7b0de149c8bb89a4743d9f984cf74b95c3309 Mon Sep 17 00:00:00 2001 From: Benjamin Reed Date: Fri, 16 Jun 2017 16:40:10 -0400 Subject: [PATCH] feat(dao): add date/number parsers for DAOs --- src/dao/AbstractDAO.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/dao/AbstractDAO.ts b/src/dao/AbstractDAO.ts index 6b205f32e..a3c02b8c0 100644 --- a/src/dao/AbstractDAO.ts +++ b/src/dao/AbstractDAO.ts @@ -8,6 +8,10 @@ import {OnmsHTTPOptions} from '../api/OnmsHTTPOptions'; import {log, catDao} from '../api/Log'; import {Category} from 'typescript-logging'; +/** @hidden */ +// tslint:disable-next-line +const moment = require('moment'); + /** * Abstract data access layer * @module AbstractDAO @@ -31,10 +35,10 @@ export abstract class AbstractDAO { public abstract fromData(data: any): T; /** get a model object given an ID */ - public abstract get(id: K): Promise; + public abstract async get(id: K): Promise; /** find all model objects given a filter */ - public abstract find(filter?: Filter): Promise; + public abstract async find(filter?: Filter): Promise; /** extract the count or totalCount values from response data */ protected getCount(data: any) { @@ -59,4 +63,18 @@ export abstract class AbstractDAO { } return ret; } + + /** convert the given value to a date, or undefined if it cannot be converted */ + protected toDate(from: any) { + if (from === undefined || from === null || from === '') { + return undefined; + } + return moment(from); + } + + /** convert the given value to a number, or undefined if it cannot be converted */ + protected toNumber(from: any) { + const ret = parseInt(from, 10); + return isNaN(ret) ? undefined : ret; + } }