PostgreSQL -配列データ型とクイズAPIの追加方法


今まであなたのアプリケーション内の配列データ型を追加するときにクイズアプリをしようとすると混乱していたしようとしたことがありますか?どのようにPostgreSQLでそれを行うには、ボーナスとしてどのようにNODEJSを使用して追加するには、郵便配達人でそれをテストする方法を示しますが、私は両方の技術の基本的な知識を持っていると仮定します.主要な段落では、それを実装する方法を参照してください.
要件
  • Nodejsインストール
  • PostgreSQLのインストール
  • 郵便配達人は、
  • をインストールしました
    何をカバー
  • Postgres
  • の配列データ型
    NODEJS を使用しているテーブルの
  • ポストデータ
  • 郵便配達人
  • を使ってそれをテストしてください
    フォローするステップ
    * NPMインストール
    npm i express pg cors
    
    ファイル
    データベース.SQLファイル
    CREATE TABLE test(
      test_id uuid DEFAULT uuid_generate_v4(),
      test_name VARCHAR(255) NOT NULL,
      test_question VARCHAR(255) NOT NULL,
      test_choice varchar[4] NOT NULL,  --this is important you can choose  it to be text number any thing you want but always specify the limit of your array
      test_answer  VARCHAR(255) NOT NULL,
      teacher_email VARCHAR(255) NOT NULL,
      class_year_content VARCHAR(255) NOT NULL,
      timestamp timestamp default current_timestamp,
      PRIMARY KEY(test_id)
    );
    
    PostgreSQLのデータ型Postgreresで配列を宣言する方法
    CREATE TABLE sal_emp (
        name            text,
        pay_by_quarter  integer[],
        schedule        text[][],
       summary        varchar[]
    );
    
    
    Nodejsパート2
    npm init -y 
    
    データベース.親フォルダのJSファイルとこれを含める必要があります
    const Pool = require("pg").Pool;
    
    const pool = new Pool({
      user: "postgres",
      password: "your password",
      host: "localhost",
      port: 5432,
      database: "your database"
    });
    
    module.exports = pool;
    
    インデックス.親フォルダのJSファイルとこれを含める必要があります
    const express = require("express");
    const app = express();
    const cors = require("cors");
    const pool = require("./db");
    const PORT = process.env.PORT || 5000;
    //middleware
    app.use(cors());
    app.use(express.json()); //req.body
    
    //ROUTES//
    
    app.post('/test', async (req, res) => {
      try {
        const {name, question, answers, email, classe} = req.body;
        const newTodo = await pool.query(
          'INSERT INTO test (test_name, test_question, test_choice, test_answer, teacher_email, class_year_content) VALUES ($1, $2, $3,$4,$5, $6) RETURNING *',
          [name, certificate, question, req.body.choices, answers, email, classe],
        );
    
        res.json(newTodo.rows[0]);
      } catch (err) {
        console.error(err.message);
      }
    });
    
    
    app.get('/test', async (req, res) => {
      try {
        const user = await pool.query('SELECT * from test');
    
        res.json(user.rows);
      } catch (err) {
        console.error(err.message);
        res.status(500).send('Server error');
      }
    });
    
    app.listen(PORT, () => {
      console.log(`server started ${PORT}`);
    });
    
    必ず実行する
     npm start
    
    郵便配達人

    をクリック
    結論として、これは非常に重要なので、私たちは将来的にあなたがどんなエラーに直面している場合は、コメントで私を教えてください、私はあなたを助けるために刺激される多くのことを学びました.ありがとうございました、そして、私はそれがあなたを助けたことを望みます