Handlebars lookup object using key from array? - handlebars.js

I have an object with an array and an object. I iterate the array (fields) - using it as a template to create form elements. I want to - in the process - get the corresponding object value from the object (data).
{
"fields": [
{
"name": "id",
"type": "int",
"max_length": 11,
},
{
"name": "email",
"type": "varchar",
"max_length": 191,
}
],
"data": {
"id": "4",
"email": "person#domain.com",
}
}
Something like this (mind map):
{{#each fields}}
<li>
lookup {{lookup ../data => VALUE OF CORRESPONDING KEY ("id" or "email" etc.)}}
<label for="{{name}}">{{CamelCase name}}</label>
{{InputType name type}}
</li>
{{/each}}
So, when the field name is 'id' I'd like to grab the value of 'id' from the object etc.
I can't seem to wrap my head around the lookup ... or are there some other more clever and direct way of achieving this?

I think you summarized the desired logic very well when you said "when the field name is 'id' I'd like to grab the value of 'id' from the object".
In other words, you want to perform a lookup on the data Object and the value of the name variable is the key to be looked up.
The lookup would be simply: {{lookup ../data name}}. name will evaluate to the desired key to be looked-up on the context object ../data.

Thanks, ended up doing this (passing the data to the helper):
{{#each fields}}
<li>
<label for="{{name}}">{{CamelCase name}}</label>
{{InputType name type ../data }}
</li>
{{/each}}
And then in the helper extracted the value by doing:
Handlebars.registerHelper("InputType", function ( name, type, value) {
switch ( type ) { …
… value="' + value[name] + '">');

Related

Api-platform how to use assertJsonContains with dynamic data in test class?

Sometime, the json response has some properties that have "dynamic data", like id, createdAt below:
$this->assertJsonContains(
[
"data" => [
"id" => "#integer",
"type" => "resource-category",
"attributes" => [
"name" => "Office",
"slug" => "office",
"createdAt => "#string"
]
]
]
);
So I would like to expect these field same the type like integer, string,... How could I do that?
API-Platform will generate a schema for your operations, i.e. describe what the output should look like, which fields it should have and what the expected types are. You can store this schema and then test against it using something like justinrainbow/json-schema, which should come with api-platform anyway. Here is the basic example from the docs, which you can use for your test:
<?php
$data = json_decode(file_get_contents('data.json'));
// Validate
$validator = new JsonSchema\Validator;
$validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
self::assertIsTrue($validator->isValid());
The thing you need to look out for, is that you can’t use the automatically generated schema directly. Your test will always fit the output as the schema is generated from it.

Cloud Firestore : Query nested map of arrays

My firestore document structure looks like this
doc1
{
name: 'Test',
categories: [
{
allNames: ['name1', 'name2', 'name3],
id: 'abcd'
},
{
allNames: ['name4', 'name5'],
id: 'xyz'
},
]
doc2
{
name: 'Test2',
categories: [
{
allNames: ['name7'],
id: 'abcd3'
},
{
allNames: ['name4', 'name5'],
id: 'xyz'
},
]
I am using the JS SDK and wanted to query all document with a certain category(id or name). So if I query with categories.id equal to xyz or categories.allNames contains name1, both the above documents should be returned.
Are both these queries possible or do I need to remodel my data ?
Thank you !
You cannot query arrays that way. If you know the exact object then you could use array-contains as shown below:
firestore.collection('col').where('categories', 'array-contains', {id: 'abc', allNames: ['name1']})
One solution would be to store another array which contains category IDs only like this:
{
name: 'Test1',
categories: [...],
categoryIds: ['abcd', 'xyz']
}
Now you can use arrayContains even if you know the category ID only and not the whole object in categories array. The same method can be used for allNames.
Categories can also be made a sub-collection where each object in categories array would be a document. Then you can use Collection Group queries to find category documents with that ID or allNames and find parent document ID using the DocumentReference.

Symfony API Platform Filter

I have entity who have attributes column which is #ORM\Column(type="json", nullable=true)
And each entity have multiple keys/values in this attributes column
"attributes": {
"name": "Item 1",
"slug": "item-1",
"term": "item-1"
}
Is it possible to create a filter to search slug or some other key in attributes column?
I know the api platform have #ApiFilter(SearchFilter::class, properties={"attributes": "ipartial"}) and this kind of filter in url looks like /api/items.json?attributes[]=item-1.
If posible to set key via url like: ?attributes[slug]=item-1
Or there is another way to do it?

Show data in meteor-template from array in

I have restaurants in a MongoDB with this structure
"_id" : "R68ZkDqdfj7Qsc9Kx",
"clubname" : "Italiano",
"description" : "Best italian food in town.",
"capacity" : "100",
"createdAt" : ISODate("2016-04-13T16:20:20.683Z"),
"visitors" : [
{
"persons" : 0
}
],
"createdBy" : "mFWrAd3SdyP4dRgEW"
I have no problems retrieving the name, description, capacity... data, but I can't figure out how to read the value 0 in the array under visitors.
In MongoDB I can do:
db.clubs.findOne({_id: "R68ZkDqdfj7Qsc9Kx"}).visitors[0].persons
But how do I do something similar in the Meteor template?
{{visitors}} returns [object Object]
I have also tried
{{#with visitors}}
visitors is an array, so you you'll probably want to iterate over it and show your data in a list. Here's an example:
<ul>
{{#each visitors}}
<li>Persons: {{persons}}</li>
{{/each}}
</ul>

Define Multiple-Columns as PK in YDN-DB

When define a schema with ydn-db I can define a single column as PK with this code:
var products = {
name: 'products',
keyPath: 'id_product',
autoIncrement: true,
indexes: [
{keyPath: 'id_product'},
{keyPath: 'id_supplier'}
]
};
var schema = {
stores: [products]
};
How I can define a Store (table) with one PK with two columns or more? Thanks
How I can define a Store (table) with one PK with two columns or more?
I am not sure answering your question. IndexedDB (ynd-db is very IDB centric) can have only one PK. But primary key can be compound key compose of multiple keyPath (column) using array keypath. It is define like this
var products = {
name: 'products',
keyPath: ['id_category', id_product']
};

Resources