#[Help required] Logging guidance and multi-tenant database connection setup.

15 messages · Page 1 of 1 (latest)

opaque depot
#

I searched already asked questions and couldn't find any answers to this.

  1. Logging:

I want to log messages from hooks to see if it works as expected. I used the payload.logger.info in one of the hooks but it doesn't work. I am using the multi-tenant template. https://github.com/payloadcms/payload/tree/1.x/examples/multi-tenant

I tried to log in the src/fields/tenant/index.ts in the beforeChange hook like the following payload.logger.info('req.user>>', req.user) but it is not logged to the terminal. What am I missing here?

  1. Changing db connection on the fly.

How do I use the payload.db.create or payload.db.drizzle object to change a db connection based on the tenantId. For ex: if TenantId is 1, I want to open a connection to shard-1. The db I am using is postgres. I just want some examples on how to change connections to db on-the-fly programatically.

GitHub

The best way to build a modern backend + admin UI. No black magic, all TypeScript, and fully open-source, Payload is both an app framework and a headless CMS. - payloadcms/payload

opaque hareBOT
opaque depot
#

Any help is apprreciated

#

[Help required] Logging guidance and multi-tenant database connection setup.

manic schooner
#

you can use classic console.log to log to the server console

#

And you can access the payload property inside a hook either by importing payload, or accessing the { req } object from the beforeChange hook props.

example;

export const revalidateDocument: AfterChangeHook = ({
    doc,
    req,
    collection,
    operation,
}) => {
    if (operation === 'update' && doc._status === 'published') {
        const url = formatUrl({ doc });

        const revalidate = async (): Promise<void> => {
            try {
                const res = await fetch(
                    `${process.env.PAYLOAD_PUBLIC_WEBSITE_URL}/api/revalidate?secret=${process.env.REVALIDATION_KEY}&collection=${collection.slug}&slug=${doc.slug}&path=${url}`
                );

                if (res.ok) {
                    req.payload.logger.info(
                        `Successfully hit revalidate route for ${url}`
                    );
                } else {
                    req.payload.logger.error(`Error revalidate path ${url}`);
                }
            } catch (err) {
                req.payload.logger.error(`Error hitting revalidate route for ${url}`);
            }
        };

        revalidate();
    }

    return doc;
};

#

Keep in mind that hooks are server-side operations, so logging will not show anything in the browser terminal, only your nodejs terminal

opaque depot
#

Hey @manic schooner Thanks for the quick reply! I tried console.log in the hook but it didn't show up in the node terminal. I will give the req.payload option a try. Also, do you have any advice on the second point I have mentioned above?

manic schooner
opaque depot
manic schooner
# opaque depot Hey <@191776538205618177> Thanks for the quick reply! I tried console.log in th...

And I'm not sure about changing the current DB on the fly. I would really recommend against doing this as it will result in long times between connections.

You could try using payload.db, that interfaces directly with the underlying database. But I have a feeling that something will break if you attempt to open and close a connection inside of a hook.

Usually in a multi-tenant application, you either share a database but partition data by using some kind of static key to denote the tenant for each record. Or you create seperate databases and seperate applications per tenant, and connect to them when the application first starts

manic schooner
opaque depot
#

Before

manic schooner
#

it sounds more like the hook isn't firing, rather than console.log not working

#

beforeChange runs when you press the "save" button