Skip to content

Instantly share code, notes, and snippets.

@xavierchow
Created April 24, 2022 06:04
Show Gist options
  • Save xavierchow/a995771877a9bac3ff56e192ae3aef2f to your computer and use it in GitHub Desktop.
Save xavierchow/a995771877a9bac3ff56e192ae3aef2f to your computer and use it in GitHub Desktop.
Ottoman_findById
const { Ottoman, Schema, model, start, close } = require('ottoman');
const main = async () => {
const ottoman = new Ottoman({
scopeName: '_default',
collectionName: '_default',
keyGenerator: () => {
return '';
},
keyGeneratorDelimiter: '',
});
await ottoman.connect({
connectionString: 'couchbase://localhost',
bucketName: 'test_bucket',
username: 'Administrator',
password: 'password',
});
const userSchema = new Schema(
{ name: String, age: { type: Number, required: true } },
{ strict: true, timestamps: true }
);
const User = model('User', userSchema);
const blogPostSchema = new Schema(
{ title: String, postedAt: String, votes: Number },
{ strict: true, timestamps: true }
);
const BlogPost = model('BlogPost', blogPostSchema);
const user = new User({ name: 'Jane Doe', gendar: 'female', age: 28 });
await start();
await user.save();
const post = await BlogPost.findById(user.id);
console.log('post = %j\n', post);
/* output: post = {"name":"Jane Doe","age":28,"createdAt":"2022-04-24T04:49:38.654Z",
"updatedAt":"2022-04-24T04:49:38.654Z","id":"1de7046d-a73d-4ced-90cc-3577a0f292cd","_type":"User"} */
await close();
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment