Skip to content

Instantly share code, notes, and snippets.

@dunstontc
Last active June 4, 2017 19:28
Show Gist options
  • Save dunstontc/36ef8fda84ad6a428597c8e8272af431 to your computer and use it in GitHub Desktop.
Save dunstontc/36ef8fda84ad6a428597c8e8272af431 to your computer and use it in GitHub Desktop.
SQLite w/ Knex
exports.seed = function(knex, Promise)
{
var tableName = 'Notes';
var rows =
[
{category_id: 1, subject_id: 1, title: 'Numero Uno', note: "Test #1"}, // 1
{category_id: 2, subject_id: 1, title: 'Numero Dos', note: "Finish this list"}, // 2
{category_id: 3, subject_id: 1, title: 'Numero Tres', note: "www.google.com"}, // 3
{category_id: 2, subject_id: 1, title: 'Numero Quatro', note: "Finish this app"}, // 4
];
return knex(tableName)
.del()
.then(function(){ // Remove all rows from table
return knex.insert(rows).into(tableName); // Insert new rows
});
};
'use strict';
var cfg = require('./knexfile');
var knex = require('knex')(cfg.development);
module.exports = knex;
// Update with your config settings.
module.exports = {
development: {
client: "sqlite3",
connection: { filename: "./data.sqlite" },
migrations: { tableName: 'knex_migrations' },
seeds: { directory: './seeds' },
pool: { afterCreate: function (conn, cb) { conn.run("PRAGMA foreign_keys=ON", cb); }},
debug: false,
useNullAsDefault: true
},
production: {
client: 'postgresql',
connection:
{
database: 'my_db',
user: 'username',
password: 'password'
},
pool: { min: 2, max: 10 },
migrations: { tableName: 'knex_migrations' }
}
};
exports.up = function (knex, Promise) {
return knex.schema
// <Notes>
.createTable('Notes', function (table) {
// Primary Key
table.increments('id');
// Fields
table.text('title');
table.text('note');
table.integer('category_id');
table.integer('subject_id');
// Foreign Keys
// table.integer('category_id').notNullable().references('id').inTable('Category');
// table.integer('subject_id').notNullable().references('id').inTable('Subject');
})
};
exports.down = function (knex, Promise) {
return knex.schema.dropTableIfExists('Notes');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment