-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathknexfile.ts
More file actions
74 lines (64 loc) · 1.97 KB
/
knexfile.ts
File metadata and controls
74 lines (64 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type { Knex } from 'knex';
import path from 'path';
import { credentials } from './lib/credentials.ts';
import { parseSupabaseConfig } from './lib/supabase-config-parser.ts';
import type { SupabaseConfig } from './types/index.ts';
/**
* Knex Configuration for Ycode Supabase Migrations
*
* This configuration is used to run migrations programmatically
* against the user's Supabase PostgreSQL database.
*/
/**
* Load Supabase credentials from centralized storage
* Uses environment variables on Vercel, file-based storage locally
*/
async function getSupabaseConnectionParams() {
const config = await credentials.get<SupabaseConfig>('supabase_config');
if (!config?.connectionUrl || !config?.dbPassword) {
throw new Error('Supabase not configured. Please run setup first.');
}
const connectionParams = parseSupabaseConfig(config);
return {
host: connectionParams.dbHost,
port: connectionParams.dbPort,
database: connectionParams.dbName,
user: connectionParams.dbUser,
password: connectionParams.dbPassword,
ssl: { rejectUnauthorized: false },
};
}
const createConfig = (): Knex.Config => {
const isVercel = process.env.VERCEL === '1';
return {
client: 'pg',
connection: async () => {
const connectionParams = await getSupabaseConnectionParams();
return connectionParams;
},
migrations: {
directory: path.join(process.cwd(), 'database/migrations'),
extension: 'ts',
tableName: 'migrations',
},
pool: isVercel ? {
// Serverless-optimized pool settings
min: 0,
max: 1,
// Aggressive connection cleanup for serverless
acquireTimeoutMillis: 10000,
createTimeoutMillis: 10000,
idleTimeoutMillis: 1000,
reapIntervalMillis: 1000,
createRetryIntervalMillis: 200,
} : {
min: 2,
max: 10,
},
};
};
const config: { [key: string]: Knex.Config } = {
development: createConfig(),
production: createConfig(),
};
export default config;