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[]
}
Related
I have a table of data which has a field called details which contains a json object.
The object looks something like this:
{
"name": "Persons Name",
"list": [
{
"name": "Persons Name",
"assigned": {
"company": "Company 1",
"number": "AA1"
}
},
{
"name": "Persons Name",
"assigned": {
"company": "Company 2",
"number": "BB2"
}
},
{
"name": "Persons Name",
"assigned": {
"company": "Company 3",
"number": "AA3"
}
}
],
"total_results": 3
}
Essentially, I want to return all data if any of the person's 'assigned'->>'number' field begins with an A. In the above example, two of the individuals numbers are prefixed with an A so I want all data returned.
I've been playing around and had have been making some progress but can't figure out how to bring it all together.
select f->'assigned'->>'number' from jsonb_array_elements((select details->'list' from table_name)) f;
The above query can get me a list of the three 'number' fields but I'm not sure how I can combine that with a query to return all the information, if any of these fields contain a prefix A
You are so close. Just add your condition in where clause.
SELECT *
FROM JSON_ARRAY_ELEMENTS((SELECT details -> 'list' FROM TABLE_NAME)) f
WHERE f -> 'assigned' ->> 'number' LIKE 'A%'
CHECK DEMO HERE
i have a model like this
[ {"name":"Main 1","description":"main1 Description",
"children": [{
"name": "SUB 1",
"description": "SUB 1 Description",
"children":[
{
"name": "SUB 1.1",
"description": "SUB 1.1 Description"
},
{
"name": "SUB 1.2",
"description": "SUB 1.2 Description"
} ]
}],
"parent":[{"name": "parent sub"}]
},
{"name":"Main 2","description":"main2 Description",children:[],parent:[]},
{"name":"Main 3","description":"main3 Description",children:[],parent:[]},
{"name":"Main 4","description":"main4 Description",children:[],parent:[]}
]
and i want to display name and description property. The contents in the "children" property should be a sub-level in the row, and i don't want to display "parent" content in this tree table. how can i restrict "parent" property from the tree table.
The sap.ui.model.ClientTreeBinding used by the TreeTable with a JSONModel or XMLModel supports the parameter arrayNames.
This parameter expects an array of the model property names that will create a sublevel (if containing an object).
So in your example you should use something like this:
treeTable.bindRows({path: '/pathToData', parameters: { arrayNames: ['children'] }});
or in XMLView:
<TreeTable rows="{path: '/pathToData', parameters: { arrayNames: ['children'] } }" >
...
</TreeTable>
Theres not much to find in the documentation about this except for one example. You can find the source for the example in the openui5 github.
Try This Should Work
var oData={
"children":[
{"name":"Main 1","description":"main1 Description", "children": [], "parent":[]},
{"name":"Main 2","description":"main2 Description","children":[],parent:[]},
{"name":"Main 3","description":"main3 Description","children":[],parent:[]},
{"name":"Main 4","description":"main4 Description","children":[],parent:[]}
]
};
var oTable = new sap.ui.table.TreeTable({
columns: [
new sap.ui.table.Column({label: "Name", template: "name"}),
new sap.ui.table.Column({label: "Description", template: "description"})
],
selectionMode: sap.ui.table.SelectionMode.Single,
enableColumnReordering: true,
expandFirstLevel: true,
});
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(oData);
oTable.setModel(oModel);
oTable.bindRows("/children");
I have some documents in the "company" collection structured this way :
[
{
"company_name": "Company 1",
"contacts": {
"main": {
"email": "main#company1.com",
"name": "Mainuser"
},
"store1": {
"email": "store1#company1.com",
"name": "Store1 user"
},
"store2": {
"email": "store2#company1.com",
"name": "Store2 user"
}
}
},
{
"company_name": "Company 2",
"contacts": {
"main": {
"email": "main#company2.com",
"name": "Mainuser"
},
"store1": {
"email": "store1#company2.com",
"name": "Store1 user"
},
"store2": {
"email": "store2#company2.com",
"name": "Store2 user"
}
}
}
]
I'm trying to retrieve the doc that have store1#company2.com as a contact but cannot find how to query a specific value of a specific propertie of an "indexed" list of objects.
My feeling is that the contacts lists should not not be indexed resulting in the following structure :
{
"company_name": "Company 1",
"contacts": [
{
"email": "main#company1.com",
"name": "Mainuser",
"label": "main"
},
{
"email": "store1#company1.com",
"name": "Store1 user",
"label": "store1"
},
{
"email": "store2#company1.com",
"name": "Store2 user",
"label": "store2"
}
]
}
This way I can retrieve matching documents through the following request :
db.company.find({"contacts.email":"main#company1.com"})
But is there anyway to do a similar request on document using the previous structure ?
Thanks a lot for your answers!
P.S. : same question for documents structured this way :
{
"company_name": "Company 1",
"contacts": {
"0": {
"email": "main#company1.com",
"name": "Mainuser"
},
"4": {
"email": "store1#company1.com",
"name": "Store1 user"
},
"1": {
"email": "store2#company1.com",
"name": "Store2 user"
}
}
}
Short answer: yes, they can be queried but it's probably not what you want and it's not going to be really efficient.
The document structure in the first and third block is basically the same - you have an embedded document. The only difference between are the name of the keys in the contacts object.
To query document with that kind of structure you will have to do a query like this:
db.company.find({ $or : [
{"contacts.main.email":"main#company1.com"},
{"contacts.store1.email":"main#company1.com"},
{"contacts.store2.email":"main#company1.com"}
]});
This query will not be efficient, especially if you have a lot of keys in the contacts object. Also, creating a query will be unnecessarily difficult and error prone.
The second document structure, with an array of embedded objects, is optimal. You can create a multikey index on the contacts array which will make your query faster. The bonus is that you can use a short and simple query.
I think the easiest is really to shape your document using the structure describe in your 2nd example : (I have not fixed the JSON)
{
"company_name": "Company 1",
"contacts":{[
{"email":"main#company1.com","name":"Mainuser", "label": "main", ...}
{"email":"store1#company1.com","name":"Store1 user", "label": "store1",...}
{"email":"store2#company1.com","name":"Store2 user", "label": "store2",...}
]}
}
like that you can easily query on email independently of the "label".
So if you really want to use the other structure, (but you need to fix the JSON too) you will have to write more complex code/aggregation pipeline, since we do not know the name and number of attributes when querying the system. Theses structures are also probably hard to use by the developers independently of MongoDB queries.
Since it was not clear let me show what I have in mind
db.company.save(
{
"company_name": "Company 1",
"contacts":[
{"email":"main#company1.com","name":"Mainuser", "label": "main"},
{"email":"store1#company1.com","name":"Store1 user", "label": "store1"},
{"email":"store2#company1.com","name":"Store2 user", "label": "store2"}
]
}
);
db.company.save(
{
"company_name": "Company 2",
"contacts":[
{"email":"main#company2.com","name":"Mainuser", "label": "main"},
{"email":"store1#company2.com","name":"Store1 user", "label": "store1"},
{"email":"store2#company2.com","name":"Store2 user", "label": "store2"}
]
}
);
db.company.ensureIndex( { "contacts.email" : 1 } );
db.company.find( { "contacts.email" : "store1#company2.com" } );
This allows you to store many emails, and query with an index.
I've just started working with MongoDB. And I have a document like this:
{
"_id": "12345"
"body": "Here is the body"
"comments":[
{
"name": "Person 1"
"comm": "My comment"},
{
"name": "Person 2"
"comm": "Comment 2"}
]
"author":"Author 1"
}
And I want to change this document to :
{
"_id": "12345"
"body": "Here is the body"
"comments":[
{
"name": "Person 1"
"comm": "My comment"
"checks_": 1
},
{
"name": "Person 2"
"comm": "Comment 2"
"checks_": 4
}
]
"author": "Author 1"
}
I've tried:
db.coll.update({ "_id":12345},{ "$set":{ "comments" :{ "checks_": 1}}})
And this removed all sub documents within comments and added {checks_:1} to it.
Where am I going wrong?
So what you are doing wrong is that the $set operator is doing exactly what it should, and it is replacing only the comments field with the value you have specified. This is not adding an additional document to the array.
You need to be specific and use "dot notation" to "indentify" which array element you are replacing. So to get to your result, you need two updates:
db.coll.update({ "_id":12345},{ "$set":{ "comments.0.checks_" : 1 }})
db.coll.update({ "_id":12345},{ "$set":{ "comments.1.checks_" : 4 }})
That is at least until the next version (as of writing) of MongoDB is released, where you can do bulk updates. And that will not be long now.
A little more geneirc solution (for MongoDb 3.6+):
db.coll.update(
{},
{$set: {"comments.$[element].checks_": 1}},
{multi: false, arrayFilters: [{"element.name": {$eq: "Person 1"}}]}
)
This will add field into specific sub document from the list, matching criteria (name = 'Person 1').
Adding my two cents here.
If you want to add a field to the all the cells, with the same value (in this example: 1 will be added to all of them). You can use the following command:
db.coll.updateMany( {"_id": 12345}, {"$set": {"comments.$[].checks_": 1} }});
And you will get
{
"_id": "12345"
"body": "Here is the body"
"comments":[
{
"name": "Person 1"
"comm": "My comment"
"checks_": 1
},
{
"name": "Person 2"
"comm": "Comment 2"
"checks_": 1
},
...
{
"name": "Person 300"
"comm": "Comment 300"
"checks_": 1
}
]
"author": "Author 1"
}
i have data view with following tpl.
<tpl for=".">
<tpl for="departments">
{title}
</tpl>
<tpl for="records">
<div class="thumb-wrap">
{name}
</div>
</tpl>
</tpl>
and my json redear like this
reader : new Ext.data.JsonReader(
{
root : 'data',
},
[
'departments' ,
'records'
]
),
and my item selectore in on my recordes
itemSelector : 'div.thumb-wrap',
and this is my json data
{
"success": true,
"data": [
{
"departments": [
{
"title": "name"
}
],
"records": [
{
"name": "name"
},
{
"name": "name"
}
]
},
{
"departments": [
{
"title": "name"
}
],
"records": [
{
"name": "name"
}
]
}
]
}
how cat i select my records in data view with extjs?
and how can i get selected recordes ?
i used getSelectedRecords() but it return array of departments and records.
tnx
I'm pretty sure that getSelectedRecords() is deprecated on ExtJS 4.
You can try doing something like this:
var records = data.getRecords();
That will return a Ext.data.Model[]. Than, you can access all the properties like an usual model:
record.get('departments');
record.get('records');
You are working against the system here with your record structure. DataView wants one record to correspond with one selectable item. But you want to have multiple selectable items inside one record. Doesn't work - the API was not designed for such a thing.
Your options seem to be:
Create a separate DataView component for each set of records.
Create your own DataView component that can handle grouping.
Use Grid with GroupingView. You have to change your record structure to something like below, but it's the easiest option to get working. Though... you have to sacrifice quite a bit of flexibility offered by DataView.
"data": [
{
{
"department": "Department 1"
"name": "record 1"
},
{
"department": "Department 1"
"name": "record 2"
},
{
"department": "Department 2"
"name": "record 1"
},
{
"department": "Department 2"
"name": "record 2"
},
]
}