gothinkster / koa-knex-realworld-example

240 stars 79 forks source link

Does not work out of the box - sqlite3 error #3

Closed longsoft-dev closed 7 years ago

longsoft-dev commented 7 years ago

The code doesn't work out of box if you are using default development environment with sqlite3.

copy .example-env .env
npm start

throws the following error: TypeError: Argument 0 must be a string at F:\work\exp\koa\koa-knex-realworld-example\node_modules\knex\lib\dialects\sqlite3\index.js:99:16 at Client_SQLite3.acquireRawConnection (F:\work\exp\koa\koa-knex-realworld-example\node_modules\knex\lib\dialects\sqlite3\index.js:98:12) at Object.create (F:\work\exp\koa\koa-knex-realworld-example\node_modules\knex\lib\client.js:239:16) at Pool._createResource (F:\work\exp\koa\koa-knex-realworld-example\node_modules\generic-pool\lib\generic-pool.js:354:17) at Pool.dispense [as _dispense] (F:\work\exp\koa\koa-knex-realworld-example\node_modules\generic-pool\lib\generic-pool.js:314:10) at Pool.acquire (F:\work\exp\koa\koa-knex-realworld-example\node_modules\generic-pool\lib\generic-pool.js:436:8) at F:\work\exp\koa\koa-knex-realworld-example\node_modules\knex\lib\client.js:289:19

Problem lies in the config/knexfile.js line 9: connection: DB_CONNECTION || path.join(ROOT, 'data/dev.sqlite3'), It should use filename for sqlite3 configuration: connection: DB_CONNECTION || { filename: path.join(ROOT, 'data/dev.sqlite3') },

Ref: Knex Initializing the Library

dimonnwc3 commented 7 years ago

@beatgates thanks for your feedback.

I can't reproduce it, works fine.

I looked into knex source code as well, to make sure that connection string works well, and it parses filename automatically: https://github.com/tgriesser/knex/blob/master/src/util/parse-connection.js#L15

Can you tell more information about you env | platform?

edwardkcyu commented 6 years ago

I can reproduce the error in Window but not in Mac, not quite sure about the reason. Generally the solution from @beatgates, can work for both cases.

With reference to the official doc, the correct initialization config for sqlite3 should contain the filename property.

var knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: "./mydb.sqlite"
  }
});

compared to the PostgreSQL, only connection string is needed

var pg = require('knex')({
  client: 'pg',
  connection: process.env.PG_CONNECTION_STRING,
  searchPath: ['knex', 'public'],
});

So I made a PR(#6) for this to solve the problem.