EmberJS Data hasMany sideloading with ActiveModelSerializers - ember-data

I'm using Ember Data canary build: 1.0.0-beta.8+canary.267214b9 together with a rails back-end and ActiveModelSerializer. My configuration on ember side looks like this:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend()
App.ApplicationAdapter = DS.ActiveModelAdapter.extend
namespace: "api/v1"
App.Authentication = DS.Model.extend
provider: DS.attr('string')
user: DS.belongsTo('user')
App.User = DS.Model.extend
username: DS.attr('string')
email: DS.attr('string')
authentications: DS.hasMany('authentication')
I have working hasMany and belongsTo relation for a model that isn't side loaded. The JSON for the relation look like this:
{
objectA: {
property1: 'prop1',
property2: 'prop2',
objectB_ids: ['1','2']
}
}
At the moment I try to get a user model with multiple authentications to work. But there the authentications should be side loaded. It doesn't work for the following JSON:
JSON - not working
{
authentications: [{ id:1, provider:"google" }],
user: {
id: '1',
username: 'max',
email: 'max#examle.com',
authentication_ids:[1],
}
}
But it does work for this:
JSON - working
{
authentications: [{ id:1, provider:"google" }],
user: {
id: '1',
username: 'max',
email: 'max#examle.com',
authentications:[1],
}
}
The only useful information I found on the web is this SO question:
Serialising async hasMany relationships
Is this a bug in the DS.ActiveModelSerializer or did I miss some configuration?
EDIT 1:
In the docs of DS.ActiveModelSerializer you can find the following:
It has been designed to work out of the box with the activemodelserializers Ruby gem.
And the version with authentication_ids:[...] is the way, how the ActiveModelSerializers Ruby gem does it out of the box. So maybe it should be added?

I think you're confusing what ActiveModelSerializer does with other conventions of Ember Data. You're working second example is correct. This section describes the current expectation of JSON layout. The _ids is not present.
{
"post": {
"id": 1,
"title": "Rails is omakase",
"comments": ["1", "2"],
"user" : "dhh"
},
"comments": [{
"id": "1",
"body": "Rails is unagi"
}, {
"id": "2",
"body": "Omakase O_o"
}]
}
The ActiveModelSerializer adapter allows you to pass underscored keys in your JSON instead of camelcased keys. For example, if your user had a camelcased name:
App.User = DS.Model.extend
firstName: DS.attr()
Your JSON should look like this:
{
"user": {
"first_name": "kunerd"
}
}

Solved my issue and DS.ActiveModelSerializer works as expected and did accept _ids array for side loaded models.
My problem was, that I had overwritten my App.UserSerializer with that:
App.UserSerializer = DS.RESTSerializer.extend
# some custom logic
,but it has to be:
App.UserSerializer = App.ApplicationSerializer.extend
# some custom logic
Maybe someone has similar problems after changing from DS.RESTSerializer to DS.ActiveModelSerializer`

Related

Fastify and json schema validation

I'm trying to learn fastify throught the official documentation. I'm really intrested in the validation of an incoming post request with a json schema. Following the instructions i added to my routes:
fastify.addSchema({
$id: 'http://example.com/',
type: 'object',
properties: {
hello: { type: 'string' }
}
})
fastify.post('/', {
handler() { },
schema: {
body: {
type: 'array',
items: { $ref: 'http://example.com#/properties/hello' }
}
}
})
Now the problem is that I can not write a json that can be accepted by this schema. From my basic understanding a simple post request like the following should be accepted
[
{
"hello": "bye"
},
{
"hello": "bye bye"
}
]
However server keeps telling me that body[0] should be string. Where am I wrong?
The reference $ref: 'http://example.com#/properties/hello' points to the hello property schema value, which is { type: 'string' }.
This means the schema in fastify.post('/', { expects the body to be an array of strings.

How to make a relationship of data from user pool to aws appsync model

I am new to amplify and appsync. I am trying to create a Post model that needs to have a relationship of the user who is creating the Post. The problem is my user is from cognito user pool.
What I want is the user from cognito I don't want to create new user table on dynamo db because cognito user pool already has its information I just want to get the user info that is creating that Post if I query the post.
How should I create a relationship of this?
I create amlify api like this
? Please select from one of the below mentioned services: GraphQL
? Choose the default authorization type for the API API key
? Enter a description for the API key:
? After how many days from now the API key should expire (1-365): 7
? Do you want to configure advanced settings for the GraphQL API Yes, I want to make some additional changes.
? Configure additional auth types? Yes
? Choose the additional authorization types you want to configure for the API Amazon Cognito User Pool
Cognito UserPool configuration
Use a Cognito user pool configured as a part of this project.
? Configure conflict detection? No
Here is my current schema.grapql
type Post
#model
#versioned
#aws_cognito_user_pools
#auth(rules: [{ allow: owner, queries: null }, { allow: public }]) {
id: ID!
title: String!
content: String
thumb: String
slug: String!
allow_comments: Boolean
owner: String!
post_type: String!
add_to_nav: Boolean!
version: Int!
comments: [Comment] #connection(name: "PostComments")
}
type Comment
#model
#versioned
#aws_cognito_user_pools
#auth(rules: [{ allow: owner, queries: null }]) {
id: ID!
content: String
version: Int!
post: Post #connection(name: "PostComments")
}
====================================================
EDIT: Added result of data that I want
Here is the query that I want to perform
query ListPost {
listPosts {
items {
title
content
owner{
username
id
email
first_name
last_name
}
}
}
}
}
Result that I want
{
"data": {
"listPosts": {
"items": [
{
"title": "title 1"
"content": "Test long text cotent"
"owner": {
"username": "user1"
"id": "234234234"
"email": "user1#test.com"
"first_name": "John"
"last_name": "Doe"
}
},
{
"title": "title 1"
"content": "Test long text cotent"
"owner": {
"username": "user1"
"id": "234234234"
"email": "user1#test.com"
"first_name": "John"
"last_name": "Doe"
}
},
]
}
}
}
I can't find any documentation how to build something like this.
This should help for anyone else that is looking into doing this:
https://docs.amplify.aws/cli-legacy/graphql-transformer/function/#usage
Scroll to:
Example: Get the logged in user from Amazon Cognito User Pools

How to describe response object in swagger, using yaml?

We have following json response for /papers/15
{
"data": [
{
"id": 1,
"title": "foo"
},
{
"id": 2,
"title": "bar"
}
],
"meta": {
"total": 15
}
}
Does anyone know how to describe it swagger yaml file?
Ok, I just figured out how to do this, in case somebody will need id.
Beside dedicated model definitions section ("definitions") it is possible to do inline model descriptions. Code above will looks like:
responses:
"200":
description: Matched Objects
schema:
type: object
properties:
data:
type: object
properties:
authors:
type: array
items:
$ref: "#/definitions/object_with_id_and_title"

Saving Model with hasOne association

I've got these models :
Ext.define('TestApp.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'username', type: 'string'},
],
proxy: {
type: 'localstorage',
},
},
});
Ext.define('TestApp.model.Config', {
extend: 'Ext.data.Model',
config: {
hasOne : {model: 'TestApp.model.User', name: 'user'},
proxy: {
type: 'localstorage',
},
}
});
I've tried to save a config entry like this :
var david = Ext.create('TestApp.model.User', {username: 'david'})
david.save();
var config = Ext.create('TestApp.model.Config');
config.setUser(david);
config.save();
When I restart the application, I have my user entry saved and my config entry saved but, my association is not saved:
config.getUser() -> I get undefined
According to Mitchell Simoens storing associated data isn't supported.
Unsure if it helps in your case, but an approach is sketched (that approach also seems to adress my own current (unrelated) problem).
Hope it helps

ExtJS bidirectional complex data binding Form

give a JSON data like this
{ name: { firstname: 'First Name', lastname: 'Last Name' } }
I can model use model mapping
fields: [
{Name: 'firstname', mapping: 'name.firstname'},
{Name: 'lastname', mapping: 'name.lastname'}
]
It allow us to load the data into form like this
First Name: [ First Name ]
Last Name: [ Last Name ]
However when we submit the form Ext serialize the content as
{ "firstname": "New first name", "lastname": "New last name"}
// instead of
{ "name": { "firstname": "...", "lastname": "..."} }
Is there anyway I can tell Ext to serialize the object back to the nest form, regards.
P.S: my Edit.js taking from Ext MVC application guide http://docs.sencha.com/extjs/4.2.2/#!/guide/application_architecture
You need to configure two properties in your json writer. You need to set nameProperty: 'mapping', and expandData: true. For example, if you are configuring this writer in the proxy in your model, it would look like this:
proxy: {
// other proxy config...
writer: {
type: 'json',
nameProperty: 'mapping',
expandData: true
}
}
nameProperty determines where the property name for each field comes from: either the name property or the mapping property. So, using your example, this will produce an object like this:
{ 'name.firstname': 'New first name', 'name.lastname': 'New last name' }
This is quite what you want yet, and that is where expandData comes in. This tells it to expand those dot-delimited properties in the example above, and create nested objects.
{ 'name': { 'firstname': 'New first name', 'lastname': 'New last name' } }
Docs:
nameProperty
expandData