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
Fixes #138 - Implement custom id schema's #143
Fixes #138 - Implement custom id schema's #143
Conversation
4124375
to
320463c
Compare
@@ -104,8 +106,14 @@ export default class Collection { | |||
}); | |||
} | |||
|
|||
use(transfomer) { | |||
this._transformers[transfomer.type].push(transfomer); | |||
use(plugin) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda like plugin
:) Thought about processor
, but plugin
sounds even more generic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this brings a situation where different extensibility mechanisms (e.g. plugins, adapters, transformers) overlaps, without a clear distinction in their names.
Until now, we had a clear scoping with each of the concepts, if we introduce "plugins", I fear that we might lose this. Should we consider generators
(like we did for Kinto server) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry I misunderstood this! We have different kinds of plugins (adapters, transformers, etc). Ignore these comments: this is a good proposal.
It definitely needs at least a name change, eg.
The good news is, integration tests pass just fine (as you can see in Travis anyway) :)
Could we imagine having a single
Super clear! All in all this is shaping super good. Thanks again. |
OK! And then I'll also change the mentions of UUID to just 'id' in error messages. So technically, these are breaking changes, which implies a bump in the version string in package.json, correct? |
return this._next++; | ||
} | ||
validate(id) { | ||
return ((id == parseInt(id, 10)) && (id >= 0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should show an example that doesn't handle unicity, maybe it worth adding a note or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you mean just return (typeof id == 'number');
? Sure, that's simpler to read, yet still shows how the validate function should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I like that, plus a note saying that:
In a real application, you want to make sure you do not generate twice the same record id on a collection.
This dummy example doesn't take care of id unicity. In case of id conflict you may loose data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I like that because it makes the user learn about common issues when reading the docs.
OK, I'll move |
|
||
### Limitations | ||
|
||
There's currently no way to deal with adding a custom id schema to an already filled remote database; that would mean remote data migrations, and both Kinto and Kinto.js don't provide this feature just yet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's currently no way to deal with adding a custom id schema to an already filled remote database
Isn't it what we are doing with Syncto? I think we should specify that there are no way to switch from UUID4 to custom id schema because if your remote database already respect the schema there are no limitation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! True. The only thing to avoid is adding an id schema which the /local/ data does not adhere to. Will correct that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
This looks good man :) |
I would go for it 👍 |
Yes, rc4 it is. I'll package and publish it as soon as this is merged.
I'm wondering about |
|
||
By default, kinto.js uses UUID4 strings for record id's. If you want to work with an existing body of data, this may not be what you want. | ||
|
||
You can define a custom id schema on a collection by calling the `Collection#use()` method, which accepts a `Kinto.IdSchema`-derived object instance: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel having one use
API which accepts different types of input is misleading. I've seen this pattern used for express (the node.js framework) in the past and it seems to be counter intuitive (e.g. really easy to use, not as easy to read).
Instead, we could have different methods to install the different configurations.
Here are different proposals:
- Have a
Collection#configure
to which we would pass an object with anid-schema
key; - Variant, pass two arguments to
Collection#setConfig
. e.g.setConfig('id-schema', object)
- Have a
Collection#setIdSchema
to which we pass an object;
Thoughts?
Thanks for working on this @michielbdejong. |
I like the first option! So that would mean, change collection.use(myIdSchema);
collection.use(myRemoteTransformer); it would become: collection.configure({
idSchema: myIdSchema,
remoteTransformers: [ myRemoteTransformer ]
}); Looks much more readable to me, indeed. Since this PR is going to introduce (small) breaking API changes anyway, like changes in error message strings, and already changes |
Definitely, let's do this!
Hmm that's gonna be a rather big patch :) If you feel like it, do it — but please try to split the different facets of the implementation in distinct commits, for easier reviews. |
Of course we could also iterate here; landing this patch first, filing a new issue for the |
OK, created #148 for this change, will submit a separate PR for that, and then rebase this one on that. |
d19d1ae
to
1df0fa1
Compare
Rebased this branch on #148 and applied the following review comments:
Did not yet apply the review comment about moving |
That would make a lot of sense to me. |
BTW, how about supporting passing the config object as an arg to const kinto = new Kinto();
const collection = kinto.collection("foo", {
idSchema: myIdSchema,
remoteTransformers: [ myRemoteTransformer ]
}); |
1df0fa1
to
8f880d4
Compare
Will improve test coverage, particularly for |
8f880d4
to
df1c28a
Compare
df1c28a
to
5678ab2
Compare
Need to fix how UuidSchema inherits from IdSchema, and check/improve test coverage. |
83cb854
to
be9c4af
Compare
r? @Natim |
import { cleanRecord } from "./api"; | ||
|
||
import UuidSchema from "./schemas/uuidschema"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: UUIDSchema
?
Looks awesome to me; r=r+wc from me, though I'd like to hear from @Natim as well :) |
Note that CI reported an error: |
@@ -558,3 +559,70 @@ coll = kinto.collection("articles", { | |||
There's currently no way to deal with adding transformers to an already filled remote database; that would mean remote data migrations, and both Kinto and Kinto.js don't provide this feature just yet. | |||
|
|||
**As a rule of thumb, you should only start using transformers on an empty remote collection.** | |||
|
|||
## Id schemas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use "ID", as id is used to refer to something completely different.
Coverage being okay on coveralls, I think we can now safely land this. @michielbdejong please do :) |
Just take care of squashing the commits first. |
0216b15
to
82b00c2
Compare
Ah sorry, hadn't searched properly. I did not change the ones in code comments in Thanks for your reviews! |
Fixes #138 - Implement custom id schema's
There are still variable names like
options.forceUUID
which refer to the default IdSchema, not sure if that's worth an API change?I did not run the integration test locally (I don't have my environment set up correctly yet for that), but I added some unit tests, and unit tests all seem to pass.
The approach I took was:
src/transformers/
tosrc/plugins/
.src/plugins/IdSchema.js
.this._idSchema
in the collection constructor.Kinto.IdSchema
for people to derive their custom id schema from.Kinto.createIdSchema
in parallel withKinto.createRemoteTransformer
.doc/api.md
, let me know if it's clearIn any case, I thought maybe you can give me some early feedback and let me know if you like the API, what you think of variable naming, and what I should improve?
Cheers!
Michiel.