Browse Source

working on notes db model

Issue #2

WIP - this isn't working yet; the rows aren't being passed through the callback correctly.  not sure where the issue is yet, though.
additionally, renamed some of the files and restructured the project as a whole.  still playing with layout and researching what's best practice for node apps.
master
Blaine Motsinger 1 year ago
parent
commit
8786a7a90e
6 changed files with 24 additions and 7 deletions
  1. 1
    1
      config/services/notes.service
  2. 1
    1
      dbh.js
  3. 18
    0
      models/notes.js
  4. 2
    2
      routes/api/notes.js
  5. 2
    3
      routes/index.js
  6. 0
    0
      server.js

+ 1
- 1
config/services/notes.service View File

@@ -1,5 +1,5 @@
[Service]
ExecStart=/usr/local/bin/node /home/notes/git/notes/notes.js
ExecStart=/usr/local/bin/node /home/notes/git/notes/server.js
Restart=always

StandardOutput=syslog

lib/dbh.js → dbh.js View File

@@ -2,7 +2,7 @@
// dbh connection initialization

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


+ 18
- 0
models/notes.js View File

@@ -0,0 +1,18 @@
// notes
// notes db methods

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

function all() {

// 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
};

+ 2
- 2
routes/api/notes.js View File

@@ -3,13 +3,13 @@

const express = require( 'express' );
const router = express.Router();
const dbh = require( './../../lib/dbh' );
const notes = require( './../../models/notes' );

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

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

res.status( 200 )
.header( 'Content-Type', 'application/json' )

+ 2
- 3
routes/index.js View File

@@ -3,18 +3,17 @@

const express = require( 'express' );
const router = express.Router();
const dbh = require( './../lib/dbh' );
const notes = require( './../models/notes' );

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

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

// send to the index.pug view
res.render( 'index', { rows: rows } );
});

});

module.exports = router;

notes.js → server.js View File


Loading…
Cancel
Save