-
Notifications
You must be signed in to change notification settings - Fork 114
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
Assist AutoIncrement FieldValidator with SQL backend #266
Comments
There is no specific support for Auto-Incremental IDs in rest-layer itself. However I belive a
The SQL/Postgre SQL storer backend is thirdparty, and not maintained by us. It's probably best to start the implementation of such a feature there. Otherwise, be sure to understand the limitations of auto incremental IDs, and when (not) to use them:
If you don't have a specific use-case where Auto-Incremental IDs are essential, I would at least personal recommend against using them. |
I don't have control over the schema to change the I was thinking of forking the repo, adding the functionality of field exclusion from POST requests only. |
The SQL backend I know about is here: A good starting point may be to fork that and start trying out different things. You can start experimenting with saving an item without an ID, and see where it fails using the If it fails in the storage backend, then that would be a good place to try to implement AutoIncremental ID support. If it fails in rest-layer, we can look at what changes are needed to allow for an empty ID. An alternative to an empty ID, could also to define a "dummy" value (in the OnOnit field hook), that the storer backend ignores or translates into a relevant. Such a field-type could be defined in the storer backend. |
I was running out of time and needed to show something, so I hacked my way to using auto incrementing integers with https://github.com/apuigsech/rest-layer-sql
I tracked the problem and found this: Line 97 in 0fa67ca
Then we try to validate the ID with the field ID here rest-layer/rest/resource_path.go Line 57 in 0fa67ca
id as string and not int.
I have noticed two assumptions (correct me if I am wrong): For my use case I need these two features supported: I understand the fact that this requires changes in Storer interface rest-layer/resource/storage.go Line 10 in 0fa67ca
I understand that these are fundamental changes and would require lot of effort and careful considerations. Having said that, I am more than willing and happy to contribute and make all the changes required to support the above mentioned features. However, I will need your help and guidance to get this done. I am hoping that if you agree and we work to get these features supported, then I will be able to use it in my production. |
a) ID is a string. ID is always a string when read from the URL as opposed to read from JSON, where it can have different types. To fix this, you can try to let the ID field validator support a "string" input and convert it to an integer (basically write your own FieldValidator). After an ID is parsed, there should be no further assumptions requiring the ID to be a string. b) ID is randomly generated ID and not database generated. There is no assumption that it is generated. However, it's possible that there could be some places where it's assumed that the ID field is set before the resource.Item reaches the storer backend (even when the field is not set as Either way there should be no need of changing the Storer interface to support this. the storer interface is passed pointers to |
There indeed are places where it is assumed that ID field is set as you have already pointed in b). This setup gets me going. Going forward, I will create PRs for both Thanks for all the help that you have provided. P.S.: If you wish we can close this issue and open another one once I have created the PRs and/or need help. Alternatively we can label this issue appropriately to indicate that initial issues are resolved and more work is expected in future. |
Here is a possible less hacky approach you can try (add this field definition to your type autoIncrType uint8
const (
AutoIncr autoIncrType = iota // Dummy value set in rest-layer that we can detect in the backend.
)
type AutoIncrInt64Field = schema.Field{
Required: true,
Oninit: func() {return AutoIncr},
FieldValidatior: &AutoIncrInt64Validator{},
}
type AutoIncrValidator struct{}
func (*AutoIncrValidator) Validate(v interface{}) (out interface{}, error) {
var i int64
switch vt := v.(type) {
case autoIncrType {
// Let SQL backend detect and remove this..
return v, nil
case string:
i = ... // parse vt to int64
case int64:
i = vt
case int32:
i = int64(vt)
default:
return nil, ErrNotAnIntger
}
return i, nil
} You can imagine a similar type for |
Let's keep it open for now @ishan1608, and you can keep asking your questions here. Relabeled renamed it. |
I have an auto incrementing integer field (ID) in postgres db.
Whole creating an object using POST request I have to provide OnInit hook with myself manually handling the values.
Instead the better approach would be to let the db handle the ID generation and give me the newly generated ID for the new object.
Is it possible in the current system?
If yes how?
If no should we add this feature?
Also if no what is the solution for now other than making a db query myself and finding out the largest ID and adding 1 to it?
I have posted a StackOverflow question with more details here: https://stackoverflow.com/q/59734290/1641882
The text was updated successfully, but these errors were encountered: