Making a Flow union type of strings from properties in a union type of objects - flowtype

I have a union type of object types like:
type Action = { type: 'A' } | { type: 'B' } | { type: 'C' } // and so on
I want to make a union type of their type properties, like:
type ActionType = 'A' | 'B' | 'C' // and so on
Is there a way to do this programmatically in Flow (i.e. so I don't have to manually list out all the string literals to make ActionType)? I couldn't figure it out using $ObjMap, $PropertyType, $Call, or any combination thereof.

$PropertyType and $ElementType both work here.
type ActionType = $PropertyType<Action, "type">;
// OR
type ActionType = $ElementType<Action, "type">;

Related

Query ADX table name dynamically

I have a need to be able to query Azure Data Explorer (ADX) tables dynamically, that is, using application-specific metadata that is also stored in ADX.
If this is even possible, the way to do it seems to be via the table() function. In other words, it feels like I should be able to simply write:
let table_name = <non-trivial ADX query that returns the name of a table as a string>;
table(table_name) | limit 10
But this query fails since I am trying to pass a variable to the table() function, and "a parameter, which is not scalar constant string can't be passed as parameter to table() function". The workaround provided doesn't really help, since all the possible table names are not known ahead of time.
Is there any way to do this all within ADX (i.e. without multiple queries from the client) or do I need to go back to the drawing board?
if you know the desired output schema, you could potentially achieve that using union (note that in this case, the result schema will be the union of all tables, and you'll need to explicitly project the columns you're interested in)
let TableA = view() { print col1 = "hello world"};
let TableB = view() { print col1 = "goodbye universe" };
let LabelTable = datatable(table_name:string, label:string, updated:datetime)
[
"TableA", "MyLabel", datetime(2019-10-08),
"TableB", "MyLabel", datetime(2019-10-02)
];
let GetLabeledTable = (l:string)
{
toscalar(
LabelTable
| where label == l
| order by updated desc
| limit 1
)
};
let table_name = GetLabeledTable('MyLabel');
union withsource = T *
| where T == table_name
| project col1

Azure Data Explorer-Use scalar input as column name argument in extend operator in a user-defined function

I would like to dynamically name the calculated column, like this:
.create-or-alter function ToNewName(T:(Id:string), columnName: string)
{
T | extend columnName = Id
}
However, this create a new column called columnName instead. How do I make the second columnName link to the parameter?
There is no built in support for this in the language, however here is a possible workaround:
let foo = (p:string)
{
print z=pack(p, 1)
| evaluate bag_unpack(z)
};
foo('this is a run-time column name')

my query works with timestamp but gives an error with datetime

I have a mysql table with the following columns
name | dateone | mail | created_at
the "dateone" field is date type
the "created_at" field is timestamp type
in my controller I have a method to group the result by date
$users = Post :: orderBy ('dateone') -> get () -> groupBy (function ($ item) {
return $ item-> created_at-> format ('Y-m-d');
it works, but, when I replace
return $ item-> created_at-> format ('Y-m-d');
by
return $ item-> dateone-> format ('Y-m-d');
I have the following error:
Call to a member function format() on string
is there a solution through Carbon or other to solve my problem ?
thank for any help.
Yes, by default created_at and updated_at are in the Carbon Instance. You can use dateone as created_at by parsing it to the carbon instance.
$convertedDateOne = null;
if(!empty($item->dateone)){
$convertedDateOne = Carbon::parse($item)->format('Y-m-d');
}
If you want the dateone to be the instance of Carbon every time, you can cast in the datetime format in the model.
Supposing the model name is Item.php
class Item extends Model{
protected $dates = ['dateone'];
}

What data type is returned by this LocalStorage command?

There is an LocalStorage example in the Qt documentation
function findGreetings() {
var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000);
db.transaction(
function(tx) {
// Some other commands
// Show all added greetings
var rs = tx.executeSql('SELECT * FROM Greeting');
}
)
}
What's the data type of rs?
See the Quick Local Storage QML module documentation:
results = tx.executeSql(statement, values)
This method executes a SQL statement, binding the list of values to
SQL positional parameters ("?").
It returns a results object, with the following properties:
| Type | Property | Value | Applicability |
-----------------------------------------------------------------------------------------
| int | rows.length | The number of rows in the result | SELECT |
-----------------------------------------------------------------------------------------
| var | rows.item(i) | Function that returns row i of the result | SELECT |
-----------------------------------------------------------------------------------------
| int | rowsAffected | The number of rows affected by a modification | UPDATE,DELETE |
-----------------------------------------------------------------------------------------
| string | insertId | The id of the row inserted | INSERT |
results = tx.executeSql(statement, values)
This method executes a SQL statement, binding the list of values to SQL positional parameters ("?").
It returns a results object, with the following properties: link
If all you want is to know the type of returned object, just do:
var rs = tx.executeSql(...);
console.log(rs);
qml: [object Object]

Linq. Anonymous type error when joining to multiple tables

Im trying to return an IQueryable based on my model.
But I need to join to the same lookup table twice. Then return the query variable to the gridview.
public IQueryable<Benchmark> GetBenchMarks([QueryString("hydrant")] string hydrant,
[QueryString("revdate")] string revdate, [QueryString("street")] string street,
[QueryString("quadrant")] string quadrant, [QueryString("desc")] string desc) {
IQueryable<Benchmark> query = from p in _db.Benchmarks
join s in _db.Streets on p.Street1Number equals s.Id
join s2 in _db.Streets on p.Street2Number equals s2.Id
select new {
Street1Name = s.StreetName,
p.OrderNumber,
p.HydrantNumber,
Street2Name = s2.StreetName,
p.RevisionDate,
p.Quadrant,
p.Description,
p.Street1Number
};
}
So there is a red squiggle line on the 2nd join to s2. And the following error.
Error 5 Cannot implicitly convert type
'System.Linq.IQueryable<AnonymousType#1>' to
'System.Linq.IQueryable<Benchmarks.Model.Benchmark>'. An explicit
conversion exists (are you missing a
cast?) C:\Projects\Benchmarks\Benchmarks\Benchmarks_Home.aspx.cs 63 25 Benchmarks
Since you end your query with select new {...}, you are creating an anonymous object for each result. Instead, use select p, and each result will be a Benchmark.
However, it looks like returning a Benchmark is not what you want. In this case, you would want to change query to be of type IQueryable or IQueryable<dynamic> (and probably change the return type of the GetBenchMarks function as well, unless it does return IQueryable<Benchmark>!).
A second (potentially better) alternative would be to create a class to represent this anonymous type, and use that.
The result of your query is IEnumerable of anonymous objects, thus it cannot be converted to Benchmark.
If you want to set some additional properties (Street1Name - that are evidently not mapped on DB) from joined relations you can do:
IQueryable<Benchmark> query = from p in _db.Benchmarks
join s in _db.Streets on p.Street1Number equals s.Id
join s2 in _db.Streets on p.Street2Number equals s2.Id
select new {
....
};
var ex = query.ToList();
var result = new List<Benchmark>();
foreach(bn in ex){
result.Add(new Benchmark{ OrderNumber = bn.OrderNumber .... });
}
// return result.AsQueryable();
// but now it losts the point to return it as queryable, because the query was already executed so I would simply reurn that list
return result;
Another option is to make new class representing the object from the query and return it from the method like:
... select new LoadedBenchmark { Street1Name = s.StreetName ....}

Resources