Proper JSON construction to add different languages - postgresql

I need to change this Query to be able to handle the different languages.
I'm already on my way but I want to be able to write out languages also.
with data (code, bez) as (
values
('01', 'Test 1'),
('02', 'Test 2'),
('03', 'Test 3'),
('04', 'Test 4')
), languages (lang) as (
values ('de'), ('fr'), ('it'), ('en')
)
select jsonb_build_object('cus_test', jsonb_object_agg('1'||code, l))
from (
select code,
jsonb_object_agg('bezeichnung_'||lang, bez) ||
jsonb_build_object('code', '1'||code) ||
jsonb_build_object('cus_immopaccode', code) ||
jsonb_build_object('cus_immopac_mandant_cd', 1) as l
from data
cross join languages
group by data.code
) x
;
At the moment the output is like this:
{
"cus_test": {
"101": {
"code": "101",
"bezeichnung_de": "Test 1",
"bezeichnung_en": "Test 1",
"bezeichnung_fr": "Test 1",
"bezeichnung_it": "Test 1",
"cus_immopaccode": "01",
"cus_immopac_mandant_cd": 1
},
"102": {
"code": "102",
"bezeichnung_de": "Test 2",
"bezeichnung_en": "Test 2",
"bezeichnung_fr": "Test 2",
"bezeichnung_it": "Test 2",
"cus_immopaccode": "02",
"cus_immopac_mandant_cd": 1
},
"103": {
"code": "103",
"bezeichnung_de": "Test 3",
"bezeichnung_en": "Test 3",
"bezeichnung_fr": "Test 3",
"bezeichnung_it": "Test 3",
"cus_immopaccode": "03",
"cus_immopac_mandant_cd": 1
},
"104": {
"code": "104",
"bezeichnung_de": "Test 4",
"bezeichnung_en": "Test 4",
"bezeichnung_fr": "Test 4",
"bezeichnung_it": "Test 4",
"cus_immopaccode": "04",
"cus_immopac_mandant_cd": 1
}
}
}
Now I want to add the language translations to it like this:
with data (code, bez_de, bez_fr, bez_it, bez_en) as (
values
('01','Pruefung 1', 'Test 1','la proova 1','Test 1'),
('02', 'Pruefung 1', 'Test 1','la proova 1','Test 1'),
('03', 'Pruefung 1', 'Test 1','la proova 1','Test 1'),
('04', 'Pruefung 1', 'Test 1','la proova 1','Test 1'))
But I don't how to implement it correctly.

Related

It is not possible to make nested migrations to Prisma

I'm trying to create my first API on nets.js/prisma/postgresql
But I had difficulties describing the models in the prisma
I want that on request https://my-api/seasons the server gave a response of the form:
[
{
"Season1": {
"name": "Season 1",
"series": {
"seria1": {
"title": "text 1 1",
"url": "url 1 1",
"date": "date 1 1"
},
"seria2": {
"title": "text 1 2",
"url": "url 1 2",
"date": "date 1 2"
}
}
}
},
{
"Season2": {
"name": "Season 2",
"series": {
"seria1": {
"title": "text 2 1",
"url": "url 2 1",
"date": "date 2 1"
},
"seria2": {
"title": "text 2 2",
"url": "url 2 2",
"date": "date 2 2"
}
}
}
}
]
To make it clearer and clearer:
json data example
json data example
This is a simplified kind of migration that doesn't work for me
How can I make the same level of nesting using Prisma migration as I described above?
model Seasons {
id Int #id #default(autoincrement())
name Season #relation(fields: [seasonId], references: [id])
seasonId Int
}
model Season {
id Int #id #default(autoincrement())
name String #db.VarChar(40)
Seasons Seasons[]
}

Adding elements from one array to another array

I have a list of data array from an API and I need to check the id with another hardcoded data array id.And I need to add two data elements which I'm getting from the api data array to the hardcoded array.
This is the hardcoded data array.
List<Data> data = [
Data(id: 1, title: 'title 1', comment: null, selected: null),
Data(id: 2, title: 'title 2', comment: null, selected: null),
Data(id: 3, title: 'title 3', val: null, comment: null, selected: null),
];
This is the data response from the API.
"data": [
{
"id": 1,
"value": "value 1",
"comment": "comment 1",
"userId": 1,
},
{
"id": 2,
"value": "value 2",
"comment": "comment 2",
"userId": 2,
},
{
"id": 3,
"value": "value 3",
"comment": "comment 3",
"userId": 3,
},
]
What I wanna do is the value I'm getting for comment and value from this response data list to add to the hardcoded array comment and selected.They're initially null.
Consider a dynamic list
List<dynamic> data = [
{ 'id': 1, 'title': 'title 1', 'comment': null, 'selected': null },
{ 'id': 2, 'title': 'title 2', 'comment': null, 'selected': null },
{ 'id': 3, 'title': 'title 3', 'val': null, 'comment': null, 'selected': null},
];
and the response from api is
List<dynamic> apiResp = [
{
"id": 1,
"value": "value 1",
"comment": "comment 1",
"userId": 1,
},
{
"id": 2,
"value": "value 2",
"comment": "comment 2",
"userId": 2,
},
{
"id": 3,
"value": "value 3",
"comment": "comment 3",
"userId": 3,
},
];
to update the comment and selected fields from api response you can do something like this
for (var each in data) {
var index = apiResp.indexWhere((element) => element!['id']!.toString() == each!['id']!.toString());
if (index > -1) {
data[index]!['comment'] = apiResp[index]!['comment'];
data[index]!['selected'] = apiResp[index]!['selected'];
}
}
if you want to make sure your data is updated just print it like this
// print to ensure the data is update
for (var each in data) {
print(each);
}
Note: I added .toString with id incase if you are not sure about data type and also if your response from api is not in list form you can convert it,
var data = json.decode(utf8.decode(response.bodyBytes));
return data.toList();

MongoDB - SyntaxError: Unexpected token ILLEGAL

Am trying to run this simple query and am getting
"SyntaxError: Unexpected token ILLEGAL".
db.Bookdetail.insert({
"Subject":"Book Details",
"Details": [
{
"Book Id":"001",
"Book Name":"C# step by step",
"Book Description":"Programming book on C#"
},
{
"Book Id":"002",
"Book Name":"Head First Java",
"Book Description":"Programming Book on Java"
},
{
"Book Id":"003",
"Book Name":"Steve Jobs",
"Book Description":"biography of Jobs"
}
]
});
It seems that the : in "Book Id":"001" is invalid. Per your code, the :in "Book Id":"001" is full width letter, which is invalid character for JS
please use this code.
db.Bookdetail.insert({
"Subject":"Book Details",
"Details": [
{
"Book Id":"001",
"Book Name":"C# step by step",
"Book Description":"Programming book on C#"
},
{
"Book Id":"002",
"Book Name":"Head First Java",
"Book Description":"Programming Book on Java"
},
{
"Book Id":"003",
"Book Name":"Steve Jobs",
"Book Description":"biography of Jobs"
}
]
});
It seems you copied query form the source, it has invalid character.
Thanks

Concatenate list from JSON array

Let's say I have json in the database of items:
Row1:
{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}
Row2:
{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}
How do I get rows formatted like this (| is column seperator):
1 | Test Item, Test Item #2
2 | Test Item #3, Test Item #1
SELECT ID || '|' || ARRAY_TO_STRING( ARRAY_AGG( ITEMS ), ', ')
FROM
(
SELECT T.J->>'Id' AS ID, json_array_elements((T.J->'Items')::json)->>'Item' AS ITEMS
FROM
(
SELECT ('{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}')::json AS J
UNION all
SELECT ('{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}')::json AS J
) T
) T
GROUP BY ID

Selecting data from a datatable in PowerShell

I'm trying to figure out how to get the data i need from a Datatable.
I'm new to datatables and cannot figure out by reading the documentation how to properly build the needed query.
I made a test script in PowerShell that i posted below:
# clear the screen:
CLS
# Create the Datatable:
$dt1 = New-Object System.Data.Datatable
# Name the datatable:
$dt1.TableName = "MyTable"
# Add collumns to the table:
[void]$dt1.Columns.Add("datacolumnone")
[void]$dt1.Columns.Add("datacolumntwo")
[void]$dt1.Columns.Add("datacolumnthree")
# Add data:
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 2", [string]"black")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 3", [string]"green")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 4", [string]"green")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 1", [string]"blue")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 2", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 3", [string]"purple")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 4", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 2", [string]"black")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 3", [string]"green")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 4", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 2", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 3", [string]"purple")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 4", [string]"yellow")
# Write the data to a xml and data file:
$dt1.WriteXmlSchema('.\CacheSchema.xsd')
$dt1.WriteXml(".\CacheData.xml")
##############################
# End of this script #
# Going on with the next one #
##############################
# Set the search criteria:
$column1 = ""
$column2 = "fact 1"
$column3 = "purple"
# build the query:
$qquery1 = "
datacolumnone like '%"+$column1+"%'
AND datacolumntwo like '%"+$column2+"%'
AND datacolumnthree like '%"+$column3+"%'
";
# Create the Datatable:
$dt2 = New-Object System.Data.Datatable
# Load the data:
[void]$dt2.ReadXmlSchema(".\CacheSchema.xsd")
[void]$dt2.ReadXml(".\CacheData.xml")
# Run the query:
$dt2.Select($qquery1)
Now what i try to achieve is:
Find all id's (datacolumnone) from the rows that have the value: "fact 1" in "datacolumntwo" and "purple" in "datacolumnthree"
Then further filter the id's where "datacolumnone" is in the results from the query above, "datacolumntwo" is "fact 4" and
"datacolumnthree" is "yellow"
Then when we have the filtered id's, show the rows that have in "datacolumnone" an id from the filtered query above and in
"datacolumntwo" the value "fact 2"
https://msdn.microsoft.com/en-us/library/system.data.datatable.select%28v=vs.110%29.aspx
If I am understanding the question correctly, querying a DataTable is the same as selecting rows that meet certain criteria using the Pipeline:
So, query one would be:
$results1 = $dt | Where-Object {$.datacolumntwo -eq "fact 1" -and $.datacolumnthree -eq "purple"} | Select Id
And query2 would be:
$results2 = $dt | Where-Object {$.datacolumnone -in $result1 -and $.datacolumntwo -eq "fact 4" -and $_.datacolumnthree -eq "yellow"} | Select Id
or you can combine the two queries if the intermediate results are not needed.