I'm using typeorm to get data form DB and I want to list all my issues with the last status for each issue.
My database has 2 tables: Issues and IssueStatus
Into IssueStatus I'll save every status changing and comments about how to solve the Issue.
My code:
var issues = await issuesRepository
.createQueryBuilder('issue')
.select(['issue', 'history.status'])
.innerJoin('issue.history', 'history')
.leftJoin(
'issue.history',
'history2',
'history.date < history2.date OR (history.date = history2.date AND history.id < history2.id)'
)
.where('history2.id IS NULL')
.getMany();
I'm getting this result:
{
"id": "5ff86c81-a202-4211-84f4-afe2d5c0fc0d",
"cod": 1,
"opendate": "2020-12-08T13:18:55.683Z",
"closedate": null,
"type": "systems",
"subtype": "avance",
"description": "first test",
"mailto": "sustentacao#bonnjur.com.br",
"hostname": "dskcwbti02",
"username": "glenn.veloso",
"solution": null,
"supportby": null,
"history": [
{
"status": 0
}
]
}
But I want this:
{
"id": "5ff86c81-a202-4211-84f4-afe2d5c0fc0d",
"cod": 1,
"opendate": "2020-12-08T13:18:55.683Z",
"closedate": null,
"type": "systems",
"subtype": "avance",
"description": "first test",
"mailto": "sustentacao#bonnjur.com.br",
"hostname": "dskcwbti02",
"username": "glenn.veloso",
"solution": null,
"supportby": null,
"status": 0
}
You could make smth like below, but you will need to overwrite all required fields with aliases in select:
var issues = await issuesRepository
.createQueryBuilder('issue')
.select(['issue', 'history.status AS status']) // <- changes here
.innerJoin('issue.history', 'history')
.leftJoin(
'issue.history',
'history2',
'history.date < history2.date OR (history.date = history2.date AND history.id < history2.id)'
)
.where('history2.id IS NULL')
.getRawMany(); // <- changes here
And what I mean by
you will need to overwrite all required fields with aliases in select
.select([
'issue.id AS id',
'issue.cod AS cod',
'issue.opendate AS opendate',
...
...
...
'issue.solution AS solution',
'issue.supportby AS supportby',
'history.status AS status'
])
Related
I have this json :
{
"meta": {
"status": 200,
"pagination": {
"page": 1,
"perPage": 15,
"hasNext": true
}
},
"data": [
{
"id": "1",
"title": "Movie title1"
"rating": null,
"playProviders": [
]
},
{
"id": "2",
"title": "Movie title2"
"rating": {
"ratingAssessment": "7.1"
},
"playProviders": [
"HBO", "Netflix"
]
},
....
}
I want to create a page with a list of movies, I need to fetch movies but only those which have a rating and playProviders, what parameters should I use in this request?
https://api.com/movies?orderBy=views
When I filters in the code:
programs.filter((program) => program.rating !== null);
it only gets a few films per page, those that don't have null. For example, 15 are per page and I get 2. How do I filter this? (I am using react typescript)
I don't have access to the API code. I need to filter what is returned by the API or write a query so that you get already filtered data from the API.
programs = [
{rating: 1,
playProviders: ["sf"]
},
{
rating: 4,
playProviders: []
}
]
programs.filter(function(program) {
if (program.rating !== null && program.playProviders.length !== 0) {
return program;
}
})
I have started to use this library and hit an issue.
Basically i have the following object structure which i receive from an api request.
{
"introduction": "hello",
"imageUri": "someimage.jpg",
"sections": [
{
"subSections": [
{
"sectionMedia": [
{
"externalUri": "https://vimeo.com/1212",
"id": 17127,
"type": "video",
"name": null,
"description": null,
"displayOrder": null,
}
],
"id": 172,
"name": "Section 1",
"displayOrder": 1,
},
{
"sectionMedia": [
{
"externalUri": "https://vimeo.com/1212",
"id": 178121,
"type": "video",
"name": null,
"description": null,
"displayOrder": null
}
],
"id": 178121,
"name": "Section 2",
"displayOrder": 2
},
],
"sectionMedia": [
{
"externalUri": "external.jpg",
"id": 176,
"type": "download",
"name": "Bar Modelling - Series 1 Workbook",
"description": null,
"displayOrder": null,
"createdAt": "2020-06-08T05:13:25+00:00",
"createdByUser": "/users/109",
"updatedAt": "2020-07-16T23:08:29+00:00",
"updatedByUser": "/users/109"
}
],
"id": 17111,
"name": "Series",
"description": "some description",
"displayOrder": 1,
]
}
So once the user has edited the data and this may not be all fields i need to submit and merge it with the object that we received and post full object back to the server.
However if i edit something within the array i need to show as the array name such as e.g if i updated the externalUri in sectionMedia with id 176 it should have the correct object structure when submitting the structure is flat like so:
name: "some name" externalUri: "newimage.jpg"
So wanted to see if there is a nice way to do this without actually searching the object and replacing when doing an onblur as will affect performance
Hope this makes sense
Not sure whether this is what you want but we can loop through the object
const [ fields, setFields ] = useState(
{
"externalUri": "external.jpg",
"id": 176,
"type": "download",
"name": "Bar Modelling - Series 1 Workbook",
"description": null,
"displayOrder": null,
"createdAt": "2020-06-08T05:13:25+00:00",
"createdByUser": "/users/109",
"updatedAt": "2020-07-16T23:08:29+00:00",
"updatedByUser": "/users/109"
})
We render the inputs here.
return Object.keys(fields).map( item => <input name={item} value={fields[item]} onChange={onChange} />
Then for the onchange.. we use the input name and add it back to the object.
const onChange = (e) => {
const value = e.target.value
const name = e.target.name
setFields( prev => ({ ...fields, [name]: value})) //this retains the original object, but change only the edited field
}
THOUGH.. submitting back the entire document back to the server is probably bad practice.
You should probably only submit fields that were changed.
WHY?
Because we cannot trust anything submitted from client, if you merge and resubmit the entire document, you need to make sure you re-validate the ENTIRE document.
I want to get data from another bucket using the ID inside a document
The query I'm trying to execute:
SELECT meta().id,`docId`,`createdAt`,`updatedAt`,`data` FROM sales AS ccc
LET contact = (SELECT meta().id,`docId`,`createdAt`,`updatedAt`,`data` FROM contacts AS bbb USE KEYS [ccc.contactId])
WHERE status = 'active' LIMIT 10 OFFSET 0
Source doc:
{
"status": "active",
"data": {
"billingAddress": null,
"contactId": "1b529239ea294da687559e1464a8c5a8",
"count": 1,
"currency": "USD"
}
}
The doc I want to get "1b529239ea294da687559e1464a8c5a8",
{
"id" : "1b529239ea294da687559e1464a8c5a8"
"status": "active",
"data" : {
"name": "SpaceX", "location": {}
}
}
The query response I'm trying to get:
{
"status": "active",
"data": {
"billingAddress": null,
"contactId": "1b529239ea294da687559e1464a8c5a8",
"contact": { "data": { "name": "SpaceX"} }, // *trying to the contact in a contact var by selecting the name*
"count": 1,
"currency": "USD"
}
}
You have never projected contact
SELECT ccc.*, OBJECT_CONCAT(ccc.data,concat) AS data
FROM sales AS ccc
LET contact = (SELECT {bbb.data.name} AS data
FROM contacts AS bbb USE KEYS ccc.contactId)[0]
WHERE status = 'active'
LIMIT 10 OFFSET 0
I have a json file and I'm trying to remove an element with below code .
exports.delete = function(req, res) {
var deletearticle = articles[req.params.id];
delete articles[req.params.id];
fs.writeFile('server/articles.json',JSON.stringify(articles, null, 4),finished);
function finished(err){
console.log("--->After deletion, article list:\n" + JSON.stringify(articles, null, 4) );
}
res.end(JSON.stringify(deletearticle, null, 4));
};
after removing the element my json file will be like :
[
{
"header": "Jack",
"body": "Davis",
"date": "",
"id": 0
},
null,
{
"header": "dfd",
"body": "dgfgfgfgfdgfg",
"date": "2018-11-24T16:33:48.842Z",
"id": 2
}]
and I get error on client side :
{articles ? articles.map(({ id, header ,body}) => (
<div key={id} timeout={500} className="fade">
<div className="article">
<h2 className="article_title">{header}</h2>
Because one of my JSON's element is null it says that can't read id from null.Is There any better way to remove this element from my json file ? or i can check if srticle is not null in client side.
Just filter your object, Assume here a is your JSON array and you have to filter all object which does not have null
a = a.filter(function(obj){ if(obj != null) return obj })
Now print a, It will filter out all object except null objects
You can use array.splice to remove object from an array, as you have id of an object, you first need to find the index of an object to be removed and then you can remove it easily using splice.
For your reference avoid using delete
var sampleArray= [{
"header": "Jack",
"body": "Davis",
"date": "",
"id": 5
},
{
"header": "Jack",
"body": "Davis",
"date": "",
"id": 6
},
{
"header": "dfd",
"body": "dgfgfgfgfdgfg",
"date": "2018-11-24T16:33:48.842Z",
"id": 7
}
];
var id = 5;
var index = sampleArray.findIndex(a=> a.id === id);
if (index > -1) {
sampleArray.splice(index, 1);
}
console.log(sampleArray);
You can delete element without keeping null like this...
array.splice(index, 1);
Or,You can remove null elements by doing like this...
var array = [
{
"header": "Jack",
"body": "Davis",
"date": "",
"id": 0
},
null,
{
"header": "dfd",
"body": "dgfgfgfgfdgfg",
"date": "2018-11-24T16:33:48.842Z",
"id": 2
}];
// filter array
var filtered = array.filter(function (el) {
return el != null;
});
var a = [
{
"header": "Jack",
"body": "Davis",
"date": "",
"id": 0
},
null,
{
"header": "dfd",
"body": "dgfgfgfgfdgfg",
"date": "2018-11-24T16:33:48.842Z",
"id": 2
}];
//using es6 array.filter to filter null or undefined or empty object
var filtered = array.filter((x)=>{
for (var key in filter) {
if (x[key] === undefined || x[key] !== null || x[key]!=='')
return false;
}
return true;
});
Recently I've encountered a strange behaviour regarding elasticsearch with rails4/mongoid4/tire. I managed to do a temporary fix, but I want to know if there is a cleaner solution and where exactly the problem lies (is it an elasticsearch issue?)
Relevant part of my Gemfile
gem 'rails', '4.0.0'
gem "mongoid", github: 'mongoid/mongoid'
gem 'tire'
Elasticsearch version:
"version" : {
"number" : "0.90.2",
"snapshot_build" : false,
"lucene_version" : "4.3.1"
}
My model:
relevant part of my model consists of Ad class:
class Ad
include Mongoid::Document
field :title, type: String
[... other stuff...]
end
and Ad subclasses, one of which is:
class AdInAutomotiveAutomobile < Ad
field :make
field :model
field :body_type
tire.index_name 'ads'
[... other stuff ...]
end
using inheritance doesn't seem to have any importance, but I'm mentioning it just for the record
The problem
Inserting new Ad doesn't update the mapping of 'ads' index
{
"ads": {
"ad_in_automotive_automobile": {
"properties": {
"$oid": {
"type": "string"
}
}
}
}
}
Logs output, trimmed down:
# 2013-08-02 15:40:58:387 [ad_in_automotive_automobile/51fbb6b26f87e9ab1d000001] ("ads")
#
curl -X POST "http://localhost:9200/ads/ad_in_automotive_automobile/51fbb6b26f87e9ab1d000001" -d '{
"_id": {
"$oid": "51fbb6b26f87e9ab1d000001"
},
"active": null,
"body_type": "hatchback",
"c_at": "2013-08-02T13:40:57.647Z",
"category_id": {
"$oid": "51e8020c6f87e9b8e0000001"
},
"color": null,
"description": null,
"engine_displacement": null,
"expire_at": null,
"fuel_type": null,
"locale": null,
"make": "ford",
"meta": {},
"mileage": null,
"model": "focus",
"power": null,
"price": null,
"title": "foo",
"transmission": null,
"u_at": "2013-08-02T13:40:57.647Z",
"year": null,
"category_slug": "automotive-automobile"
}'
# 2013-08-02 15:40:58:388 [201]
#
#
{
"ok": true,
"_index": "ads",
"_type": "ad_in_automotive_automobile",
"_id": "51fbb6b26f87e9ab1d000001",
"_version": 1
}
The solution
Somehow, this:
"_id":{"$oid":"51fbb6b26f87e9ab1d000001"}
is stopping elasticsearch from updating the mapping
So I've 'fixed' this in #to_indexed_json method:
def to_indexed_json
to_json(methods: [:category_slug]).gsub( /\{\"\$oid\"\:(\".{24}\")\}/ ) { $1 }
end
Which results in:
# 2013-08-02 15:50:08:689 [ad_in_automotive_automobile/51fbb8fb6f87e9ab1d000002] ("ads")
#
curl -X POST "http://localhost:9200/ads/ad_in_automotive_automobile/51fbb8fb6f87e9ab1d000002" -d '{
"_id": "51fbb8fb6f87e9ab1d000002",
"active": null,
"body_type": "hatchback",
"c_at": "2013-08-02T13:50:08.593Z",
"category_id": "51e8020c6f87e9b8e0000001",
"color": null,
"description": null,
"engine_displacement": null,
"expire_at": null,
"fuel_type": null,
"locale": null,
"make": "ford",
"meta": {},
"mileage": null,
"model": "focus",
"power": null,
"price": null,
"title": "foo",
"transmission": null,
"u_at": "2013-08-02T13:50:08.593Z",
"year": null,
"category_slug": "automotive-automobile"
}'
# 2013-08-02 15:50:08:690 [201]
#
#
{
"ok": true,
"_index": "ads",
"_type": "ad_in_automotive_automobile",
"_id": "51fbb8fb6f87e9ab1d000002",
"_version": 1
}
And now the mapping is OK:
{
"ads": {
"ad_in_automotive_automobile": {
"properties": {
"$oid": {
"type": "string"
},
"body_type": {
"type": "string"
},
"c_at": {
"type": "date",
"format": "dateOptionalTime"
},
"category_id": {
"type": "string"
},
"category_slug": {
"type": "string"
},
"make": {
"type": "string"
},
"meta": {
"type": "object"
},
"model": {
"type": "string"
},
"title": {
"type": "string"
},
"u_at": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}
The question(s), once again
Why does it happen?
What part of stack is responsible for that?
Can it be fixed in cleaner way?
I'm the guy from the comment, it looks like this is fixed in tire HEAD, look at this issue https://github.com/karmi/tire/issues/775. I havent verified the fix since I monkey patched the class. This is the patch in case you want to go that way:
require "tire"
module Tire
class Index
def get_id_from_document(document)
case
when document.is_a?(Hash)
document[:_id] || document['_id'] || document[:id] || document['id']
when document.respond_to?(:id) && document.id != document.object_id
document.id.to_s # was document.id.as_json
end
end
end
end