Files
hereconnect/services/db-migrations/migrations/1732483677017-dbs.js
T

113 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-11-24 23:52:52 +02:00
import { fetch } from "pouchdb-fetch";
const dbsSecurity = {
2024-12-03 18:25:24 +02:00
_users: null,
_replicator: null,
_global_changes: null,
2024-12-03 13:33:37 +02:00
2024-11-24 23:52:52 +02:00
server_settings: {
admins: { roles: ["_admin"] },
2024-12-02 08:50:25 +02:00
members: { roles: ["_admin"] },
},
2024-12-03 13:33:37 +02:00
web_push_subscriptions: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
messages: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
2024-12-06 23:00:31 +02:00
chats: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
2024-12-02 08:50:25 +02:00
qr: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
public_settings: {
admins: { roles: ["_admin"] },
members: { roles: [] },
},
user_chat_settings: {
admins: { roles: ["_admin"] },
members: { roles: [] },
2024-11-24 23:52:52 +02:00
},
2024-12-15 03:14:39 +02:00
tg_bot_updates: {
2024-12-10 13:21:38 +02:00
admins: { roles: ["_admin"] },
2024-12-12 13:30:28 +02:00
members: { roles: ["_admin"] },
},
tg_bot_sessions: {
admins: { roles: ["_admin"] },
members: { roles: ["_admin"] },
2024-12-10 13:21:38 +02:00
},
2024-11-24 23:52:52 +02:00
};
2024-12-03 17:32:50 +02:00
if (!process.env.API_URL) {
throw new Error("Environment variable API_URL is not set");
}
const urlObject = new URL(process.env.API_URL);
2024-12-06 11:15:32 +02:00
const auth = `${urlObject.username}:${urlObject.password}`;
2024-12-03 17:32:50 +02:00
const headers = {
"Content-Type": "application/json",
2024-12-06 11:15:32 +02:00
Authorization: "Basic " + btoa(auth),
2024-12-03 17:32:50 +02:00
};
const getUrl = (dbUrlPath) => {
2024-12-03 17:59:10 +02:00
return `${urlObject.protocol}//${urlObject.host}${urlObject.pathname}/${dbUrlPath}${urlObject.search}${urlObject.hash}`;
2024-12-03 17:32:50 +02:00
};
2024-11-24 23:52:52 +02:00
export const up = async () => {
for (const [dbName, dbSecurity] of Object.entries(dbsSecurity)) {
2024-12-03 17:32:50 +02:00
const responseCreateDb = await fetch(getUrl(dbName), {
2024-11-24 23:52:52 +02:00
method: "PUT",
2024-12-03 17:32:50 +02:00
headers,
2024-12-03 15:10:23 +02:00
}).then((response) => response.json());
2024-12-03 18:25:24 +02:00
if (responseCreateDb.ok) {
console.log(`created ${dbName}`);
} else if (responseCreateDb.error === "file_exists") {
console.log(`exists ${dbName}`);
} else {
2024-12-03 17:32:50 +02:00
throw new Error(
2024-12-03 18:25:24 +02:00
`${dbName} create error ${JSON.stringify(responseCreateDb)}`,
2024-12-03 17:32:50 +02:00
);
}
2024-12-03 15:10:23 +02:00
2024-12-03 18:25:24 +02:00
if (!dbSecurity) {
continue;
}
2024-12-03 17:32:50 +02:00
const responseSetSecurity = await fetch(getUrl(`${dbName}/_security`), {
method: "PUT",
headers,
body: JSON.stringify(dbSecurity),
}).then((response) => response.json());
2024-12-03 15:10:23 +02:00
2024-12-03 18:25:24 +02:00
if (responseSetSecurity.ok) {
console.log(`security ${dbName}`);
} else {
2024-12-03 17:32:50 +02:00
throw new Error(
`${dbName} security error ${JSON.stringify(responseSetSecurity)}`,
);
}
2024-11-24 23:52:52 +02:00
}
};
export const down = async () => {
2024-12-03 18:25:24 +02:00
const promises = Object.keys(dbsSecurity).map(async (dbName) => {
2024-12-03 17:32:50 +02:00
const responseDeleteDb = await fetch(getUrl(dbName), {
headers,
2024-11-24 23:52:52 +02:00
method: "DELETE",
2024-12-03 18:25:24 +02:00
}).then((response) => response.json());
if (responseDeleteDb.ok) {
console.log(`deleted ${dbName}`);
} else {
2024-12-10 13:21:38 +02:00
console.error(
2024-12-03 18:25:24 +02:00
`${dbName} delete error ${JSON.stringify(responseDeleteDb)}`,
);
}
});
await Promise.allSettled(promises);
await Promise.all(promises);
2024-11-24 23:52:52 +02:00
};