Sequlize Query get last added value - express

I am using Sequlize. I have a table status_config in this table I have a column create_time as timestamp( 2021-06-02 12:04:52.293977). Now I need to query on this table and get the latest value created on this table.
status_config.findOne({ where: created_time **need to implement here to get last added value})

We can use order by [['createdAt', 'desc']]
let last_status_config = await status_config.findOne({
order:[['createdAt', 'desc']]
});
console.log(JSON.parse(JSON.stringify(last_status_config)))
Output
{ id: 19, name: 'test_last', password: null, confirmation: null }

Related

Get Empty Rows in TypeORM

I'm trying to write a typeORM query which includes multiple where clauses. I am able to achieve this via where option as follows:
const categories = [
{ name: 'master', categoryTypeId: 2, parentId: 1, locationId: null },
{ name: 'food', categoryTypeId: 3, parentId: null, locationId: null }];
const rows = await Repo.find({
where: categories.map((category) => ({
name: category.name,
categoryTypeId: category.categoryTypeId,
locationId: category.locationId
})),
});
I would want to maintain the mapping b/w the input array and the rows returned. For example, I know that the second category doesn't exist in the DB. I would want to have an empty object in the rows variable so that I know which categories didn't yield any result.
Upon research I have found that we can do something with SQL as mentioned here. But, I'm not sure how do I translate into typeORM if I can.

knex increment upsert - increment if record creation fails

I want to try and create a new record, but if that fails, simply update (increment) a value in that record
await databaseService.knex('myTable')
.insert({
id: id1,
value: 0
})
.onConflict(['id1'])
.increment({
value: 1
});
Unfortunately it seems you cannot chain .increment after onConflict. I am wondering if there is a way I can do this with knex or do I need to drop into raw SQL?
Thanks
With on conflict for update part merge should be used https://knexjs.org/#Builder-merge
something like this might work:
await databaseService.knex('myTable')
.insert({
id: id1,
value: 0
})
.onConflict(['id'])
.merge({
value: knex.raw("?? + ?", ["value", 1])
});
https://runkit.com/embed/krdgetjij9xh

Query from a table to get a JSON result

I have a table
I'd like to get the JSON result like this:
[
{
"Type":"I", //If group name is empty display ‘I’ otherwise, display ‘G’
"StartDate":"20/12/2020 8:00am", //This is the start time
"Additional":{ //Because it is non-group (GroupName is null), so Additional is an object
"Info":"having food"
}
},
{
"Type":"G", //Group by Date and Group Name.
"GroupName":"School", //If record has the same group name,
"StartDate":"20/12/2020 9:00am", //!!! Display the first entry’s start date
"Additional":[ // It is an array due to Group Name being a real value, the items in array is the record whose StartDate has the same Date value.
{
"StartDate":"20/12/2020 9:00am", //Display the start date
"Info":"Take bus"
},
{
"StartDate":"20/12/2020 9:30am",", //Display the start date
"Info":"On the beach"
}
]
},
{
"Type":"G",
"GroupName":"Class 1",
"StartDate":"20/12/2020 11:00am",
"Additional":[
{
"StartDate":"20/12/2020 11:00am",
"Info":"Restaurant "
},
{
"StartDate":"20/12/2020 13:00pm",
"Info":"Walk on Lane"
}
]
},
{
"Type":"I",
"StartDate":"20/12/2020 15:00pm",
"Additional":{
"Info":"In museum"
}
}
]
Would any one help me on this? Thank you so much.
I also attach the JSON data in this picture so the JSON format would be clearer.
There is a typo sorry, this is the new picture:
As per Microsoft SQL Server documentation, FOR JSON, will work best for you. It is supported on SQL Server 2016 and above.
SELECT CASE
WHEN GroupName is null THEN 'I'
ELSE 'G'
END as Type
,GroupName
,StartDate .....
FROM table
FOR JSON AUTO;
https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver15
When you form a JSON structure. Better to use with "INCLUDE_NULL_VALUES". So that it will have NULL for attribute If the value is missing.
SELECT Column1, Column2,... FROM Table FOR JSON AUTO, INCLUDE_NULL_VALUES

MongoDB Aggregate $min not calculating

I am using the aggregate:
db.quantum_auto_keys.aggregate([
{$match: {table_name: 'PIZZA_ORDERS'}},
{
$group: {
_id: { onDate: { $dateToString: {format: "%Y-%m-%d", date: '$created_on', timezone: 'America/Los_Angeles'}}, table_name: '$table_name' },
min: { $min: '$last_number' }
}},
{$sort: {_id: 1}}
]);
It ignores the onDate grouping and returns the min for the collection where table_name = PIZZA_ORDERS.
When I use $max it calculates the maximum pizza orders by day. $count also returns the number of orders per day correctly.
How should I go about getting the minimum and maximum values via Aggregate or is there a different way to get that information from my collection?
I updated to MongoDB 4.4 and the table_name was not the correct group. Changing it to include another field got min calculation to be correct.

SuiteScript 2.0 Estimate Not Updating

I am trying to null out a custom numeric field on an estimate. The code I am using is;
record.submitFields({
type: record.Type.ESTIMATE,
id : idNext,
values : {
fieldID: null
}
});
It wasn't updating so I changed the value to 0 instead of null and it's still not updating the field. The field id matches to a field id on the estimate, the estimate id matches an internal is of an estimate??
Please help.
try this
record.submitFields({
type: record.Type.ESTIMATE,
id : idNext,
values : {
fieldID: ''
}
});