Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an error when creating a structure file #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/dbms.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ const getPreviousVersion = async (modelPath) => {
const create = async (modelPath, outputPath = modelPath) => {
console.log('Generating SQL DDL script ' + shorten(outputPath));
const model = await loadModel(modelPath);
const delayedKeys = {};
for (const key of model.order) {
delayedKeys[key] = null;
}
const script = [];
const ins = [];
const upd = [];
const { createEntity, registerEntity } = dbms[model.database.driver];
for (const name of model.order) {
const entity = model.entities.get(name);
if (metaschema.KIND_STORED.includes(entity.kind)) {
script.push(createEntity(model, name), '');
script.push(createEntity(model, name, delayedKeys), '');
if (model.entities.get('Identifier')) {
const { inserts, updates } = registerEntity(model, name);
ins.push(inserts);
upd.push(updates);
}
}
delete delayedKeys[name];
}
if (ins.length) script.push(...ins, '', ...upd, '');
const dbPath = path.join(outputPath, 'database.sql');
Expand Down
10 changes: 8 additions & 2 deletions lib/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const flatFields = (fields) => {
return flat;
};

const createEntity = (model, name) => {
const createEntity = (model, name, delayedKeys = {}) => {
const entity = model.entities.get(name);
const sql = [];
const idx = [];
Expand Down Expand Up @@ -183,7 +183,12 @@ const createEntity = (model, name) => {
const ref = model.entities.get(def.type);
if (!ref) throw new Error(`Unknown schema: ${def.type}`);
const refId = ref.kind === 'registry';
idx.push(foreignKey(name, field, def, refId));
const fKey = foreignKey(name, field, def, refId);
if (def.type in delayedKeys && def.type !== name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite without in

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delayedKeys is an object with fields filled with nulls, so reading the property via [ key ] will not work. Therefore, I can change it to hasOwnProperty or Object.prototype.hasOwnProperty.call. Or suggest something else? Because hasOwnProperty is almost 100 times slower, although it is not critical here, but this is system code - that's why I used 'in'. Write what to put.
in dbms.js

for (const key of model.order) {
    delayedKeys[key] = null;
  }

delayedKeys[def.type] = fKey;
} else {
idx.push(fKey);
}
}
}
}
Expand All @@ -201,6 +206,7 @@ const createEntity = (model, name) => {
}
sql[sql.length - 1] = sql[sql.length - 1].slice(0, -1);
sql.push(');');
if (delayedKeys[name]) idx.push(delayedKeys[name]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use intermediate variable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can:

const delayedKeyCurrent = delayedKeys[name];
  if (delayedKeyCurrent) idx.push(delayedKeyCurrent);

return sql.join('\n') + '\n\n' + idx.join('\n');
};

Expand Down