Skip to main content

Command Palette

Search for a command to run...

Knex.js

Updated
•0 min read•View as Markdown

http://knexjs.org/

Knex.js is a "batteries included" SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use. It features both traditional node style callbacks as well as a promise interface for cleaner async flow control, a stream interface, full featured query and schema builders, transaction support (with savepoints), connection pooling and standardized responses between different query clients and dialects.

Node.js The primary target environment for Knex is Node.js, you will need to install the knex library, and then install the appropriate database library: pg for PostgreSQL and Amazon Redshift, mysql for MySQL or MariaDB, sqlite3 for SQLite3, or mssql for MSSQL.

$ npm install knex --save

$ npm install pg

psql console:

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

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

checking below change.

app.post('/register',(req,res) => {
    const {email,name,password} =req.body;
    database.users.push({
        id: '125',
        name: name,
        email: email,
        password: password,
        entries: 0,
        joined: new Date()        
    })
    res.json(database.users[database.users.length-1])
})

app.post('/register',(req,res) => {
    const {email,name,password} =req.body;
**    db('users').insert({
        email: email,
        name: name,
        joined: new Date()
    }).then(console.log)**
    res.json(database.users[database.users.length-1])
})
app.get('/profile/:id',(req,res) => {
    const {id} = req.params;
    let found = false;
    database.users.forEach(user => {
        if (user.id === id) {
            found = true;
            return res.json(user);
        }
    })
    if (!found) {
        res.status(400).json('not found');
    }
})

app.get('/profile/:id',(req,res) => {
    const {id} = req.params;
    db.select('*').from('users').where({id})
    .then(user => {
        if (user.length) {
            res.json(user[0])
        } else {
            res.status(400).json('not found')
        }
    })
    .catch(err => res.status(400).json('error getting user'))
})
app.put('/image',(req,res) => {
    const {id} = req.body;
    let found = false;
    database.users.forEach(user => {
        if (user.id === id) {
            found = true;
            user.entries++
            return res.json(user.entries);
        }
    })
    if (!found) {
        res.status(400).json('not found');
    }
})

app.put('/image',(req,res) => {
    const {id} = req.body;
    db('users').where('id','=',id)
    .increment('entries',1)
    .returning('entries')
    .then(entries=> {
        res.json(entries[0]);
    })
    .catch(err=> res.status(400).json('unable to get entries'))
})
app.post('/register',(req,res) => {
    const {email,name,password} =req.body;
    db('users')
        .returning('*')
        .insert({
            email: email,
            name: name,
            joined: new Date()
        }).then(user => {
            res.json(user[0]);
        })
        .catch(err => res.status(400).json('unable to register'))
})
app.post('/signin',(req,res) => {
    if (req.body.email === database.users[0].email &&
        req.body.password === database.users[0].password) {
        res.json(database.users[0]);
    } else {
        res.status(400).json('error logging in');
    }
})

app.post('/signin',(req,res) => {
    db.select('email','hash').from('login')
    .where('email','=', req.body.email)
    .then(data => {
        const isValid = bcrypt.compareSync(req.body.password,data[0].hash);
        if(isValid) {
            return db.select('*').from('users')
            .where('email','=',req.body.email)
            .then(user=> {
                console.log(user);
                res.json(user[0])
            })
            .catch(err=> res.status(400).json('unable to get user'))
        }
    })
    .catch(err => res.status(400).json('wrong credentials'))
})
88 views
C

A chosen blog is marked for entitlement for all people. The reforms of the https://www.pwinsider.com/article/146944/top-benefits-of-playing-sports.html?p=14 are marked for the changes. The ornament is found at the top of the CHU blog for the factor and element for the fundamentals for all individuals.