0

I have a created a simple model using dynamoose and now I want to migrate it into postgresql using sequelize. I am acquainted with dynamodb and how hash keys and range keys are set in it but how do I accomplish the same in postgresql using sequelize? Below is my previous model using dynamoose and my effort to migrate it using sequelize, but I'm not sure what to do with the keys:

//dynamoose
var dynamoose = require("dynamoose");

var MessageRatingSchema = new dynamoose.Schema({
  id: {
    type: String,
    index: {
      global: true
    }
  },
  chatId: {
    type: String,
    hashKey: true
  },
  ratingType: {
    type: String,
    enum: ["NEGATIVE", "POSITIVE", "NEUTRAL", "MIXED"],
    rangeKey: true
  },
  rating: {
    type: Number
  }
});

module.exports = MessageRatingSchema;

Trying to migrate it using sequelize:

const Sequelize = require("sequelize");
const sequelize = new Sequelize('PGDATABASE', 'PGUSER', 'PGPASS', {
    host: 'localhost',
    dialect: 'postgres'
  });

  const MessageRating = sequelize.define('MessageRating', {
    id: {
      type: Sequelize.STRING,
      primaryKey: true
    },
    chatId: {
      type: Sequelize.STRING
      //hashkey
    },
    ratingType: {
        type: Sequelize.STRING,
        enum: ["NEGATIVE", "POSITIVE", "NEUTRAL", "MIXED"]
        //sortkey
    },
    rating:{
        type: Sequelize.NUMBER
    }

  }, {
    // options
  });

  module.exports = MessageRating;

Please let me know if it is the right way and how can I set the corresponding hashKeys and rangeKeys using sequelize. Thank you.

Bikal Nepal
  • 477
  • 1
  • 10
  • 26

1 Answers1

0

I am in the process of doing something similar with TypeORM, but it seems to me for Sequelize Associations you just need to set the foreign key, like so:

 chatId: {
    type: Sequelize.STRING,
    references: {
      model: Chat,
      key: 'id'
    }
  }
Ben Hulan
  • 537
  • 2
  • 7