Browse Source

finished notes db model

Issue #2

for the dbh, I ended up using knex and promises, which will lead to better error handling as a whole within the application.
master
Blaine Motsinger 1 year ago
parent
commit
3df9ec8e7e
6 changed files with 39 additions and 25 deletions
  1. 9
    2
      dbh.js
  2. 5
    10
      models/notes.js
  3. 1
    0
      package.json
  4. 12
    7
      routes/api/notes.js
  5. 10
    4
      routes/index.js
  6. 2
    2
      server.js

+ 9
- 2
dbh.js View File

@@ -3,7 +3,14 @@

const path = require( 'path' );
const dbPath = path.resolve( __dirname, './db/notes.db' );
const sqlite3 = require( 'sqlite3' ).verbose();
const dbh = new sqlite3.Database( dbPath );
const knex = require( 'knex' );

const dbh = knex({
client: 'sqlite3',
connection: {
filename: dbPath,
},
useNullAsDefault: true,
});

module.exports = dbh;

+ 5
- 10
models/notes.js View File

@@ -3,16 +3,11 @@

const dbh = require( './../dbh' );

function all() {
const all = function() {

// get all of the notes
dbh.all( 'SELECT id, note, created_at from notes', function( err, rows ) {

// return the rows
return rows;
});
}

module.exports = {
all
return dbh.select( 'id', 'note', 'created_at' )
.from( 'notes' );
};

module.exports.all = all;

+ 1
- 0
package.json View File

@@ -1,6 +1,7 @@
{
"dependencies": {
"express": "^4.16.3",
"knex": "^0.15.2",
"moment": "^2.22.2",
"path": "^0.12.7",
"pug": "^2.0.3",

+ 12
- 7
routes/api/notes.js View File

@@ -6,15 +6,20 @@ const router = express.Router();
const notes = require( './../../models/notes' );

// GET /notes
router.get( '/', function ( req, res ) {
router.get( '/', function( req, res ) {

// get all of the notes
notes.all( function ( rows ) {

res.status( 200 )
.header( 'Content-Type', 'application/json' )
.send( JSON.stringify( rows || [] ) );
});
notes.all()
.then( function( rows ) {
res.status( 200 )
.header( 'Content-Type', 'application/json' )
.send( JSON.stringify( rows || [] ) );
})
.catch( function( err ) {
res.status( 500 )
.header( 'Content-Type', 'text/plain' )
.send( 'Database Error' );
});
});

module.exports = router;

+ 10
- 4
routes/index.js View File

@@ -9,11 +9,17 @@ const notes = require( './../models/notes' );
router.get( '/', function ( req, res ) {

// get all of the notes
notes.all( function( err, rows ) {
notes.all()
.then( function( rows ) {

// send to the index.pug view
res.render( 'index', { rows: rows } );
});
// send to the index.pug view
res.render( 'index', { rows: rows } );
})
.catch( function( err ) {
res.status( 500 )
.header( 'Content-Type', 'text/plain' )
.send( 'Database Error' );
});
});

module.exports = router;

+ 2
- 2
server.js View File

@@ -1,7 +1,7 @@
// notes
// app and api in nodejs

"use strict";
'use strict';

const version = '1.0.1';

@@ -11,7 +11,7 @@ const config = require( './config/application' );
const app = express();
const path = require( 'path' );

const logger = function ( req, res, next ) {
const logger = function( req, res, next ) {
console.log(
timestamp() + ' ' +
req.connection.remoteAddress + ' ' +

Loading…
Cancel
Save