Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add charset to Knex.js connection string?

I am using Knex.js for database connections. I would like to use a connection string instead of the connection object for connecting to a database and be able to specify the charset.

Example:

connection: 'mysql://root:[email protected]:3306/test-db'

NOT

connection: {
    host     : '127.0.0.1',
    user     : 'root',
    password : 'secret',
    database : 'test-db',
    charset  : 'utf8'
}

I have the connection string working.

Could someone explain how I would specify the charset in the Knex.js connection string?

like image 985
Kevin Baker Avatar asked Jul 26 '16 04:07

Kevin Baker


People also ask

What is KNEX NPM?

Knex is a technique that is used to build queries. It supports various databases like Postgres, MySQL, SQLite, Oracle, and some others as well. It provides both callbacks and promise interface. It provides connection pooling and standardized responses. It is an efficient tool for node.

Is KNEX an ORM?

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.

What is a KNEX schema?

The knex. schema is a getter function, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the knex. schema for every query.

How connect Postgres to KNEX?

Connecting Knex with Postgres We specify the connection parameters for Postgres and point Knex to connect to the pg client. const db = require("knex")({ client: "pg", connection: { host: "localhost", user: "postgres", password: "", database: "knex-test" } }); app. set("db", db);


1 Answers

Try:

connection: 'mysql://root:[email protected]:3306/test-db?charset=utf8mb4'

See connection string options as described here: https://github.com/mysqljs/mysql#connection-options

like image 91
ΔO 'delta zero' Avatar answered Sep 21 '22 10:09

ΔO 'delta zero'