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.dateoryea…


This content originally appeared on DEV Community and was authored by Stillnoturdad

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



This content originally appeared on DEV Community and was authored by Stillnoturdad


Print Share Comment Cite Upload Translate Updates
APA

Stillnoturdad | Sciencx (2024-09-04T01:57:58+00:00) CRUD USING STATIC METHOD. Retrieved from https://www.scien.cx/2024/09/04/crud-using-static-method/

MLA
" » CRUD USING STATIC METHOD." Stillnoturdad | Sciencx - Wednesday September 4, 2024, https://www.scien.cx/2024/09/04/crud-using-static-method/
HARVARD
Stillnoturdad | Sciencx Wednesday September 4, 2024 » CRUD USING STATIC METHOD., viewed ,<https://www.scien.cx/2024/09/04/crud-using-static-method/>
VANCOUVER
Stillnoturdad | Sciencx - » CRUD USING STATIC METHOD. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/04/crud-using-static-method/
CHICAGO
" » CRUD USING STATIC METHOD." Stillnoturdad | Sciencx - Accessed . https://www.scien.cx/2024/09/04/crud-using-static-method/
IEEE
" » CRUD USING STATIC METHOD." Stillnoturdad | Sciencx [Online]. Available: https://www.scien.cx/2024/09/04/crud-using-static-method/. [Accessed: ]
rf:citation
» CRUD USING STATIC METHOD | Stillnoturdad | Sciencx | https://www.scien.cx/2024/09/04/crud-using-static-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.