-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON schema specification #64
Comments
I think this feature can be implemented in user land. IndexedDB API is low level enough to cover this case. For example I built idb-schema which can be combined with idb-factory for similar results: import Schema from 'idb-schema'
import { open } from 'idb-factory'
const schema = new Schema()
.version(1)
.addStore('books', { key: 'isbn' })
.addIndex('byTitle', 'title', { unique: true })
.addIndex('byAuthor', 'author')
.version(2)
.getStore('books')
.addIndex('byDate', ['year', 'month'])
.version(3)
.addStore('magazines')
.addIndex('byPublisher', 'publisher')
.addIndex('byFrequency', 'frequency')
open('mydb1', schema.version(), schema.callback()).then((db) => {}) Handling of |
As far as defining stores and indexes via JSON, I've made a suggestion that JSON Schema itself specify IndexedDB properties so that a store and indexes can be built out of such JSON Schema documents (as well as add constraints above and beyond those within IndexedDB), and also made a suggestion for allowing JSON Schema (and its IndexedDB features) to be incrementally upgraded by JSON-based means. (And FWIW, in the case of incremental versioning of JSON without JSON Schema, I'm working on such code for db.js built on top of @alekseykulikov's idb-schema and hope to add as a separate library.) As far as whether to add support for JSON-based schema upgrades within IndexedDB, there are, as intimated in the OP, limitations to what an upgrade can accomplish non-programmatically. For example, it may be possible that you have a string identifier stored in a number of places but with a version upgrade, you want to pad it with 0's up to a certain length. For this, the programmatic approach (as used in Although this can be done in user land, it would be handy to have out-of-the-box for the common use cases. While the |
Since the JSON Schema project was not interested in incorporating IndexedDB elements, here's an alternative approach which covers the OP use case along with providing an even greater justification for defining such a format...
|
Oops, forgot to actually close. |
IndexedDB indexes are defined procedurally during database opening sequence on upgradeneeded event. Creating and deleting index requires a lot of boilerplate code and needed to compare with existing indexes. Instead of defining index in procedural, it is easier to defined indexes in JSON in database connection and let underlying database to do the job.
For example
If version is not specified, version will be increment as necessary. Additionally, in this case, database connection will be reconnect for new version on the event of version change as illustrated in the following code snippet. This will solved a lot of pain point.
Some use case require to modify existing data. To allow for that db instance should add
oldVersion
property. ProbablyoldSchema
property may needed to cover most use case.The text was updated successfully, but these errors were encountered: