开发JS存储库:是否存在Local Storage与IndexedDB的标准URL Scheme?
Great question! When building a unified URL-based abstraction for storage mechanisms in your JavaScript library, it makes perfect sense to look for standardized schemes to pair with the already-defined http:///https:// and file:// schemes. Here's what you need to know:
No Official Standard Schemes
First off: there are no formal, cross-browser-endorsed URL schemes for LocalStorage or IndexedDB in the official web standards. These storage APIs were designed without a corresponding URL scheme specification, unlike HTTP or file system access.
Common Community-Adopted Custom Schemes
That said, the developer community has settled on some widely used custom schemes to abstract these storage layers into URL-like strings. These are de facto standards that many libraries and projects use:
For LocalStorage
localstorage://(most common): This scheme typically uses the "host" portion as a namespace (to avoid key collisions between apps) and the path as the specific storage key. Example:localstorage://my-app/user-settings/themels://(shorthand variant): A shorter version for cases where brevity matters. Example:ls://auth/session-token
For IndexedDB
Since IndexedDB has a hierarchical structure (database → object store → record), the custom schemes often reflect this hierarchy:
indexeddb://: The host represents the database name, the first path segment is the object store, and subsequent segments or query parameters represent record identifiers or filters. Examples:// Access a specific record in an object store indexeddb://product-db/products/1453 // Query records with parameters indexeddb://user-db/users?email=john@example.comidb://(shorthand variant): A more concise version, used similarly:idb://todo-app/tasks?status=incomplete
Key Considerations for Your Library
- Consistency: Pick one scheme for each storage type and stick with it to keep your API intuitive.
- Collision Avoidance: Ensure your chosen schemes don't overlap with any potential future standard schemes (avoid overly generic names that might be adopted officially later).
- Parsing Logic: Build clear parsing rules to extract relevant details from the URL (e.g., namespace/key for LocalStorage, database/store/record for IndexedDB) to map the URL to actual storage operations.
Since http:///https:// and file:// are already standardized, you can seamlessly integrate these custom schemes alongside them to create your unified storage interface.
内容的提问来源于stack exchange,提问作者Shanqing Cai

