Skip to content

Commit 329ab41

Browse files
committed
add getter for courseLookup...
allows for initDataLayer to injest env values first
1 parent c6f1372 commit 329ab41

File tree

1 file changed

+69
-16
lines changed

1 file changed

+69
-16
lines changed

packages/db/src/impl/pouch/courseLookupDB.ts

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pouch from './pouchdb-setup';
2-
// import { ENV } from '@/factory';
2+
import { ENV } from '@/factory';
33

44
const courseLookupDBTitle = 'coursedb-lookup';
55

@@ -13,23 +13,76 @@ interface CourseLookupDoc {
1313
console.log(`COURSELOOKUP FILE RUNNING`);
1414

1515
/**
16-
* a k-v indexes created courses.
17-
*
18-
* k: courseID
19-
* v: course name
16+
* A Lookup table of existant courses. Each docID in this DB correspondes to a
17+
* course database whose name is `coursedb-{docID}`
2018
*/
2119
export default class CourseLookup {
22-
// [ ] this db should be read only for public, admin-write only
23-
// static _db: PouchDB.Database = new pouch(
24-
// ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + courseLookupDBTitle,
25-
// {
26-
// // skip_setup: true,
27-
// }
28-
// );
29-
30-
static _db: PouchDB.Database = new pouch('http://localhost:5984/' + courseLookupDBTitle, {
31-
skip_setup: true,
32-
});
20+
// [ ] this db should be read only for public, admin-only for write
21+
// Cache for the PouchDB instance
22+
private static _dbInstance: PouchDB.Database | null = null;
23+
24+
/**
25+
* Static getter for the PouchDB database instance.
26+
* Connects using ENV variables and caches the instance.
27+
* Throws an error if required ENV variables are not set.
28+
*/
29+
private static get _db(): PouchDB.Database {
30+
// Return cached instance if available
31+
if (this._dbInstance) {
32+
return this._dbInstance;
33+
}
34+
35+
// --- Check required environment variables ---
36+
if (ENV.COUCHDB_SERVER_URL === 'NOT_SET' || !ENV.COUCHDB_SERVER_URL) {
37+
throw new Error(
38+
'CourseLookup.db: COUCHDB_SERVER_URL is not set. Ensure initializeDataLayer has been called with valid configuration.'
39+
);
40+
}
41+
if (ENV.COUCHDB_SERVER_PROTOCOL === 'NOT_SET' || !ENV.COUCHDB_SERVER_PROTOCOL) {
42+
throw new Error(
43+
'CourseLookup.db: COUCHDB_SERVER_PROTOCOL is not set. Ensure initializeDataLayer has been called with valid configuration.'
44+
);
45+
}
46+
47+
// --- Construct connection options ---
48+
const dbUrl = `${ENV.COUCHDB_SERVER_PROTOCOL}://${ENV.COUCHDB_SERVER_URL}/${courseLookupDBTitle}`;
49+
const options: PouchDB.Configuration.RemoteDatabaseConfiguration = {
50+
skip_setup: true, // Keep the original option
51+
// fetch: (url, opts) => { // Optional: Add for debugging network requests
52+
// console.log('PouchDB fetch:', url, opts);
53+
// return pouch.fetch(url, opts);
54+
// }
55+
};
56+
57+
// Add authentication if both username and password are provided
58+
if (ENV.COUCHDB_USERNAME && ENV.COUCHDB_PASSWORD) {
59+
options.auth = {
60+
username: ENV.COUCHDB_USERNAME,
61+
password: ENV.COUCHDB_PASSWORD,
62+
};
63+
console.log(`CourseLookup: Connecting to ${dbUrl} with authentication.`);
64+
} else {
65+
console.log(`CourseLookup: Connecting to ${dbUrl} without authentication.`);
66+
}
67+
68+
// --- Create and cache the PouchDB instance ---
69+
try {
70+
this._dbInstance = new pouch(dbUrl, options);
71+
console.log(`CourseLookup: Database instance created for ${courseLookupDBTitle}.`);
72+
return this._dbInstance;
73+
} catch (error) {
74+
console.error(`CourseLookup: Failed to create PouchDB instance for ${dbUrl}`, error);
75+
// Reset cache attempt on failure
76+
this._dbInstance = null;
77+
// Re-throw the error to indicate connection failure
78+
throw new Error(
79+
`CourseLookup: Failed to initialize database connection: ${
80+
error instanceof Error ? error.message : String(error)
81+
}`
82+
);
83+
}
84+
}
85+
3386
/**
3487
* Adds a new course to the lookup database, and returns the courseID
3588
* @param courseName

0 commit comments

Comments
 (0)