1

I am trying to generate a schema dynamically using Spring-Boot when I make a REST API call. For instance, I make a rest call to http://localhost:8080/school with two different JSON bodies.

Call 1:

{id: 1111, name: "XYZ"}

Call 2:

/school
{id: 1234, name: "ABCD", contact: "test@test.com"}

The request body is transformed into a schema of the table or collection for each request.

table or collection name school

For Call 1

{id: Number, name: String}

I make another API call but pass different JSON body (For Call 2):

table or collection name school

{ id: Number, name: String, contact: String}

Can anyone tell me how to do this, please? I am using MySQL database on the backend.

Shidersz
  • 16,846
  • 2
  • 23
  • 48
reactdontact
  • 53
  • 1
  • 10
  • Before answering the first question that comes up is: Why? Why are you trying to do this? As a rule-of-thumb, dynamically creating database structures (DDL) is a bad idea and usually a sign that the design/architecture needs to be revisited. If you're building a multi-tenant system, it'd be worth knowing some of your requirements. In other words, my short answer/suggestion is: don't do this. – sofend Dec 13 '18 at 02:52
  • I am doing this to learn how to dynamically create schemas. – reactdontact Dec 13 '18 at 02:53
  • Please let me know if there is a way to do it using Springboot – reactdontact Dec 13 '18 at 02:55
  • If you are using Spring Data and Hibernate, here is a useful link - https://stackoverflow.com/q/42135114/9121293 – Pranjal Gore Dec 13 '18 at 11:31
  • Are you wanting different schemas dynamically or are you wanting to store records with different structure / schema? If the latter, then you probably want one of the following: a) nullable columns for the fields that are optional (for example "contact" in the example you gave); b) inheritance, which is supported by JPA mappings; c) storing raw JSON in the database, which Postgres (and others) supports; d) a "schemaless" database such as CouchDB or Mongo. – sofend Dec 14 '18 at 23:18

1 Answers1

0

You have a connection to the database. It accepts arbitrary statements.

So long as the user you're connecting with has the necessary privileges, nothing prevents you from using the connection to send DDL statements (like CREATE TABLE and similar) to the DB.

sofend
  • 647
  • 8
  • 17