Sequelize Models, registered, synced, but how to do CRUD-Operations?

46 Views Asked by At

i used a course on udemy to make my first steps with sequelize: https://github.com/DavidArmendariz/sequelize-course/tree/master

Because I only want to use ES-Modules I had to make a few adjustments.

My register models Code:

import Quote from './quote.js';
import Variant from './variant.js';
import Version from './version.js';
import Log from './log.js';

let imports = [Variant, Version, Log, Quote]; 
let models = {};

export function registerModels(sequelize) {
  
  imports.forEach((item) => {
    models[item.constructor.name] = item(sequelize);
  } ) 
  
  

  sequelize.models = models;
}

export default models;

The models are ES6-Classes. For example:

import { Model, DataTypes } from 'sequelize';

export default (sequelize) => {
  class Log extends Model {
    static associate(models) {
      Log.Quote_id = Quote.hasOne(models.Version);
    }

    static async newEntry() {
      return sequelize.transaction(async() => {
        // here we put all the logic, when we create a new quote
        await Log.create({queueMicrotaskuote_id, user, action});

      })
     }

  }

  Log.init({
    id:{
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
  },
  quote_id: {
      type: DataTypes.INTEGER,
      allowNull: false
  },
  user: {
      type: DataTypes.INTEGER,
      allowNull: false
  },
  action: {
      type: DataTypes.TEXT,
      allowNull: false
  }
  }, { sequelize, modelName: 'Log' }
  );
  return Log;
}

After registering the models I sync it, and get tables in my database:

// Register the models
    registerModels(this.connection);

    // Sync the models
    await this.sync();
  }

My Problem

Now I want to use that class and it´s methods:

import { Router } from 'express';
import Quote from '../models/quote.js';
import asyncWrapper from '../utils/asyncWrapper.js';

const router = Router();

router.get('/', asyncWrapper(async (req, res) => {

  await Quote.addQuote();
  return res.status(200).send({success: true})

}))

export { router };

This is just an example, i don´t want to add a quote with a get request. Error: Quote.addQuote is not a function

The original Code from the course (example):

import { Router } from 'express';
import models from '../../models';
import asyncWrapper from '../../utils/asyncWrapper';
import JWTUtils from '../../utils/jwt-utils';

const router = Router();
const { User } = models;

router.post(
  '/register',
  asyncWrapper(async (req, res) => {
    const { email } = req.body;
    const user = await User.findOne({ where: { email } });

    if (user) {
      return res
        .status(200)
        .send({ success: false, message: 'User already exists' });
    }

    const payload = { email };
    const accessToken = JWTUtils.generateAccessToken(payload);
    const refreshToken = JWTUtils.generateRefreshToken(payload);
    await User.createNewUser({ ...req.body, refreshToken });

    return res.status(200).send({
      success: true,
      message: 'User successfully registered',
      data: {
        accessToken,
        refreshToken,
      },
    });
  })
);

export default router;

I usually code PHP and my experience with JS is limited to client-side use. So I would very much appreciate every help to get this work, and understand where I did things wrong.

0

There are 0 best solutions below