Thai Address Database
The `thai-address-database` package offers a client-side solution for managing and searching Thai address data within JavaScript applications. Currently at version 0.0.30, it provides functions like `searchAddressByDistrict` to query a local `db.json` file, eliminating the need for a server-side component for address lookups. The library is based on the database and `preprocess()` function from `jquery.Thailand.js` version 1.5.1. While it allows for database updates via an `npm run migrate` script, which converts a `database.xlsx` file into `db.json`, it is still in early development, with noted limitations such as exclusive support for `db.json` and no current support for `db.zip` or geodatabases. Its primary differentiator is enabling offline or client-only address search capabilities for Thai addresses.
Common errors
-
Cannot find module 'thai-address-database'
cause The package has not been installed or is not correctly resolved by your module bundler/Node.js environment.fixRun `npm install thai-address-database` or `yarn add thai-address-database` in your project directory. -
searchAddressByDistrict is not a function
cause The `searchAddressByDistrict` function was imported incorrectly, often due to attempting a default import or incorrect CommonJS `require`.fixUse a named import: `import { searchAddressByDistrict } from 'thai-address-database';` -
Error: ENOENT: no such file or directory, open '.../node_modules/thai-address-database/database/db.json'
cause The `db.json` file, which contains the address data, is missing or inaccessible in the expected location.fixEnsure the package is fully installed and the database file is present. If you are developing or have manually altered the database, run `npm run migrate` to generate/restore `db.json` from `database.xlsx`.
Warnings
- gotcha The library currently only supports `db.json` for its address data. It does not support `db.zip` or `geodb` formats that might be present in related or original projects.
- gotcha To update the local address database, you must use the provided `npm run migrate` command after modifying the `database.xlsx` file. Manual manipulation of `db.json` is not recommended as it can be overwritten.
- gotcha This package is based on an older version (1.5.1) of the database and `preprocess()` function from `jquery.Thailand.js`. Be aware of potential differences in data structure or available functionality compared to newer versions of the original project.
Install
-
npm install thai-address-database -
yarn add thai-address-database -
pnpm add thai-address-database
Imports
- searchAddressByDistrict
const searchAddressByDistrict = require('thai-address-database'); import searchAddressByDistrict from 'thai-address-database';import { searchAddressByDistrict } from 'thai-address-database';
Quickstart
import { searchAddressByDistrict } from 'thai-address-database';
// In a browser environment, ensure db.json is accessible or bundled.
// For Node.js, the package typically handles database resolution.
// Simulate an input change or a search query
function performAddressSearch(query) {
const results = searchAddressByDistrict(query);
console.log(`\n--- Search results for "${query}" ---`);
if (results && results.length > 0) {
results.forEach(item => {
console.log(`${item.district} » ${item.amphoe} » ${item.province} » ${item.zipcode}`);
});
} else {
console.log("No matching addresses found.");
}
}
// Example usage:
performAddressSearch('สีลม');
performAddressSearch('ดินแดง');
performAddressSearch('ปทุมวัน');
performAddressSearch('ถนนเพชรบุรีตัดใหม่'); // A query that might not yield direct district results
performAddressSearch('NonExistentPlace');