UNPKG

@t1mmen/srtd

Version:

Supabase Repeatable Template Definitions (srtd): ๐Ÿช„ Live-reloading SQL templates for Supabase DX. Make your database changes reviewable and migrations maintainable! ๐Ÿš€

55 lines โ€ข 2 kB
// src/__tests__/helpers.ts import fs from 'node:fs/promises'; import path from 'node:path'; import { TEST_FN_PREFIX } from './vitest.setup.js'; /** * Creates a test context with all the goodies we need! ๐ŸŽ */ export function createTestContext(name = 'test') { return { timestamp: Date.now(), testDir: path.join(process.env.TMPDIR || '/tmp', `srtd-${name}-${Date.now()}`), testFunctionName: `${TEST_FN_PREFIX}${Date.now()}`, templateCounter: 0, }; } /** * Get unique template names - perfect for testing! ๐Ÿท๏ธ */ export function getNextTemplateName(context, prefix = 'template') { context.templateCounter++; return `${prefix}_${context.timestamp}_${context.templateCounter}`; } /** * Create a template file with whatever content you want! ๐Ÿ“ */ export async function createTemplate(context, name, content, dir) { const fullPath = dir ? path.join(context.testDir, 'test-templates', dir, name) : path.join(context.testDir, 'test-templates', name); try { await fs.mkdir(path.dirname(fullPath), { recursive: true }); await fs.writeFile(fullPath, content); return fullPath; } catch (error) { console.error('Error creating template:', error); throw error; } } /** * Create a template with a basic Postgres function - the bread and butter of testing! ๐Ÿž */ export async function createTemplateWithFunc(context, prefix, funcSuffix = '', dir) { const name = `${getNextTemplateName(context, prefix)}.sql`; const funcName = `${context.testFunctionName}${funcSuffix}`; const content = `CREATE OR REPLACE FUNCTION ${funcName}() RETURNS void AS $$ BEGIN NULL; END; $$ LANGUAGE plpgsql;`; return createTemplate(context, name, content, dir); } /** * Clean up after our tests like good citizens! ๐Ÿงน */ export async function cleanupTestContext(context) { await fs.rm(context.testDir, { recursive: true, force: true }); } //# sourceMappingURL=helpers.js.map