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

94 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-11-24 23:52:52 +02:00
import { fetch } from "pouchdb-fetch";
const dbsSecurity = {
2024-12-03 13:33:37 +02:00
_users: {
2024-11-24 23:52:52 +02:00
admins: { roles: ["_admin"] },
2024-12-03 13:33:37 +02:00
members: { roles: ["_admin"] },
2024-11-24 23:52:52 +02:00
},
2024-12-03 13:33:37 +02:00
_replicator: {
2024-11-24 23:52:52 +02:00
admins: { roles: ["_admin"] },
2024-12-03 13:33:37 +02:00
members: { roles: ["_admin"] },
},
_global_changes: {
admins: { roles: ["_admin"] },
members: { roles: ["_admin"] },
2024-11-24 23:52:52 +02:00
},
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-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-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);
const headers = {
"Content-Type": "application/json",
Authorization: "Basic " + btoa(`${urlObject.username}:${urlObject.password}`),
};
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 17:32:50 +02:00
if (!responseCreateDb.ok) {
throw new Error(
`${dbName} security error ${JSON.stringify(responseCreateDb)}`,
);
}
2024-12-03 15:10:23 +02:00
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 17:32:50 +02:00
if (!responseSetSecurity.ok) {
throw new Error(
`${dbName} security error ${JSON.stringify(responseSetSecurity)}`,
);
}
2024-11-24 23:52:52 +02:00
}
};
export const down = async () => {
for (const dbName of Object.keys(dbsSecurity)) {
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-02 08:50:25 +02:00
console.assert(
(await responseDeleteDb.json()).ok,
`${dbName} create db error`,
);
2024-11-24 23:52:52 +02:00
}
};