Is it possible to map a list of fields in a Play/Scala form - scala

Given some object that has the ability to query its fields, is it possible to iterate over the fields to generate a list of mappings and to use the list of mappings to create a form?
For example:
class SomeModel ...
val model = new SomeModel
val mappings = model.fields { f =>
f.fieldName -> text // Just use String/text mappings to simplify the example
}
// Assuming 'of' is the correct mapping to use, What should be
// specified for <?1>, <?2> and <?3>
val form = Form(
of( <?1> , <?2> ) {
<?3>
}
)
Background:
I need to build a basic crud interface for our data model which consists of a 100+ tables, some with up to a 100 columns. Refactoring the data model is not an option since this crud interface is just one of many other users of the data. We generate the model classes so it is possible to also generate the form classes but I am hoping that I can get around the 18 / 22 field limit by using a list of fields instead.

Related

Alternatives for withFilterExpression for supporting composite key

I'm trying to query dynamoDB through withFilterExpression. I get an error as the argument is a composite key
Filter Expression can only contain non-primary key attributes: Primary key attribute: question_id
and also as it uses OR operator in the query and it cannot be passed to withKeyConditionExpression.
The query that was passed to withFilterExpression is similar to this question_id = 1 OR question_id = 2. The entire code is like follows
def getQuestionItems(conceptCode : String) = {
val qIds = List("1","2","3")
val hash_map : java.util.Map[String, Object] = new java.util.HashMap[String, Object]()
var queries = ArrayBuffer[String]()
hash_map.put(":c_id", conceptCode)
for ((qId, index) <- qIds.zipWithIndex) {
val placeholder = ":qId" + index
hash_map.put(placeholder, qId)
queries += "question_id = " + placeholder
}
val query = queries.mkString(" or ")
val querySpec = new QuerySpec()
.withKeyConditionExpression("concept_id = :c_id")
.withFilterExpression(query)
.withValueMap(hash_map)
questionsTable.query(querySpec)
}
Apart from withFilterExpression and withConditionExpression is there any other methods that I can use which is a part of QuerySpec ?
Let's raise things up a level. With a Query (as opposed to a GetItem or Scan) you provide a single PK value and optionally an SK condition. That's what a Query requires. You can't provide multiple PK values. If you want multiple PK values, you can do multiple Query calls. Or possibly you may consider a Scan across all PK values.
You can also consider having a GSI that presents the data in a format more suitable to efficient lookup.
Side note: With PartiQL you can actually specify multiple PK values, up to a limit. So if you really truly want this, that's a possibility. The downside is it raises things up to a new level of abstraction and can make inefficiencies hard to spot.

EF Core - projections as string

I would like to create an API which lets query all fields and relations' fields (transitively) of an entity.
Let's see through an example, using from EF-demo Students-Courses-Ennrollment data model: https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro
The following HTTP request would request 3 fields (I hope the syntax is understandable):
GET /students?$select=FirstName,Ennrollments(12345).Grade,Ennrollments(67890).Course.Title
Then the corresponding query would be something like:
context.Students.Select(s => new
{
s.FirstName,
s.Ennrollments.First(e => e.EnnrollmentId == 12345).Grade,
s.Ennrollments.First(e => e.EnnrollmentId == 67890).Course.Title
});
So I'd dynamically map string field "references" to projections/EF expressions. Is this something possible to achieve with EF Core? For example using Metadata?

How to implement Polymorphic objects in CouchDB / NoSQL?

I'd like to implement Polymorphic objects in a NoSQL / Document DB?
What is best practice?
Example:
Master Class
Item Object (All should have Item.Title, Item.Subtitle, Item.IconURL)
SubClasses: ItemPhoto, ItemPDF, ItemURL, ItemHTML
(Each subclass would have different properties)
I'd like to list all Items Generically - then get specific data when i drill down.
Possible Options:
Save a two different Documents -with Master/Child Type & ID
Save all as SubClass Documents with Internal Item Object
Other options??
Thanks
CouchDB stores documents (data), not classes (data with code). There's the code in map, validation, list, and show functions which handle documents, but those documents are plain objects that carry data only.
In your example, you can define a library function to check that a given document contains the data of an item, and then use this function to decide what to do. For example:
// in a "appTypes" library:
exports.isItem = function(doc) {
return doc.Title && doc.Subtitle && doc.IconURL;
}
// in a map function
function(doc) {
var appTypes = require('appTypes');
if (appTypes.isItem(doc)) {
// doc is an Item...
}
}
Obviously you can put all code belonging to an Item in an Item class and create instances of that class initialized with the data in the doc. But that's your choice, and does not change how CouchDB will handle the document.

Having a list of strings represented in a database using ORMLite

First of I am new to ORMLite. I would like my model class to have a field which is a list of strings, that would eventually hold a list of tags for my model object.
Which ORMLite annotations should I use?
Firstly I don't want to have a table of all tags, and then use the #ForeignCollectionField.
Also I thought of using the #DatabaseField(dataType=DataType.SERIALIZABLE) annotation, but it turns out that List<String> doesn't implement the Serializable interface.
What are your suggestions?
First of all, List doesn't implement Serializable but ArrayList certainly does as well as most of the common collection implementations. But storing a huge list is probably not the best of doing this from a pure object model standpoint.
So why don't you want to have a table of all tags? That's the best way from a pure model standpoint. It will require a 2nd query if you need them every time. That's the way hibernate would store a list or array of tags.
After reading your comment #creen, I still think you do want a table of tags. Your model class would then have:
#ForeignCollectionField
Collection<Tag> tags;
The tags table would not have a single tag named "red" with multiple model classes referring to it but multiple "red" entries. It would look like:
model_id name
1 "red"
1 "blue"
2 "red"
3 "blue"
3 "black"
Whenever you are removing the model object, you would first do a tags.clear(); which would remove all of the tags associated with that model from the tags table. You would not have to do any extra cleanup or anything.
No need to go for #ForeignCollectionField for simple String Array
Change your code
#DatabaseField(dataType=DataType.SERIALIZABLE)
List<String> users;
to
#DatabaseField(dataType = DataType.SERIALIZABLE)
String[] users;
Database doesn't want to store dynamically grow able arrays. That is the reason it allows only static array like string[] and not List.
I added two properties... one that gets written to the database as a csv string and the other that translates this:
[Ignore]
public List<string> Roles
{
get
{
return new List<string>(RolesCsv.Split(new char[] { ',' }));
}
set
{
RolesCsv = string.Join(",", value);
}
}
public string RolesCsv { get; set; }

EF4 inheritance and Stored procedures

I implemented inheritance with a discriminator field so all my records are in the same table. My basetype is Person (also the name of the table) and Driver and Passenger inherit from it. I receive instances of the correct type (Driver and Passenger) when I perform a query on the object context to Person. example:
var q = from d in ctx.Person
select d;
But I also create a function that calls a stored procedure and mapped the output of the function to the type Person. But now I get a list of Person and not Drivers or Passengers when I execute this method.
Anybody an idea how to solve this or is this a bug in EF4?
AFAIK, you can't use discriminator mapping (e.g TPH) when dealing with stored procedure mappings.
The stored procedure must be mapped to a complex type or custom entity (e.g POCO), the mapping cannot be conditional.
What you could do is map it to a regular POCO, but then project that result set into the relevant derived type (manual discrimination).
E.g:
public ICollection<Person> GetPeople()
{
var results = ExecuteFunction<Person>(); // result is ObjectResult<Person>
ICollection<Person> people = new List<Person>();
foreach (var result in results)
{
if (result.FieldWhichIsYourDiscriminator == discriminatorForDriver)
{
people.Add((Driver)result);
}
// other discriminators
}
}
If your always expecting a collection of one type (e.g only Drivers), then you wouldn't need the foreach loop, you could just add the range. The above is in case you are expecting a mixed bag of different people types.
Would be interested to see other answers, and if there is a better way though - but the above should work.