Thai Address Custom Database
The `thai-address-custom-database` package offers a client-side database for Thai addresses, removing the need for server-side lookups. It builds upon the `Sellsuki/thai-address-database` project and specifically incorporates the database and `preprocess()` function from `jquery.Thailand.js` version 1.5.1 (commit 4e5f496). The current stable version is 1.0.2. This library is updated infrequently, primarily providing database maintenance and minor library updates. A key differentiator is its ability to generate the client-side `db.json` from an `xlsx` spreadsheet via a migration script. However, it currently only supports the `db.json` format and does not include support for `db.zip` or `geodb` variants found in some related projects. It requires Node.js version 12 or higher.
Common errors
-
Error: Cannot find module 'thai-address-database'
cause The module was imported using the name 'thai-address-database' as seen in some documentation examples, but the actual package name is 'thai-address-custom-database'.fixChange your import statement from `import { ... } from 'thai-address-database'` to `import { ... } from 'thai-address-custom-database'`. -
TypeError: searchAddressByDistrict is not a function
cause This error typically occurs when attempting to use CommonJS `require()` syntax with an ES Module, or if the named export is incorrect or not found.fixEnsure you are using ES Module `import { searchAddressByDistrict } from 'thai-address-custom-database';` in an environment configured for ESM. Verify the export name is correct. -
Error: Input file must be 'database.xlsx' in .xlsx format.
cause When running `npm run migrate`, the script expects a specific Excel file named `database.xlsx` saved in the modern Excel Workbook format.fixConfirm that the address data spreadsheet is correctly named `database.xlsx` and saved with the .xlsx extension (not .xls) before executing `npm run migrate`.
Warnings
- gotcha The README examples incorrectly show `import ... from 'thai-address-database'` instead of the actual package name `thai-address-custom-database`. Copying the README's import path will result in a 'module not found' error.
- gotcha This package is explicitly limited to `db.json` database files. It does not support `db.zip` or `geodb` formats that might be available in other related Thai address database projects.
- gotcha The database migration script (`npm run migrate`) strictly requires the input file to be named `database.xlsx` and saved in the Excel Workbook (.xlsx) format. Using an older `.xls` format or an incorrect filename will cause the migration to fail.
- gotcha The package's database and `preprocess()` function are based on `jquery.Thailand.js` version 1.5.1. Future updates or breaking changes in the original `jquery.Thailand.js` project's database schema or logic may not be automatically reflected or compatible, potentially requiring manual updates to this custom database package.
Install
-
npm install thai-address-custom-database -
yarn add thai-address-custom-database -
pnpm add thai-address-custom-database
Imports
- searchAddressByDistrict
const { searchAddressByDistrict } = require('thai-address-custom-database');import { searchAddressByDistrict } from 'thai-address-custom-database'; - preprocess
import preprocess from 'thai-address-custom-database';
import { preprocess } from 'thai-address-custom-database'; - * as ThaiAddress
import ThaiAddress from 'thai-address-custom-database';
import * as ThaiAddress from 'thai-address-custom-database';
Quickstart
import { searchAddressByDistrict } from 'thai-address-custom-database';
interface AddressResult {
district: string;
amphoe: string;
province: string;
zipcode: string;
}
function simulateAddressSearch(query: string): void {
console.log(`\n--- Searching for "${query}" ---`);
const results: AddressResult[] = searchAddressByDistrict(query);
if (results && results.length > 0) {
results.forEach(item => {
console.log(`${item.district} » ${item.amphoe} » ${item.province} » ${item.zipcode}`);
});
} else {
console.log(`No results found for "${query}".`);
}
}
// Example searches
simulateAddressSearch('บางกะปิ');
simulateAddressSearch('จตุจักร');
simulateAddressSearch('บางพลี');
simulateAddressSearch('สุขุมวิท 21'); // Should return some results if found
simulateAddressSearch('กรุงเทพ'); // Might return many results or none depending on specific search logic
simulateAddressSearch('xyz-nonexistent'); // Expected no results