Rename the key field to id (NzTreeNodeOptions) - ng-zorro-antd

Good afternoon.
I use ng-zorro Tree.
By default, the NzTreeNodeOptions interface has a key field, my structure only contains the id field, how can I change the name of the key field to id?
Thanks

I was thinked only this:
addKeyField(item: ProductsInterface[]): ProductsInterface[]{
return item.map((el: ProductsInterface) => {
// return (el.children) ? this.addKeyField(el.children) : ({...item, key: el.id});
if(el.children){
return ({...el, children: this.addKeyField(el.children), key: el.id})
//this.addKeyField(el.children);
}
return ({...el, key: el.id})
})
// return t;
}
It is good, only async data some tree nodes.

Related

Variable for Key in DynamoDB params?

I have the following code. I was able to use a variable for the table name, and for the search param. But I don't seem to be able to get tableKey as a variable to work. When I run the code I get "Error Lambda: ValidationException: The provided key element does not match the schema". tableKey is set in this case to "PhoneNumber" which is the name of the field in my DDB table.
Is this possible to use a variable in this location?
async function handleRequest(searchParam,dbTable,tableKey) {
let Details = {
TableName: dbTable,
Key: {
tableKey: searchParam,
}
};
return docClient.get(Details).promise();
}
You cannot pass in the keys as a variable, only the values.
Actually, I was able to figure it out.
async function handleRequest(searchParam, dbTable, tableKey) {
let Details = {
TableName: dbTable,
Key: {
}
};
Details.Key[tableKey] = searchParam;
return docClient.get(Details).promise();

Fetching data by composite primary key in dynamodb

I am using following function ->
const params = {
TableName: process.env.dummyTable,
Key: {
outlet_id:event.pathParametrs.id,
id:{"S":"default"}
}
}
dynamoDb.get(params).promise()
.then(result => {
console.log('-->',result);
const response = {
statusCode: 200,
body: JSON.stringify(result),
};
callback(null, response);
})
.catch(error => {
console.error(error);
callback(new Error('Couldn\'t fetch table Data'));
return;
});
}
I want to fetch records based on outlet_id and id.Here outlet_id is primary partition key while id is primary sort key(with uuid).
How to specify id(primary sort key) with default value so that I can fetch data
It's unclear whether you are trying to fetch a single item by specifying a composite partition+sort key, or trying to query all items with the same partition key (ie. without specifying a sort key).
If you were trying to get a single item, with a particular partition key and sort key, your code looks good with one exception. The partition key should be specified with the type as well, like so:
const params = {
TableName: process.env.dummyTable,
Key: {
outlet_id:{"S": event.pathParametrs.id},
id:{"S":"default"}
}
}
However, if you are trying to query for all items with the same partition key (ie. without specifying a sort key) then you would need to modify the code to do a query rather than a get:
const params = {
TableName: process.env.dummyTable,
Key: {
outlet_id: {"S": event.pathParametrs.id}
}
}
dynamoDb.query(params).promise()
.then(
//...
)
.catch(
//...
)

Query List of Maps in DynamoDB

I am trying to filter list of maps from a dynamodb table which is of the following format.
{
id: "Number",
users: {
{ userEmail: abc#gmail.com, age:"23" },
{ userEmail: de#gmail.com, age:"41" }
}
}
I need to get the data of the user with userEmail as "abc#gmail.com". Currently I am doing it using the following dynamodb query. Is there any another efficient way to solve this issue ?
var params = {
TableName: 'users',
Key:{
'id': id
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.get(params, function (err, data) {
if (!err) {
const users = data.Item.users;
const user = users.filter(function (user) {
return user.email == userEmail;
});
// filtered has the required user in it
});
The only way you can get a single item in dynamo by id if you have a table with a partition key. So you need to have a table that looks like:
Email (string) - partition key
Id (some-type) - user id
...other relevant user data
Unfortunately, since a nested field cannot be a partition key you will have to maintain a separate table here and won't be able to use an index in DynamoDB (neither LSI, nor GSI).
It's a common pattern in NoSQL to duplicate data, so there is nothing unusual in it. If you were using Java, you could use transactions library, to ensure that both tables are in sync.
If you are not going to use Java you could read DynamoDB stream of the original database (where emails are nested fields) and update the new table (where emails are partition keys) when an original table is updated.

Redux - cross-entity state management

I'm using Redux and ImmutableJS to manage the state of my app. I've created the following two Records:
export const OrderRecord = Record({
id: null,
productId: null,
amount: 1,
});
export const ProductRecord = Record({
id: null,
name: '',
price: 0,
});
My global state is normalized based on the normalizr approach like this:
const state = {
entities: {
orders: new OrderedMap(new Map({
1: new OrderRecord(createOrderItem(1, 1)),
})),
products: new OrderedMap(new Map({
1: new ProductRecord(createProductItem(1)),
})),
},
};
I'm using this specification for testing purposes.
Now I'm trying to make some selects with computed fields using Reselect.
export const getVisibleOrders = createSelector(
[getProducts, getOrders],
(products, orders) => {
orders.map(order => {
const product = products.get(order.productId.toString());
if (!product) {
return order;
}
const totalPrice = order.amount * product.price;
order.set('productName', product.name);
order.set('totalPrice', totalPrice);
return order;
});
}
);
, but I get the following error message:
Error: Cannot set unknown key "productName" on Record
I know the reason - Record cannot contain any undefined keys, but my question is: Is there any suggested approach how gracefully solved this problem?
I don't want to extend my Records to support this kind of computed parameters (product.name and totalPrice).
I don't want to keep the static and computed parameters in one place, because for example the 'productName' parametr is from "Product" entity and not from "Order" entity.
Thank you.
The whole point of using Immutable.Record is to not let you add new keys to your record, hence the error message you get. And the whole point of selectors is to expose such "computed" property if you want to consume them outside. In your case, you can simply return a new Map() object or a new record type if you need to use the dotted syntax :
return Map({
productName: 'foobar'
}).merge(order)

Bookshelf.js accessing multiple tables that are related

I'm dealing with three tables.
Right now I can get the feed of a user (activities table) and activityTypes object (id,type from activitytypes table), but I also need to get related_user (users table) object of a given activity, so I'll know the details of the related_user if the column isn't empty.
users
id
email
username
password_hash
activitytypes
id
type
activities
id
user_id (foreign key from users, owner of the activity)
status
activitytype_id (foreign key from activitytypes)
related_user (foreign key from users.
Shows relation with the user_id, if there's any.
if activitytype_id is 2 or 3, this will be shown as "followed, "commented", etc.)
My models
var activityType = db.Model.extend({
tableName: 'activitytypes',
hasTimestamps: false
});
var activity = db.Model.extend({
tableName: 'activities',
hasTimestamps: true,
activityTypes: function() {
return this.belongsTo(activityType);
}
});
var user = db.Model.extend({
tableName: 'users',
hasTimestamps: true,
feed: function() {
return this.hasMany(activity);
}
});
This is my existing query, how can I add related_user object to it?
user.where({id : 43}).fetchAll({withRelated : ['feed.activityTypes']}).then(function(data) {
data = data.toJSON();
res.send(data);
});
I solved it.
I added a new method to activity, like below.
userRelated : function() {
return this.belongsTo(user,'related_user');
}
And here's the updated query. I don't know whether it's the right way or not in terms of optimization, but it works.
user.where({
id: 43
}).fetchAll({
withRelated: ['feed.userRelated', 'feed.activityTypes']
}).then(function(data) {
data = data.toJSON();
res.send(data);
});
Right now feed, userRelated and activityTypes return without a problem, everything is in data in JSON format.

Resources