MCPcopy
hub / github.com/kriasoft/graphql-starter-kit / getDatabase

Function getDatabase

db/cli.ts:177–228  ·  view source on GitHub ↗

* Creates a new Knex.js database connection.

(options?: { env?: string; schema?: string })

Source from the content-addressed store, hash-verified

175 * Creates a new Knex.js database connection.
176 */
177function getDatabase(options?: { env?: string; schema?: string }) {
178 loadEnv(options?.env);
179 let connector: Connector | undefined = undefined;
180 const db = knex({
181 client: "pg",
182 async connection() {
183 if (/^\S+:\S+:\S+$/.test(process.env.PGHOST ?? "")) {
184 connector = new Connector();
185 const options = await connector.getOptions({
186 instanceConnectionName: process.env.PGHOST!,
187 ipType: IpAddressTypes.PUBLIC,
188 });
189 return {
190 ...options,
191 user: process.env.PGUSER,
192 password: process.env.PGPASSWORD,
193 database: process.env.PGDATABASE,
194 };
195 }
196 return {
197 user: process.env.PGUSER,
198 password: process.env.PGPASSWORD,
199 host: process.env.PGHOST,
200 port: parseInt(process.env.PGPORT!),
201 database: process.env.PGDATABASE,
202 };
203 },
204 migrations: {
205 directory: relative(process.cwd(), migrationsDir),
206 tableName: "migration",
207 schemaName: options?.schema,
208 extension: "ts",
209 },
210 seeds: {
211 directory: relative(process.cwd(), seedsDir),
212 extension: "ts",
213 },
214 });
215
216 const destroy = db.destroy;
217 Object.defineProperty(db, "destroy", {
218 value: async () => {
219 await destroy.call(db);
220 connector?.close();
221 },
222 writable: false,
223 enumerable: false,
224 configurable: true,
225 });
226
227 return db;
228}
229
230async function version(options?: { env?: string; schema?: string }) {
231 const db = getDatabase(options);

Callers 6

cli.tsFile · 0.85
versionFunction · 0.85
createDatabaseFunction · 0.85
interactiveFunction · 0.85
backupFunction · 0.85
restoreFunction · 0.85

Calls 1

loadEnvFunction · 0.70

Tested by

no test coverage detected