Kinto/kinto

Kinto automatically create non existing collection

Opened this issue · 2 comments

Is there a way to create the collection on the fly ?

Lets say im trying to create a record for the collection 'user' and user collection does not exists yet. Is it possible to create a default collection right away ?

Currently we only implemented this behavior on the default_bucket plugin, which has a lot of other specificities.

One possibility is to use the concurrency control requests headers in your client, to create only if it doesn't exist.

$ http PUT https://kinto.dev.mozaws.net/v1/buckets/a "If-None-Match: *" -a user:pass
HTTP/1.1 201 Created
Access-Control-Expose-Headers: Alert, Content-Length, Backoff, Retry-After, Content-Type
Connection: keep-alive
Content-Length: 152
Content-Security-Policy: default-src 'none'; frame-ancestors 'none'; base-uri 'none';
Content-Type: application/json
Date: Wed, 10 Nov 2021 14:48:40 GMT
ETag: "1636555720093"
Last-Modified: Wed, 10 Nov 2021 14:48:40 GMT
Server: nginx
X-Content-Type-Options: nosniff

{
    "data": {
        "id": "a",
        "last_modified": 1636555720093
    },
    "permissions": {
        "write": [
            "basicauth:631c2d625ee5726172cf67c6750de10a3e1a04bcd603bc9ad6d6b196fa8257a6"
        ]
    }
}

If it exists it will return 412 (that your client code can ignore on purpose)

$ http PUT https://kinto.dev.mozaws.net/v1/buckets/a "If-None-Match: *" -a user:pass
HTTP/1.1 412 Precondition Failed
Access-Control-Expose-Headers: Alert, Content-Length, Backoff, Retry-After, Content-Type
Connection: keep-alive
Content-Length: 162
Content-Security-Policy: default-src 'none'; frame-ancestors 'none'; base-uri 'none';
Content-Type: application/json
Date: Wed, 10 Nov 2021 14:48:42 GMT
ETag: "1636555720093"
Last-Modified: Wed, 10 Nov 2021 14:48:40 GMT
Server: nginx
X-Content-Type-Options: nosniff

{
    "code": 412,
    "details": {
        "existing": {
            "id": "a",
            "last_modified": 1636555720093
        }
    },
    "errno": 114,
    "error": "Precondition Failed",
    "message": "Resource was modified meanwhile"
}

also you can manually create empty collection based on notification on backend level.
For example: create profile collection when new account created.