Source: libdb.js

/*
* (c) Copyright, Real-Time Innovations, 2012.  All rights reserved.
* RTI grants Licensee a license to use, modify, compile, and create derivative
* works of the software solely for use with RTI Connext DDS. Licensee may
* redistribute copies of the software provided that all such copies are subject
* to this license. The software is provided "as is", with no warranty of any
* type, including any warranty for fitness for any purpose. RTI is under no
* obligation to maintain or support the software. RTI shall not be liable for
* any incidental or consequential damages arising out of the use or inability
* to use the software.
*/

const libDynamoDB = require('./libdynamodb.js')


/**
 * Represents a library for interacting with a database.
 * It contains methods for adding, getting, deleting, and querying types from the database.
 * It also contains methods for getting domains, groups, modules, and purposes from the database.
 * @class
 */
class libdbapi {

  /**
   * In this implementation, the library uses DynamoDB as the database.
   * @constructor
   */
  constructor (settings) {
    this.DbApi = new libDynamoDB.TypeRepoDbApi(settings)
  }
  
 
  /**
   * Adds a type to the database.
   *
   * @param {string} path - The path of the type.
   * @param {string} typename - The name of the type.
   * @param {string} description - The description of the type.
   * @param {string} documentation - The documentation of the type.
   * @param {string} purpose - The purpose of the type.
   * @param {string} version - The version of the type.
   * @param {string} previous_type - The previous type of the type.
   * @param {string} topic - The topic of the type.
   * @param {string} domain - The domain of the type.
   * @param {string} group - The group of the type.
   * @param {string} contact - The contact of the type.
   * @returns {Promise} A promise that resolves when the type is added to the database.
   */
  add(path, typename, description, documentation, purpose, version, previous_type, topic, domain, group, contact) {
    //TODO (CACHE): CACHE BEFORE ADDING TO DB
    return this.DbApi.addTypeToDB(
        path,
        typename,
        description,
        documentation,
        purpose,
        version,
        previous_type,
        topic,
        domain,
        group,
        contact)
  }
  

  /**
   * Retrieves a type from the database based on the given typeid.
   * 
   * @param {string} typeid - The identifier of the type to retrieve.
   * @returns {Promise<Type>} A promise that resolves to the retrieved type.
   */
  get(typeid) {
    //TODO (CACHE): CHECK CACHE BEFORE GETING FROM DB
    return this.DbApi.getTypeFromDB(typeid)
    //TODO (CACHE): IF NOT IN CACHE, ADD TO CACHE
  }
  
  /**
   * Returns the description of the table.
   * @returns {Promise<Object>} A promise that resolves to the description of the table.
   */
  describe() {
    return this.DbApi.describeTable()
  }
  

  /**
   * Deletes a type from the database.
   * 
   * @param {string} typeid - The ID of the type to delete.
   * @returns {Promise} A promise that resolves when the type is successfully deleted.
   */
  delete(typeid) {
    //TODO (CACHE): DELETE FROM CACHE BEFORE DELETING FROM DB
    return this.DbApi.deleteTypeFromDB(typeid)
  }
  
  /**
   * Queries the database for types based on the specified parameters.
   * 
   * @param {string} domain - The domain of the types.
   * @param {string} group - The group of the types.
   * @param {string[]} modules - The modules of the types.
   * @param {string} topic - The topic of the types.
   * @param {string} type - The type of the types.
   * @param {string} contains - The string that the types should contain.
   * @param {Date} begin - The start date for the types.
   * @param {Date} end - The end date for the types.
   * @returns {Promise<any>} A promise that resolves with the query results.
   */
  query(domain, group, modules, topic, type, contains, begin, end) {
    //TODO (CACHE): QUERY CACHE FIRST
      return this.DbApi.queryTypesFromDB(
        domain,
        group,
        modules,
        topic,
        type,
        contains,
        begin,
        end)
    //TODO (CACHE): CACHE QUERY RESULTS
  }
  
  /**
   * Queries the database with the last index.
   *
   * @param {string} domain - The domain to query.
   * @param {string} group - The group to query.
   * @param {string[]} modules - The modules to query.
   * @param {string} topic - The topic to query.
   * @param {string} type - The type to query.
   * @param {string} contains - The string to search for.
   * @param {string} last_typeid - The last type ID.
   * @param {number} limit - The maximum number of results to return.
   * @returns {Promise} A promise that resolves with the query results.
   */
  queryWithLastIndex(domain, group, modules, topic, type, contains, last_typeid, limit) {
    //TODO (CACHE): QUERY CACHE FIRST
      return this.DbApi.queryTypesFromDBWithLastIndex(
        domain,
        group,
        modules,
        topic,
        type,
        contains,
        last_typeid,
        limit)
    //TODO (CACHE): CACHE QUERY RESULTS
  }
  
  /**
   * Retrieves the previous type from the database based on the given typeid.
   *
   * @param {string} typeid - The ID of the type.
   * @returns {Object} - The previous type object.
   */
  previous(typeid) {
    return this.DbApi.getTypeFromDB(typeid)
  }
  

  /**
   * Retrieves the domains from the database.
   * @returns {Array} An array of domains.
   */
  getDomains() {
    return this.DbApi.getDomainsFromDB()
  }
  

  /**
   * Retrieves the groups from the database.
   * @returns {Array} An array of groups.
   */
  getGroups() {
    return this.DbApi.getGroupsFromDB()
  }
  
  /**
   * Retrieves the modules from the database.
   * @returns {Array} An array of modules.
   */
  getModules() {
    return this.DbApi.getModulesFromDB()
  }
  
  /**
   * Retrieves the purposes from the database.
   * @returns {Array} An array of purposes.
   */
  getPurpose() {
    return this.DbApi.getPurposesFromDB()
  }
}

module.exports.libdbapi = libdbapi