Skip to content

Instantly share code, notes, and snippets.

@stockmind
Forked from GarthStevens5/Knex Node Cheat Sheet
Created October 5, 2019 17:12
Show Gist options
  • Save stockmind/e50d598f1a633f4993103034dd5c9a7d to your computer and use it in GitHub Desktop.
Save stockmind/e50d598f1a633f4993103034dd5c9a7d to your computer and use it in GitHub Desktop.
Knex cheat sheet
in cli for local db
-Install postgres (one time only for the psql cli tools)
-Install pg in the working folder of code(postgres for node) npm install pg
-createdb nameofdb
-Install knex in the working folder of code npm install knex (-g first time)
-knex init (gives you the knexfile -see below)
-knex migrate:make 'nameofmigrationsfile' <-- then edit
-knex migrate:latest
-optional for seeding- knex seed:make 'nameofseedfile' <--then edit
-optional for seeding- knex seed:run
____________________________________________________________________________
Deploy knex/pg to heroku
-git init
-heroku create ‘name’
-git add .
-git commit -m “”
-git push heroku master
-add heroku dependency Postgres
-heroku run knex migrate:latest
-heroku run knex seed:run
POSTMAN to check GET request and should be all set!
____________________________________________________________________________
database-connection.js <-- needs to be created
const CONFIG = require('./knexfile')[process.env.NODE_ENV || 'development']
module.exports = require('knex')(CONFIG)
____________________________________________________________________________
knexfile
module.exports = {
development: {
client: 'pg',
connection: 'postgres://localhost/g-memory-1' <-Make sure this is pointing to the right place. This is for local testing
},
production: {
client: 'pg',
connection: `${process.env.DATABASE_URL}?ssl=true`
}
}
____________________________________________________________________________
seedList
exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('*currentdb*').del()
.then(function () {
// Inserts seed entries
return knex('currentdb').insert([
{id: 1,
dueDate: "1997-02-01",
resolution: "Go skiing"},
{id: 2,
dueDate: "1997-05-01",
resolution: "Do stand-up"},
{id: 3,
dueDate: "1997-09-01",
resolution: "Start knitting"}
])
}).then(() => {
return knex.raw("ALTER SEQUENCE resolutions_id_seq RESTART WITH 4;");
})
}
____________________________________________________________________________
queries.js:
const database = require("./database-connection");
module.exports = {
list(){
return database('nameofdb').select()
},
read(id){
return database('nameofdb').select().where('id', id).first()
},
create(athing){
return database('nameofdb').select()
.insert(resolution)
.returning('*')
.then(record => record[0])
},
update(id, athing){
return database('nameofdb')
.update(athing)
.where('id', id)
.returning('*')
.then(record => record[0])
},
delete(id){
return database('nameofdb').delete().where('id', id)
}
};
____________________________________________________________________________
in migrations folder nameofmigrationsfile file
exports.up = function(knex, Promise) {
return knex.schema.createTable('resolutions', (resolutions) => {
resolutions.increments()
resolutions.date('dueDate')
resolutions.text('resolution')
})
}
exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('resolutions')
}
____________________________________________________________________________
in cli
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment