Expand Records dynamically - powerquery

I want to dynamically expand records because the entrys i get are not fixed.
This is working but it's fixed and i have to say which elements should be expanded.
let
Quelle = Sage.Contents(),
records = Quelle{[Name="Kontakte"]}[Data],
#"SelectItems" = Table.SelectColumns(records,{"$items"}),
#"$items1" = #"SelectItems"{0}[#"$items"],
#"ToTable" = Table.FromList(#"$items1", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"ExpandedColumn" = Table.ExpandRecordColumn(#"ToTable", "Column1", {"id", "displayed_as", "$path"}, {"Column1.id", "Column1.displayed_as", "Column1.$path"})
in
#"ExpandedColumn"
Webservice Response:
{
"$total":3,"$page":1,"$next":null,"$back":null,"$itemsPerPage":20,
"$items":[
{"id":"1","displayed_as":"Test","$path":"/contacts/Test"},
{"id":"2","displayed_as":"Test2","$path":"/contacts/Test2"},
{"id":"3","displayed_as":"Test3","$path":"/contacts/Test3"}
]}
"$items" are dynamically. It depends on how much informations are behind the contacts.
"id", "displayed_as", "$path"
could also be
"id", "displayed_as", "$path", "city", "zip", "street", "number"
or
"id", "displayed_as", "$path", "city", "zip", "street", "number", "phone", "mobile"

You just need to add two variables and edit the expand line:
SelectAllColumns = Table.ColumnNames(#"$items1"), //Create a list with the columns names
SelectAllColumns2 = List.Transform(SelectAllColumns, each "a."&_), //Create a second list with columns names changed like a.columnname
#"ExpandedColumn" = Table.ExpandRecordColumn(#"ToTable", "Column1", SelectAllColumns, SelectAllColumns2) //Replace these lists inside your last line
I think the final result will be something like:
let
Quelle = Sage.Contents(),
records = Quelle{[Name="Kontakte"]}[Data],
#"SelectItems" = Table.SelectColumns(records,{"$items"}),
#"$items1" = #"SelectItems"{0}[#"$items"],
SelectAllColumns = Table.ColumnNames(#"$items1"),
SelectAllColumns2 = List.Transform(SelectAllColumns, each "a."&_),
#"ToTable" = Table.FromList(#"$items1", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"ExpandedColumn" = Table.ExpandRecordColumn(#"ToTable", "Column1", SelectAllColumns, SelectAllColumns2)
in
#"ExpandedColumn"

Assuming that the previous step in M was named Removed Other Columns, and that the column to expand is named Data, then I replace the static line of #"Expanded Data" with the following code:
#"Expanded Data" =Table.ExpandTableColumn(#"Removed Other Columns", "Data", List.Union(List.Transform(#"Removed Other Columns"[Data], each Table.ColumnNames(_))))
It dynamically adapt to any column names.

Related

Get meta information from the database with Eloquent

I'm probably missing something simple here but how do I get the primary key for a table using Laravel/Eloquent? When I execute the following I'm getting an Incorrect Syntax error while it works when executing directly on the database.
$primarykey = DB::table('table')->whereRaw('SHOW KEYS FROM table WHERE Key_name = "PRIMARY"')->get();
I tried it with following code and it worked.
$primarykey = DB::select('SHOW KEYS FROM users WHERE Key_name = "PRIMARY"');
I had used select of DB without get method because it returns array like this
[
{#3558
+"Table": "users",
+"Non_unique": 0,
+"Key_name": "PRIMARY",
+"Seq_in_index": 1,
+"Column_name": "id",
+"Collation": "A",
+"Cardinality": 1,
+"Sub_part": null,
+"Packed": null,
+"Null": "",
+"Index_type": "BTREE",
+"Comment": "",
+"Index_comment": "",
},
]
So please try and let me know if you find any issue in it.
Thanks

Django rest_framework not serializing relationships

I am trying to serialize Foreign keys inline with Django rest_framework. Foreign keys are used to link lookup tables as per a normal DB normalisation setup.
An example of my model with the lookup:
class OrderStatus(models.Model):
StatusId = models.PositiveSmallIntegerField(primary_key=True)
StatusDescription = models.CharField(max_length=50)
class Order(models.Model):
OrderId = models.AutoField(primary_key=True)
OrderDate = models.DateTimeField(auto_now_add=True)
Status = models.ForeignKey(OrderStatus, on_delete=models.CASCADE)
My serializers:
class OrderStatusSerializer(serializers.ModelSerializer):
class Meta:
model = OrderStatus
fields = ['StatusId', 'StatusDescription']
class OrderSerializer(serializers.ModelSerializer):
class Meta:
model = Order
fields = ('OrderId', 'OrderDate', 'Status')
What I obtain when I call the REST API is the following:
{
"type": "Order",
"id": "1",
"attributes": {
"OrderId": 1,
"OrderDate": "2020-05-19T08:23:54"
},
"relationships": {
"Status": {
"data": {
"type": "OrderStatus",
"id": "1"
}
}
}
}
I would like to have the Status inline in the "attributes", either as a simple id or even as a JSON object with the two values inline. Both are good options, as long as it's not in that "relationships" field.
I tried to add the following to OrderSerializer:
Status = serializers.PrimaryKeyRelatedField(queryset=OrderStatus.objects.all())
No difference.
I tried the following:
Status = OrderStatusSerializer(many=False)
No difference.
I tried all the other options in
https://github.com/encode/django-rest-framework/blob/master/docs/api-guide/relations.md
including the SlugField to include the description instead that the ID, with no result.
It seems that what I change has no effect on the serialization.
As per my own comment, the issue was caused by rest_framework_json_api.renderers.JSONRenderer.
By switching it back to rest_framework.renderers.JSONRenderer the expected documented behaviour has been re-established.

Using a Regex against a simple list with `ElemMatch` in MongoDB .NET

Given a document with a list of strings, how would you assemble a C# MongoDB query to regex against each list item?
For example, here's some data.
{
"_id": {
"$oid": "4ded270ab29e220de8935c7b"
},
// ... some other stuff ...
"categories": [
{
"Some Category",
"Another Category",
"Yet Another Category",
"One Last Category"
}
]
},
Ultimately, how would I structure a query like this that could be strongly-typed through the MonoDB LINQ provider?
{ "categories": { $elemMatch: { $regex: someSearch, $options: "i" } } }
I'm trying to make this work with ElemMatch, but I can't seem to structure a BsonRegularExpression to work with that method. If the data was a list of keyed elements, it looks like I could make this work for some key, say itemName.
// Doesn't translate to a list of raw strings.
Query.ElemMatch ("categories", Query.Match("itemName", new BsonRegularExpression (new Regex (someSearch, RegexOptions.IgnoreCase)));
As soon as I try to make that regex work directly on ElemMatch, though, I can't match the overloads.
// Doesn't compile: cannot convert BsonRegularExpress to IMongoQuery.
Query.ElemMatch ("categories", new BsonRegularExpression (new Regex (someSearch, RegexOptions.IgnoreCase)));
Is there some method for converting a BsonRegularExpression into an IMongoQuery object directly?
Or is there some Matches syntax for applying the current iterated element in a list that would allow for a hybrid of the two? Something like this made up code.
// Doesn't work: totally making this up.
Query.ElemMatch ("categories", Query.Matches (iteratorElement, new BsonRegularExpression (new Regex (someSearch, RegexOptions.IgnoreCase)));
I was hoping to avoid just sending a raw string into the MongoDB driver, both so I can escape the search string from injection and so the code isn't littered with magic string field names (instead limited to just the BsonElement attribute on the DB model fields).
This might not be 100% what you are after (as it's not IQueryable); but it does achieve what you want (strongly typed regex filtering of a collection, using Linq):
var videosMongo = DbManager.Db.GetCollection<Video> ("videos");
var videosCollection = videosMongo.AsQueryable<Video> ();
videosMongo.Insert (new Video () {
Id = new ObjectId (),
Tags = new string[]{ "one", "two", "test three", "four test", "five" },
Name = "video one"
});
videosMongo.Insert (new Video () {
Id = new ObjectId (),
Tags = new string[]{ "one", "two", "test three", "four test", "five" },
Name = "video two"
});
videosMongo.Insert (new Video () {
Id = new ObjectId (),
Tags = new string[]{ "one", "two" },
Name = "video three"
});
videosMongo.Insert (new Video () {
Id = new ObjectId (),
Tags = new string[]{ "a test" },
Name = "video four"
});
var videos = videosCollection.Where (v => v.Name == "video four").ToList ();
var collection = DbManager.Db.GetCollection<Video> ("videos");
var regex = new BsonRegularExpression ("test");
var query = Query<Video>.Matches (p => p.Tags, regex);
var results = collection.Find (query).ToList ();
I worked this out by using the excellent resource here : http://www.layerworks.com/blog/2014/11/11/mongodb-shell-csharp-driver-comparison-cheat-cheet

how to push a column in slickgrid at particular index

Hi i want to add a column on slickgrid at particular index.
I am trying to do columns.push({ id: "", name: "", field: "" }).
Its trying to add at last but i want this column in the middle of slickgrid. Is there any way to do this
var newColumn = { id: '', name: '', field: '' };
// create a copy of the columns
var newColumns = grid.getColumns().slice(0);
// insert the new column in the middle somewhere
newColumns.splice(indexForNewColumn, 0, newColumn);
// set the grid's columns as the new columns
grid.setColumns(newColumns);

update in a nested array using C# Driver in MongoDB

Here is my exact schema:
{
"_id" : ObjectId("4fb4fd04b748611ca8da0d45"),
"Name" : "Agent name",
"City" : "XXXX",
"BranchOffice" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d46"),
"Name" : "Branch name",
"City" : "XXXX",
"SubBranch" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d47"),
"Name" : "Sub-Branch Name",
"City" : "XXXX"
"Users" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d48"),
"Name" : "User",
"City" : "XXXX"
}]
}]
}]
}
Its Inserted successfully in c#. insert code was below but update condition is failed .
I want to update field 3 level and 4 level of array using SubBranch and users
Insert code
IMongoQuery query = Query.And(Query.EQ("_id", new ObjectId(4fb4fd04b748611ca8da0d45)),
Query.EQ("BranchOffice._id", new ObjectId(4fb4fd04b748611ca8da0d46)));
Agent agent = dc.Collection.FindOne(query);
BsonDocument branchOffice = agent.BranchOffice.Find(objId => objId._id == new ObjectId(4fb4fd04b748611ca8da0d46)).ToBsonDocument();
subBranch I had get List object convert to BsonDocument
Files: name,city,_Id, and users for array
BsonDocument subBranchOffice = **subBranch.ToBsonDocument()**;
if (branchOffice.Contains("SubBranch"))
{
if (branchOffice["SubBranch"].IsBsonNull)
{
branchOffice["SubBranch"] = new BsonArray().Add(BsonValue.Create(subBranchOffice));
}
else
{
branchOffice["SubBranch"].AsBsonArray.Add(BsonValue.Create(subBranchOffice));
}
var update = Update.Set("BranchOffice.$.SubBranch",branchOffice["SubBranch"]);
SafeModeResult s = dc.Collection.Update(query, update, UpdateFlags.Upsert,SafeMode.True);
}
Here SafemodeResult is UpdateExisting = true
Here Inserted Option is successfully
next I try to update in else Statement. I am not get it answer
Update code
else
{
var queryEdit = Query.And(Query.EQ("_id", new ObjectId(4fb4fd04b748611ca8da0d45)),
Query.EQ("BranchOffice._id", new ObjectId(4fb4fd04b748611ca8da0d46)),
Query.EQ("SubBranchlist._id", new ObjectId(4fb4fd04b748611ca8da0d47)));
**//Index value 1 or 2 or 3**
var update = Update.Set("BranchOffice.$.SubBranch."index value".Name", "sname").
Set("BranchOffice.$.SubBranch."index value".city", "yyyyy" ?? string.Empty);
SafeModeResult s = dc.Collection.Update(queryEdit, update, UpdateFlags.None,SafeMode.True);
}
Here SafemodeResult is UpdateExisting = False
Here updated Option is fail
Please explain how to solve this probelm and how to update field 2 and 3 level of array
Please show any Example
There's a lot there, but it looks like at least part of your problem is that you've spelled BranchOffice differently between the data and the query you are using to update, also you've missed the hierarchy in SubBranch, so your queryEdit in the last code sample won't match the document. This will;
db.so.find({
_id: ObjectId("4fb4fd04b748611ca8da0d45"),
"BrancheOffice._id": ObjectId("4fb4fd04b748611ca8da0d46"),
"BrancheOffice.SubBranch._id": ObjectId("4fb4fd04b748611ca8da0d47"),
}).toArray()

Resources