How to store JSON data using GORM to MySQL database? - mysql

I have a dynamic object I need to persist whose definition and values change according to different users. You can think of them as dynamic forms which admin first defines and later is filled up by different users.
I do not have any alternative than MySQL for my project so schema-less db is out of question.
I could have done it using normal relational model storing fields and there values on multiple table but it felt too clumsy. So I decided to store those dynamic objects as JSON on MySQL table.
Is this a good approach ? If not can you suggest any proper alternatives?
In any case I am having problem retrieving the object as JSON from the database using Grails. This is my sample class where definition is the field where JSON needs to be stored.
String subJobType
String definition
static constraints = {
definition nullable: false
}
static mapping = {
definition sqlType: 'MEDIUMTEXT',type: 'text'
}
I can only retrieve the definition field as a string but want to return a object instead.
I could first fetch all the objects and then convert all definition field of each object to JSON but that doesn't sound a good idea.

Related

Database of weakly typed objects

I want to organize some part of my system, but i can't choose convenient form for data representation for interaction with my app.
So i have some local "repository" of data object, descripted as follows:
Object1
{ id = TypeId, field1 = value1, otherObjectSpecifedField = value2 ... }
...
There are many objects (for example 1000) of many types (for example 50). Each type have it own UniqueId and his own description and set of fields.
Next thing is that for each object i have a set of filters, which corresponds that this object is actual right now. It looks like this:
Filter
{ filterName1 = filterValue, filterName2 < filterValue }
Object // filter is applied for this object
{ ... }
The process of using this "repository":
In my app i have application states, which means filters from above.
Example: application localization can be 'en' (my application knows this value and can change it on start) and we have filter, named 'localization' and in our repository we can use it like this:
Filter { localization = 'en'}
Object1 { ... } // this object i should choose when localization is en
When my app decides to check which set of obects is actual right now it cames to repository and asks it: "Here you an TypeId and please, walk through each filter+object pair and say what object is actual by filters. If you need to resolve some filter values (localization from example above) i will resolve them for you".
Then repository walks through each object and compare which is actual by filters now, and which is not and give actual to app. So he check every filter of every object and gives it only if all of them is actual and he did it in runtime.
In current implementation this set of fiters + objects is stored in xml file in very specific xml format, which is comfortably to read from app, but very hard to maintain by human. And i think that there is some place to optimization of all process. I think we can delegate of walking through Objects and comparing it filters to someone else.
Now i think in side of NoSQL document oriented databases. Because each Object has his unique structure and maybe using select routine i can choose what i need.
Maybe someone have any suggestions about that type of database organization? Maybe you know some specific data structure for that type of data?
Maybe I've missed something, because it looks to me like you have a number of different types of objects: one type per TypeId. If that's so, then I think this can be done with a standard SQL database, assuming that the fields within an object have a consistent type. If not, it can still be done with a NoSQL database.
In a SQL database, you would use a separate table for each type (since they each have their own set of fields), and search across the appropriate table with SQL. So, for example, you can create a table with two fields (I'm using SQLite here, which doesn't require types for fields):
create table Object1 (field1, otherObjectSpecifiedField);
This table can then have data inserted into it:
insert into Object1 values ("field1value", "otherfieldValue");
Filtering uses standard SQL:
select * from Object1 where field1 = "field1value";
As I mentioned, this could also be done with a NoSQL database such as MongoDB. That would look something like this in the Mongo CLI:
Create table and insert first object:
db.test.insert({ id: "TypeId", field1: "value1", otherObjectSpecifedField: "value2"});
Select an object from the table:
db.test.find({id: "TypeId", field1: "value1"});
/* { "_id" : ObjectId("57cf97060216d33b891615ba"), "id" : "TypeId", "field1" : "value1", "otherObjectSpecifedField" : "value2" } */

Save api json to database using laravel

Is it possible to get data from an api url and save it directly to database when working with laravel? the data i get from the url is of the format {"name":"100KVA SUKAM Generator","level":"5.965"}.
Yes, you can create table with json type field (or text) and keep data there:
$table->json('data_from_api');
https://laravel.com/docs/5.2/migrations#writing-migrations
If you want to persist data as usual data, you can use mass assignment. First, convert JSON to an array with [json_decode][1] and save data like that:
$data = json_decode($jsonData, true)
Model::create($data);
Don't forget to add all columns to a $fillable property of a model.

How to model data for a JSON API and a Document Database

I am making a simple REST API in front of a NoSQL database that stores records as documents similar to JSON (but not exactly the same). Each record has some fields, including id for the database, and also including some derived fields, like dateCreated.
Any time I GET anything, I want to return the objects with all the fields.
// GET /users returns an array of these in JSON
// [{id:"xxx", name:"Bobby", dateCreated:"YYYY-MM-DD"]
data User = User { id :: String, name :: String, dateCreated :: XXX }
But any time I POST or PUT anything, they client should send an object with the id field and any derived fields missing. The database is responsible to create the id when I save it, and I would create some derived fields
// POST /users would need you to post only the name.
// {name:"Henry"}
data PartialUser = PartialUser { name :: String }
If resource represents objects of type User, what should I call the thing client is sending to me? Would you make all the derived fields Maybe values? Or would you create a second object called PostedUser or something?
It can be many things:
a request body
the representation of the intended resource state of the client
a command DTO which you can send to the domain logic in order to process it by CQRS
I would make it CreateUser command, but if you don't want to use CQRS and DDD, then you would probably call it as PartialUserRepresentation, or you don't create a data structure, just use the properties to create a new User entity. Ofc. if you use entities.
So I would say it depends on the architecture of your system.

ServiceNow - JSON Web Service, display related tables

I'm working on a C# program that retrieves data from a ServiceNow database and converts that data into C# .NET objects. I'm using the JSON Web Service to return my data in JSON format.
What I want to achieve is as follows: If there is a relational mapping between a value (for
example: I have a table called Company, where CEO is not a TEXT field but an sys_id to a Employee Table) I want to be able to output that data not with an sys_id (or just displaying the name property by using the 'displayvariable' parameter) but by an object displayed in JSON.
This means that the value of a property should be an object in JSON instead of just a single value.
A few examples:
// I don't want the JSON like this
{"Company":{"CEO":"b181e841c9212c008aeb36850331fab2"}}
// Or by displaying the name of the sys_id table
{"Company":{"CEO":"James Henderson" }}
// I want the data as follows, so I can have all the data I need inside a single JSON record.
{"Company":{"CEO":{"name":"James Henderson", "age":34, "sex":"male", "office":"SBN Left Floor 23"}}}
From reading the documentation I couldn't find anything in the JSON Web Service that allowed me to display the information like this nor
find any other alternative. It should have something to do with joining the tables and displaying it all in the right format.
I have been using SNC for almost three years and have not found you can automatically join tables in a web service. Your best option would be to use a scripted web service which possibly takes a query parameter and table parameter. Then you can json serialized your result as you see fit.
Or, another option would be to generate a new processor that will traverse the GlideRecord object. The ?JSON parameter you pass in to the URL is merely a flag to pass your request to a particular processor. Unfortunately the OOB one I believe is a Java class not a JS script, so you would need to write a script much like I mentioned earlier to traverse the object path serializing the object graph as far down as your want to go.

adding auto-generated date/time stamp to a mapped collection of Strings (hibernate annotations)

I am using annotations to map a basic collection of Strings to an existing entity like this. Inside the parent entity class:
#org.hibernate.annotations.CollectionOfElements
#JoinTable (name="GoalToAchieve_entry", joinColumns=#JoinColumn(name="goalToAchieve_id"))
#org.hibernate.annotations.Sort(type = org.hibernate.annotations.SortType.NATURAL)
private SortedSet<String> entries = new TreeSet<String>();
This works fine. I have a 2-column (goalToAchieve_id and element) table resulting from the join. So I was wondering, how can I add a date/time stamp (auto-generated by MySQL) to each String of the collection. So that I can display a 3rd column with a time stamp every time a new String is added to the collection? The objective is to use a collection of simple objects (Strings) and not have to create a whole new entity (with a time/date field).
Is there a recommended way to do that? I believe even if I provide a timestamp field at the jsp interface which handles the adding of new Strings to the collection, it would still be a hibernate issue, since I need the timestamp to be persisted on the db.
Thanks in advance for any help.
Kindest regards
Hibernate #CollectionOfElements can be used with more than just simple types. You can define a class which contains two fields, your String value, plus a timestamp, and annotate that class with #Embeddable. Hibernate will then persist that to the database as two separate data columns. This class doesn't represent an Entity, just a composite value-type.
The next problem is how do you generate that timestamp. You could do it in java, with the default initialised value of the field being "new Date()" (or whatever).