feat(plugin-china-region): import data with sqlite

This commit is contained in:
chenos 2022-04-26 00:19:31 +08:00
parent 219dbee467
commit 3fb66d345f

View File

@ -1,5 +1,5 @@
import { Database } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import { areas, cities, provinces } from 'china-division';
import { resolve } from 'path';
export class ChinaRegionPlugin extends Plugin {
@ -18,6 +18,15 @@ export class ChinaRegionPlugin extends Plugin {
const timer = Date.now();
const ChinaRegion = this.db.getModel('chinaRegions');
const db = new Database({
dialect: 'sqlite',
storage: resolve(process.cwd(), 'node_modules/china-division/dist/data.sqlite'),
});
const [provinces] = await db.sequelize.query('SELECT `code`, `name` FROM `province`') as any;
const [cities] = await db.sequelize.query('SELECT `code`, `name`, `provinceCode` FROM `city`') as any;
const [areas] = await db.sequelize.query('SELECT `code`, `name`, `cityCode` FROM `area`') as any;
await ChinaRegion.bulkCreate(
provinces.map((item) => ({
code: item.code,