ADEEL FAROOQ
ADEEL FAROOQ

Reputation: 51

how to create multi dynamic table in sql using nodejs

i am creating dynamic table in sql using nodejs.just like first one will be parent table and all others will be child there no limit for child tables.

here is my json

here is my code to create table

           var sql = [
  "CREATE TABLE " + tableName + " (",
  tableName + "Id int IDENTITY(1,1) PRIMARY KEY NOT NULL,",
  "IsActive bit NULL,",
  "CreatedBy int NULL,",
  "UpdatedBy int NULL,",
  "CreatedAt datetime NULL,",
  "UpdatedAt datetime NULL,",
  parent
    .map(
      (k) =>
        k.name +
        ` int or any datatype
          ,`
    )
    .join("\n"),
  ")",
].join("\n");

this code is working fine for this json

    "Table": [
      {
    "name": "FirstName",
    "type": "nvarchar(50)"
     },
     {
    "name": "DOB",
    "type": "datetime"
   },
   {
    "name": "Age",
    "type": "nvarchar(50)"
   }]

here is a problem I want to create a child table using other arrays( MustansarAdvanceGroup, MustansarBasicGroup,....)

please guide me on what to do. what i want

CREATE TABLE Parent(
Id int IDENTITY(1,1) PRIMARY KEY NOT NULL,
IsActive bit NULL,
CreatedBy int NULL,
UpdatedBy int NULL,
CreatedAt datetime NULL,
UpdatedAt datetime NULL,
FirstName nvarchar(255) NULL,
DOB nvarchar(255) NULL,
Age nvarchar(255) NULL,
)
...no limit for child table
CREATE TABLE Child1(
Id int IDENTITY(1,1) PRIMARY KEY NOT NULL,

MustansarAdvanceGroup nvarchar(255) NULL,
DOB nvarchar(255) NULL,
Age nvarchar(255) NULL,
ParentId int null
)

CREATE TABLE Child2(
Id int IDENTITY(1,1) PRIMARY KEY NOT NULL,

MustansarBasicGroup nvarchar(255) NULL,
DOB nvarchar(255) NULL,
Age nvarchar(255) NULL,
ParentId int null
)


Upvotes: 0

Views: 544

Answers (1)

Naqsh Ali
Naqsh Ali

Reputation: 36

If you want to get dynamic keys to be iterated in a loop you can do it like this.

obj = {}
for (const [key, value] of Object.entries(obj)) {
    console.log(key,value)
}

Upvotes: 1

Related Questions