Is it possible to query on a embedded document? - eve

Using the example of the tutorial (http://python-eve.org/features#embedded-resource-serialization):
DOMAIN = {
'emails': {
'schema': {
'author': {
'type': 'objectid',
'data_relation': {
'resource': 'users',
'field': '_id',
'embeddable': True
},
},
'subject': {'type': 'string'},
'body': {'type': 'string'},
}
}
Is it possible to query the emails of the author.name "Nicola Iarocci" for example? I tried
/emails?where={"author.name":"Nikola Iarocci"}&embbeded={"author":1}
but it doesn't work.
It works if the document is embedded, but not if it's declared as embeddable.

That is not possible as it is not supported by MongoDB itself. You might want to consider the Aggregation Framework, which is supported by Eve 0.7 (in development, but you can install it).

Related

faust Record does not deserialize avro Union type correctly

I'm using faust-streaming and python-schema-registry-client to serialize faust.Record classes to avro.
When I try to deserialize a Union of two complex types however faust cannot reconstruct the correct records and only presents a dictionary instead of a faust.Record class.
I have the following code and schema:
schema_union = schema.AvroSchema({
'type': 'record',
'name': 'UnionRecord',
'fields': [
{
'name': 'entity',
'type': [
'null',
{
'name': 'company',
'type': 'record',
'fields': [
{'name': 'name_company', 'type': 'string'},
]
},
{
'name': 'person',
'type': 'record',
'fields': [
{'name': 'name_person', 'type': 'string'},
]
}
]
}
]
})
client = SchemaRegistryClient(url='http://localhost:8081')
faust_serializer_union = FaustSerializer(client, 'schema-union', schema_union)
class Company(faust.Record):
name_company: str
class Person(faust.Record):
name_person: str
class UnionRecord(
faust.Record,
validation=True,
serializer=faust_serializer_union
):
entity: Optional[Union[Company, Person]] # Union is typing.Union
record = UnionRecord(entity=Company(name_company='name'))
If I now try to serialize record and then deserialize it:
out = record.dumps() # == b'\x00\x00\x00\x00\x13\x02\x08name'
UnionRecord.loads(out)
I get this:
<UnionRecord: entity={'name_company': 'name'}>
Whereas I expect to get this:
<UnionNoneRecord: entity=<Company: name_company='name'>>
If I remove the Union type and alter the schema so that I can have a faust.Record that has only this field: Optional[Company], I do get the correct deserialization.

Breeze URL generation for REST API and navigation properties

Ive been trying to use breeze with third party RESTful API - the API expects parameters of two types - deep linking like - localhost/request/5 for single entities (ie. request with id = 5) and parametrised queries using JSON encoded in URL (transformed by breeze with no problem).
The main problem is to make breeze to create request with URL such as:
localhost/request/{id}
not JSON encoded GET, while using breeze query "withParameters( {workorderid: id})"
And the second part of problem is using syntax like:
var query = breeze.EntityQuery
.from('request')
.withParameters(parameters)
.expand('requestDetails');
To query for two entities - main - request - and secondary - requestDetails (with deffered on access querying for value of the secondary object).
The result should be like on this image:
http://postimg.org/image/prurk75ol/
My model is defined by hand with two entities:
metadataStore.addEntityType({
shortName: "request",
namespace: "servicedesk",
dataProperties: {
workorderid: {
dataType: DT.Identity,
isPartOfKey: true
},
ignorerequest: {
dataType: DT.Boolean
}
},
navigationProperties: {
requestDetails: {
entityTypeName: "requestDetails:#servicedesk",
isScalar: true,
associationName: "request_requestDetails",
foreignKeyNames: ["workorderid"]
}
}
});
metadataStore.addEntityType({
shortName: "requestDetails",
namespace: "servicedesk",
dataProperties: {
workorderid: {
dataType: DT.Identity,
isPartOfKey: true
},
group: {
dataType: DT.String
},
description: {
dataType: DT.String
}
},
navigationProperties: {
request: {
entityTypeName: "request:#servicedesk",
isScalar: true,
associationName: "requestDetails_request",
foreignKeyNames: ["workorderid"]
}
}
Ive found example of this: https://github.com/Breeze/breeze.js.labs/blob/master/breeze.ajaxrestinterceptor.js , it looks like i can change url generation by intercepting ajax calls, can this be done for angular.breeze adapter?
I don't fully understand your question. What query are you having trouble with? Please edit your answer so we can help.
BUT I do see what looks like a metadata problem with your definition of the request type's requestDetails navigation property. Both the property name and the semantics suggest this should return a collection but you've defined it as a scalar.
requestDetails: {
entityTypeName: "requestDetails:#servicedesk",
isScalar: true, // <-- HUH?
associationName: "request_requestDetails",
foreignKeyNames: ["workorderid"]
}
I think you want isScalar: false,

Requesting list of embedded objects

I have items endpoint which contains a list of embedded images. The scheme looks like:
_schema = {
'name': required_string, # group name
'description': {
'type': 'string',
'maxlength': 140,
},
'images': {
'type': 'list',
'scheme': {
'type': 'objectid',
'data_relation': {
'resource': 'images',
'embeddable': True,
'field': '_id',
}
},
}
}
So I'm trying to make a request to the items endpoint to get embedded objects
/items/549ae47f4fb9041305403292?embedded={"images":1}
But instead of embedded images I receive just the regular object with the list of images _ids.
Here is an example of object:
{
"_updated": "Wed, 24 Dec 2014 16:06:23 GMT",
"name": "New Item",
"images": [
"549ae47f4fb904130540328b",
"549ae47f4fb904130540328e",
"549ae47f4fb9041305403291"
],
"_created": "Wed, 24 Dec 2014 16:06:23 GMT",
"_id": "549ae47f4fb9041305403292",
"_etag": "949e3b731823bb2c08682ba4b6696b86856ef941",
"description": "The best item ever"
}
I tried to convert images ids in list to objectids, but it doesn't help. Any ideas why it doesn't work? Thanks
You have an incorrect schema definition. Replace scheme with schema when defining the images list:
_schema = {
'name': required_string, # group name
'description': {
'type': 'string',
'maxlength': 140,
},
'images': {
'type': 'list',
'schema': { # this was 'scheme' in your def
'type': 'objectid',
'data_relation': {
'resource': 'images',
'embeddable': True,
'field': '_id',
}
},
}
}
It will then properly embed your list of images.

Breeze How to write entity related queries against local cache

I am trying to figure out how to write a query that will filter my related entities using the executeQueryLocally.
Here is my example:
var queryStageConfigs = breeze.EntityQuery
.from("Stages")
.where("StageConfig.ConfigValue", '==', 'Green')
.expand("StageConfig");
var stageConfigs = self.manager.executeQueryLocally(queryStageConfigs);
This the error I am getting back:
Error: unable to locate property: ConfigValue on entityType: Stages:#
MetadataStore
//Stages Entity
metadataStore.addEntityType({
shortName: "Stages",
//namespace: "MonitoringCenter",
dataProperties: {
id: { dataType: DT.Int64, isPartOfKey: true },
institutionId: { dataType: DT.Int64 },
qualifiedName: { dataType: DT.String },
displayName: { dataType: DT.String },
displayOrder: { dataType: DT.Int64 },
},
navigationProperties: {
institution: {
entityTypeName: "Stages",
isScalar: true,
associationName: "Institution_Stages",
foreignKeyNames: ["institutionId"]
},
stageConfig: {
entityTypeName: "StageConfig",
isScalar: false,
associationName: "Stages_StageConfig"
}
}
});
metadataStore.setEntityTypeForResourceName("Stages", "Stages");
//StageConfig Entity
metadataStore.addEntityType({
shortName: "StageConfig",
//namespace: "MonitoringCenter",
dataProperties: {
id: { dataType: DT.Int64, isPartOfKey: true },
stageId: { dataType: DT.Int64 },
configName: { dataType: DT.String },
configValue: { dataType: DT.String },
},
navigationProperties: {
stages: {
entityTypeName: "StageConfig",
isScalar: true,
associationName: "Stages_StageConfig",
foreignKeyNames: ["stageId"]
}
}
});
metadataStore.setEntityTypeForResourceName("StageConfig", "StageConfig");
I am hand writing the JsonResultsAdpater to get the JSON data into the entities that have created using the metadataStore setting up the relationship between the entities.
When I query the Stages entities I can see the StageConfigs array and it is not empty.
Any clue on what I may be doing wrong?
Any help would be greatly appreciated!
Ok, your example is a little confusing because I can't tell the cardinality of the relationships based on your names. In general, I think of a pluralized name like 'stages' as being a collection of 'Stage' entities (i.e. isScalar = false). But in your case the 'stages' property is actually scalar and its inverse, the 'stageConfig' property, a singularized name, is actually non-scalar. If this assumption is correct then the reason that your query will not work is because the stage.stageConfig property needs to be a scalar in order for your query to work, unless you want to use the 'any/all' operators. i.e. something like
var queryStageConfigs = breeze.EntityQuery
.from("Stages")
.where("StageConfig", "any", "ConfigValue", "==", 'Green');
.expand("StageConfig");
( and shouldn't the query be called 'queryStages' because you are querying and returning 'stages')
Also, just a nit, but its probably a good idea to keep all of your entityType names singular and your resource names plural. This is not a requirement, but it is certainly confusing the way you have one entityType singularized ( StageConfig) and another pluralized (Stages).
I don't see any mention in your code about using the camelCase naming convention, but the example seems to indicate that you are assuming that this is set.

Can I export translations of place names from freebase.com

So I've looked at this use of the freebase API and I was really impressed with the translations of the name that it found. IE Rome, Roma, Rom, Rzym, Rooma,로마, 罗马市. This is because I have a database of some 5000+ location names and I would very much like all French, German or Korean translations for these English names.
The problem is I spent about two hours clicking around freebase, and could find no way to get a view of city/location names in a different language mapped to English. So I'd love it if someone who understands what freebase is and how it's organized could get me a link to that view which theoretically I could then export.
Also I just wanted to share this question because I'm totally impressed with freebase and think if people haven't looked at it they should.
The query
[{
limit: 100,
type: '/location/location',
name: [{
value: null,
lang: {
name: {
value: null,
lang: '/lang/en',
},
}
}],
}];
returns for every location and every language, the name of that location in that language. The results are organized by language. For example, here is a very small segment of the return value:
{
'lang': {
'name': {
'lang': '/lang/en',
'value': 'Russian'
}
},
'value': 'Сан-Франциско'
},
{
'lang': {
'name': {
'lang': '/lang/en',
'value': 'Swedish'
}
},
'value': 'San Francisco'
},
{
'lang': {
'name': {
'lang': '/lang/en',
'value': 'Portuguese'
}
},
'value': 'São Francisco (Califórnia)'
},
For a no-programming solution, copy-paste the following into an HTML file and open it with your browser:
<html><head>
<script type="text/javascript" src="http://mjtemplate.org/dist/mjt-0.6/mjt.js"></script>
</head>
<body onload="mjt.run()">
<div mjt.task="q">
mjt.freebase.MqlRead([{
limit: 10,
type: '/location/location',
name: [{
value:null,
lang:{
name:{
value:null,
lang:'/lang/en',
},
}
}],
}])
</div>
<table><tr mjt.for="topic in q.result"><td>
<table><tr mjt.for="(var rowi = 0; rowi < topic.name.length; rowi++)"
mjt.if="rowi < topic.name.length" style="padding-left:2em"><td>
<pre mjt.script="">
var name = topic.name[rowi];
</pre>
${(name.lang['q:name']||name.lang.name).value}:
</td><td>$name.value</td></tr></table></td></tr></table></body></html>
Of course, that will only include the first 10 results. Up the limit above if you want more. (By the way, not only is Freebase cool, so is this mjt templating language!)
The link you posted uses mjt, a javascript framework designed for Freebase.
The Query they use.
mjt.freebase.MqlRead([{
limit: 100,
id:qid,
/* allow fuzzy matches in the value for more results... */
/* 'q:name': {'value~=': qname, value:null, lang: '/lang/'+qlang}, */
'q:name': {value: qname, lang: '/lang/'+qlang},
type: '/common/topic',
name: [{
value:null,
lang:{
id:null,
name:{
value:null,
lang:'/lang/en',
optional:true
},
'q:name':{
value:null,
lang:'/lang/'+qlang,
optional:true
}
}
}],
article: [{id:null, limit:1}],
image: [{id:null, limit:1, optional:true}],
creator: null,
timestamp:null
}])
Where:
qlang - is your desired language to translate too.
qname - is is the location to query.
To get the link you want, you'll need the API, and you can convert the above query to a link that will return a JSON object containing the translated string.

Resources