db: refactored factory code for reuse