UNPKG

@tachybase/plugin-field-china-region

Version:

Provides data and field type for administrative divisions of China.

88 lines (87 loc) 2.75 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var server_exports = {}; __export(server_exports, { PluginChinaRegion: () => PluginChinaRegion, default: () => server_default }); module.exports = __toCommonJS(server_exports); var import_server = require("@tego/server"); function getChinaDivisionData(key) { try { require.resolve(`../china-division/${key}.json`); return require(`../china-division/${key}.json`); } catch (error) { return require(`china-division/dist/${key}.json`); } } class PluginChinaRegion extends import_server.Plugin { async install() { await this.importData(); } async load() { this.app.acl.allow("chinaRegions", "list", "loggedIn"); this.app.resourcer.use( async (ctx, next) => { const { resourceName, actionName } = ctx.action.params; if (resourceName === "chinaRegions" && actionName !== "list") { ctx.throw(404, "Not Found"); } else { await next(); } }, { tag: "chinaRegions404" } ); } async importData() { const areas = getChinaDivisionData("areas"); const cities = getChinaDivisionData("cities"); const provinces = getChinaDivisionData("provinces"); 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 })) ); const count = await ChinaRegion.count(); } } var server_default = PluginChinaRegion; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PluginChinaRegion });