Home > Web Front-end > JS Tutorial > body text

CRUD USING STATIC METHOD

WBOY
Release: 2024-09-04 11:00:06
Original
813 people have browsed it

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;
    }
  }
}
Copy after login

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;
Copy after login

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;
  }
}

Copy after login

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;
  }
}
Copy after login

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;
  }
}
Copy after login

Validation Data

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

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

  return errors;
}
}

Module.export = Model


Copy after login

The above is the detailed content of CRUD USING STATIC METHOD. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!