AppSync: create resource with a sub type - aws-appsync

Good evening everyone,
I'm using the AWS AppSync "Create Resources" option, "Define new Type". So I wanted to add a new type.
The type I want to add has a sub type inside a list. Here is my example of schema:
type Object {
id:ID!
sub_objects: [SubObject]
}
type SubObject {
id:ID!
}
However, I'm having the following error:
You must provide exactly 1 object type definition.
So, how can I use this feature, "Create Resource" with a resource that has sub objects?
Thanks.

I think I've found it.
I've started creating the resources from the sub object up, one by one.

Related

How to use recursive_update

I just started to use DBx::Class and begun slightly to understand, but it's complex.
I want to call "recursive_update" but I was not able to manage how I can use it.
If I understand the documentation right, I have to include it in the .../My/Schema.pm which was create by DBIx::Class::Schema::Loader?
__PACKAGE__->load_namespaces(default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate');
When I want to update the data I use the ResultSet with the relationships.
my $result = $schema->resultset('Table')->find($id});
$result->recursive_update({
'rel_table' => [....),
});
unfortunately I got an error:
Can't locate object method "recursive_update" via package My::Schema::Result::Table"
Where is my fault?
recursive_update has to be called on a ResultSet object, not a Result object.
You might want to add a helper method to your Result base class (if you already have one else create it, as it makes sense for many things) which gets a ResultSet restricted to the Result object it is called on and calls recursive_update on it.

"Table name too long" crash with realm-cocoa (Realm DB)

I'm getting a "Table name too long" error after moving some code into a framework. After going through the stack trace, breaking to get the table names on schema creation, then manually trying to create the longer table names. I've determined the name that's problematic. The table name that's problematic is a linking table, is there a way in Realm to force the name of that table using className() or something else for a linking table?
Screenshot of the Realm error being thrown.
After further investigation it turns out the way to resolve this was to create a subclass of the type in my app. Due to the how my code was structured, Realm was creating the table name using the class name, package name and the generic type handed in. This made the name way too long. When you subclass the type with it's explicit generic type in the actual app, Realm no longer needs to worry about package names or the generic name. Below should help illustrate the problem and solution.
class PackageA.One<I>: RealmSwift.Object {
var List<I> = List<I>()
}
class App.Two: RealmSwift.Object {
}
let realmObjectsToRegister = [Package.One<App.Two>.self]
The above lead to Realm creating a Table name of "TtGC11PackageA9OneC9App18Two" except the real names in my app made this over 57 characters (the max table name length).
By doing the following, i shortened the name and fixed the problem
class PackageA.One<I>: RealmSwift.Object {
var List<I> = List<I>()
}
class App.Two: RealmSwift.Object {
}
class App.AppOne: PackageA.One<App.Two> {}
let realmObjectsToRegister = [App.AppOne.self]
The solution then lead to realm naming the table "AppOne" instead and fixed the long name issue.

How to use singelton (object) in Scala with parameters?

I want to build a role functionality for my application. So, I thought object would come in handy because I need a Singelton of all different roles.
Therefore, I have the following code:
trait Role {
def id: UUID
def name: String
}
object Admin extends Role {
val id = UUID.randomUUID()
val name = "admin"
}
object Pro extends Role {
val id = UUID.randomUUID()
val name = "pro"
}
However, after I persisted these roles in my database and restarted the application, I noticed that the id of the roles changed, meaning it's not the same role as I persisted them when I started the application in the first place. So, I would need to set the id if a role with the same name has already been stored in the database and set it to the Singelton object. I thought that I could use parameters to initialize the Admin and Pro object, but apparently this does not work.
How can this be done?
First, it is difficult to discuss the problem by only seeing this code, without knowing how you try to do the database persistence part.
Following your code, the id is initialised by calling randomUUID, so surely you get a new one with each start. System works as designed.
Second, I am not sure if we would agree about what a singleton is and what is the semantic of the two 'objects'.
To me it looks as if you indeed would like to have two different instances of the type Role, instead of one singleton type Admin and one different singleton type Pro, because the two differ only in the attributed values, not in structure.
A singleton object is already an object, indeed the sole object of its type. So the notion of setting its values from outside during some sort of construction, like you would do with classes during instantiation, is not really applicable here.
Take a look at the below code:
object Admin extends Role {
val id = getPersistedIDFromDatabase(name).getOrElse {
pesistID(name, UUID.randomUUID())
}
val name = "admin"
}
// getThePersistedIDFromDatabase => executes the `select` SQL query and returns an Optional ID, i.e., Some(id) if the admin already exists; Otherwise None.
Whenever you restart your application, its memory is wiped out. So it has no way to know about your previous ID.

validating that a field is unique using Bean Validation (or JSF)

I have an simple object that has a name
public class Foo {
private String name
}
Each user on the site may have up to 10 Foo's associated with them. Within this context, when a new Foo is created, I would like to validate that there isn't another foo associated with the same user that already exists.
I could Create a custom Bean Validator But annotations require the paramaeters to be defined during compilation. How would I then pass across the names of the existing Foos?
As suggested in various places, I could use EL expressions as an alternative way to pick up the data. This feels like using a sledgehammer to crack a nut. It also brings in a whole bunch of potential issues to consider least of all being ease of testing.
I could do class-wide validation using a boolean field
#AssertTrue(message="Name already exists")
public boolean isNameUnique() {
return (existingNames.contains(name));
}
But the validation message would not show up next to the name field. It is a cosmetic issue and this can be a backup plan. However, its not ideal.
Which brings me to the question:
Is there a simple way to write a Bean Validator that can check the value against a collection of values at the field level and meet the following restrictions ?
Previous values determined at runtime
Not using things like EL expressions
Field level validation instead of class level.
EDIT in reponse to Hardy:
The Foo class is an entity persisted within a database. They are picked up and used through a DAO interface.
I could loop through the entities but that means plugging the DAO into the validator and not to mention that the I would need to write the same thing again if I have another class that too has this constraint.
It would help to see how you want to use the Foo class. Can you extend your example code? Are they kept in a list of Foo instances. A custom constraint seems to be a good fit. Why do you need to pass any parameters to the constraints. I would just iterate over the foos and check whether the names are unique.

Select ObjectSet by entity type

I need a way to select objects given the name:string of the object and ObjectContext, but dont know how to do this.
I will use this to create a generic lookup dropdown editor template in ASP.MVC
So when view contains #Html.EditorFor (student=>student.School), it will show dropDown containing list of schools.
I get the target entity name from relation.ToMember, but don't know how to query data records with this input.
Currently I have added a custom method which gets string and returns innumerable and inside that I have a big switch case "School": return this.SchooleSet;
Is there a right way to do this.
I also want to add a generic method which allows me to query using syntax like ctx.Select<Teacher>().Where(...)
again here I have implemented with switch but there should be a better way to do this.
Try the CreateObjectSet method.
var q = ctx.CreateObjectSet<Teacher>().Where(...);