-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (57 loc) · 2.11 KB
/
Copy pathindex.js
File metadata and controls
63 lines (57 loc) · 2.11 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
import { MemoryDataLayer } from './memory.js';
/** @type {import('./interface.js').DataLayer|null} */
let dataLayer = null;
/**
* Resolve which data-layer backend to instantiate. Precedence:
*
* 1. DEMO_MODE=true → MemoryDataLayer (unconditional, for dev).
* 2. DATA_LAYER env var → explicit backend selection in production.
* 3. default → FirestoreDataLayer (backwards-compatible).
*
* The two non-memory backends are lazily imported so non-Postgres deployments
* don't pay for `pg`, and vice versa for firebase-admin.
*/
export async function initializeDataLayer(config) {
if (config.demoMode) {
dataLayer = new MemoryDataLayer();
return dataLayer;
}
const backend = process.env.DATA_LAYER || 'firestore';
if (backend === 'memory') {
dataLayer = new MemoryDataLayer();
} else if (backend === 'postgres') {
const { Pool } = await import('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
// Adopters who need TLS / pool size / idle timeout tuning should set
// those via a custom pool in a bootstrap script and register the
// PostgresDataLayer directly rather than letting this factory build
// the pool.
});
const { PostgresDataLayer } = await import('./postgres.js');
dataLayer = new PostgresDataLayer(pool);
} else if (backend === 'firestore') {
const admin = await import('firebase-admin');
const app = admin.default.initializeApp({
credential: admin.default.credential.cert({
projectId: config.firebase.projectId,
clientEmail: config.firebase.clientEmail,
privateKey: config.firebase.privateKey,
}),
});
const { FirestoreDataLayer } = await import('./firestore.js');
dataLayer = new FirestoreDataLayer(app);
} else {
throw new Error(
`Unknown DATA_LAYER '${backend}'. Expected one of: memory, firestore, postgres.`
);
}
return dataLayer;
}
/**
* @returns {import('./interface.js').DataLayer}
*/
export function getDataLayer() {
if (!dataLayer) throw new Error('Data layer not initialized');
return dataLayer;
}