Any plans of hardening Insert()?

I might have a pretty good use case for using Insert() to inject past “update” events…

We have a Bandwidth.com integration for sending & receiving text messages. Bandwidth sends events to our webhook in pages, and I don’t think there is any guaranteed ordering. In tracking subscribe/unsubscribe events (receiving the keywords STOP, START, or UNSTOP), it could be nice to be able to Insert() a document event that updates or clears an unsubscribedAt timestamp. This way I wouldn’t need to worry about the order of operations. The message events do have timestamps, so I can insert document events based on those timestamps. This should address the message ordering without the extra juggling of figuring out whether I’ve already re-subscribed this number before unsubscribing the number. As it is, I probably need to store discrete SubscriptionEvents in my database and query for the most recent in order to determine the subscription status.

Hi @colllin,

You can do that with Insert()

 > CreateCollection({name:'insert'})
{ ref: Collection("insert"),
  ts: 1605256817275000,
  history_days: 30,
  name: 'insert' }
> 
> 
> 
> Insert(Ref(Collection("insert"), "1"),ToMicros(TimeSubtract(Now(),10,'days')),'create',{data:{val1:1,val2:2}})
{ ts: 1604392825587992,
  action: 'create',
  document: Ref(Collection("insert"), "1"),
  data: { val1: 1, val2: 2 } }
> 
> Insert(Ref(Collection("insert"), "1"),ToMicros(TimeSubtract(Now(),9,'days')),'update',{data:{val1:10,val2:20}})
{ ts: 1604479234489172,
  action: 'create',
  document: Ref(Collection("insert"), "1"),
  data: { val1: 10, val2: 20 } }
> 
> Insert(Ref(Collection("insert"), "1"),ToMicros(TimeSubtract(Now(),8,'days')),'update',{data:{val1:100,val2:200}})
{ ts: 1604565640903046,
  action: 'create',
  document: Ref(Collection("insert"), "1"),
  data: { val1: 100, val2: 200 } }
> 
> 
> Insert(Ref(Collection("insert"), "1"),ToMicros(TimeSubtract(Now(),7,'days')),'delete',{data:{val1:100,val2:200}})
{ ts: 1604652047206858,
  action: 'delete',
  document: Ref(Collection("insert"), "1"),
  data: null }
> 
> 
> Paginate(Events(Ref(Collection("insert"), "1")))
{ data:
   [ { ts: 1604392825587992,
       action: 'create',
       document: Ref(Collection("insert"), "1"),
       data: { val1: 1, val2: 2 } },
     { ts: 1604479234489172,
       action: 'update',
       document: Ref(Collection("insert"), "1"),
       data: { val1: 10, val2: 20 } },
     { ts: 1604565640903046,
       action: 'update',
       document: Ref(Collection("insert"), "1"),
       data: { val1: 100, val2: 200 } },
     { ts: 1604652047206858,
       action: 'delete',
       document: Ref(Collection("insert"), "1"),
       data: null } ] }
> 
> 
> 
> Paginate(Documents(Collection("insert")))
{ data: [] }
> 
> 
> At(1604479234489172,Paginate(Documents(Collection("insert"))))
{ data: [ Ref(Collection("insert"), "1") ] }
> At(1604479234489172,Get(Ref(Collection("insert"), "1")))
{ ref: Ref(Collection("insert"), "1"),
  ts: 1604479234489172,
  data: { val1: 10, val2: 20 } }

Is that what you are looking for?

Luigi

Assuming that this is a typo and that it is actually creating 'update' events rather than 'create' events, and that I can update only part of the document, and that reading the document gives the proper “current” state based on past update events, then yes, that’s what I’m looking for.

But I don’t think it does all those things, and I think most people would expect it to do all these things. I should have mentioned that what prompted this post was this warning in the docs for Insert():

We do not recommend using the update action. Currently, it operates like a create action. For example, if you create a document, and then use Insert to create an update , the document then looks identical to the update; any fields not specified are removed.

And also this, which is labeled as important, but I’d love to know what a “snapshot” is referring to:

Since documents are stored immutably within FaunaDB, events are stored as snapshots of the associated document. Directly manipulating document history, using the Insert and Remove functions, does not currently affect subsequent event snapshots.

I suppose I can query for the document At(thePastTimestamp, ...), then Insert() an 'update' event containing the full past document and the updated field? That’s not so bad. I’ll try it. Thanks for the encouragement.

If I’m understanding the Important part of the docs correctly, then this won’t actually do what I need. Let’s say that the user modified the document at Time 10. Then an event comes through my webhook with timestamp of 5. If I insert the 'updated' event at timestamp 5, then the 'updated' event at Time 10 will completely overwrite the change, because it stores a full “snapshot” of the document (clued in by the “immutable” storage bit)? So when reading the current state of the document, the inserted event would have no effect?

I would actually need to update every 'update' event snapshot through to the present, in order for the current state of the document to reflect my inserted event.