fogine/couchbase-odm

View on GitHub
tutorials/3a.documentIDLookups.md

Summary

Maintainability
Test Coverage
Model exposes `getById(id, [options])` method. Which accepts `id` string or instance of `Key`.  
Note that in CouchbaseODM `id` and `key` represents different document values:
> *a key of a document is composed of static and dynamic (id) part.*  
> *Static part of key determines type of document within a bucket whereas dynamic part is actual identificator (`id`) usually represented by auto incremented integer or generated `uuid` string value.*  
> *Both static part of document's key and document's `id` forms a unique representation of the document within the bucket*

#### Example

Document's key:  
````
User_1 //auto-incremented integer

User_000a2644-a151-4771-98ef-132134e1606e //uuidv4 id
````

In both examples, static portion of document's key is represented by `User_`.  
`id` value in first example is equal to `1` in second example `id` = `000a2644-a151-4771-98ef-132134e1606e`  

Getting a document by `id`/`Key` considering above document key samples would look like:
````javascript
    return User.getById(1).then(function(user) {
        console.log(user);
    });

    //or

    return User.getById('000a2644-a151-4771-98ef-132134e1606e').then(function(user) {
        console.log(user);
    });

    //or

    const key = User.buildKey('000a2644-a151-4771-98ef-132134e1606e');
    return User.getById(key).then(function(user) {});
````

> *Model can only have one key generation strategy eg.: `UUID4Key` or `IncrementalKey`, not both*

Static part of key can be customized by {@link Model `schemaSettings.key`} option on `Model`.
For `id` generation there is couple of `key` generation strategies like {@link UUID4Key} or {@link IncrementalKey} .  
if you have special needs, you can create a strategy yourself. See {@tutorial 7.customizingDocumentsKey}