reduce unnecessary lot queries
parent
31f439597e
commit
f33ddb604f
|
|
@ -65,26 +65,33 @@ export async function getWorkOrders(filters, options, connectedDatabase) {
|
||||||
let workOrders = [];
|
let workOrders = [];
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
workOrders = database
|
workOrders = database
|
||||||
.prepare('select w.workOrderId,' +
|
.prepare(`select w.workOrderId,
|
||||||
' w.workOrderTypeId, t.workOrderType,' +
|
w.workOrderTypeId, t.workOrderType,
|
||||||
' w.workOrderNumber, w.workOrderDescription,' +
|
w.workOrderNumber, w.workOrderDescription,
|
||||||
' w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,' +
|
w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,
|
||||||
' w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,' +
|
w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,
|
||||||
' ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,' +
|
ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,
|
||||||
' ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount' +
|
ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount,
|
||||||
' from WorkOrders w' +
|
ifnull(l.workOrderLotCount, 0) as workOrderLotCount
|
||||||
' left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId' +
|
from WorkOrders w
|
||||||
(' left join (select workOrderId,' +
|
left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId
|
||||||
' count(workOrderMilestoneId) as workOrderMilestoneCount,' +
|
left join (
|
||||||
' sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount' +
|
select workOrderId,
|
||||||
' from WorkOrderMilestones' +
|
count(workOrderMilestoneId) as workOrderMilestoneCount,
|
||||||
' where recordDelete_timeMillis is null' +
|
sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount
|
||||||
' group by workOrderId) m on w.workOrderId = m.workOrderId') +
|
from WorkOrderMilestones
|
||||||
sqlWhereClause +
|
where recordDelete_timeMillis is null
|
||||||
' order by w.workOrderOpenDate desc, w.workOrderNumber desc' +
|
group by workOrderId) m on w.workOrderId = m.workOrderId
|
||||||
(options.limit === -1
|
left join (
|
||||||
? ''
|
select workOrderId, count(lotId) as workOrderLotCount
|
||||||
: ` limit ${options.limit} offset ${options.offset}`))
|
from WorkOrderLots
|
||||||
|
where recordDelete_timeMillis is null
|
||||||
|
group by workOrderId) l on w.workOrderId = l.workOrderId
|
||||||
|
${sqlWhereClause}
|
||||||
|
order by w.workOrderOpenDate desc, w.workOrderNumber desc
|
||||||
|
${options.limit === -1
|
||||||
|
? ''
|
||||||
|
: ` limit ${options.limit} offset ${options.offset}`}`)
|
||||||
.all(sqlParameters);
|
.all(sqlParameters);
|
||||||
}
|
}
|
||||||
const hasInclusions = (options.includeComments ?? false) ||
|
const hasInclusions = (options.includeComments ?? false) ||
|
||||||
|
|
@ -96,13 +103,18 @@ export async function getWorkOrders(filters, options, connectedDatabase) {
|
||||||
workOrder.workOrderComments = await getWorkOrderComments(workOrder.workOrderId, database);
|
workOrder.workOrderComments = await getWorkOrderComments(workOrder.workOrderId, database);
|
||||||
}
|
}
|
||||||
if (options.includeLotsAndLotOccupancies ?? false) {
|
if (options.includeLotsAndLotOccupancies ?? false) {
|
||||||
const workOrderLotsResults = await getLots({
|
if (workOrder.workOrderLotCount === 0) {
|
||||||
workOrderId: workOrder.workOrderId
|
workOrder.workOrderLots = [];
|
||||||
}, {
|
}
|
||||||
limit: -1,
|
else {
|
||||||
offset: 0
|
const workOrderLotsResults = await getLots({
|
||||||
}, database);
|
workOrderId: workOrder.workOrderId
|
||||||
workOrder.workOrderLots = workOrderLotsResults.lots;
|
}, {
|
||||||
|
limit: -1,
|
||||||
|
offset: 0
|
||||||
|
}, database);
|
||||||
|
workOrder.workOrderLots = workOrderLotsResults.lots;
|
||||||
|
}
|
||||||
const lotOccupancies = await getLotOccupancies({
|
const lotOccupancies = await getLotOccupancies({
|
||||||
workOrderId: workOrder.workOrderId
|
workOrderId: workOrder.workOrderId
|
||||||
}, {
|
}, {
|
||||||
|
|
|
||||||
|
|
@ -121,26 +121,35 @@ export async function getWorkOrders(
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
workOrders = database
|
workOrders = database
|
||||||
.prepare(
|
.prepare(
|
||||||
'select w.workOrderId,' +
|
`select w.workOrderId,
|
||||||
' w.workOrderTypeId, t.workOrderType,' +
|
w.workOrderTypeId, t.workOrderType,
|
||||||
' w.workOrderNumber, w.workOrderDescription,' +
|
w.workOrderNumber, w.workOrderDescription,
|
||||||
' w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,' +
|
w.workOrderOpenDate, userFn_dateIntegerToString(w.workOrderOpenDate) as workOrderOpenDateString,
|
||||||
' w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,' +
|
w.workOrderCloseDate, userFn_dateIntegerToString(w.workOrderCloseDate) as workOrderCloseDateString,
|
||||||
' ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,' +
|
ifnull(m.workOrderMilestoneCount, 0) as workOrderMilestoneCount,
|
||||||
' ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount' +
|
ifnull(m.workOrderMilestoneCompletionCount, 0) as workOrderMilestoneCompletionCount,
|
||||||
' from WorkOrders w' +
|
ifnull(l.workOrderLotCount, 0) as workOrderLotCount
|
||||||
' left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId' +
|
from WorkOrders w
|
||||||
(' left join (select workOrderId,' +
|
left join WorkOrderTypes t on w.workOrderTypeId = t.workOrderTypeId
|
||||||
' count(workOrderMilestoneId) as workOrderMilestoneCount,' +
|
left join (
|
||||||
' sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount' +
|
select workOrderId,
|
||||||
' from WorkOrderMilestones' +
|
count(workOrderMilestoneId) as workOrderMilestoneCount,
|
||||||
' where recordDelete_timeMillis is null' +
|
sum(case when workOrderMilestoneCompletionDate is null then 0 else 1 end) as workOrderMilestoneCompletionCount
|
||||||
' group by workOrderId) m on w.workOrderId = m.workOrderId') +
|
from WorkOrderMilestones
|
||||||
sqlWhereClause +
|
where recordDelete_timeMillis is null
|
||||||
' order by w.workOrderOpenDate desc, w.workOrderNumber desc' +
|
group by workOrderId) m on w.workOrderId = m.workOrderId
|
||||||
(options.limit === -1
|
left join (
|
||||||
? ''
|
select workOrderId, count(lotId) as workOrderLotCount
|
||||||
: ` limit ${options.limit} offset ${options.offset}`)
|
from WorkOrderLots
|
||||||
|
where recordDelete_timeMillis is null
|
||||||
|
group by workOrderId) l on w.workOrderId = l.workOrderId
|
||||||
|
${sqlWhereClause}
|
||||||
|
order by w.workOrderOpenDate desc, w.workOrderNumber desc
|
||||||
|
${
|
||||||
|
options.limit === -1
|
||||||
|
? ''
|
||||||
|
: ` limit ${options.limit} offset ${options.offset}`
|
||||||
|
}`
|
||||||
)
|
)
|
||||||
.all(sqlParameters)
|
.all(sqlParameters)
|
||||||
}
|
}
|
||||||
|
|
@ -160,18 +169,22 @@ export async function getWorkOrders(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.includeLotsAndLotOccupancies ?? false) {
|
if (options.includeLotsAndLotOccupancies ?? false) {
|
||||||
const workOrderLotsResults = await getLots(
|
if (workOrder.workOrderLotCount === 0) {
|
||||||
{
|
workOrder.workOrderLots = []
|
||||||
workOrderId: workOrder.workOrderId
|
} else {
|
||||||
},
|
const workOrderLotsResults = await getLots(
|
||||||
{
|
{
|
||||||
limit: -1,
|
workOrderId: workOrder.workOrderId
|
||||||
offset: 0
|
},
|
||||||
},
|
{
|
||||||
database
|
limit: -1,
|
||||||
)
|
offset: 0
|
||||||
|
},
|
||||||
|
database
|
||||||
|
)
|
||||||
|
|
||||||
workOrder.workOrderLots = workOrderLotsResults.lots
|
workOrder.workOrderLots = workOrderLotsResults.lots
|
||||||
|
}
|
||||||
|
|
||||||
const lotOccupancies = await getLotOccupancies(
|
const lotOccupancies = await getLotOccupancies(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -264,6 +264,7 @@ export interface WorkOrder extends Record {
|
||||||
workOrderMilestoneCompletionCount?: number;
|
workOrderMilestoneCompletionCount?: number;
|
||||||
workOrderComments?: WorkOrderComment[];
|
workOrderComments?: WorkOrderComment[];
|
||||||
workOrderLots?: Lot[];
|
workOrderLots?: Lot[];
|
||||||
|
workOrderLotCount?: number;
|
||||||
workOrderLotOccupancies?: LotOccupancy[];
|
workOrderLotOccupancies?: LotOccupancy[];
|
||||||
}
|
}
|
||||||
export interface User {
|
export interface User {
|
||||||
|
|
|
||||||
|
|
@ -353,7 +353,10 @@ export interface WorkOrder extends Record {
|
||||||
workOrderMilestoneCompletionCount?: number
|
workOrderMilestoneCompletionCount?: number
|
||||||
|
|
||||||
workOrderComments?: WorkOrderComment[]
|
workOrderComments?: WorkOrderComment[]
|
||||||
|
|
||||||
workOrderLots?: Lot[]
|
workOrderLots?: Lot[]
|
||||||
|
workOrderLotCount?: number
|
||||||
|
|
||||||
workOrderLotOccupancies?: LotOccupancy[]
|
workOrderLotOccupancies?: LotOccupancy[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue