Let a super admin read the database from the app
A new super admin settings page, /admin/settings/database, lists every table with its record count and size, and one page of the open table's records newest key first. Values are cut inside SQL (400 characters, with a flag saying so) and a record opens whole in a modal, so a table of long texts reads without shipping it all. A search matches every column's text, word by word or as a phrase, which is the one thing that also works on a virtual table. Nothing in it writes: the catalog, the records and the single record are all reads, GET only.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import grabTableRecord from "@/src/functions/backend/database/grab-table-record";
|
||||
import { DatabaseRecordRequestSchema } from "@/src/schemas/database-schema";
|
||||
import type { DatabaseRecordPayload } from "@/src/types";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Super admin only: one record of one table, in full for its modal,
|
||||
* read-only.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject<DatabaseRecordPayload>> = async ({ req, query }) => {
|
||||
try {
|
||||
if (req.method != "GET") {
|
||||
return { success: false, msg: `Method not allowed` };
|
||||
}
|
||||
|
||||
const { user } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status || !user.id) {
|
||||
return { success: false, msg: `Unauthorized`, logoutUser: true };
|
||||
}
|
||||
|
||||
if (!(await grabSuperAdminUser({ req }))) {
|
||||
return { success: false, msg: `Only a super admin can read the database.` };
|
||||
}
|
||||
|
||||
const request = parseWithSchema({
|
||||
schema: DatabaseRecordRequestSchema,
|
||||
data: query,
|
||||
});
|
||||
|
||||
return { success: true, singleRes: await grabTableRecord(request) };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import grabTableRecords from "@/src/functions/backend/database/grab-table-records";
|
||||
import { DatabaseRecordsRequestSchema } from "@/src/schemas/database-schema";
|
||||
import type { DatabaseRecordsPayload } from "@/src/types";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Super admin only: one page of one table's records, read-only. Values
|
||||
* come back already cut short; a record opened in full is
|
||||
* `api/admin/database/record`.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject<DatabaseRecordsPayload>> = async ({ req, query }) => {
|
||||
try {
|
||||
if (req.method != "GET") {
|
||||
return { success: false, msg: `Method not allowed` };
|
||||
}
|
||||
|
||||
const { user } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status || !user.id) {
|
||||
return { success: false, msg: `Unauthorized`, logoutUser: true };
|
||||
}
|
||||
|
||||
if (!(await grabSuperAdminUser({ req }))) {
|
||||
return { success: false, msg: `Only a super admin can read the database.` };
|
||||
}
|
||||
|
||||
const request = parseWithSchema({
|
||||
schema: DatabaseRecordsRequestSchema,
|
||||
data: query,
|
||||
});
|
||||
const records = await grabTableRecords(request);
|
||||
|
||||
return { success: true, singleRes: records };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user