sequelize create model code example

Example 1: sequelize cli model generate

npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string

Example 2: create or update in sequelize

async function updateOrCreate (model, where, newItem) {
    // First try to find the record
   const foundItem = await model.findOne({where});
   if (!foundItem) {
        // Item not found, create a new one
        const item = await model.create(newItem)
        return  {item, created: true};
    }
    // Found an item, update it
    const item = await model.update(newItem, {where});
    return {item, created: false};
}

Example 3: sequelize path

app.get('/fruit/:fruitName', function(req, res) {
    console.log(req.params.fruitName);
});