I have a state that looks like this:
{
id1: { houses: { name: 'x' }, cars: {} },
id2: { houses: { name: 'x' }, cars: {} },
...
}
I want to return a state that looks like this:
{
id1: { houses: {}, cars: {} },
id2: { houses: {}, cars: {} },
...
}
That is, I empty the houses entry. I don't know how to do that in an immutable reducer way, given the fact that the state can have many entries (id1, id2, id3,..)
Tricky, but the way I would try to do it is with Object.entries and .reduce
const originalObj = {
id1: { houses: { name: 'x' }, cars: { dontTouch: 'me1'} },
id2: { houses: { name: 'x' }, cars: { dontTouch: 'me2'} },
id3: { houses: { name: 'x' }, cars: { dontTouch: 'me2'} }
};
const initalValue = {};
const objWithResetHouses =
Object.entries(originalObj)
.reduce((acc, [propName, value]) => {
// "copy" every property, then write over "houses" with an empty object
acc[propName] = {...value, houses: {}};
return acc;
}, initalValue);
console.log(objWithResetHouses);
Related
I want to filter my table data by createdAt,status_invitor and status_invited. I write query Like this
const query = qs.stringify({
filters: {
$and: {
createdAt: { $gte: firstDayOfTheMonth },
createdAt: { $lt: lastDayOfTheMonth },
},
$and: {
status_invitor: statusFilter
},
status_invited: statusFilter,
},
});
but is not working correctly
The filters are combined by default so unless I have misunderstood what you are trying to achieve, you don't need to use the $and operator.
const query = qs.stringify({
filters: {
createdAt: { $gte: firstDayOfTheMonth },
createdAt: { $lt: lastDayOfTheMonth },
status_invitor: statusFilter,
status_invited: statusFilter,
},
});
If you do want to mix AND and OR filters then you need to specify an array. You can see examples of that here: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/filtering-locale-publication.html#complex-filtering
I'm working on a project where I wanted to query more than one field with a search term. This is the code I used to get it working. I think all you need to do is change the $or for the $and and it should work.
const query = qs.stringify( {
filters: {
$or: [
{ name: { $contains: term }},
{ venue: { $contains: term } },
{ performers: { $contains: term } },
{ description: { $contains: term } },
],
},
},
)
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"]
}
So I have this JSON object
{
"id": "c66c588e",
"players": {
"M2cfydGooAMCLpQ=": {},
"ygjjgy7678": {}
}
}
For a given player Id, I want to update that particular object, so it becomes
{
"id": "c66c588e",
"players": {
"M2cfydGooAMCLpQ=": {
"cards": [
{
"cardId": "id1",
"cardTitle": "title here"
}
]
},
"ygjjgy7678": {}
}
}
This is the query I have
const params = {
TableName: process.env.DYNAMODB_GAMES_TABLE,
Key: {
id: gameId
},
UpdateExpression: 'set players.#player = list_append(if_not_exists(#cards, :empty_list), :card)',
ExpressionAttributeNames: {
'#cards': 'cards',
'#player': playerId
},
ExpressionAttributeValues: {
':card': [{
"cardId": cardId,
"cardTitle": cardTitle,
"pun": pun
}],
':empty_list': []
},
ReturnValues: "ALL_NEW"
};
But I get this error
{
"message": "The document path provided in the update expression is invalid for update",
"code": "ValidationException",
"time": "2020-05-21T00:50:32.236Z",
"requestId": "HLJJA2QQ2POAQEAJUD3143T6PJVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 38.70011614235671
}
I cannot seem to figure out how to update a particular player.
I think creating a new Index will result in additional AWS costs which I want to avoid.
I figured it out.
The key should be a valid key which you can access by obj.key and not obj[key]
Then this query will work
const params = {
TableName: process.env.DYNAMODB_GAMES_TABLE,
Key: {
id: gameId
},
UpdateExpression: 'set players.#player = list_append(if_not_exists(#cards, :empty_list), :card)',
ExpressionAttributeNames: {
'#cards': 'cards',
'#player': playerId
},
ExpressionAttributeValues: {
':card': [{
"cardId": cardId,
"cardTitle": cardTitle,
"pun": pun
}],
':empty_list': []
},
ReturnValues: "ALL_NEW"
};
I'm having some trouble storing arrays in DynamoDB on PutItem with AppSync, and it's turning me crazy :P
The problem is that the array I pass from GraphQl disappears when I run $util.dynamodb.toMapValuesJson. What am I doing wrong?
I'm really stuck on this, and would very much appreciate any help I can get!
This is the template:
#set($failing_list = [ "foo", 123, { "bar" : "baz" } ])
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key": {
"userId": { "S": "$ctx.args.input.email" },
"createdAt": { "N": "$util.time.nowEpochMilliSeconds()" }
},
"data" : {
"test": $util.dynamodb.toMapValuesJson({
"working_list": [ "foo", 123, { "bar" : "baz" } ],
"failing_list": $failing_list
})
}
}
This is the result:
{
version: '2017-02-28',
operation: 'PutItem',
key: {
userId: { S: 'xxxxxxxxxxxx' },
createdAt: { N: '1531521284789' }
},
data: {
test: {
working_list: {
L: [ { S: 'foo' }, { N: '123' }, { M: { bar: { S: 'baz' } } } ]
},
failing_list: {
M: { L: { L: [ { M: { map: { M: {} } } }, { M: { map: { M: {} } } }, { M: { map: { M: {} } } } ] } }
}
}
}
}
I have a Meteor application with a Mongo database containing documents with the following structure:
_id: "1234567890",
blocks: [{
type: "block",
block_id: "foobar",
items: [
{
type: "sub",
sub_id: "111",
items: [
{
type: "question",
question_id: "aaa"
},
{
type: "question",
question_id: "bbb"
}
]
},
{
type: "question",
question_id: "aaa"
}
]
}]
I want to be able to find all the questions with a question_id of 'aaa'. So far I have tried these queries, but am struggling to return any results:
questions = MyColl.find({
$or: [{
blocks: {
items: {
$elemMatch: {question_id: 'aaa'}
}
}
},{
blocks: {
items: {
type: "sub",
items: {
question_id: 'aaa'
}
}
}
}]
}).count();
Any ideas?
This is how I mananged it:
questions = MyColl.find({
$or: [{
'blocks.items': {
$elemMatch: {question_id: 'aaa'}
}
},{
'blocks.items.items': {
$elemMatch: {question_id: 'aaa'}
}
}]
}).count();