()
| 35 | } |
| 36 | |
| 37 | write() { |
| 38 | |
| 39 | if (this.options.noWrite) { |
| 40 | return Promise.resolve(); |
| 41 | } |
| 42 | |
| 43 | mkdirp.sync(path.resolve(this.options.directory || "./models")); |
| 44 | |
| 45 | const tables = _.keys(this.tableText); |
| 46 | |
| 47 | // write the individual model files |
| 48 | const promises = tables.map(t => { |
| 49 | return this.createFile(t); |
| 50 | }); |
| 51 | |
| 52 | const isTypeScript = this.options.lang === 'ts'; |
| 53 | const assoc = this.createAssociations(isTypeScript); |
| 54 | |
| 55 | // get table names without schema |
| 56 | // TODO: add schema to model and file names when schema is non-default for the dialect |
| 57 | const tableNames = tables.map(t => { |
| 58 | const [schemaName, tableName] = qNameSplit(t); |
| 59 | return tableName as string; |
| 60 | }).sort(); |
| 61 | |
| 62 | // write the init-models file |
| 63 | if (!this.options.noInitModels) { |
| 64 | const initString = this.createInitString(tableNames, assoc, this.options.lang); |
| 65 | const initFilePath = path.join(this.options.directory, "init-models" + (isTypeScript ? '.ts' : '.js')); |
| 66 | const writeFile = util.promisify(fs.writeFile); |
| 67 | const initPromise = writeFile(path.resolve(initFilePath), initString); |
| 68 | promises.push(initPromise); |
| 69 | } |
| 70 | |
| 71 | return Promise.all(promises); |
| 72 | } |
| 73 | private createInitString(tableNames: string[], assoc: string, lang?: string) { |
| 74 | switch (lang) { |
| 75 | case 'ts': |
no test coverage detected