【问题标题】:trying to add jsdocs to dynamically generated functions试图将 jsdocs 添加到动态生成的函数中
【发布时间】:2022-08-24 18:24:28
【问题描述】:

我有一个类可以生成给定表的简单 CRUD 函数。当我初始化一次时,我的 jsdoc 文档按我想要的方式工作(主要用于自动填充目的)。即const providerService = new CrudFunctions(\'provider\')。但是我将来可能会添加更多表,并且我希望在循环中生成所有表,这样我就不必重复一堆代码。到目前为止,这是我生成所有函数的内容:

const generateTableFunctions = (tableArray) => {
  const tableFunctions = {};

  tableArray.forEach((table) => {
    tableFunctions[table] = new CrudFunctions(table);
  });
  return tableFunctions;
};

const foo = generateTableFunctions([
  \'service\',
  \'payment\',
  \'certification\',
  \'provider\',
  \'provider_certifcation\',
  \'provider_payment\',
  \'provider_service\'
]);

我真正想要的是让 \"foo.\" 建议一个表名,但如果没有别的,我真的希望 \"foo.provider.\" 建议 getAll, getOne, and add。我尝试将生成器函数设为 jsdoc 模板,我尝试使用 typedef 但无论如何我都没有得到任何关于 foo 的建议。这是课程:

/** Class representing crud functions of a given table */
class CrudFunctions {
  /**
   * @param {string} table - the name of the table from the database
   */
  constructor(table) {
    this.table = table;
  }
  /**
   * get all items from the table
   *
   * @returns {Object} all rows from table
   */
  async getAll() {
    const { rows } = await handleQuery(`SELECT * FROM ${this.table}`);
    return rows;
  }

  /**
   * gets a single item from the table
   *
   * @param {number} id - the unique id of the item we\'re looking up
   * @returns {Object} the item from the table
   */
  async getOne(id) {
    const { rows } = await handleQuery(
      `SELECT * FROM ${this.table} WHERE ID=${id}`
    );
    return rows;
  }

  /**
   *
   * @param {object} item - an item to be added to the table. all keys should be valid in the database already
   * @returns confirmation that the item got added
   */
  async add(item) {
    const res = await handleQuery(
      buildQuery(`INSERT INTO ${this.table}`, Object.keys(item)),
      Object.values(item)
    );
    return res;
  }
}

    标签: javascript intellisense jsdoc jsdoc3


    【解决方案1】:

    以下方法可以解决问题:

    /**
     * @template {string} T
     * @param {T[]} tableArray
     */
    const generateTableFunctions = (tableArray) => {
      const tableFunctions = /** @type {Record<T, CrudFunctions>} */({});
    
      tableArray.forEach((table) => {
        tableFunctions[table] = new CrudFunctions(table);
      });
      return tableFunctions;
    };
    
    
    const foo = generateTableFunctions([
      'service',
      'payment',
      'certification',
      'provider',
      'provider_certifcation',
      'provider_payment',
      'provider_service'
    ]);
    

    【讨论】:

      猜你喜欢
      • 2014-05-02
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多