How to correct refer to according normalized entity data in Redux Reducer - redux

All:
I am pretty new to redux reducer, most answers point to normalizr.js. Taking example like:
const blogPosts = [
{
id : "post1",
author : {username : "user1", name : "User 1"},
body : "......",
comments : [
{
id : "comment1",
author : {username : "user2", name : "User 2"},
comment : ".....",
},
{
id : "comment2",
author : {username : "user3", name : "User 3"},
comment : ".....",
}
]
},
{
id : "post2",
author : {username : "user2", name : "User 2"},
body : "......",
comments : [
{
id : "comment3",
author : {username : "user3", name : "User 3"},
comment : ".....",
},
{
id : "comment4",
author : {username : "user1", name : "User 1"},
comment : ".....",
},
{
id : "comment5",
author : {username : "user3", name : "User 3"},
comment : ".....",
}
]
}
// and repeat many times
]
I define schema as below:
var authorSchm = new schema.Entity("authors", {}, {idAttribute: "username"});
var commentSchm = new schema.Entity("comments", {author:authorSchm})
var commentList = [commentSchm];
var postSchm = new schema.Entity("posts", {author:authorSchm, comments:commentList});
var postList = [postSchm];
var normalizedData = normalize(blogPosts, postList);
console.log(JSON.stringify(normalizedData, null, 4));
And I get result like:
{
"entities": {
"authors": {
"user1": {
"username": "user1",
"name": "User 1"
},
"user2": {
"username": "user2",
"name": "User 2"
},
"user3": {
"username": "user3",
"name": "User 3"
}
},
"comments": {
"comment1": {
"id": "comment1",
"author": "user2",
"comment": "....."
},
"comment2": {
"id": "comment2",
"author": "user3",
"comment": "....."
},
"comment3": {
"id": "comment3",
"author": "user3",
"comment": "....."
},
"comment4": {
"id": "comment4",
"author": "user1",
"comment": "....."
},
"comment5": {
"id": "comment5",
"author": "user3",
"comment": "....."
}
},
"posts": {
"post1": {
"id": "post1",
"author": "user1",
"body": "......",
"comments": [
"comment1",
"comment2"
]
},
"post2": {
"id": "post2",
"author": "user2",
"body": "......",
"comments": [
"comment3",
"comment4",
"comment5"
]
}
}
},
"result": [
"post1",
"post2"
]
}
I wonder how do I use this normalized data in Redux reducer and combineReducers?
And how do I know which entity I should go to get next level data according to the reference string( for example: when I get "post1", how do I know I should go to posts entity to get post1 object and inside post1 object, how to do that for those comment1, comment2.. )

Any answer to this is going to be largely subjective and dependent on your specific application and use-case. For a generic and recommended approach, check out the official example. Here's what the combineReducers looks like.

Related

Bypass random id in Firebase Realtime REST

If my data structure looks like this:
documents.json:
{
"-NG8qzvgs46A5gojZbJO": {
"-NG8r-2q1-47MWK35aT2": {
"description": "My Description",
"title": "My Title"
},
"author": "jim",
"date": "05/11/2022"
},
"-NG8ta4xpHGZxA4JRUQZ": {
"-NG8ta9e90ChdMQclirn": {
"description": "My Description",
"title": "My Title tom"
},
"author": "tom",
"date": "04/11/2022"
},
"-NG8tjfP_TYJHZcjouY8": {
"-NG8tjiryoxnWbb4wwQQ": {
"description": "My Description ccc",
"title": "My Title jim"
},
"author": "jim",
"date": "05/11/2022"
}
}
How to get all the entries where author="jim"?
This does not work:
https://testing-11f41-default-rtdb.firebaseio.com/documents.json?orderBy="author"&equalTo="jim"
Is it possible using Firebase REST API?
Thanks in advance.
When I access the URL you give, I get this response:
{
"error" : "Index not defined, add ".indexOn": "author", for path "/documents", to the rules"
}
As this message says, you need to add an index to allow this query.
In your rules:
{
"rules": {
...
"documents": {
".indexOn": "author"
}
}
}

Query based on where clauses in Firebase database [duplicate]

This question already has answers here:
Firebase query if child of child contains a value
(1 answer)
Many to Many relationship in Firebase
(2 answers)
Closed 1 year ago.
I have a database like below, I want to query the accounts based on user ID.
"accounts": [
{
"applications": [
{
"appId": "sfdsfsfsfsfsfsfsfweufisfsfsfshwfkwjkf",
"creationDate": "dadasffsfsfsfsfsfsfsfsfsf",
"name": "Application one",
"users": [
{
"userId": "0Mo0J6Y7aUXdhwUszYrP5hy3k1C2"
},
{
"userId": "0Mo0J6Y7aUXdhwUwszYrP5hy3k1C2qqqqqq"
},
{
"userId": "0Mo0J6Y7aUXdhwUszYrP5hy3kqqqqqqq1C2qqqqqq"
}
]
},
{
"appId": "sfdsfsfsfsfsfsfsfweufisfsfsfshwfkwjkf",
"creationDate": "dadasffsfsfsfsfsfsfsfsfsf",
"name": "Application two",
"users": [
{
"userId": "0Mo0J6Y7aUXdhwUszYrP5hy3k1C2"
},
{
"userId": "0Mo0J6Y7aUXdhwUwszYrP5hy3k1C2qqqqqq"
},
{
"userId": "0Mo0J6Y7aUXdhwUszYrP5hy3kqqqqqqq1C2qqqqqq"
}
]
}
],
"id": "ahdagjhjfsfjgsfguwuwiuwireij",
"name": "Account ONE"
},
{
"applications": [
{
"appId": "sfdssfsfs242fsfsfsfsfsfsfweufihwfkwjkf",
"creationDate": "dadasffsfsfsfsfsfsfsfsfsf",
"name": "application one",
"users": [
{
"userId": "dadassfsfs24242424ffsfsfsfsfsfsfsfsfsf"
},
{
"userId": "0Mo0J6Y7aUXdhwUszYrP5hyaaaaaaaa3k1C2qqqqqq"
},
{
"userId": "0Mo0J6Y7aUXdhwUszYrP5haaaay3kqqqqqqq1C2qqqqqq"
}
]
}
],
"id": "ahdagjhjfsfjgsfguwuwiuwireij",
"name": "Account TWO"
}
]
}
I am new to Firebase. How can I retrieve a result from the data above where userID = '0Mo0J6Y7aUXdhwUszYrP5hy3k1C2'?
How can I get the accounts/application object?
I tried using OrderByChild/OrderBy but did not work. like
.orderBy('userId')
.equalTo('0Mo0J6Y7aUXdhwUszYrP5hy3k1C2')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
console.log(JSON.stringify(movie));
});

Normalizr with Redux with nested array of objects

I'm just getting started with using normalizr with Redux, and I can't make it work. Even though I can do it with plain JavaScript.
I have an array of objects
const data = [
{
data_detail: [
{
category: 'newCategory',
_id: '123',
},
],
_id: 'abc_id',
customer: {
_id: '456',
email: 'hello#gmail.com',
name: 'Bob',
},
date: '2021-01-10T01:51:24.387Z',
},
];
And I need to transform it to
const normalizedResponse = {
customers: {
'456': {
_id: '456',
email: 'hello#gmail.com',
name: 'Bob',
},
},
details: {
'123': {
category: 'newCategory',
_id: '123',
},
},
orders: {
'abc_id: {
order_detail: [123],
_id: 'abc_id',
customer: '456',
date: '2021-01-10T01:51:24.387Z',
},
},
};
Step 1: Display just orders
What I do:
const userSchema = new schema.Entity(
'orders',
);
const userListSchema = new schema.Array(userSchema);
const normalizedData = normalize(data, userListSchema);
What I get
{
"entities": {
"orders": {
"abc_id": {
"data_detail": [
{
"category": "newCategory",
"id": "123"
}
],
"id": "abc_id",
"customer": {
"id": "456",
"email": "hello#gmail.com",
"name": "Bob"
},
"date": "2021-01-10T01:51:24.387Z"
},
"abc_id-02": {
"data_detail": [
{
"category": "newCategory1",
"id": "123-02"
}
],
"id": "abc_id-02",
"customer": {
"id": "456-02",
"email": "hello#gmail.com",
"name": "Bob"
},
"date": "2001-01-10T01:51:24.387Z"
}
}
},
"result": [
"abc_id",
"abc_id-02"
]
}
What I'm trying to get:
orders: {
'abc_id: {
order_detail: [123],
_id: 'abc_id',
customer: '456',
date: '2021-01-10T01:51:24.387Z',
},
},
The question: How to remove some fields from orders and add new ones?
You have 3 different entity types in your data object. First draft out a schema for each of them:
const detail = new schema.Entity('details');
const customer = new schema.Entity('customers');
const order = new schema.Entity('orders');
Then go back and fill in the relationships. It looks like order is the outermost entity. An order contains an array of details/categories and a single customer.
const order = new schema.Entity('orders', {
data_detail: [detail],
customer,
});
All of your entities use _id instead of id, so you need to set the idAttribute.
Your data is an array of order. You can use new schema.Array(order) but you can also just use [order].
Here's your final code:
const customer = new schema.Entity("customers", {}, { idAttribute: "_id" });
const detail = new schema.Entity("details", {}, { idAttribute: "_id" });
const order = new schema.Entity(
"orders",
{
data_detail: [detail],
customer
},
{ idAttribute: "_id" }
);
const normalizedData = normalize(data, [order]);
That gives you:
{
"entities": {
"details": {
"123": {
"category": "newCategory",
"_id": "123"
}
},
"customers": {
"456": {
"_id": "456",
"email": "hello#gmail.com",
"name": "Bob"
}
},
"orders": {
"abc_id": {
"data_detail": ["123"],
"_id": "abc_id",
"customer": "456",
"date": "2021-01-10T01:51:24.387Z"
}
}
},
"result": ["abc_id"]
}

"Invalid content entity type!" when posting to Linkedin v2/shares

Followed this doc: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/rich-media-shares to upload images to reference in a organization share in LinkedIn.
Successfully uploaded the image using Assets API. The URN is in this format - urn:li:digitalmediaAsset:XXX
Retrieving asset information using the Asset ID from the digitalmediaAsset URN shows the following status:
{
"serviceRelationships": [
{
"identifier": "urn:li:userGeneratedContent",
"relationshipType": "OWNER"
}
],
"recipes": [
{
"recipe": "urn:li:digitalmediaRecipe:feedshare-image",
"status": "AVAILABLE"
}
],
"mediaTypeFamily": "STILLIMAGE",
"created": 1579015000150,
"lastModified": 1579015039823,
"id": "XXX",
"status": "ALLOWED"
}
But when I write a share using Assets URN -
POST https://api.linkedin.com/v2/shares
{
"content": {
"contentEntities": [
{
"entity": "urn:li:digitalmediaAsset:XXX"
}
],
"description": "content description",
"title": "Test Share with Content"
},
"distribution": {
"linkedInDistributionTarget": {}
},
"subject": "Test Share Subject",
"text": {
"text": "Test Share!"
},
"owner":"urn:li:organization:YYY"
}
I get the following error:
{"message":"Invalid content entity type!","status":400}
Any idea what I am doing wrong?
Add shareMediaCategory field here like this:
POST https://api.linkedin.com/v2/shares
{
"content": {
"contentEntities": [
{
"entity": "urn:li:digitalmediaAsset:XXX"
}
],
"description": "content description",
"title": "Test Share with Content",
"shareMediaCategory": "IMAGE"
},
"distribution": {
"linkedInDistributionTarget": {}
},
"subject": "Test Share Subject",
"text": {
"text": "Test Share!"
},
"owner":"urn:li:organization:YYY"
}
See the documentation for details: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/share-api#using-images-for-shares

How can I JQ transform array into a different array, joint with other data?

I need to transform this:
{"input":{"text":"HI"},"output":{"text":["OK1","TWO"]}}
Into this:
{
"localDB": [
{
"tableName": "Default",
"mode": "append",
"data": [
{
"time": "1511281401.991815",
"message": "HI",
"from": "me"
},
{
"time": "1511281401.991837",
"message": "OK1"
"from": "bot"
}
{
"time": "1511281401.991847",
"message": "TWO"
"from": "bot"
}
]}]}
Is it possible at all?
Key issue here is that number of "records" in the localDB should vary depending on the number of entries in .output.text node. There could be just one text, or three or more.
I tried with this, but it is not quite working:
{
"localDB" : [{
"tableName": "Default",
"mode": "append",
"data": [
{"time" : now|tostring, "message" : .input.text, "from" : "me"},
{"time" : now|tostring, "message" :.output.text, "from" : "bot"}
]
}]
}
I think you are very close. You just need to use .output.text[] and take advantage of how Object Construction behaves when a member expression returns multiple results. Try this:
{
"localDB" : [{
"tableName": "Default",
"mode": "append",
"data": [
{"time" : now|tostring, "message" : .input.text, "from" : "me"},
{"time" : now|tostring, "message" :.output.text[], "from" : "bot"}
]
}]
}
Sample Output
{
"localDB": [
{
"tableName": "Default",
"mode": "append",
"data": [
{
"time": "1511283566.11608",
"message": "HI",
"from": "me"
},
{
"time": "1511283566.116094",
"message": "OK1",
"from": "bot"
},
{
"time": "1511283566.116094",
"message": "TWO",
"from": "bot"
}
]
}
]
}
Try it online!
Here is a filter which uses a settime function to assign different times to the rows:
def settime: reduce range(length) as $i (.; .[$i].time = (now + $i|tostring));
{
"localDB" : [{
"tableName": "Default",
"mode": "append",
"data": [
{"message" : .input.text, "from" : "me"},
{"message" :.output.text[], "from" : "bot"}
] | settime
}]
}
Sample Output
{
"localDB": [
{
"tableName": "Default",
"mode": "append",
"data": [
{
"message": "HI",
"from": "me",
"time": "1511285948.684203"
},
{
"message": "OK1",
"from": "bot",
"time": "1511285949.684243"
},
{
"message": "TWO",
"from": "bot",
"time": "1511285950.684261"
}
]
}
]
}
Try it online!

Resources