Thursday, 26 July 2018

Vue + SQLite + Sequelize, storing an image

I'm creating my first Vue project, based on the tutorial posted here: https://developer.okta.com/blog/2018/02/15/build-crud-app-vuejs-node

It's basically a learning project. What I'm doing is creating a rifftrax database--currently defined as title, body, amazon link, whether it's free on prime or not (boolean), a url to the rifftrax page, and a poster/cover image. The tutorial uses sequelize + sqlite. I looked up sequelize-file and installed that, but I'm not quite sure how to merge the two concepts (i.e. how the okta setup is done and adding the sequelize-file model to it). So I'm looking for some help.

Here's my code:

import { movieImage } from './models/attachments';

const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const Sequelize = require('sequelize')
const epilogue = require('epilogue')
const OktaJwtVerifier = require('@okta/jwt-verifier')

const oktaJwtVerifier = new OktaJwtVerifier({
  clientId: '0oafs5gbi8zLaURtu0h7',
  issuer: 'https://dev-943536.oktapreview.com/oauth2/default'
})

let app = express()
app.use(cors())
app.use(bodyParser.json())

// verify JWT token middleware
app.use((req, res, next) => {
  // require every request to have an authorization header
  if (!req.headers.authorization) {
    return next(new Error('Authorization header is required'))
  }
  let parts = req.headers.authorization.trim().split(' ')
  let accessToken = parts.pop()
  oktaJwtVerifier.verifyAccessToken(accessToken)
    .then(jwt => {
      req.user = {
        uid: jwt.claims.uid,
        email: jwt.claims.sub
      }
      next()
    })
    .catch(next) // jwt did not verify!
})

// For ease of this tutorial, we are going to use SQLite to limit dependencies
let database = new Sequelize({
  dialect: 'sqlite',
  storage: './test.sqlite'
})

// Define our Post model
// id, createdAt, and updatedAt are added by sequelize automatically
let Post = database.define('posts', {
  title: Sequelize.STRING,
  body: Sequelize.TEXT,
  amazon: Sequelize.STRING,
  prime: Sequelize.BOOLEAN,
  rifftrax_url: Sequelize.STRING
});

movieImage.addTo(posts)

// Initialize epilogue
epilogue.initialize({
  app: app,
  sequelize: database
})

// Create the dynamic REST resource for our Post model
let userResource = epilogue.resource({
  model: Post,
  endpoints: ['/posts', '/posts/:id']
})

// Resets the database and launches the express app on :8081
database
  .sync({ force: true })
  .then(() => {
    app.listen(8081, () => {
      console.log('listening to port localhost:8081')
    })
  })



from Vue + SQLite + Sequelize, storing an image

No comments:

Post a Comment