53 lines
2.2 KiB
JavaScript
53 lines
2.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.count = count;
|
|
exports.upsertMany = upsertMany;
|
|
exports.listForRegistration = listForRegistration;
|
|
exports.listAll = listAll;
|
|
exports.findByCode = findByCode;
|
|
exports.setRegistrationAllowed = setRegistrationAllowed;
|
|
exports.isRegistrationAllowed = isRegistrationAllowed;
|
|
const connection_1 = require("../db/connection");
|
|
async function count() {
|
|
const rows = await (0, connection_1.query)('SELECT COUNT(*)::text AS count FROM countries');
|
|
return Number(rows[0]?.count ?? 0);
|
|
}
|
|
async function upsertMany(items) {
|
|
const chunkSize = 100;
|
|
let saved = 0;
|
|
for (let i = 0; i < items.length; i += chunkSize) {
|
|
const chunk = items.slice(i, i + chunkSize);
|
|
const codes = chunk.map((item) => item.code.toUpperCase());
|
|
const names = chunk.map((item) => item.name);
|
|
await (0, connection_1.query)(`INSERT INTO countries (code, name, synced_at, updated_at)
|
|
SELECT u.code, u.name, NOW(), NOW()
|
|
FROM UNNEST($1::text[], $2::text[]) AS u(code, name)
|
|
ON CONFLICT (code) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
synced_at = NOW(),
|
|
updated_at = NOW()`, [codes, names]);
|
|
saved += chunk.length;
|
|
}
|
|
return saved;
|
|
}
|
|
async function listForRegistration() {
|
|
return (0, connection_1.query)(`SELECT code, name FROM countries WHERE registration_allowed = TRUE ORDER BY name ASC`);
|
|
}
|
|
async function listAll() {
|
|
return (0, connection_1.query)(`SELECT code, name, registration_allowed, synced_at, updated_at
|
|
FROM countries ORDER BY name ASC`);
|
|
}
|
|
async function findByCode(code) {
|
|
const rows = await (0, connection_1.query)('SELECT code, name, registration_allowed, synced_at, updated_at FROM countries WHERE code = $1', [code.toUpperCase()]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function setRegistrationAllowed(code, allowed) {
|
|
await (0, connection_1.query)('UPDATE countries SET registration_allowed = $1, updated_at = NOW() WHERE code = $2', [allowed, code.toUpperCase()]);
|
|
}
|
|
async function isRegistrationAllowed(code) {
|
|
const row = await findByCode(code);
|
|
if (!row)
|
|
return false;
|
|
return row.registration_allowed;
|
|
}
|
|
//# sourceMappingURL=countryRepository.js.map
|