I have two tables in a 1-many relationship, Device and TestResult. A Device can have many TestResults.
My application presents user with a filterable search page that returns a list of TestResults. It's effectively a query builder in the back end. The user can add filters based on both Device attributes and TestResult attributes. For instance, a user may search for all TestResults that passed and were performed during a time range, for Devices who's serial number falls within a specific range. The results should be grouped by Device, so querying from the Device class.
Here is an example of what a FindOptions object might look like, which I would pass to the Device.findAndCountAll() method:
let results = Device.findAndCountAll(
{
include: [
{
model: TestResult,
attributes: [
'id',
'testBlockId',
'deviceId',
'type',
'fieldResponses',
'stationName',
'summary',
'createdAt'
],
where: {
createdAt: { [Symbol(gte)]: '2020-03-27T11:54:43.100Z' },
stationName: { [Symbol(in)]: [ 'Red', 'Green' ] },
fieldResponses: {
[Symbol(and)]: [
{ [Symbol(like)]: '%"Customer":"CustomerA"%' },
{ [Symbol(like)]: '%"Batch":"4"%' }
]
},
testBlockId: { [Symbol(in)]: [ 2, 3 ] },
summary: 'True'
},
as: 'testResults'
}
],
attributes: [ 'id', 'serialNumber', 'createdAt' ],
limit: 100,
offset: 0,
order: [ [ 'serialNumber', 'ASC' ] ],
where: {
serialNumber: { [Symbol(between)]: [ '000000001000', '000000200000' ] }
}
}
)
I'm now trying to add an option to delete all TestResult records that are returned by one of these searches but I'm not sure what the proper way to do this with Sequelize is.
The DestroyOptions type does not have an include[]
attribute, so I don't know how to add an INNER JOIN to a DELETE query in Sequelize.
It might be possible to call TestResults.findAll()
and destroy records in the .then()
function, but I haven't figured out how to do this. Without a LIMIT set, the query might return hundreds of thousands of rows, way too many to hold in memory after Sequelize turns them all into complex objects. I also don't want to delete the records one at a time.
Ideally, a query would look like this:
DELETE FROM testResult
WHERE summary = 'True'
AND deviceId IN (
SELECT id FROM device
WHERE serialNumber BETWEEN '0000000010000' AND '000000002000'
);
But I don't know how to achieve a subquery like that in Sequelize.
Is there a proper way, using Sequelize, to do bulk deletes with complex WHERE clauses?