diff --git a/src/auto.ts b/src/auto.ts index cd44e58c..bf79c77a 100644 --- a/src/auto.ts +++ b/src/auto.ts @@ -78,6 +78,10 @@ export class SequelizeAuto { } write(tableData: TableData) { + const write = this.options.write; + if (write) { + return write(tableData, this.options); + } const writer = new AutoWriter(tableData, this.options); return writer.write(); } diff --git a/src/types.ts b/src/types.ts index cb3d52a0..313faf63 100644 --- a/src/types.ts +++ b/src/types.ts @@ -181,6 +181,8 @@ export interface AutoOptions { username?: string; /** Whether to export views (default false) */ views?: boolean; + /** Write function to use instead of the default. */ + write?: (tableData: TableData, options: AutoOptions) => Promise; /** Primary Key Suffixes to trim (default "id") */ pkSuffixes?: string[]; /** Use `sequelize.define` instead of `init` for model initialization. See issues #527, #559, #573 */ diff --git a/test/write.test.js b/test/write.test.js new file mode 100644 index 00000000..5308f30b --- /dev/null +++ b/test/write.test.js @@ -0,0 +1,62 @@ +const fs = require('fs'); +const { describe, before, after, it } = require('mocha'); +const { expect } = require('chai'); +const { AutoRelater } = require('../lib/auto-relater'); +const { SequelizeAuto } = require('../lib/auto'); +const buildTableData = require('./tabledata').buildRelatedTableData; + +/** @type {string[]} */ +let written = []; + +/** + * @param {import("../src/types").TableData} tableData + * @param {import("../src/types").AutoOptions} options + * @returns {Promise} + */ +function write(tableData, options) { + written = Object.keys(tableData.tables); + return Promise.resolve(); +} + +describe("sequelize-auto write", function() { + let td; + before(async function() { + td = buildTableData(); + const options = { + // directory: './models', + additional: {}, + dialect: 'mysql', + lang: 'ts', + caseModel: 'p', + caseFile: 'l', + caseProp: 'c', + singularize: false, + write, + } + // we've already done the build and relate steps, so we just need to write + let auto = new SequelizeAuto(null, null, null, options); + const tt = auto.generate(td); + td.text = tt; + await auto.write(td); + return td; + }); + + after(function() { + }); + + describe("should write the data", function() { + it("has written data", function() { + let has = written.length > 0; + expect(has).to.be.true; + }); + it("has the models", function() { + const modelFiles = ['customer', 'order', 'order_item', 'other_tag', 'product', 'product_tag', 'related_product', 'supplier', 'tag']; + modelFiles.forEach(function(mf) { + let has = written.includes(mf); + expect(has).to.be.true; + }); + }); + }); + + +}); \ No newline at end of file