# Ottoman V1 to Ottoman V2

Ottoman, for Couchbase, is an object document mapper (ODM) that allows you build what your object model would look like, then auto-generate all the boilerplate logic that goes with it.

Ottoman us to easily access CRUD commands for Couchbase Server using Node.js. To use Ottoman, make sure you add it to your Node.js project by using the following command:

npm install ottoman
1

Or using yarn:

yarn add ottoman
1

# Import Ottoman Module

Now that the libraries have been downloaded to our project, we need to include it in our source code. This can be done by adding the following code:

V1

const ottoman = require('ottoman');
1

Current Version

Current version entirely have support for typescript and you can also do:

import {getDefaultInstance, Ottoman} from 'ottoman';
1

# Couchbase Bucket Connection

V1

ottoman.bucket = (new couchbase.Cluster('http://127.0.0.1:8091')).openBucket('bucket-name-here');
1

Current Version

// get the default Ottoman instance
let ottoman = getDefaultInstance();

if (!ottoman) {
  // if not exist default one, then create
  ottoman = new Ottoman({collectionName: '_default'});
}

const main = async () => {
  await ottoman.connect({
    connectionString: 'couchbase://127.0.0.1', // with default port 8091
    bucketName: 'bucket-name-here',
    username: 'Administrator',
    password: 'password'
  });
}

main();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Sample Model Definition

Before we can start doing CRUD operations against Couchbase, we need to define our Ottoman model. These models represent documents in our database.

The models we create with Ottoman can have properties and methods.

V1

const UserModel = ottoman.model('User', {
  firstName: {type: 'string'},
  lastName: {type: 'string'},
  email: {type: 'string'}
});
1
2
3
4
5

Current Version

const UserModel = ottoman.model('User', {
  firstName: String,
  lastName: String,
  email: String
})
1
2
3
4
5

# Create New User Documents

const myUser = new UserModel({
  firstName: 'Perry',
  lastName: 'Mason',
  email: 'perry.mason@example.com',
});
1
2
3
4
5

# Persist Documents to Our Bucket

So far we have simply defined the model structure and created documents locally. Now we can persist the documents calling Ottoman's save() method on each object:

V1

myUser.save(function (error) {
  if (error) {
    console.log("An error happened:" + JSON.stringify(error));
    return;
  }
  console.log(`SUCCESS: user ${myUser.firstName} added!`);
});
1
2
3
4
5
6
7

Current Version

Using the async/await technique:

const runAsync = async () => {
  await myUser.save();
  console.log(`SUCCESS: user ${myUser.firstName} added!`);
}

ottoman.start()
  .then(runAsync)
  .catch((error) => console.log('An error happened: ' + JSON.stringify(error)))
  .finally(process.exit)
1
2
3
4
5
6
7
8
9

The output will be something like:

> SUCCESS: user Perry added!
1

# Retrieve Records From our Bucket

V1

// Finding All Documents
UserModel.find({}, function (error, result) {
  // Do something with the resulting Ottoman models
});

// Finding a Specific Document
UserModel.find({lastName: 'Mason'}, function (error, result) {
  // Do something with the resulting Ottoman model
});

// Find a Document by ID
UserModel.getById('document-id-here', function (error, result) {
  // Do something with the resulting Ottoman model
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Current Version

// Finding All Documents
await UserModel.find({}, {select: 'firstName,email', limit: 20});

// Finding a Specific Document
await UserModel.find({lastName: 'Mason'});

// Find a Document by ID
await UserModel.findById('document-id-here');
1
2
3
4
5
6
7
8

# Updating an Existing Document

V1

Updating documents in Version1 with Ottoman can be a little tricky because you must first have loaded the document before trying to do a manipulation on it. What you could do is find the particular document by id, then perform the save command like so:

UserModel.getById('document-id-here', function (error, result) {
  if (error) {
    console.log('An error happened: ' + JSON.stringify(error));
  }
  result.firstname = 'Nicolas',
    result.save(function (error) {
      if (error) {
        console.log('An error happened: ' + JSON.stringify(error));
      }
    });
});
1
2
3
4
5
6
7
8
9
10
11

Current version

Is more simple do that with current version:

await UserModel.updateById('document-id-here', {firstname: 'Nicolas'});
1