sequelize update
xxxxxxxxxx
@Injectable()
export class SequelizeService {
constructor(private sequelize: Sequelize) {}
async transaction(operation: (transaction: Transaction) => Promise<any>, options?: TransactionOptions): Promise<any> {
try {
const transaction = await this.sequelize.transaction({ autocommit: false, options })
try {
const responseTransaction: Record<string, any> = await operation(transaction)
await transaction.commit()
return responseTransaction
} catch (e: any) {
await transaction.rollback()
throw response(e)
}
} catch (e: any) {
response(e)
return null
}
}
async findOneAndUpdate<T = any>(repository: Repository<Model<T, T>>, data: Optional<T, NullishPropertiesOf<T>>, options: FindOptions<T>): Promise<T> {
try {
let repositoryModel: Model<T, T> = await repository.findOne(options)
if (!repositoryModel) return null
repositoryModel = Object.assign(repositoryModel, { data })
return (await repositoryModel.save(options))?.dataValues
} catch (e: any) {
response(e)
return null
}
}
}
xxxxxxxxxx
const Tokens = db.define('tokens', {
token: {
type: sequelize.STRING
}
});
// Update tokens table where id
Tokens.update(
{ token: 'new token' },
{ where: {id: idVar} }
).then(tokens => {
console.log(tokens);
}).catch(err => console.log('error: ' + err));
xxxxxxxxxx
db.connections.update({
user: data.username,
chatroomID: data.chatroomID
}, {
where: { socketID: socket.id },
returning: true,
plain: true
})
.then(function (result) {
console.log(result);
// result = [x] or [x, y]
// [x] if you're not using Postgres
// [x, y] if you are using Postgres
});
xxxxxxxxxx
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};
}
xxxxxxxxxx
Project.update(
{ title: 'a very different title now' },
{ where: { _id: 1 } }
)
xxxxxxxxxx
await Project.update(
{ title: 'a very different title now' },
{ where: { _id: 1 } }
);
xxxxxxxxxx
Your_model.update({ field1 : 'foo' },{ where : { id : 1 }});
Your_model.update({ field1 : 'bar' },{ where : { id : 4 }});
xxxxxxxxxx
// builder like this
async findOneAndUpdate<T = any>(repository: Repository<Model<T, T>>, data: Optional<T, NullishPropertiesOf<T>>, options: FindOptions<T>): Promise<T> {
try {
let repositoryModel: Model<T, T> = await repository.findOne(options)
if (!repositoryModel) return null
repositoryModel = Object.assign(repositoryModel, { data })
return (await repositoryModel.save(options))?.dataValues
} catch (e: any) {
response(e)
return null
}
}
// call builder like this
this.findOneAndUpdate<Remittances>(this.remittanceModel, data, {
where: { id: checkRemittanceExist.id },
transaction: t,
})
.then((res: Remittances) => res)
xxxxxxxxxx
const objectToUpdate = {
title: 'Hello World',
description: 'Hello World'
}
models.Locale.update(objectToUpdate, { where: { id: 2}})
xxxxxxxxxx
const jane = await User.create({ name: "Jane" });
console.log(jane.name); // "Jane"
jane.name = "Ada";
// the name is still "Jane" in the database
await jane.save();
// Now the name was updated to "Ada" in the database!
xxxxxxxxxx
Project.find({ where: { title: 'aProject' } })
.on('success', function (project) {
// Check if record exists in db
if (project) {
project.update({
title: 'a very different title now'
})
.success(function () {})
}
})