MySQL with Node.js

后端 未结 9 787
Happy的楠姐
Happy的楠姐 2020-11-22 11:52

I\'ve just started getting into Node.js. I come from a PHP background, so I\'m fairly used to using MySQL for all my database needs.

How can I use MySQL with Node.js

9条回答
  •  鱼传尺愫
    2020-11-22 12:14

    Imo, you should try MySQL Connector/Node.js which is the official Node.js driver for MySQL. See ref-1 and ref-2 for detailed explanation. I have tried mysqljs/mysql which is available here, but I don't find detailed documentation on classes, methods, properties of this library.

    So I switched to the standard MySQL Connector/Node.js with X DevAPI, since it is an asynchronous Promise-based client library and provides good documentation. Take a look at the following code snippet :

    const mysqlx = require('@mysql/xdevapi');
    const rows = [];
    
    mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        const table = session.getSchema('testSchema').getTable('testTable');
    
        // The criteria is defined through the expression.
        return table.update().where('name = "bar"').set('age', 50)
            .execute()
            .then(() => {
                return table.select().orderBy('name ASC')
                    .execute(row => rows.push(row));
            });
    })
    .then(() => {
        console.log(rows);
    });
    

提交回复
热议问题