Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

create db model script more informative #46

Merged
merged 1 commit into from
Mar 30, 2020
Merged
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
44 changes: 43 additions & 1 deletion scripts/create-update-tables.js
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
require('../models');
/*
* Copyright (c) 2017 TopCoder, Inc. All rights reserved.
*/
'use strict';

/**
* Script to sync DB model to dynamodb service.
* @author TCSCODER
* @version 1.1
*/

(async () => {
try {
console.log('Syncing database tables and indexes...');
const models = require('../models');
for (const key in models) {
await pingTable(models[key]);
}
console.log('Remote tables is up to date.');
} catch (e) {
console.error(e);
}
})();

/**
* Check if the table is exist
* @param {Model} model the db model
* @returns {Promise} the promise object
*/
async function pingTable(model) {
return new Promise((resolve, reject) => {
if (!(model.scan && typeof model.scan == 'function' && model.scan({}).exec)) {
return resolve();
}
model.scan({id: 0}).exec((err, result) => {
if (err) {
console.log(`Table is not exists ${err}`);
return reject(err);
}
return resolve();
});
});
}