2022-01-19 10:02:52 +08:00
|
|
|
import { SyncOptions } from 'sequelize';
|
|
|
|
import Database, { Collection, MagicAttributeModel } from '@nocobase/database';
|
|
|
|
import { FieldModel } from './field';
|
|
|
|
|
|
|
|
interface LoadOptions {
|
|
|
|
// TODO
|
|
|
|
skipField?: boolean;
|
|
|
|
skipExist?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CollectionModel extends MagicAttributeModel {
|
|
|
|
get db(): Database {
|
|
|
|
return (<any>this.constructor).database;
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:42:25 +08:00
|
|
|
async load(loadOptions: LoadOptions = {}) {
|
2022-01-19 10:02:52 +08:00
|
|
|
const { skipExist, skipField } = loadOptions;
|
|
|
|
const name = this.get('name');
|
|
|
|
let collection: Collection;
|
|
|
|
if (this.db.hasCollection(name)) {
|
|
|
|
collection = this.db.getCollection(name);
|
|
|
|
if (skipExist) {
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
collection.updateOptions(this.get());
|
|
|
|
} else {
|
|
|
|
collection = this.db.collection(this.get());
|
|
|
|
}
|
|
|
|
if (!skipField) {
|
|
|
|
await this.loadFields();
|
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadFields() {
|
|
|
|
// @ts-ignore
|
|
|
|
const instances: FieldModel[] = await this.getFields();
|
|
|
|
for (const instance of instances) {
|
|
|
|
await instance.load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async migrate(options?: SyncOptions) {
|
|
|
|
const collection = await this.load();
|
|
|
|
await collection.sync({
|
|
|
|
force: false,
|
|
|
|
alter: {
|
|
|
|
drop: false,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|