首頁 > web前端 > js教程 > 主體

使用靜態方法進行 CRUD

WBOY
發布: 2024-09-04 11:00:06
原創
813 人瀏覽過

CRUD USING STATIC METHOD

Read data from databases

static async getAllProdHouse() {
    try {
      const index = `
      SELECT cn.*, ph."name_fromnameTable"
      FROM "ClassName2" cn
      JOIN "ClassName1" cI
      ON cn."ClassName1Id" = cI.id
      ORDER BY cn.dateoryear DESC/ASC
    `
      const {rows} = await pool.query(index);
      const object = Factory.bulkname(rows);

      return object;
    } catch (error) {
      throw error;
    }
  }
}
登入後複製

Create Data

static async createThings(name, Id) {
  try {
    let validation = this.validation(name);
    if (validation.length > 0) {
      throw validation;
    }
    const query = `
      INSERT INTO "ClassName2" ("name")
      VALUES ($1,) `;
    await pool.query(query, [name]);
  } catch (error) {
    throw error;
登入後複製

Delete Data

static async deleteTable(id) {
  try {
    const findCn = await this.findCnById(id);

    if (findCn.length === 0) {
      throw new Error("data cn not found");
    }

    const query = 'delete from "cn" where id = $1';

    await pool.query(query, [id]);
  } catch (error) {
    throw error;
  }
}

登入後複製

Find Data

static async findCnById(id) {
  try {
    const index = `
      SELECT cn.*, ph."name_fromnameTable"
      FROM "ClassName2" cn
      JOIN "ClassName1" cI
      ON cn."ClassName1Id" = cI.id
      WHERE cn.id = $1
    `;

    const { rows } = await pool.query(query, [id]);
    const instance = Factory.bulkCn(rows);

    return instance;
  } catch (error) {
    throw error;
  }
}
登入後複製

Update Data

static async updateCn(id, name) {
  try {
    let validation = this.validation(name, date);

    if (validation.length > 0) {
      throw validation;
    }

    const query = `
      UPDATE "Cn"
      SET "name" = $1,
          "date" = $2,

      WHERE "id" = $5
    `;

    await pool.query(query, [name, date id]);
  } catch (error) {
    throw error;
  }
}
登入後複製

Validation Data

static validation(name, date) {
  let errors = [];

  if (!name) {
    errors[0] = "name empty";
  }

  return errors;
}
}

Module.export = Model


登入後複製

以上是使用靜態方法進行 CRUD的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!