2020-12-01 20:11:39 +08:00
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
import BaseModel from './base';
|
|
|
|
|
import { TableOptions } from '@nocobase/database';
|
2020-12-08 09:59:41 +08:00
|
|
|
|
import { SaveOptions, Op } from 'sequelize';
|
2020-12-04 21:09:39 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成随机数据库表名
|
|
|
|
|
*
|
|
|
|
|
* 策略:暂时使用 3+2
|
|
|
|
|
* 1. 自增 id
|
|
|
|
|
* 2. 随机字母
|
|
|
|
|
* 3. 时间戳
|
|
|
|
|
* 4. 转拼音
|
|
|
|
|
* 5. 常见词翻译
|
|
|
|
|
*
|
|
|
|
|
* @param title 显示的名称
|
|
|
|
|
*/
|
|
|
|
|
export function generateCollectionName(title?: string): string {
|
|
|
|
|
return `t_${Date.now().toString(36)}_${Math.random().toString(36).replace('0.', '').slice(-4).padStart(4, '0')}`;
|
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
|
|
2020-12-08 09:59:41 +08:00
|
|
|
|
export interface LoadOptions {
|
|
|
|
|
reset?: boolean;
|
|
|
|
|
where?: any;
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface MigrateOptions {
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 20:11:39 +08:00
|
|
|
|
export class CollectionModel extends BaseModel {
|
|
|
|
|
|
2020-12-04 21:09:39 +08:00
|
|
|
|
generateName() {
|
|
|
|
|
this.set('name', generateCollectionName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateNameIfNull() {
|
|
|
|
|
if (!this.get('name')) {
|
|
|
|
|
this.generateName();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 20:11:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通过 name 获取 collection
|
|
|
|
|
*
|
|
|
|
|
* @param name
|
|
|
|
|
*/
|
|
|
|
|
static async findByName(name: string) {
|
|
|
|
|
return this.findOne({ where: { name } });
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 09:59:41 +08:00
|
|
|
|
/**
|
|
|
|
|
* DOTO:
|
|
|
|
|
* - database.table 初始化可能存在一些缺陷
|
|
|
|
|
* - 是否需要考虑关系数据的重载?
|
|
|
|
|
*
|
|
|
|
|
* @param opts
|
|
|
|
|
*/
|
|
|
|
|
async loadTableOptions(opts: any = {}) {
|
2020-12-01 20:11:39 +08:00
|
|
|
|
const options = await this.getOptions();
|
|
|
|
|
const prevTable = this.database.getTable(this.get('name'));
|
|
|
|
|
const prevOptions = prevTable ? prevTable.getOptions() : {};
|
|
|
|
|
// table 是初始化和重新初始化
|
2020-12-08 09:59:41 +08:00
|
|
|
|
const table = this.database.table({...prevOptions, ...options});
|
|
|
|
|
// 如果关系表未加载,一起处理
|
|
|
|
|
const associationTableNames = [];
|
|
|
|
|
for (const [key, association] of table.getAssociations()) {
|
|
|
|
|
// TODO:是否需要考虑重载的情况?(暂时是跳过处理)
|
|
|
|
|
if (!this.database.isDefined(association.options.target)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
associationTableNames.push(association.options.target);
|
|
|
|
|
}
|
|
|
|
|
if (associationTableNames.length) {
|
|
|
|
|
await CollectionModel.load({
|
|
|
|
|
where: {
|
|
|
|
|
name: {
|
|
|
|
|
[Op.in]: associationTableNames,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return table;
|
2020-12-07 17:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 迁移
|
|
|
|
|
*/
|
2020-12-08 09:59:41 +08:00
|
|
|
|
async migrate(options: MigrateOptions = {}) {
|
2020-12-07 17:21:14 +08:00
|
|
|
|
const table = await this.loadTableOptions();
|
2020-12-01 20:11:39 +08:00
|
|
|
|
return await table.sync({
|
|
|
|
|
force: false,
|
|
|
|
|
alter: {
|
|
|
|
|
drop: false,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getFieldsOptions() {
|
|
|
|
|
const fieldsOptions = [];
|
|
|
|
|
const fields = await this.getFields();
|
|
|
|
|
for (const field of fields) {
|
|
|
|
|
fieldsOptions.push(await field.getOptions());
|
2020-10-24 15:34:43 +08:00
|
|
|
|
}
|
2020-12-01 20:11:39 +08:00
|
|
|
|
return fieldsOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getOptions(): Promise<TableOptions> {
|
|
|
|
|
return {
|
|
|
|
|
...this.get('options'),
|
2020-10-24 15:34:43 +08:00
|
|
|
|
name: this.get('name'),
|
2020-12-01 20:11:39 +08:00
|
|
|
|
title: this.get('title'),
|
|
|
|
|
fields: await this.getFieldsOptions(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 09:59:41 +08:00
|
|
|
|
/**
|
|
|
|
|
* TODO:需要考虑是初次加载还是重载
|
|
|
|
|
*
|
|
|
|
|
* @param options
|
|
|
|
|
*/
|
|
|
|
|
static async load(options: LoadOptions = {}) {
|
|
|
|
|
const { reset = false, where = {} } = options;
|
|
|
|
|
const collections = await this.findAll({
|
|
|
|
|
where,
|
|
|
|
|
});
|
2020-12-07 17:21:14 +08:00
|
|
|
|
for (const collection of collections) {
|
2020-12-08 09:59:41 +08:00
|
|
|
|
await collection.loadTableOptions({
|
|
|
|
|
reset,
|
|
|
|
|
});
|
2020-12-07 17:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 20:11:39 +08:00
|
|
|
|
static async import(data: TableOptions, options: SaveOptions = {}): Promise<CollectionModel> {
|
|
|
|
|
data = _.cloneDeep(data);
|
|
|
|
|
const collection = await this.create({
|
|
|
|
|
...data,
|
|
|
|
|
}, options);
|
|
|
|
|
const items: any = {};
|
|
|
|
|
const associations = ['fields', 'tabs', 'actions', 'views'];
|
|
|
|
|
for (const key of associations) {
|
|
|
|
|
if (!Array.isArray(data[key])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
items[key] = data[key].map((item, sort) => ({
|
|
|
|
|
...item,
|
|
|
|
|
sort,
|
|
|
|
|
}));
|
|
|
|
|
}
|
2020-12-04 21:09:39 +08:00
|
|
|
|
await collection.updateAssociations(items, options);
|
2020-12-01 20:11:39 +08:00
|
|
|
|
return collection;
|
2020-10-24 15:34:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CollectionModel;
|