@nocobase/plugin-snapshot-field 0.8.1-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/client.d.ts +4 -0
- package/client.js +30 -0
- package/lib/client/SnapshotBlock/SnapshotBlockInitializers/SnapshotBlockInitializers.d.ts +1 -0
- package/lib/client/SnapshotBlock/SnapshotBlockInitializers/SnapshotBlockInitializers.js +99 -0
- package/lib/client/SnapshotBlock/SnapshotBlockInitializers/SnapshotBlockInitializersDetailItem.d.ts +57 -0
- package/lib/client/SnapshotBlock/SnapshotBlockInitializers/SnapshotBlockInitializersDetailItem.js +190 -0
- package/lib/client/SnapshotBlock/SnapshotBlockProvider.d.ts +1 -0
- package/lib/client/SnapshotBlock/SnapshotBlockProvider.js +160 -0
- package/lib/client/SnapshotHistoryCollectionProvider.d.ts +4 -0
- package/lib/client/SnapshotHistoryCollectionProvider.js +72 -0
- package/lib/client/SnapshotRecordPicker.d.ts +1 -0
- package/lib/client/SnapshotRecordPicker.js +99 -0
- package/lib/client/index.d.ts +3 -0
- package/lib/client/index.js +71 -0
- package/lib/client/interface.d.ts +2 -0
- package/lib/client/interface.js +148 -0
- package/lib/client/locale/en-US.d.ts +10 -0
- package/lib/client/locale/en-US.js +16 -0
- package/lib/client/locale/index.d.ts +3 -0
- package/lib/client/locale/index.js +63 -0
- package/lib/client/locale/ja-JP.d.ts +2 -0
- package/lib/client/locale/ja-JP.js +8 -0
- package/lib/client/locale/ru-RU.d.ts +2 -0
- package/lib/client/locale/ru-RU.js +8 -0
- package/lib/client/locale/tr-TR.d.ts +2 -0
- package/lib/client/locale/tr-TR.js +8 -0
- package/lib/client/locale/zh-CN.d.ts +10 -0
- package/lib/client/locale/zh-CN.js +16 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +15 -0
- package/lib/server/collections/collectionsHistory.d.ts +3 -0
- package/lib/server/collections/collectionsHistory.js +52 -0
- package/lib/server/collections/fieldsHistory.d.ts +3 -0
- package/lib/server/collections/fieldsHistory.js +66 -0
- package/lib/server/fields/snapshot-field.d.ts +8 -0
- package/lib/server/fields/snapshot-field.js +35 -0
- package/lib/server/index.d.ts +1 -0
- package/lib/server/index.js +15 -0
- package/lib/server/plugin.d.ts +11 -0
- package/lib/server/plugin.js +190 -0
- package/package.json +10 -0
- package/server.d.ts +4 -0
- package/server.js +30 -0
- package/src/client/SnapshotBlock/SnapshotBlockInitializers/SnapshotBlockInitializers.tsx +78 -0
- package/src/client/SnapshotBlock/SnapshotBlockInitializers/SnapshotBlockInitializersDetailItem.tsx +121 -0
- package/src/client/SnapshotBlock/SnapshotBlockProvider.tsx +105 -0
- package/src/client/SnapshotHistoryCollectionProvider.tsx +42 -0
- package/src/client/SnapshotRecordPicker.tsx +52 -0
- package/src/client/index.tsx +50 -0
- package/src/client/interface.ts +102 -0
- package/src/client/locale/en-US.ts +10 -0
- package/src/client/locale/index.ts +24 -0
- package/src/client/locale/ja-JP.ts +1 -0
- package/src/client/locale/ru-RU.ts +1 -0
- package/src/client/locale/tr-TR.ts +1 -0
- package/src/client/locale/zh-CN.ts +9 -0
- package/src/index.ts +1 -0
- package/src/server/collections/.gitkeep +0 -0
- package/src/server/collections/collectionsHistory.ts +55 -0
- package/src/server/collections/fieldsHistory.ts +73 -0
- package/src/server/fields/snapshot-field.ts +12 -0
- package/src/server/index.ts +1 -0
- package/src/server/plugin.ts +110 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Model } from '@nocobase/database';
|
|
2
|
+
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
3
|
+
import { resolve } from 'path';
|
|
4
|
+
import { SnapshotField } from './fields/snapshot-field';
|
|
5
|
+
|
|
6
|
+
export class SnapshotFieldPlugin extends Plugin {
|
|
7
|
+
afterAdd() {}
|
|
8
|
+
|
|
9
|
+
async beforeLoad() {
|
|
10
|
+
const collectionHandler = async (model: Model, { transaction }) => {
|
|
11
|
+
const collectionDoc = model.toJSON();
|
|
12
|
+
const collectionsHistoryRepository = this.app.db.getRepository('collectionsHistory');
|
|
13
|
+
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
|
14
|
+
|
|
15
|
+
const existCollection: Model = await collectionsHistoryRepository.findOne({
|
|
16
|
+
filter: {
|
|
17
|
+
name: collectionDoc.name,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
if (existCollection) {
|
|
22
|
+
// 删除表和其关联字段
|
|
23
|
+
await existCollection.destroy({
|
|
24
|
+
transaction,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await collectionsHistoryRepository.create({
|
|
29
|
+
values: collectionDoc,
|
|
30
|
+
transaction,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await fieldsHistoryRepository.createMany({
|
|
34
|
+
records: collectionDoc.fields ?? [],
|
|
35
|
+
transaction,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
this.app.db.on('collections.afterCreateWithAssociations', collectionHandler);
|
|
40
|
+
|
|
41
|
+
const fieldHandler = async (model: Model, { transaction }) => {
|
|
42
|
+
const fieldDoc = model.get();
|
|
43
|
+
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
|
44
|
+
const existField: Model = await fieldsHistoryRepository.findOne({
|
|
45
|
+
filter: {
|
|
46
|
+
name: fieldDoc.name,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
if (existField) {
|
|
50
|
+
await existField.destroy({
|
|
51
|
+
transaction,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
await fieldsHistoryRepository.create({
|
|
55
|
+
values: fieldDoc,
|
|
56
|
+
transaction,
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
this.app.db.on('fields.afterCreateWithAssociations', fieldHandler);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async load() {
|
|
64
|
+
// 导入 collection
|
|
65
|
+
await this.db.import({
|
|
66
|
+
directory: resolve(__dirname, 'collections'),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.app.db.registerFieldTypes({
|
|
70
|
+
snapshot: SnapshotField,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
this.app.acl.allow('collectionsHistory', 'list', 'loggedIn');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 初始化安装的时候
|
|
77
|
+
async install(options?: InstallOptions) {
|
|
78
|
+
await this.app.db.sequelize.transaction(async (transaction) => {
|
|
79
|
+
const collectionsRepository = this.app.db.getRepository('collections');
|
|
80
|
+
const collectionsHistoryRepository = this.app.db.getRepository('collectionsHistory');
|
|
81
|
+
|
|
82
|
+
if ((await collectionsHistoryRepository.find()).length === 0) {
|
|
83
|
+
const collectionsModels: Model[] = await collectionsRepository.find();
|
|
84
|
+
await collectionsHistoryRepository.createMany({
|
|
85
|
+
records: collectionsModels.map((m) => m.get()),
|
|
86
|
+
transaction,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const fieldsRepository = this.app.db.getRepository('fields');
|
|
91
|
+
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
|
92
|
+
|
|
93
|
+
if ((await fieldsHistoryRepository.find()).length === 0) {
|
|
94
|
+
const fieldsModels: Model[] = await fieldsRepository.find();
|
|
95
|
+
await fieldsHistoryRepository.createMany({
|
|
96
|
+
records: fieldsModels.map((m) => m.get()),
|
|
97
|
+
transaction,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async afterEnable() {}
|
|
104
|
+
|
|
105
|
+
async afterDisable() {}
|
|
106
|
+
|
|
107
|
+
async remove() {}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default SnapshotFieldPlugin;
|