Skip to content

Commit

Permalink
feat(dao): add date/number parsers for DAOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jun 16, 2017
1 parent b6a072b commit d1a7b0d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/dao/AbstractDAO.ts
Expand Up @@ -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
Expand All @@ -31,10 +35,10 @@ export abstract class AbstractDAO<K, T> {
public abstract fromData(data: any): T;

/** get a model object given an ID */
public abstract get(id: K): Promise<T>;
public abstract async get(id: K): Promise<T>;

/** find all model objects given a filter */
public abstract find(filter?: Filter): Promise<T[]>;
public abstract async find(filter?: Filter): Promise<T[]>;

/** extract the count or totalCount values from response data */
protected getCount(data: any) {
Expand All @@ -59,4 +63,18 @@ export abstract class AbstractDAO<K, T> {
}
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;
}
}

0 comments on commit d1a7b0d

Please sign in to comment.