88import { v } from "convex/values" ;
99import { mutation , query } from "./_generated/server" ;
1010
11+ // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
12+ // Backward Compatibility Helpers
13+ // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14+
15+ /**
16+ * Get version number with backward compatibility for legacy contexts
17+ */
18+ function getContextVersion ( context : any ) : number {
19+ return context . version ?? 1 ;
20+ }
21+
22+ /**
23+ * Get previous versions array with backward compatibility for legacy contexts
24+ */
25+ function getContextPreviousVersions ( context : any ) : any [ ] {
26+ return context . previousVersions ?? [ ] ;
27+ }
28+
1129// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1230// Mutations (Write Operations)
1331// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -135,9 +153,12 @@ export const update = mutation({
135153 ...args . data ,
136154 } ;
137155
138- // Create version snapshot
156+ // Create version snapshot (with backward compatibility)
157+ const currentVersion = getContextVersion ( context ) ;
158+ const previousVersions = getContextPreviousVersions ( context ) ;
159+
139160 const newVersion = {
140- version : context . version ,
161+ version : currentVersion ,
141162 status : context . status ,
142163 data : context . data ,
143164 timestamp : context . updatedAt ,
@@ -149,8 +170,8 @@ export const update = mutation({
149170 await ctx . db . patch ( context . _id , {
150171 status : newStatus ,
151172 data : newData ,
152- version : context . version + 1 ,
153- previousVersions : [ ...context . previousVersions , newVersion ] ,
173+ version : currentVersion + 1 ,
174+ previousVersions : [ ...previousVersions , newVersion ] ,
154175 updatedAt : now ,
155176 completedAt :
156177 args . completedAt !== undefined
@@ -766,8 +787,12 @@ export const updateMany = mutation({
766787
767788 // Update each context
768789 for ( const context of contexts ) {
790+ // Backward compatibility for version tracking
791+ const currentVersion = getContextVersion ( context ) ;
792+ const previousVersions = getContextPreviousVersions ( context ) ;
793+
769794 const newVersion = {
770- version : context . version ,
795+ version : currentVersion ,
771796 status : context . status ,
772797 data : context . data ,
773798 timestamp : context . updatedAt ,
@@ -784,8 +809,8 @@ export const updateMany = mutation({
784809 ? args . updates . status
785810 : context . status ,
786811 data : newData ,
787- version : context . version + 1 ,
788- previousVersions : [ ...context . previousVersions , newVersion ] ,
812+ version : currentVersion + 1 ,
813+ previousVersions : [ ...previousVersions , newVersion ] ,
789814 updatedAt : now ,
790815 } ) ;
791816
@@ -959,10 +984,14 @@ export const getVersion = query({
959984 return null ;
960985 }
961986
987+ // Backward compatibility for version tracking
988+ const currentVersion = getContextVersion ( context ) ;
989+ const previousVersions = getContextPreviousVersions ( context ) ;
990+
962991 // Check if it's the current version
963- if ( context . version === args . version ) {
992+ if ( currentVersion === args . version ) {
964993 return {
965- version : context . version ,
994+ version : currentVersion ,
966995 status : context . status ,
967996 data : context . data ,
968997 timestamp : context . updatedAt ,
@@ -971,7 +1000,7 @@ export const getVersion = query({
9711000 }
9721001
9731002 // Check previous versions
974- const versionRecord = context . previousVersions . find (
1003+ const versionRecord = previousVersions . find (
9751004 ( v : any ) => v . version === args . version ,
9761005 ) ;
9771006
@@ -996,11 +1025,15 @@ export const getHistory = query({
9961025 return [ ] ;
9971026 }
9981027
1028+ // Backward compatibility for version tracking
1029+ const currentVersion = getContextVersion ( context ) ;
1030+ const previousVersions = getContextPreviousVersions ( context ) ;
1031+
9991032 // Return all previous versions + current version
10001033 const versions = [
1001- ...context . previousVersions ,
1034+ ...previousVersions ,
10021035 {
1003- version : context . version ,
1036+ version : currentVersion ,
10041037 status : context . status ,
10051038 data : context . data ,
10061039 timestamp : context . updatedAt ,
@@ -1030,10 +1063,14 @@ export const getAtTimestamp = query({
10301063 return null ;
10311064 }
10321065
1066+ // Backward compatibility for version tracking
1067+ const currentVersion = getContextVersion ( context ) ;
1068+ const previousVersions = getContextPreviousVersions ( context ) ;
1069+
10331070 // If timestamp is after current version, return current
10341071 if ( args . timestamp >= context . updatedAt ) {
10351072 return {
1036- version : context . version ,
1073+ version : currentVersion ,
10371074 status : context . status ,
10381075 data : context . data ,
10391076 timestamp : context . updatedAt ,
@@ -1044,9 +1081,9 @@ export const getAtTimestamp = query({
10441081 // Find the version that was current at the timestamp
10451082 // Walk backwards through versions
10461083 const allVersions = [
1047- ...context . previousVersions ,
1084+ ...previousVersions ,
10481085 {
1049- version : context . version ,
1086+ version : currentVersion ,
10501087 status : context . status ,
10511088 data : context . data ,
10521089 timestamp : context . updatedAt ,
0 commit comments