feat(plugin-china-region): improve code

This commit is contained in:
chenos 2022-02-12 12:38:57 +08:00
parent 785077a6f3
commit 51ca12cc87
4 changed files with 74 additions and 64 deletions

View File

@ -64,6 +64,7 @@ const plugins = [
'@nocobase/plugin-system-settings',
'@nocobase/plugin-users',
'@nocobase/plugin-acl',
'@nocobase/plugin-china-region',
];
for (const plugin of plugins) {
@ -78,5 +79,5 @@ if (process.argv.length < 3) {
console.log(process.argv);
api.parse(process.argv).then(() => {
console.log(`Start-up time: ${(Date.now() - start) / 1000}s`);
console.log(`${new Date().toLocaleTimeString()} Start-up time: ${(Date.now() - start) / 1000}s`);
});

View File

@ -138,6 +138,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
}
}
getModel<M extends Model>(name: string) {
return this.getCollection(name).model as ModelCtor<M>;
}
getRepository<R extends Repository>(name: string): R;
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;

View File

@ -1,14 +1,16 @@
import { CollectionOptions } from '@nocobase/database';
import { defineCollection } from '@nocobase/database';
export default {
name: 'china_regions',
export default defineCollection({
name: 'chinaRegions',
title: '中国行政区划',
autoGenId: false,
fields: [
// 如使用代码作为 id 可能更节省,但由于代码数字最长为 12 字节,除非使用 bigint(64) 才够放置
{
name: 'code',
type: 'string',
unique: true,
// unique: true,
primaryKey: true,
},
{
name: 'name',
@ -17,20 +19,20 @@ export default {
{
name: 'parent',
type: 'belongsTo',
target: 'china_regions',
target: 'chinaRegions',
targetKey: 'code',
foreignKey: 'parent_code',
foreignKey: 'parentCode',
},
{
name: 'children',
type: 'hasMany',
target: 'china_regions',
target: 'chinaRegions',
sourceKey: 'code',
foreignKey: 'parent_code',
foreignKey: 'parentCode',
},
{
name: 'level',
type: 'integer',
},
],
} as CollectionOptions;
});

View File

@ -1,67 +1,70 @@
import path from 'path';
import { provinces, cities, areas, streets, villages } from 'china-division';
import { Plugin } from '@nocobase/server';
import { areas, cities, provinces } from 'china-division';
import { resolve } from 'path';
async function importData(model) {
const timer = Date.now();
export class ChinaRegionPlugin extends Plugin {
await model.bulkCreate(
provinces.map((item) => ({
code: item.code,
name: item.name,
level: 1,
})),
);
await model.bulkCreate(
cities.map((item) => ({
code: item.code,
name: item.name,
level: 2,
parent_code: item.provinceCode,
})),
);
await model.bulkCreate(
areas.map((item) => ({
code: item.code,
name: item.name,
level: 3,
parent_code: item.cityCode,
})),
);
// // 乡级数据 2856 条
// await model.bulkCreate(streets.map(item => ({
// code: item.code,
// name: item.name,
// level: 4,
// parent_code: item.areaCode
// })));
// // 村级数据 658001 条
// await model.bulkCreate(villages.map(item => ({
// code: item.code,
// name: item.name,
// level: 5,
// parent_code: item.streetCode
// })));
const count = await model.count();
console.log(`${count} rows of region data imported in ${(Date.now() - timer) / 1000}s`);
}
export default class PluginChinaRegion extends Plugin {
async beforeLoad() {
this.app.on('installing', async () => {
const ChinaRegion = this.db.getCollection('china_regions').model;
await importData(ChinaRegion);
await this.importData();
});
}
async load() {
await this.db.import({
directory: path.resolve(__dirname, 'collections'),
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`);
}
}
export default ChinaRegionPlugin;