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

Add option for a custom write function. #656

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
/** Primary Key Suffixes to trim (default "id") */
pkSuffixes?: string[];
/** Use `sequelize.define` instead of `init` for model initialization. See issues #527, #559, #573 */
Expand Down
62 changes: 62 additions & 0 deletions test/write.test.js
Original file line number Diff line number Diff line change
@@ -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<void>}
*/
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;
});
});
});


});