Last active
June 4, 2017 19:28
-
-
Save dunstontc/36ef8fda84ad6a428597c8e8272af431 to your computer and use it in GitHub Desktop.
SQLite w/ Knex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var cfg = require('./knexfile'); | |
var knex = require('knex')(cfg.development); | |
module.exports = knex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' } | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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