sequelize-mv-support
Version:
Adds materialized views support to Sequelize.
94 lines (85 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Model = void 0;
var _sequelize = require("sequelize");
/**
* Model with view support added
*
* @export
* @class Model
* @extends {ModelOrig}
*/
class Model extends _sequelize.Model {
/** @inheritdoc */
/** @inheritdoc */
/** @inheritdoc */
static drop(options = {}) {
const method = this.options.treatAsView ? 'dropView' : this.options.treatAsMaterializedView ? 'dropMaterializedView' : 'dropTable';
return this.queryInterface[method](this.getTableName(), options);
}
/** @inheritdoc */
static sync(options) {
if (this.options.treatAsView || this.options.treatAsMaterializedView) return Promise.resolve();
return super.sync(options);
}
/**
* Executes the query to create a view
*
* @static
* @returns {Promise<[unknown[], unknown]>} Result of the create view request
* @memberof Model
*/
static syncView() {
const definition = this.getViewDefinition();
if (!definition) throw new Error('cannot sync view with no definition');
return this.queryInterface.createView(this.getTableName(), definition);
}
/**
* Executes the query to create a materialized view
*
* @static
* @returns {Promise<[unknown[], unknown]>} Result of the create materialized view request
* @memberof Model
*/
static syncMaterializedView() {
const definition = this.getMaterializedViewDefinition();
if (!definition) throw new Error('cannot sync materialised view with no definition');
return this.queryInterface.createMaterializedView(this.getTableName(), definition);
}
/**
* Gets the sql definition for this view
*
* @static
* @returns {string} SQL query string to create a view
* @memberof Model
*/
static getViewDefinition() {
return this.options.viewDefinition;
}
/**
* Gets the sql definition for this materialized view
*
* @static
* @returns {string} SQL query string to create the materialized view
* @memberof Model
*/
static getMaterializedViewDefinition() {
return this.options.materializedViewDefinition;
}
/**
* Refreshes the materialized view in the database
*
* @static
* @returns {Promise<[unknown[], unknown]>}
* @memberof Model
*/
static refreshMaterializedView() {
return this.queryInterface.refreshMaterializedView(this.getTableName());
}
}
exports.Model = Model;
var _default = Model;
exports.default = _default;
//# sourceMappingURL=ModelWithViews.js.map