Files
klz-cables.com/.pnpm-store/v10/files/d0/8e8db57e000cc1ed047ccc1584ff44feabd97b844fb03137f033ba7076ec07f027a8085dbe03517915b9e1bf35747ff7a44438d62a0311194738892661a3e8
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

76 lines
2.8 KiB
Plaintext

const setConnectionStringDatabase = ({ connectionString, database })=>{
const connectionURL = new URL(connectionString);
const newConnectionURL = new URL(connectionURL);
newConnectionURL.pathname = `/${database}`;
return newConnectionURL.toString();
};
export const createDatabase = async function(args = {}) {
// POSTGRES_URL - default Vercel env
const connectionString = this.poolOptions?.connectionString ?? process.env.POSTGRES_URL ?? process.env.DATABASE_URL;
let managementClientConfig = {};
let dbName = args.name;
const schemaName = this.schemaName || 'public';
if (connectionString) {
if (!dbName) {
dbName = new URL(connectionString).pathname.slice(1);
}
managementClientConfig.connectionString = setConnectionStringDatabase({
connectionString,
database: 'postgres'
});
} else {
if (!dbName) {
dbName = this.poolOptions.database;
}
managementClientConfig = {
...this.poolOptions,
database: 'postgres'
};
}
// import pg only when createDatabase is used
const pg = await import('pg').then((mod)=>mod.default);
const managementClient = new pg.Client(managementClientConfig);
try {
await managementClient.connect();
await managementClient.query(`CREATE DATABASE "${dbName}"`);
this.payload.logger.info(`Created database "${dbName}"`);
if (schemaName !== 'public') {
let createdDatabaseConfig = {};
if (connectionString) {
createdDatabaseConfig.connectionString = setConnectionStringDatabase({
connectionString,
database: dbName
});
} else {
createdDatabaseConfig = {
...this.poolOptions,
database: dbName
};
}
const createdDatabaseClient = new pg.Client(createdDatabaseConfig);
try {
await createdDatabaseClient.connect();
await createdDatabaseClient.query(`CREATE SCHEMA ${schemaName}`);
this.payload.logger.info(`Created schema "${dbName}.${schemaName}"`);
} catch (err) {
this.payload.logger.error({
err,
msg: `Error: failed to create schema "${dbName}.${schemaName}". Details: ${err.message}`
});
} finally{
await createdDatabaseClient.end();
}
}
return true;
} catch (err) {
this.payload.logger.error({
err,
msg: `Error: failed to create database ${dbName}. Details: ${err.message}`
});
return false;
} finally{
await managementClient.end();
}
};
//# sourceMappingURL=createDatabase.js.map