tachybase_todo/packages/plugin-china-region/src/server.ts
ChengLei Shao 81978711e4
featPlugin multiple apps (#248)
* feat: multiple apps plugin

* feat: multipleAppManager in Application

* stage

* fix: export error

* test: multiple app

* application model

* feat: create application with plugins

* load and install after sub application created

* create subApp database beforeInstall

* sub apps listen to main app start & stop events

* refactor: getPluginName as package name

* feat: load apps on mainApp starts

* fix: test

* feat: beforeGetApplication event

* fix: test

* fix: test with sqlite memory database

* test: lazyLoad application

* fix: test with sqlite memory

* chore: clone database collection & promise.all
2022-03-28 22:01:10 +08:00

72 lines
1.6 KiB
TypeScript

import { Plugin } from '@nocobase/server';
import { areas, cities, provinces } from 'china-division';
import { resolve } from 'path';
export class ChinaRegionPlugin extends Plugin {
async install() {
await this.importData();
}
async load() {
await this.db.import({
directory: resolve(__dirname, 'collections'),
});
}
async importData() {
const timer = Date.now();
const ChinaRegion = this.db.getModel('chinaRegions');
await ChinaRegion.bulkCreate(
provinces.map((item) => ({
code: item.code,
name: item.name,
level: 1,
})),
);
await ChinaRegion.bulkCreate(
cities.map((item) => ({
code: item.code,
name: item.name,
level: 2,
parentCode: item.provinceCode,
})),
);
await ChinaRegion.bulkCreate(
areas.map((item) => ({
code: item.code,
name: item.name,
level: 3,
parentCode: item.cityCode,
})),
);
// // 乡级数据 2856 条
// await ChinaRegion.bulkCreate(streets.map(item => ({
// code: item.code,
// name: item.name,
// level: 4,
// parentCode: item.areaCode
// })));
// // 村级数据 658001 条
// await ChinaRegion.bulkCreate(villages.map(item => ({
// code: item.code,
// name: item.name,
// level: 5,
// parentCode: item.streetCode
// })));
const count = await ChinaRegion.count();
console.log(`${count} rows of region data imported in ${(Date.now() - timer) / 1000}s`);
}
getName(): string {
return this.getPackageName(__dirname);
}
}
export default ChinaRegionPlugin;