diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index d17c500fa3..ce619e9136 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -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`); }); diff --git a/packages/database/src/database.ts b/packages/database/src/database.ts index b3beccefd3..986d72b8b2 100644 --- a/packages/database/src/database.ts +++ b/packages/database/src/database.ts @@ -138,6 +138,10 @@ export class Database extends EventEmitter implements AsyncEmitter { } } + getModel(name: string) { + return this.getCollection(name).model as ModelCtor; + } + getRepository(name: string): R; getRepository(name: string, relationId: string | number): R; diff --git a/packages/plugin-china-region/src/collections/china_regions.ts b/packages/plugin-china-region/src/collections/chinaRegions.ts similarity index 62% rename from packages/plugin-china-region/src/collections/china_regions.ts rename to packages/plugin-china-region/src/collections/chinaRegions.ts index 87d4528b66..14c40cbe77 100644 --- a/packages/plugin-china-region/src/collections/china_regions.ts +++ b/packages/plugin-china-region/src/collections/chinaRegions.ts @@ -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; +}); diff --git a/packages/plugin-china-region/src/server.ts b/packages/plugin-china-region/src/server.ts index 7fcef871a0..e022503491 100644 --- a/packages/plugin-china-region/src/server.ts +++ b/packages/plugin-china-region/src/server.ts @@ -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;