Skip to content

Commit

Permalink
feat: support script generation with field groups
Browse files Browse the repository at this point in the history
Refs: #117
  • Loading branch information
tshemsedinov committed May 7, 2021
1 parent cd6bf0f commit a321323
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
20 changes: 18 additions & 2 deletions application/schemas/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions lib/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit a321323

Please sign in to comment.