Other Stuff

knex를 통한 database, back-end 연결

Alledy 2019. 5. 7. 00:05

Database와 Back-end 연결

  • psequel에서 smart-brain 이라는 database를 만듦

  • users 테이블 만들기

    CREATE TABLE users (
        id serial PRIMARY KEY,
        name VARCHAR(100), 
        email text UNIQUE NOT NULL,
        entries BIGINT DEFAULT 0,
        joined TIMESTAMP NOT NULL
    );
    

     

  • login 테이블 만들기

    CREATE TABLE login (
        id serial PRIMARY KEY,
        hash VARCHAR(100) NOT NULL,
        email text UNIQUE NOT NULL
    );
    

     

  • 백엔드와 연결

    • npm install knex & pg(postgresql)
    // import 
    const knex = require('knex')
    
    const postgres = knex({
        client: 'pg',
        connection: {
          host : '127.0.0.1',
          user : 'Hazel',
          password : '',
          database : 'smart-brain'
        }
    });
    
    // knex syntax
    console.log(postgres.select('*').from('users'));