Skip to main content
PostgreSQL stores all local application data — customers, products, payment methods, and various lookup tables. The connection is managed by a single pg.Pool instance created in src/database.js and exported as the default export db. Every controller that touches the database imports this shared pool, ensuring connection reuse across concurrent requests without manual connection management.

Connection Configuration

The pool is configured entirely through environment variables. The client_encoding is hardcoded to utf8 to guarantee correct character handling for Spanish-language strings (names, addresses, observations).
src/database.js

Environment Variables

Set DB_SSL=true when connecting to a managed PostgreSQL service such as Azure Database for PostgreSQL, which enforces SSL by default. For a local development instance, DB_SSL=false is typically sufficient.

Tables

The application reads and writes the following tables via the generic query routes (/get-data/:table, /add-data/:table, etc.):

customer

Invoice recipients. Stores identification details, names, address, and foreign keys to lookup tables (municipality_id, type_id, id_org, tribute_id).

products

Product and service catalog. Includes code_reference, name, price, tax_rate, discount_rate, unit_measure_id, standard_code_id, tribute_id, and the is_excluded flag.

payment_method

Payment method lookup table used when creating an invoice. Fields: id, name.

municipality

City/municipality lookup. Fields: id, code, name. Referenced by the customer table.

identification_document

Document type lookup (e.g., NIT, CC). Fields: id, name. Used to identify customers.

legal_organization

Legal organization type lookup (e.g., natural person, legal entity). Fields: id, name.

customer_tribute

Tax regime lookup. Fields: id, name. Maps the tax treatment applicable to a customer.

Helper Functions

src/helpers/helpers.mjs exports several utility functions used by the query controllers to build SQL statements dynamically from arbitrary request bodies, avoiding hardcoded column lists.

generate_str_of_dict(dict, key_value, restrict)

Builds a SQL fragment from a plain JavaScript object. When key_value is true it produces a SET clause for UPDATE statements (col = value, ...). When false it produces a comma-separated values list for INSERT statements. The restrict parameter names a key that should be excluded from the output (typically the primary key id).

get_keys_dict(dict, restrict)

Returns an array of the keys of an object, optionally excluding one key. Used to build the column list for INSERT and UPDATE statements.

rd_key(limit)

Generates a random alphanumeric key of limit characters by alternating uppercase ASCII letters (A–Y) and single digits. Used to produce unique reference_code values for invoices (length 10) and code_reference values for line items (length 5).

validate_body(body)

Validates the request body of a POST /factura request before it is forwarded to the Factus API. Returns a validation result object with an ok boolean and, on failure, arrays of missing_properties and require conflicts. Required fields and types: On a successful validation the function also injects two fields into the body before returning:
  • body.numbering_range_id = 8
  • body.reference_code — a 10-character random key generated by rd_key(10)
src/helpers/helpers.mjs — validate_body

Example Validation Response