Skip to content

Commit b82c485

Browse files
committed
make remoteUserDB lookup JIT...
to prevent race against ENV init
1 parent beab6e6 commit b82c485

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@ const log = (s: any) => {
3636
};
3737

3838
const cardHistoryPrefix = 'cardH-';
39-
const remoteStr: string = ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'skuilder';
4039

41-
console.log(`Connecting to remote: ${remoteStr}`);
40+
// console.log(`Connecting to remote: ${remoteStr}`);
4241

43-
let remoteCouchRootDB: PouchDB.Database;
44-
try {
45-
remoteCouchRootDB = new pouch(remoteStr, {
46-
skip_setup: true,
47-
});
48-
} catch (error) {
49-
console.error('Failed to initialize remote CouchDB connection:', error);
50-
throw new Error(`Failed to initialize CouchDB: ${JSON.stringify(error)}`);
42+
function getRemoteCouchRootDB(): PouchDB.Database {
43+
const remoteStr: string =
44+
ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'skuilder';
45+
let remoteCouchRootDB: PouchDB.Database;
46+
try {
47+
remoteCouchRootDB = new pouch(remoteStr, {
48+
skip_setup: true,
49+
});
50+
} catch (error) {
51+
console.error('Failed to initialize remote CouchDB connection:', error);
52+
throw new Error(`Failed to initialize CouchDB: ${JSON.stringify(error)}`);
53+
}
54+
return remoteCouchRootDB;
5155
}
5256

5357
interface DesignDoc {
@@ -59,17 +63,6 @@ interface DesignDoc {
5963
};
6064
}
6165

62-
export async function doesUserExist(name: string) {
63-
try {
64-
const user = await remoteCouchRootDB.getUser(name);
65-
log(`user: ${user._id}`);
66-
return true;
67-
} catch (err) {
68-
log(`User error: ${err}`);
69-
return false;
70-
}
71-
}
72-
7366
/**
7467
* The current logged-in user, with pouch / couch functionality.
7568
*
@@ -120,13 +113,13 @@ Currently logged-in as ${this._username}.`
120113
);
121114
} else {
122115
try {
123-
const signupRequest = await remoteCouchRootDB.signUp(username, password);
116+
const signupRequest = await getRemoteCouchRootDB().signUp(username, password);
124117

125118
if (signupRequest.ok) {
126119
log(`CREATEACCOUNT: logging out of ${this.getUsername()}`);
127-
const logoutResult = await remoteCouchRootDB.logOut();
120+
const logoutResult = await getRemoteCouchRootDB().logOut();
128121
log(`CREATEACCOUNT: logged out: ${logoutResult.ok}`);
129-
const loginResult = await remoteCouchRootDB.logIn(username, password);
122+
const loginResult = await getRemoteCouchRootDB().logIn(username, password);
130123
log(`CREATEACCOUNT: logged in as new user: ${loginResult.ok}`);
131124
const newLocal = getLocalUserDB(username);
132125
const newRemote = getUserDB(username);
@@ -167,7 +160,7 @@ Currently logged-in as ${this._username}.`
167160
Log out of account ${this.getUsername()} before logging in as ${username}.`);
168161
}
169162

170-
const loginResult = await remoteCouchRootDB.logIn(username, password);
163+
const loginResult = await getRemoteCouchRootDB().logIn(username, password);
171164
if (loginResult.ok) {
172165
log(`Logged in as ${username}`);
173166
this._username = username;

0 commit comments

Comments
 (0)