This is how I have created my ember FIXTURE:
window.App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter;
App.Category = DS.Model.extend({
name: DS.attr(),
parent_id: DS.attr()
});
App.Category.FIXTURES = [
{
id: 1,
name: 'user1',
email: 'user1#gmail.com',
parent_id: 0
},
{
id: 2,
name: 'user2',
email: 'user2#gmail.com',
parent_id: 1
}
];
Here is a part of my ember view where 'parent-title' is a helper:
{{#each category in controller}}
<tr>
<td>{{category.name}}</td>
<td>{{parent-title category.parent_id}}</td>
<td>Edit/Delete</td>
</tr>
{{/each}}
What I want is that during listing if the parent_id is 0 it should print 'master' else the name of parent category. In my exapmle parent of user2 is id=1 show it should print 'user1'.
Below is the helper I have used:
Ember.Handlebars.helper('parent-title', function(parent_id){
if (parent_id > 0) {
var parent = category.findBy('id', parent_id);
return parent.name;
} else {
return 'master';
}
});
I know if I replace the line App.Category.FIXTURES = [ with var Category = [ I can get it done but I want ot do it with FIXTURES.
I can tell you that accessing data like that is a bad idea. If I were you, I would change parent_id to be a relationship, not an attribute (since that's really what it is). Then you can access the parent's name in templates with category.parent.name. Making it a relationship also gives you a few other luxuries.
But if you want to maintain backward compatibility, try using a computed property.
App.Category = DS.Model.extend({
name: DS.attr(),
parent_id: DS.attr(),
parent: function() {
return DS.PromiseObject.create({
promise: this.get('store').find('category', this.get('parent_id'))
});
}.property('parent_id'),
parent_name: function() {
return this.get('parent.name');
}.property('parent.name')
});
EDIT: If you want to change that into a relationship (which I think it should be), it's fairly simple. Instead of DS.attr, you use DS.belongsTo. For instances.
App.Categor = DS.Model.extend({
name: DS.attr(),
// I dropped the _id part because it's no longer an ID
parent: DS.belongsTo('category', { inverse: null })
});
This tells Ember-Data to interpret the ID you give in the parent field as another category object. So category.get('parent') will return another category object, not a number. But in your case, to make it work, you'll have to convert all of the 0 IDs to null IDs. I wasn't sure if that was possible, which is why I recommended the computed property.
EDIT: To display master in case of a null parent, use the Handlebars if expression.
{{#if parent}}
{{parent.name}}
{{else}}
master
{{/if}}
Related
I want to test auto-generated CRUD mutations created by calling makeAugmentedSchema from 'neo4j-graphql-js'. There is no problem with creating nodes but creating relationship does not work for me. Please advise on what I am doing wrong here.
Schema:
type Bio{
id: ID!
description: String
}
type Person{
id: ID!
name: String
dob: Date
gender: String
bioRelation: [Bio] #relation(name: "HAS_BIO", direction: "OUT")
}
Mutation:
I am following the Interface Mutations guidance https://grandstack.io/docs/graphql-interface-union-types to create mutation.
mutation {
p: CreatePerson(
name: "Anton",
gender: "Male") {
name
gender
id
}
b: CreateBio(
description: "I am a developer") {
description
id
}
r: AddPersonBioRelation(
from: {id: "p"},
to:{id: "b"}
){
from{
name
}
to{
description
}
}
}
It create Person and Bio nodes but no any relationship gets created between the two:
{
"data": {
"p": {
"name": "Anton",
"gender": "Male",
"id": "586b63fd-f9a5-4274-890f-26ba567c065c"
},
"b": {
"description": "I am a developer",
"id": "a46b4c22-d23b-4630-ac84-9d6248bdda89"
},
"r": null
}
}
This is how AddPersonBioRelation looks like:
Thank you.
I am new to GRANDstack, and I have also been struggling with these types of issues myself. I have typically broken this out separate mutations (in javascript) and used the return value for each as values for the next mutation. for example:
await createIncident({
variables: {
brief: values.brief,
date,
description: values.description,
recordable: values.recordable,
title: values.title
}
}).then((res) => {
addIncidentUser({
variables: {
from: user.id,
to: res.data.CreateIncident.id
}
});
});
the problem that i see in the example you've provided is that you are specifying a string value for from and to as "p" and "b" respectively and NOT the p.id and b.id return values from the parent mutations.
it's fine of me to point that out but what i can't for the LIFE of me figure out is how to properly reference p.id and b.id in the mutation itself. in other words you are trying to send
from: { id: "586b63fd-f9a5-4274-890f-26ba567c065c"}
to: { id: "a46b4c22-d23b-4630-ac84-9d6248bdda89" }
but in reality you are sending
from: { id: "p"}
to: { id: "b" }
which aren't actually references in neo4j so it fails.
if we can figure out how to properly reference p.id and b.id we should get this working.
Thank you, #dryhurst. It appears that there is no way to reference id of newly created nodes, but I found a solution by introducing temp id property. Please see the discussion of this matter and final solution on:
https://community.neo4j.com/t/auto-generated-mutation-does-not-create-relationship/21412/16.
I want to store who is the "creator" and "updater" of every record as a reference to the "User" table in my database. This applies to every table, including the "User" table itself. This is because it can happen that a record is created either by the user that is registering or by some other already existing user.
On the "User" table I have a DB trigger that fills the "creator" and "updater" fields to the same value as the newly generated "id" by default, hence I would like to use a NOT NULL constraint on my "creator" and "updater" column. Unfortunately, this results in the following error: "Circular relations detected: User -> User. To resolve this issue you need to set nullable: false somewhere in this dependency structure."
Is there a way for me to have a circular relation with a not null constraint using TypeORM?
I was actually able to make it work. I'm going to share my solution in case anyone is looking for an answer.
Before I was trying to auto generate the "created_by" column in the following way:
#ManyToOne(type => User, { onUpdate: 'CASCADE', onDelete: 'CASCADE', nullable: false })
#JoinColumn({ name: 'created_by' })
creator: User
All I had to do to make it work was to remove the nullable: false from there and move it to a new #Column field so that in total I would have:
#Column({ nullable: false })
created_by: number
#ManyToOne(type => User, { onUpdate: 'CASCADE', onDelete: 'CASCADE' })
#JoinColumn({ name: 'created_by' })
creator: User
I had a similar issue. I have a scenario where I have foreign key in the same table.
If I explain my scenario a bit more. I was working on storing conditional statements in an application.
condition#1 a > 4
condition#2 a + b < 0
Now I wanted to give user the ability to put a logical operator in b/w condition#1 and condition#2 . Let say user selected OR operator in b/w.
That would look like
(condition#1 OR condition#2)
Now comes the interesting part where I felt the need to have FK in the same. What if user wants to add another condition with OR operator before or after #1 and #2.
condition#3 OR (condition#1 OR condition#2)
So for storing these conditions I created relationships.
I stored 2 records for this scenario
1st for storing (condition#1 OR condition#2) and 2nd record for storing
condition#3 OR (condition#1 OR condition#2)
Now come to the point how I fixed it.
Previously it was something like :
export class ConditionLogicalOperator {
#PrimaryGeneratedColumn({
type: "int",
name: "ConditionLogicalOperatorID"
})
ConditionLogicalOperatorID: number;
#Column("int", {
nullable: false,
name: "LogicalOperatorID"
})
LogicalOperatorID: number;
#ManyToOne(() => ConditionLogicalOperator, (ConditionLogicalOperator: ConditionLogicalOperator) => ConditionLogicalOperator.conditionLogicalOperators)
#JoinColumn({ name: 'ConditionID1' })
conditionLogicalOperator1: ConditionLogicalOperator | null;
#ManyToOne(() => ConditionLogicalOperator, (ConditionLogicalOperator: ConditionLogicalOperator) => ConditionLogicalOperator.conditionLogicalOperators2)
#JoinColumn({ name: 'ConditionID2' })
conditionLogicalOperator2: ConditionLogicalOperator | null;
....
....
}
The trick was to add an empty object { } to Relationship. Please notice { } at the end of #ManyToOne line in each property. It worked for me.
export class ConditionLogicalOperator {
#PrimaryGeneratedColumn({
type: "int",
name: "ConditionLogicalOperatorID"
})
ConditionLogicalOperatorID: number;
#Column("int", {
nullable: false,
name: "LogicalOperatorID"
})
LogicalOperatorID: number;
#ManyToOne(() => ConditionLogicalOperator, (ConditionLogicalOperator: ConditionLogicalOperator) => ConditionLogicalOperator.conditionLogicalOperators, { })
#JoinColumn({ name: 'ConditionID1' })
conditionLogicalOperator1: ConditionLogicalOperator | null;
#ManyToOne(() => ConditionLogicalOperator, (ConditionLogicalOperator: ConditionLogicalOperator) => ConditionLogicalOperator.conditionLogicalOperators2, { })
#JoinColumn({ name: 'ConditionID2' })
conditionLogicalOperator2: ConditionLogicalOperator | null;
....
....
}
I have a simple model in Backbone.js with few configurations:
budget.Group = Backbone.Model.extend({
url: '/groups'
})
...now when I do something like this:
budget.Group.save({ id: 1, name: 'Food' });
... it attempts to PUT it to the following URL:
/groups
.. but my (Rails) app would post/put it to:
/groups/1
Do I need to configure my model to update to the correct url? Thanks
You should be doing
var budget = new budget.Group({ id: 1, name: 'Food' });
budget.save();
Backbone will do the rest for you (and use the correct URL).
As the two answers before mentioned you must instantiate your Model :
var budget = new budget.Group({ id: 1, name: 'Food' });
budget.save();
But you have also to change the url property to urlRoot
budget.Group = Backbone.Model.extend({
urlRoot: '/groups'
})
Hypothetical example to illustrate a problem I am having using angular-UI select2. Let's say I have a screen where I want to edit a "game" model. A game, among other things has players. I want to be able to set the players via a select2 drop down menu. Here's some example code:
app.js
$scope.getGamePromise().then(function(results) {
$scope.game = results;
console.log(game.players); //prints [{name:'Joe',age: 15},{name:'Sally',age:16}]
});
$scope.players = [
{
name: 'Joe',
age: 15
},
{
name: 'Fred',
age: 14
},
{
name: 'Sally',
age: 16
},
{
name: 'Lucy',
age: 13
}
]
view.html
<select ngModel="game.players" ui-select2 multiple>
<option ng-repeat="player in players" value="player">{{ player.name }}</option>
</select>
When I want to save this 'game' object, I send the game object up to the server. The server is expecting game.players to be an array of objects. However, what is being sent up is a string. I am moderately familiar with angular, and completely new to select2. How can I get my select2 to set game.players as an array of objects instead of strings?
I guess you find another solution or you don't have the problem anymore. Anyway I post it here:
Use
<input>
instead of
<select>
Example:
<input type="hidden" ui-select2="playersCfg" ng-model="players"/>
And following configuration:
$scope.playersCfg = {
multiple: true,
allowClear: true,
data: { results: Player.query(), text: 'name' },
formatResult: function(item) {
return "<div class='select2-user-result'>" + item.name + "</div>";
},
formatSelection: function(item) {
return item.name;
}
};
Player.query()
is a resource which returns a list of player containing a name (name) and an identifier (id)
Hope it would help somebody!
I am having two table one is post and other is comment. I have a page with only post list, I need to display a icon for post if it contains comments.
App.Comment = DS.Model.extend({
user_name: DS.attr('string'),
user_id: DS.attr('number'),
comment: DS.attr('string')
});
App.Post = DS.Model.extend({
user_id: DS.attr('number'),
comments: DS.hasMany('App.Comment',{embedded:true}),
This is a sample handlebar
1. testpost <show icon only if comment not empty>
To achieve this you could create a computed property which listens to your comments property if it has at least the firstObject set, this would mean that there are comments, something like this:
App.Post = DS.Model.extend({
user_id: DS.attr('number'),
comments: DS.hasMany('App.Comment',{embedded:true}),
hasComments: function() {
var commentsLength = this.get('comments.length');
return (commentsLength > 0);
}.property('comments.firstObject')
});
And then in your template you add a if helper to listen to the hasComments computed property, this will show the content between the if helper if post.hasComments returns true, this would be when (commentsLength > 0) evaluates to true:
1. testpost {{#if post.hasComments}}<show icon>{{/if}}
EDIT
See here for a working DEMO.
Hope it helps.