2022-01-19 10:02:52 +08:00
|
|
|
import Database, { Collection, MagicAttributeModel } from '@nocobase/database';
|
2022-06-11 20:46:30 +08:00
|
|
|
import { SyncOptions, Transactionable } from 'sequelize';
|
2022-01-19 10:02:52 +08:00
|
|
|
import { FieldModel } from './field';
|
|
|
|
|
2022-01-24 14:57:04 +08:00
|
|
|
interface LoadOptions extends Transactionable {
|
2022-01-19 10:02:52 +08:00
|
|
|
// 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-24 14:57:04 +08:00
|
|
|
const { skipExist, skipField, transaction } = loadOptions;
|
2022-01-19 10:02:52 +08:00
|
|
|
const name = this.get('name');
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-19 10:02:52 +08:00
|
|
|
let collection: Collection;
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-19 10:02:52 +08:00
|
|
|
if (this.db.hasCollection(name)) {
|
|
|
|
collection = this.db.getCollection(name);
|
|
|
|
if (skipExist) {
|
|
|
|
return collection;
|
|
|
|
}
|
2022-01-24 14:41:27 +08:00
|
|
|
collection.updateOptions({
|
|
|
|
...this.get(),
|
|
|
|
fields: [],
|
|
|
|
});
|
2022-01-19 10:02:52 +08:00
|
|
|
} else {
|
2022-01-24 14:41:27 +08:00
|
|
|
collection = this.db.collection({
|
|
|
|
...this.get(),
|
|
|
|
fields: [],
|
|
|
|
});
|
2022-01-19 10:02:52 +08:00
|
|
|
}
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-01-19 10:02:52 +08:00
|
|
|
if (!skipField) {
|
2022-01-24 14:57:04 +08:00
|
|
|
await this.loadFields({ transaction });
|
2022-01-19 10:02:52 +08:00
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:57:04 +08:00
|
|
|
async loadFields(options: Transactionable = {}) {
|
2022-01-19 10:02:52 +08:00
|
|
|
// @ts-ignore
|
2022-01-24 14:57:04 +08:00
|
|
|
const instances: FieldModel[] = await this.getFields(options);
|
2022-04-19 16:35:44 +08:00
|
|
|
|
2022-01-19 10:02:52 +08:00
|
|
|
for (const instance of instances) {
|
2022-01-24 14:57:04 +08:00
|
|
|
await instance.load(options);
|
2022-01-19 10:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 20:46:30 +08:00
|
|
|
/**
|
|
|
|
* TODO: drop table from the database
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
async remove(options?: any) {
|
|
|
|
const name = this.get('name');
|
|
|
|
// delete from memory
|
|
|
|
const result = this.db.removeCollection(name);
|
|
|
|
// TODO: drop table from the database
|
|
|
|
// this.db.sequelize.getQueryInterface().dropTable(this.get('name'));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:57:04 +08:00
|
|
|
async migrate(options?: SyncOptions & Transactionable) {
|
|
|
|
const collection = await this.load({
|
2022-02-08 20:58:57 +08:00
|
|
|
transaction: options?.transaction,
|
2022-01-24 14:57:04 +08:00
|
|
|
});
|
2022-06-11 20:46:30 +08:00
|
|
|
try {
|
|
|
|
await collection.sync({
|
|
|
|
force: false,
|
|
|
|
alter: {
|
|
|
|
drop: false,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
const name = this.get('name');
|
|
|
|
this.db.removeCollection(name);
|
|
|
|
}
|
2022-01-19 10:02:52 +08:00
|
|
|
}
|
|
|
|
}
|