在循环中从脚本创建表时中断循环

时间:2017-12-20 11:29:16

标签: javascript node.js loopbackjs

require("events").EventEmitter.defaultMaxListeners = 36;

const includeUpsertMeta = true;

const modelFolder = "../../common/models";
const fs = require("fs");
const fileExtension = require("file-extension");
const loopback = require("loopback");
const jsonfile = require("jsonfile");

// Get array of models from model json files
let modelNames = [];
let excludeModels = [
  "email.json",
  "web-services.json",
  "customer-optin-optout.json"
];
fs.readdirSync(modelFolder).forEach(file => {
  if (fileExtension(file) == "json" && !excludeModels.includes(file)) {
    modelNames.push(file);
  }
});

// Get json object of models
const modelconfig = require("../model-config.json");
const excludeKeys = [
  "_meta",
  "User",
  "AccessToken",
  "ACL",
  "RoleMapping",
  "Role",
  "InternalUser",
  "WebServices",
  "Email"
];

let dsPool = [];
const dataSources = require("../datasources.local.json");
const includedDataSources = ["ace", "campaign_db", "loyalty_db", "cdp_db"];
for (dataSource in dataSources) {
  if (dataSources.hasOwnProperty(dataSource)) {
    if (includedDataSources.includes(dataSource)) {
      dsPool[dataSource] = loopback.createDataSource(
        dataSources[dataSource].connector,
        dataSources[dataSource]
      );
    }
  }
}

// Loop through files
// use file.name to get the modelName.
// use modelName to get datasource name from modelconfig
// create datasource object using datasourceconfig and the datasource name
countOfModel = 0;
let modelconfigLength = Object.keys(modelconfig).length;
for (let modelObj in modelconfig) {
  countOfModel = countOfModel + 1;
  if (!excludeKeys.includes(modelObj)) {
    let dbSchema = modelconfig[modelObj]["dataSource"];
    let dataSourceConfig = dataSources[dbSchema];
    let fileName = modelObj;
    fileName = toHypenCase(fileName);
    // createTable(ds, fileName)
    createTable(dsPool[dbSchema], fileName, dbSchema);
  }
  if (modelconfigLength == countOfModel) {
    // process.exit()
  }
}

function createTable(ds, fileName, dbSchema) {
  console.log(fileName);
  let jsonFileName = modelFolder + "/" + fileName + ".json";
  let options = {
    idInjection: false
  };
  options[dataSources[dbSchema].connector] = {};
  options[dataSources[dbSchema].connector]["schema"] =
    dataSources[dbSchema].name;

  jsonfile.readFile(jsonFileName, function(err, obj) {
    options.mysql["table"] = obj.options.mysql.table;
    let properties = obj.properties;

    if (includeUpsertMeta) {
      properties["createdBy"] = { type: "String", required: false, length: 40 };
      properties["updatedBy"] = { type: "String", required: false, length: 40 };
      properties["createdOn"] = { type: "date", required: false };
      properties["updatedOn"] = { type: "date", required: false };
    }
    try {
      ds.createModel(fileName, properties, options);
      ds
        .autoupdate(fileName)
        .then(() => {
          return ds.discoverModelProperties(fileName);
        })
        .catch(err => {
          console.log(err);
        });
    } catch (err) {
      console.log(err);
    }
  });
}

function toHypenCase(input) {
  return input
    .split(/(?:([A-Z]))/)
    .reduce((a, b, i) => (i == 1 || i % 2 == 0 ? a + b : a + "-" + b))
    .toLowerCase();
}


这是我用来从环回模型在MySQL中创建表的代码。 因为我必须使用shell脚本运行这些脚本,所以我希望这个特定的代码在创建表之后中断并出来,这样我就可以运行其他脚本但是当我在代码中给process.exit()创建后创建代码时table,因为它是在创建表之前执行的异步编程process.exit。因此,当我尝试在此脚本之后执行其他shell脚本时,它不会发生

0 个答案:

没有答案