javascript - Can knex.js be used without migrations/seeding? Is there something wrong with this knex.js code? -
the cannonical way of using knex.js, seems, create migrations define schema, , insert said schema in normal node.js code.
is there way not use migrations? can use knex.schema.createtable(...)
function normal node.js code? there documentation on sort of thing?
edit: wrote question because thought couldn't execute knex.schema.createtable(...)
functions within normal code base. seems can't use knex @ within code. if use migrations generate schema sqlite3 database, works, never seem able insert or query data, within migrations or otherwise.
my migrations file:
exports.up = function(knex, promise) { return promise.all([ knex.schema.createtableifnotexists('test', function(table){ console.log("creating user table"); table.increments('id'); table.text('test'); }) ]); }; exports.down = function(knex, promise) { return promise.all([ knex.schema.droptableifexists('test') ]); };
knexfile.js:
module.exports = { development: { client: "sqlite3", connection: { filename: "devel.db" }, usenullasdefault: true }, deployment: { client: "sqlite3", connection: { filename: "deploy.db" }, usenullasdefault: true } };
and mocha/chai test wrote:
const chai = require("chai"); var knex = require("knex")({ client: "sqlite3", connection: { filename: "devel.db" }, usenullasdefault: true }); var expect = chai.expect; // tests. describe("db", ()=> { it("simple connect, query, , destroy.", ()=> { knex('test').insert({ test: 'wow' }) .catch(function(e){ console.error(e); }); }); });
your migration works correctly. in test need return knex object executed.
describe("db", ()=> { it("simple connect, query, , destroy.", ()=> { return knex('test').insert({ test: 'wow' }) .catch(function(e){ console.error(e); }); }); });
in answer actual question, yes can. code similar migration code then() or catch() need called execute it. knex documentation sort of covers it's not clear. here's example of modifying schema outside migration file:
knex.schema.table('test', function (table) { table.string('foo'); table.string('baa'); }) .catch(function(err) { console.log(err); });
i can't think of many times when modifying database schemas without migrations work better depends on application.
Comments
Post a Comment