diff --git a/application/schemas/database.sql b/application/schemas/database.sql index 96a181d..4a4a826 100644 --- a/application/schemas/database.sql +++ b/application/schemas/database.sql @@ -19,11 +19,13 @@ CREATE TABLE "City" ( "countryId" bigint NOT NULL, "name" varchar NOT NULL, "location" geometry(Point, 4326), - "population" integer NOT NULL DEFAULT 0 + "population" integer NOT NULL DEFAULT 0, + "includeId" bigint NOT NULL ); ALTER TABLE "City" ADD CONSTRAINT "pkCity" PRIMARY KEY ("city"); ALTER TABLE "City" ADD CONSTRAINT "fkCityCountry" FOREIGN KEY ("countryId") REFERENCES "Country" ("countryId"); +ALTER TABLE "City" ADD CONSTRAINT "fkCityInclude" FOREIGN KEY ("includeId") REFERENCES "Changes" ("changesId"); CREATE TABLE "Address" ( "cityId" bigint NOT NULL, @@ -38,10 +40,24 @@ ALTER TABLE "Address" ADD CONSTRAINT "fkAddressCity" FOREIGN KEY ("cityId") REFE CREATE TABLE "SystemUser" ( "systemUserId" bigint generated always as identity, "login" varchar(30) NOT NULL, - "password" varchar NOT NULL + "password" varchar NOT NULL, + "givenName" varchar, + "middleName" varchar, + "surname" varchar, + "birthDate" varchar, + "birthPlace" varchar, + "countryId" bigint, + "provinceId" bigint, + "cityId" bigint, + "address1" varchar, + "address2" varchar, + "zipCode" varchar ); ALTER TABLE "SystemUser" ADD CONSTRAINT "pkSystemUser" PRIMARY KEY ("systemUser"); +ALTER TABLE "SystemUser" ADD CONSTRAINT "fkSystemUserCountry" FOREIGN KEY ("countryId") REFERENCES "Country" ("countryId"); +ALTER TABLE "SystemUser" ADD CONSTRAINT "fkSystemUserProvince" FOREIGN KEY ("provinceId") REFERENCES "Province" ("provinceId"); +ALTER TABLE "SystemUser" ADD CONSTRAINT "fkSystemUserCity" FOREIGN KEY ("cityId") REFERENCES "City" ("cityId"); CREATE TABLE "Changes" ( "changesId" bigint generated always as identity, diff --git a/lib/pg.js b/lib/pg.js index f84a0bd..df3a44c 100644 --- a/lib/pg.js +++ b/lib/pg.js @@ -109,6 +109,20 @@ const primaryKey = (entityName) => { return primaryCustom(entityName, [fieldName]); }; +const flatFields = (fields) => { + const flat = {}; + const names = Object.keys(fields); + for (const name of names) { + const value = fields[name]; + if (value.constructor.name === 'Schema') { + Object.assign(flat, value.fields); + } else { + flat[name] = value; + } + } + return flat; +}; + const createEntity = (model, name) => { const entity = model.entities.get(name); const sql = []; @@ -117,9 +131,10 @@ const createEntity = (model, name) => { const pk = toLowerCamel(name) + 'Id'; sql.push(` "${pk}" bigint generated always as identity,`); idx.push(primaryKey(name)); - const fields = Object.keys(entity.fields); + const flat = flatFields(entity.fields); + const fields = Object.keys(flat); for (const field of fields) { - const def = entity.fields[field]; + const def = flat[field]; const nullable = def.required ? ' NOT NULL' : ''; if (def.type) { const kind = isFirstUpper(def.type) ? DB_RELATION : DB_FIELD; diff --git a/package-lock.json b/package-lock.json index e542597..8514682 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1134,9 +1134,9 @@ } }, "metaschema": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/metaschema/-/metaschema-1.1.0.tgz", - "integrity": "sha512-bnwA3bzl170duvh3IjzcDPjLR790c8svPpR+5CPbGCo4rD0LN2xdfZ6pMj+eWQkld1Ozs94dC3Abfsr5+Dfmiw==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/metaschema/-/metaschema-1.1.1.tgz", + "integrity": "sha512-l4QDysOioAoAxz/g2WenYHMuoHxx5BatLKLmKdz5/EDA63ChD6nRj+5c3ixq83Pvi4MaqbBt35maz4Ogqv9LHQ==", "requires": { "metautil": "^3.5.3", "metavm": "^1.0.1" diff --git a/package.json b/package.json index 48129c3..1c60761 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ }, "homepage": "https://github.com/metarhia/metasql#readme", "dependencies": { - "metaschema": "^1.1.0", + "metaschema": "^1.1.1", "metavm": "^1.0.0", "pg": "^8.5.1" },