Giter VIP home page Giter VIP logo

Comments (4)

lukeed avatar lukeed commented on June 20, 2024 1

Yup. PR in the works for all metadata-stuff. Was a busy week so havent had time to finish it up yet.

from worktop.

lukeed avatar lukeed commented on June 20, 2024

In the TODO example, we will lose data when multiple users create TODO's in the time range

There will be synchronization issues if UserA and UserB are writing into the same key, but that doesn't happen in this example. The example is setup in a way so that UserA only deals with UserA's todos (eg, user::UserA::todos) and UserB only cares about their own todos (eg, user::UserB::todos).

That said, if UserA adds multiple todos in close succession, then that may cause replication issues within the user::UserA::todos document. This is because the save() helper is called at the end of the insert model method, which is hard-coded to write into the same key.

This save method could be skipped entirely in favor of a new list() model method that would use a prefix match for keys. Across regions, there will still be replication delay, but there won't ever be actual data loss or (true) data inconsistency because there's no write/update into the same document. In practice, this means that users in other regions may see items missing during their KV.list result, but it's guaranteed to show up within the minute.

This updated, hypothetical list method would look like this:

export async function list(username: string): Promise<Todo['uid'][]> {
  const prefix = key_owner(username) + '::';
  const keys = await TODOS.list({ prefix, limit: 10 });

  // The current example returns Array<Todo['uid']>
  // So stopping here, but could do Promise.all(/* key lookup */)
  
  // strip prefix; eg `key_item - (key_owner + '::') = uid`
  return keys.map(key => key.substring(prefix.length));
}

Durable Objects will solve it but I'm surprised that they can only hold 32KiB

DOs are still in beta & worktop (purposefully) doesn't integrate with or support them yet. This is to avoid early/unnecessary breaking API changes while DO-things continue to finalize.

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

Hi @lukeed thanks for the write-up. Indeed, the list is scoped to the user. Great tip with the prefix-match. In combination with the metadata, it allows storing a kind of search index next to the item to avoid loading all key values.

Do you plan to support the metadata option in the WriteOptions ?

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

metadata is also missing on the ListOptions type.

from worktop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.