@@ -79,11 +79,14 @@ export class BaseUser implements UserDBInterface, DocumentUpdater {
7979 return ! this . _username . startsWith ( GuestUsername ) ;
8080 }
8181
82- private remoteDB ! : PouchDB . Database ;
8382 public remote ( ) : PouchDB . Database {
8483 return this . remoteDB ;
8584 }
85+
8686 private localDB ! : PouchDB . Database ;
87+ private remoteDB ! : PouchDB . Database ;
88+ private writeDB ! : PouchDB . Database ; // Database to use for write operations (local-first approach)
89+
8790 private updateQueue ! : UpdateQueue ;
8891
8992 public async createAccount (
@@ -597,7 +600,11 @@ Currently logged-in as ${this._username}.`
597600 private setDBandQ ( ) {
598601 this . localDB = getLocalUserDB ( this . _username ) ;
599602 this . remoteDB = this . syncStrategy . setupRemoteDB ( this . _username ) ;
600- this . updateQueue = new UpdateQueue ( this . localDB ) ;
603+ // writeDB follows local-first pattern: static mode writes to local, CouchDB writes to remote/local as appropriate
604+ this . writeDB = this . syncStrategy . getWriteDB
605+ ? this . syncStrategy . getWriteDB ( this . _username )
606+ : this . localDB ;
607+ this . updateQueue = new UpdateQueue ( this . localDB , this . writeDB ) ;
601608 }
602609
603610 private async init ( ) {
@@ -697,7 +704,9 @@ Currently logged-in as ${this._username}.`
697704 * @returns The updated state of the card's CardHistory data
698705 */
699706
700- public async putCardRecord < T extends CardRecord > ( record : T ) : Promise < CardHistory < CardRecord > > {
707+ public async putCardRecord < T extends CardRecord > (
708+ record : T
709+ ) : Promise < CardHistory < CardRecord > & PouchDB . Core . RevisionIdMeta > {
701710 const cardHistoryID = getCardHistoryID ( record . courseID , record . cardID ) ;
702711 // stringify the current record to make it writable to couchdb
703712 record . timeStamp = moment . utc ( record . timeStamp ) . toString ( ) as unknown as Moment ;
@@ -735,8 +744,8 @@ Currently logged-in as ${this._username}.`
735744 streak : 0 ,
736745 bestInterval : 0 ,
737746 } ;
738- void this . remoteDB . put < CardHistory < T > > ( initCardHistory ) ;
739- return initCardHistory ;
747+ const putResult = await this . writeDB . put < CardHistory < T > > ( initCardHistory ) ;
748+ return { ... initCardHistory , _rev : putResult . rev } ;
740749 } else {
741750 throw new Error ( `putCardRecord failed because of:
742751 name:${ reason . name }
@@ -793,7 +802,7 @@ Currently logged-in as ${this._username}.`
793802 const deletePromises = duplicateDocIds . map ( async ( docId ) => {
794803 try {
795804 const doc = await this . remoteDB . get ( docId ) ;
796- await this . remoteDB . remove ( doc ) ;
805+ await this . writeDB . remove ( doc ) ;
797806 log ( `Successfully removed duplicate review: ${ docId } ` ) ;
798807 } catch ( error ) {
799808 log ( `Failed to remove duplicate review ${ docId } : ${ error } ` ) ;
@@ -891,7 +900,7 @@ Currently logged-in as ${this._username}.`
891900
892901 if ( err . status === 404 ) {
893902 // doc does not exist. Create it and then run this fcn again.
894- await this . remoteDB . put < ClassroomRegistrationDoc > ( {
903+ await this . writeDB . put < ClassroomRegistrationDoc > ( {
895904 _id : BaseUser . DOC_IDS . CLASSROOM_REGISTRATIONS ,
896905 registrations : [ ] ,
897906 } ) ;
0 commit comments