Storing nested JSON with Cassandra - json

I am trying to store nested JSON object using composite tables in Cassandra and the nodejs bindings.
Let's say my data looks like this (friends and foes actually have more complex data structures than simple map):
{
id: 001,
name: 'luke',
lastname: 'skywalker',
friends: [
{ id: 002,
name: 'han',
lastname: 'solo' },
{ id: 003,
name: 'obiwan',
lastname: 'kenobi' },
{ id: 004,
name: 'leila',
lastname: 'skywalker' }
],
foes: [
{ id: 005,
name: 'dark',
lastname: 'vador' },
{ id: 006,
name: 'boba',
lastname: 'feet' }
]
}
From what I understood from composite keys (here: https://pkghosh.wordpress.com/2013/07/14/storing-nested-objects-in-cassandra-composite_columns/), I expected to store my data like this:
001 | luke | skywalker | friend:002:han:solo | friend:003:obiwan:kenobi | ... | foe:006:boba:feet
I created my table like this:
CREATE TABLE heroes (
id int,
name text,
lastname text,
friend_id int,
friend_name text,
friend_lastname text,
foe_id int,
foe_name text,
foe_lastname text,
PRIMARY KEY ((id, name, lastname), friend_id, foe_id)
);
And then run for each friends or foes:
client.execute(
'INSERT INTO heros (id, name, lastname, friend_id, friend_name, friend_lastname) VALUES (?, ?, ?, ?, ?)',
[001, 'luke', skywalker', 002, 'han', 'solo'],
function(err) {
//some code
}
)
Now, when runing the query 'SELECT * FROM heroes WHERE id=001' I expected to get only one row with all the friends or foes added as column.
Instead I get as many row as there as friends and foes. On top of this, a row for a friend looks like this:
{ rows:
[ { __columns: [Object],
id: 001,
name: 'luke',
lastname: 'skywalker',
friend_id: 002,
friend_name: 'han',
friend_lastname: 'solo',
foe_id: null,
foe_name: null,
foe_lastname: null } ],
meta:
// some data
}
I would have expected it not to have foe_* field at all.
Am I doing something wrong or is it the way cassandra handles composite items?

The result you are getting is be expected, because you have included friend and foes as part of the primary key. Hero has one to many association with friend and foe. In the blog post of mine that you are referring, I have used only the primary entity attributes as primary key. All the children entity attributes are mapped as dynamic columns.

Related

Prisma 2 - Unkown arg 'where' in select.fruit.where for type UserFruit. Did you mean 'select'? Available args

Trying to query in prisma and filter results from a related object but get the error:
Unknown arg 'where' in select.fruit.where for type UserFruit. Did you
mean 'select'? Available args fruit{}
async findShops(req) {
const userId = parseInt(req.params.id);
const shop = await prisma.shop.findMany({
select: {
id: true,
name: true,
logo: true,
fruit:{
select:{
id:true,
userId:true,
fruitNumber:true,
created: true,
updated: true,
},
where: {
userId: userId
}
}
}
})
return shop;
};
example payload expected:
[
{ id: 1001, name: 'test1', logo: 'log.png', fruit: null },
{ id: 1002, name: 'test2', logo: 'log2.jpg', fruit: null },
{ id: 1003, name: 'test3', logo: 'log3.jpg', fruit: null },
{
id: 1005,
name: 'test4',
logo: 'log4.png',
fruit: {
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 111,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
}
},
{
id: 1004,
name: 'test5',
logo: 'log5.jpg',
fruit: {
id: '20e9af37-2e6f-4070-8475-c5a914f311dc',
userId: 111,
fruitNumber: '123878',
created: 2022-07-01T07:21:27.898Z,
updated: 2022-07-01T07:21:27.901Z
}
}
]
I can easily achieve the expected output by not having the "where" but I need it because the fruit object can contain more than 1 object so I need to filter by userId e.g.
fruit: {
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 111,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
},
{
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 999,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
}
For the fruit object I need nulls and anything that matches the userId and based on design it should only ever be 1 record for each shop for the specific user.
At somepoint my code seemed to work but after I did a prisma generate it stopped working. Is there another way I can achieve the same result or is there someway to fix this?
Note:version info below.
model UserFruit {
id String #id #default(uuid())
fruitNumber String #map("fruit_number")
shopId Int #unique #map("shop_id")
userId Int #map("user_id")
created DateTime #default(now())
updated DateTime #updatedAt
fruit Fruit #relation(fields: [fruitId], references: [id])
##unique([userId, fruitId], name: "userFruit")
##map("user_Fruit")
}
model Shop {
id Int #id #default(autoincrement())
name String #unique
logo String
created DateTime #default(now())
updated DateTime #updatedAt
fruit UserFruit?
##map("Shop")
}
model User {
id Int #id #default(autoincrement())
created DateTime #default(now())
updated DateTime #updatedAt
uid String #unique
email String #unique
phone String #unique
firstName String #map("first_name")
lastName String #map("last_name")
dob DateTime?
gender String?
roleId Int #default(1) #map("role_id")
role Role #relation(fields: [roleId], references: [id])
##map("user")
}
Why not do a nested where at the top level to only search for shops whose fruit has the userId you need, rather than doing a nested select? It should make your query simpler and also solve your problem.
const userId = parseInt(req.params.id);
const shop = await prisma.shop.findMany({
select: {
id: true,
name: true,
logo: true,
fruit: {
select: {
id: true,
userId: true,
fruitNumber: true,
created: true,
updated: true,
},
// Removed the nested "where" from here
},
},
where: {
// One of the following conditions must be true
OR: [
// Return shops who have a connected fruit AND
// the fruit's "userId" attribute equals the variable "userID"
{
fruit: {
is: {
userId: userId,
// Can also simplify this to the below line if you want
// userId
},
},
},
// Return shops who do not have a connected fruit
// this will be true if "fruitId" is null
// could also write this as {fruit: {is: {}}}
{
fruitId: {
equals: null,
},
},
],
},
});
This query should output an array of shops where the connected fruit model's userId property equals your userId variable.

How to store multiple values on a mysql table

I'm new to SQL and I try to understand some basic stuff.
I have 2 tables:
users: ID | NAME | EMAIL | APPS
apps: ID | NAME
QUESTIONS
How do I create a third table based on those two, that as a JSON will look like this:
[
{ id: 1, name: 'Daniel', email: 'dan#iel.com', apps: [1,2,3]},
{ id: 2, name: 'Ben', email: 'dan#iel.com', apps: [1,5]},
{ id: 3, name: 'Maya', email: 'dan#iel.com', apps: [4]}
]
Query wise, how do I write a query that create a user like this, where apps is a list of ID's:
{ id: 1, name: 'Daniel', email: 'dan#iel.com, apps: [1,2,3]}
would it not be better to use a pivot table.
pivot table:
[
{ pivot_id: user_id, app_id },
{ pivot_id: user_id, app_id },
]
a simple loop could be used to insert into pivot table
foreach(apps as app_id)
{
query = "INSERT INTO pivot_table set user_id = `user_id`, app_id = `app_id` ";
}

Retrive all the value that satisfy the condition of first table

I have two tables users and location. I need to join both tables
what i need is get all the area number of all the users which are present in the user table.
ie user 1 has 3 entries in the second table so i need to join the table in such a way that is,
id1 = 1
area = 2,3
area 2 is repeating so do not include it twice
i tried the join but now getting the correct way to doing it.
What i tried?
$location = User::
join('addresses','users.id1','=','addresses.id1') ->select('users.id1','addresses.area')
->get();
Expected Output
User 1 -> area ->2,3
Here are the two ways to do this.
Firstly you can use Laravel relationship:-
In your model User create relationship:-
function addresses()
{
return $this->hasMany(Address::class, 'id1', 'id1');
}
Now in your User controller you can get User addresses (areas) like this
$users = User::with('addresses')->get();
dd($users->toArray());
This will print something like this
[
{
id1: 1,
name: abaa
pwd: 12345
addresses: [
{
id2: 1,
id1: 1,
area: 2
},
{
id2: 2,
id1: 1,
area: 3
},
{
id2: 3,
id1: 1,
area: 3
}
]
},
{
...
}
]
Second you can use Laravel relationship:-
$builder = new User;
$builder->join('addresses','users.id1','=','addresses.id1')
->selectRaw("users.*, GROUP_CONCAT(DISTINCT addresses.area SEPARATOR ',') as distinct_areas")
->groupBy("users.id1")
->get();
This query will give you result something like this
[
{
id1: 1,
name: abaa,
pwd: 12345,
distinct_areas: 2,3
},
{
...
}
]
I think this will help you.

Get records with nested items node-mysql

Im using node-mysql for database query manpulation in my expressjs app, And I want to get a nested results from a query. But seems I can't find a way how to do it in a simple way.
In their document I found Joins with overlapping column names
which can query a table name and nested with a columns. However this
is not suitable work around for one-to-many or many-to-many structure.
Following are examples of table structure and expected results .
Tables
tbl_order
id customer_name date
1 Perona 6/7/2018
2 Zorro 6/8/2018
tbl_order_items
id order_id item_name
1 2 compass
2 2 sword
3 2 yakuta
4 1 umbrella
5 1 doll
Expected Results
I want to get all orders and items containing the order.
[
{
id: 1,
customer_name: perona,
data: 6/7/2018,
items: [
{ id: 4, item_name: umbrella },
{ id: 5, item_name: doll },
]
},
{
id: 2,
customer_name: zorro,
data: 6/8/2018,
items: [
{ id: 1, item_name: compass },
{ id: 2, item_name: sword },
{ id: 3, item_name: yakuta },
]
}
]
Is there any option how to do the same in node-mysql?
You can use INNER JOIN to get nested data based on your association.
SELECT [order].*,
[items].[id] AS [items.id]
[items].[item_name] AS [items.item_name]
FROM
(
SELECT [id],
[customer_name],
[date]
FROM [tbl_order]
) AS [order]
INNER JOIN
[tbl_order_items] AS [items]
ON
[order].[id] = [items].[order_id];

Select from two tables and create object with data

I'm trying to fetch data from two MySQL tables and push each into array.
I have to tables item and user with the same column - item_id. Now im requesting the data from them with
var query = "SELECT * FROM items, users WHERE items.item_id=?";
query = connection.format(query,req.params.item_id);
After this, im getting a wall of data. Like this:
{
{ user_id: 13213,
user_name: 'John',
item_id: 1337,
item_name: Leet stuff,
item_price: 13,37
},
{ user_id: 12345,
user_name: 'Mike',
item_id: 1337,
item_name: Leet stuff,
item_price: 13,37
}
But what I actualy need is this:
{
users: [
{ user_id: 13213,
user_name: 'John'
},
{ user_id: 12345,
user_name: 'Mike'
}
],
item_id: 1337,
item_name: Leet stuff,
item_price: 13,37
}
You can't do that in a single query. However, you can use two different queries and combine the output, e.g.:
SELECT u.user_id, u.user_name
FROM users u JOIN ITEMS i ON j.user_id = i.user_id
WHERE i.item_id = ?;
This will give you the list of users. You can then use the below query to get the item details:
SELECT item_id, item_name, item_price
FROM items
WHERE item_id = ?
You can then construct the required structure in your application.