You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import PocketBase from "pocketbase";
const pb = new PocketBase("http://localhost:8090");
await pb.collection("_superusers").authWithPassword('tad17@list.ru', '0987654321');
async function appendItem(table_name, item) {
//console.log("item:", item)
try {
const p = await pb.collection(table_name).create(item)
//console.log("ответ PocketBase:", p)
//console.log("запись:", p.id, p.patientName);
} catch (err) {
console.error('Ошибка:', err.message);
process.exit(1);
}
}
async function importJSON(json_name, table_name) {
const items = await Bun.file(json_name).json();
console.log(`Прочитано: ${items.length} записей`)
let appended = 0
for (const item of items) {
await appendItem(table_name, item)
appended++
}
console.log(`Загружено: ${appended} записей`)
}
async function clearCollection(name) {
let totalDeleted = 0;
let batch = 50; // размер пачки
let page = 1;
while (true) {
// получаем страницу записей
const records = await pb.collection(name).getList(page, batch, { fields: "id" });
if (records.items.length === 0) break;
for (const rec of records.items) {
await pb.collection(name).delete(rec.id);
totalDeleted++;
//process.stdout.write(`\rУдалено: ${totalDeleted}`);
}
// после удаления текущей страницы снова начинаем с 1,
// потому что нумерация сдвигается
page = 1;
}
console.log(`\nКоллекция "${name}" очищена. Всего удалено: ${totalDeleted}`);
}
async function test() {
const resultList = await pb.collection('rmias').getList(1, 50, {
filter: 'patientName ~ "К"',
});
console.log(resultList)
const record = await pb.collection('rmias').getOne('sp3ircowx2ckg63');
console.log(record)
}
//test()
//await clearCollection("rmias");
await importJSON("excel/ДВН2.json", "dvn2")
console.log("Готово")