import { CREATE as MSSQL_CREATE } from '../../providers/mssql/queries/tax.js'; import { CREATE as SQLITE_CREATE } from '../../providers/sqlite3/queries/tax.js'; import makeRunner from '../runner.js'; export default function create(provider) { const sql = provider.dialect === 'sqlite3' ? SQLITE_CREATE : MSSQL_CREATE; /** * Creates a new tax rate. * * @param {number} rate - The tax rate in percent (e.g. `19`). * @param {object} [tx] - Optional transaction to run within. * @returns {Promise} The id of the created tax rate. * * @example * const id = await store.tax.create(19); * // => 1 */ return async (rate, tx) => { if (typeof rate !== 'number' || !Number.isFinite(rate) || rate < 0) { throw new Error('Invalid tax rate'); } const result = await makeRunner(provider, tx).query(sql, { rate }); return result.rows[0]?.id ?? result.insertId; }; }