You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/01-introduction.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ sidebar_position: 01
4
4
5
5
# Introduction
6
6
7
-
Welcome to the documentation for Couchbase ORM, a powerful Object-Relational Mapping (ORM) library for Ruby that simplifies interactions with Couchbase Server. This guide will walk you through the features and usage of Couchbase ORM, helping you build efficient and scalable Ruby applications with Couchbase.
7
+
Welcome to the documentation for Couchbase ORM, a Object-Relational Mapping (ORM) library for Ruby that simplifies interactions with Couchbase Server. This guide will walk you through the features and usage of Couchbase ORM, helping you build efficient and scalable Ruby applications with Couchbase.
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/04-querying.md
+3-20Lines changed: 3 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,24 +55,7 @@ users = User.order(age: :desc)
55
55
56
56
You can also chain multiple `order` clauses to sort by multiple attributes.
57
57
58
-
## 4.4. Limit and Offset
59
-
60
-
<!-- TODO: Have to implement this -->
61
-
62
-
To limit the number of records returned or to skip a certain number of records, you can use the `limit` and `offset` methods.
63
-
64
-
```ruby
65
-
# Find the first 10 users
66
-
users =User.limit(10)
67
-
68
-
# Find users starting from the 11th record
69
-
users =User.offset(10)
70
-
71
-
# Find users starting from the 11th record and limit to 10 records
72
-
users =User.offset(10).limit(10)
73
-
```
74
-
75
-
## 4.5. Scopes
58
+
## 4.4. Scopes
76
59
77
60
Scopes allow you to define reusable query snippets that can be chained with other query methods. Scopes are defined as class methods within your model.
78
61
@@ -88,7 +71,7 @@ users = User.active.adults
88
71
89
72
Scopes provide a clean and DRY way to encapsulate commonly used query conditions.
90
73
91
-
## 4.6. Pluck
74
+
## 4.5. Pluck
92
75
93
76
The `pluck` method allows you to retrieve specific attributes from the matched records instead of loading the entire objects. It returns an array of values for the specified attributes.
94
77
@@ -100,7 +83,7 @@ names = User.pluck(:name)
100
83
name_emails =User.active.pluck(:name, :email)
101
84
```
102
85
103
-
## 4.7. Destroy All
86
+
## 4.6. Destroy All
104
87
105
88
To delete multiple records that match specific conditions, you can use the `destroy_all` method.
The `update` method updates only the specified attributes of the record in the database.
73
73
74
-
## 5.6. Atomic Operations
75
-
76
-
CouchbaseOrm supports atomic operations, which allow you to perform certain database operations in a single, indivisible step. Atomic operations ensure data consistency and help avoid race conditions in concurrent environments.
77
-
78
-
Some common atomic operations include:
79
-
80
-
-`increment`: Atomically increments a numeric attribute by a specified value.
81
-
-`decrement`: Atomically decrements a numeric attribute by a specified value.
82
-
-`append`: Atomically appends a value to a string attribute.
83
-
-`prepend`: Atomically prepends a value to a string attribute.
84
-
-`touch`: Updates the document's expiration time without modifying its content.
85
-
86
-
Here's an example of using atomic operations:
87
-
88
-
```ruby
89
-
user =User.find('user_id_123')
90
-
user.increment(:login_count)
91
-
user.touch(expiry:1.hour.from_now)
92
-
```
93
-
94
-
In this example, the `increment` operation atomically increments the `login_count` attribute of the user, and the `touch` operation updates the document's expiration time to one hour from now.
95
-
96
-
## 5.7. Callbacks
74
+
## 5.6. Callbacks
97
75
98
76
As mentioned in the previous section on defining models, CouchbaseOrm supports lifecycle callbacks that allow you to execute code at certain points in a record's persistence lifecycle.
99
77
@@ -119,23 +97,3 @@ end
119
97
In this example, the `encrypt_password` callback is executed before saving a user record, encrypting the password if it has been changed. The `send_welcome_email` callback is executed after creating a new user record, sending a welcome email to the user.
120
98
121
99
Callbacks provide a way to encapsulate and reuse common logic related to the persistence lifecycle of your records.
122
-
123
-
## 5.8. Transactions
124
-
125
-
CouchbaseOrm supports transactions, which allow you to group multiple database operations into a single, atomic unit of work. Transactions ensure that all the operations within the transaction either succeed together or fail together, maintaining data integrity.
126
-
127
-
```ruby
128
-
CouchbaseOrm.transaction do
129
-
user =User.create(name:'John')
130
-
account =Account.create(user: user, balance:100)
131
-
# ...
132
-
end
133
-
```
134
-
135
-
In this example, the creation of a user and an associated account are wrapped inside a transaction. If any operation within the transaction fails, all the changes made within the transaction will be rolled back, ensuring that the database remains in a consistent state.
136
-
137
-
Transactions are useful when you need to perform multiple related operations that should succeed or fail together, such as creating related records or updating multiple documents atomically.
138
-
139
-
That covers the basics of persistence with CouchbaseOrm. With these methods and features, you can easily create, update, delete, and manipulate records in Couchbase Server using a simple and expressive API.
140
-
141
-
In the next section, we'll explore the powerful associations feature of CouchbaseOrm, which allows you to define and work with relationships between your models.
0 commit comments