samedi 5 septembre 2020

can anyone translate the following code script into class form (ES6) in javascript?

This code script works well, it's just that I'm not familiar with the form of this writing. Can anyone translate the script into a class (ES6) like in Java / C # / PHP?

const connection = require('../../database.js');

const User = function(user) {
    this.name = user.name;
    this.email = user.email;
    this.password = user.password;
}

User.read = (callback) => {
    connection.query('SELECT * FROM users', (err, res) => {
        if (err) {
            callback(err, null);
            return;
        }

        callback(null, res);
    });
}

User.readById = (id, callback) => {
    connection.query('SELECT * FROM users WHERE id=?', id, (err, res) => {
        if (err) {
            callback(err, null);
            return;
        }

        if (!res.length) {
            callback({kind: true}, null);
            return;
        }

        callback(null, res[0]);
    });
}

User.create = (data, callback) => {
    connection.query('INSERT INTO users SET ?', data, (err, res) => {
        if (err) {
            callback(err, null);
            return;
        }

        callback(null, {id: res.insertId, ...data});
    });
}

thank you all for your help.




Aucun commentaire:

Enregistrer un commentaire