2022-10-06 09:21:20 +00:00
|
|
|
import lodash from 'lodash';
|
2023-02-13 13:38:47 +00:00
|
|
|
import { Model as SequelizeModel, ModelStatic } from 'sequelize';
|
2022-02-23 10:18:38 +00:00
|
|
|
import { Collection } from './collection';
|
|
|
|
import { Database } from './database';
|
2022-02-26 07:12:18 +00:00
|
|
|
import { Field } from './fields';
|
2022-11-16 04:53:58 +00:00
|
|
|
import { SyncRunner } from './sync-runner';
|
2022-02-23 10:18:38 +00:00
|
|
|
|
2022-11-20 06:40:41 +00:00
|
|
|
const _ = lodash;
|
|
|
|
|
2022-02-23 10:18:38 +00:00
|
|
|
interface IModel {
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2022-02-26 07:12:18 +00:00
|
|
|
interface JSONTransformerOptions {
|
2022-12-24 08:30:01 +00:00
|
|
|
model: ModelStatic<any>;
|
2022-02-26 07:12:18 +00:00
|
|
|
collection: Collection;
|
|
|
|
db: Database;
|
|
|
|
key?: string;
|
|
|
|
field?: Field;
|
|
|
|
}
|
|
|
|
|
2022-02-23 10:18:38 +00:00
|
|
|
export class Model<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>
|
|
|
|
extends SequelizeModel<TModelAttributes, TCreationAttributes>
|
|
|
|
implements IModel
|
|
|
|
{
|
|
|
|
public static database: Database;
|
|
|
|
public static collection: Collection;
|
|
|
|
|
2022-10-06 09:21:20 +00:00
|
|
|
[key: string]: any;
|
2023-02-13 13:38:47 +00:00
|
|
|
|
2022-10-23 10:17:59 +00:00
|
|
|
protected _changedWithAssociations = new Set();
|
|
|
|
protected _previousDataValuesWithAssociations = {};
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
public toChangedWithAssociations() {
|
|
|
|
// @ts-ignore
|
|
|
|
this._changedWithAssociations = new Set([...this._changedWithAssociations, ...this._changed]);
|
|
|
|
// @ts-ignore
|
|
|
|
this._previousDataValuesWithAssociations = this._previousDataValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
public changedWithAssociations(key?: string, value?: any) {
|
|
|
|
if (key === undefined) {
|
|
|
|
if (this._changedWithAssociations.size > 0) {
|
|
|
|
return Array.from(this._changedWithAssociations);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value === true) {
|
|
|
|
this._changedWithAssociations.add(key);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
if (value === false) {
|
|
|
|
this._changedWithAssociations.delete(key);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return this._changedWithAssociations.has(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public clearChangedWithAssociations() {
|
|
|
|
this._changedWithAssociations = new Set();
|
|
|
|
}
|
2022-10-06 09:21:20 +00:00
|
|
|
|
2022-02-26 07:12:18 +00:00
|
|
|
public toJSON<T extends TModelAttributes>(): T {
|
|
|
|
const handleObj = (obj, options: JSONTransformerOptions) => {
|
|
|
|
const handles = [
|
|
|
|
(data) => {
|
|
|
|
if (data instanceof Model) {
|
|
|
|
return data.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
this.hiddenObjKey,
|
|
|
|
];
|
|
|
|
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleArray = (arrayOfObj, options: JSONTransformerOptions) => {
|
|
|
|
const handles = [this.sortAssociations];
|
2022-04-03 11:45:59 +00:00
|
|
|
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
|
2022-02-26 07:12:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const opts = {
|
2022-12-24 08:30:01 +00:00
|
|
|
model: this.constructor as ModelStatic<any>,
|
2022-02-26 07:12:18 +00:00
|
|
|
collection: (this.constructor as any).collection,
|
|
|
|
db: (this.constructor as any).database as Database,
|
|
|
|
};
|
|
|
|
|
|
|
|
const traverseJSON = (data: T, options: JSONTransformerOptions): T => {
|
|
|
|
const { model, db, collection } = options;
|
|
|
|
// handle Object
|
|
|
|
data = handleObj(data, options);
|
|
|
|
|
|
|
|
const result = {};
|
|
|
|
for (const key of Object.keys(data)) {
|
|
|
|
// @ts-ignore
|
|
|
|
if (model.hasAlias(key)) {
|
|
|
|
const association = model.associations[key];
|
|
|
|
const opts = {
|
|
|
|
model: association.target,
|
|
|
|
collection: db.getCollection(association.target.name),
|
|
|
|
db,
|
|
|
|
key,
|
|
|
|
field: collection.getField(key),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (['HasMany', 'BelongsToMany'].includes(association.associationType)) {
|
|
|
|
result[key] = handleArray(data[key], opts).map((item) => traverseJSON(item, opts));
|
|
|
|
} else {
|
2022-04-12 09:07:13 +00:00
|
|
|
result[key] = data[key] ? traverseJSON(data[key], opts) : null;
|
2022-02-23 10:18:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-26 07:12:18 +00:00
|
|
|
result[key] = data[key];
|
2022-02-23 10:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-26 07:12:18 +00:00
|
|
|
|
|
|
|
return result as T;
|
|
|
|
};
|
|
|
|
|
|
|
|
return traverseJSON(super.toJSON(), opts);
|
2022-02-23 10:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 07:12:18 +00:00
|
|
|
private hiddenObjKey(obj, options: JSONTransformerOptions) {
|
|
|
|
const hiddenFields = Array.from(options.collection.fields.values())
|
|
|
|
.filter((field) => field.options.hidden)
|
|
|
|
.map((field) => field.options.name);
|
|
|
|
|
|
|
|
return lodash.omit(obj, hiddenFields);
|
|
|
|
}
|
|
|
|
|
|
|
|
private sortAssociations(data, { field }: JSONTransformerOptions): any {
|
|
|
|
const sortBy = field.options.sortBy;
|
|
|
|
return sortBy ? this.sortArray(data, sortBy) : data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private sortArray(data, sortBy: string | string[]) {
|
|
|
|
if (!lodash.isArray(sortBy)) {
|
|
|
|
sortBy = [sortBy];
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:02:26 +00:00
|
|
|
const orderItems = [];
|
|
|
|
const orderDirections = [];
|
|
|
|
|
|
|
|
sortBy.forEach((sortItem) => {
|
|
|
|
orderDirections.push(sortItem.startsWith('-') ? 'desc' : 'asc');
|
|
|
|
orderItems.push(sortItem.replace('-', ''));
|
2022-02-23 10:18:38 +00:00
|
|
|
});
|
2022-02-26 07:12:18 +00:00
|
|
|
|
2022-02-27 15:02:26 +00:00
|
|
|
return lodash.orderBy(data, orderItems, orderDirections);
|
2022-02-23 10:18:38 +00:00
|
|
|
}
|
2022-11-16 04:53:58 +00:00
|
|
|
|
|
|
|
static async sync(options) {
|
|
|
|
const model = this as any;
|
|
|
|
|
2023-03-01 09:55:37 +00:00
|
|
|
const _schema = model._schema;
|
|
|
|
|
|
|
|
if (_schema && _schema != 'public') {
|
|
|
|
await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_schema}";`, {
|
|
|
|
raw: true,
|
|
|
|
transaction: options?.transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:16:01 +00:00
|
|
|
// fix sequelize sync with model that not have any column
|
|
|
|
if (Object.keys(model.tableAttributes).length === 0) {
|
|
|
|
if (this.database.inDialect('sqlite', 'mysql')) {
|
2023-01-08 04:45:02 +00:00
|
|
|
console.error(`Zero-column tables aren't supported in ${this.database.sequelize.getDialect()}`);
|
|
|
|
return;
|
2022-11-24 10:16:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 02:00:46 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const queryInterface = this.sequelize.queryInterface;
|
|
|
|
|
|
|
|
if (!queryInterface.patched) {
|
|
|
|
const oldDescribeTable = queryInterface.describeTable;
|
|
|
|
queryInterface.describeTable = async function (...args) {
|
|
|
|
try {
|
|
|
|
return await oldDescribeTable.call(this, ...args);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.message.includes('No description found for')) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
queryInterface.patched = true;
|
|
|
|
}
|
2022-11-24 10:16:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 04:53:58 +00:00
|
|
|
if (this.collection.isInherited()) {
|
|
|
|
return SyncRunner.syncInheritModel(model, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SequelizeModel.sync.call(this, options);
|
|
|
|
}
|
2022-02-23 10:18:38 +00:00
|
|
|
}
|