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
If this is your first time using REST API, create an API token. Refer to [Create API token](/fundamentals/api/get-started/create-token/).
66
75
67
76
### Start a D1 Session without constraints
68
77
69
-
To start a new D1 Session without any constraints, pass the parameter `first-unconstrained` to `.withSession()`. This is also the default behavior when no parameters are specified.
78
+
You can start a new D1 Session without constraints. D1 will route the first read query in the Session to any database instance, which could be either the primary database instance or a read replica.
79
+
80
+
Use this option if you do not need to read the latest version of the database in the primary database instance.
81
+
82
+
To do this, do not pass any parameters to `withSession()`.
70
83
71
84
```ts
72
85
// synchronous
73
-
let session =env.DB_D1.withSession('first-unconstrained')
74
-
const stmt =session.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
86
+
let session =env.DB_D1.withSession()
87
+
const stmt =session
88
+
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
75
89
// wait for Session condition, queries within batch have casual consistency
76
90
const result =awaitsession.run()
77
91
```
78
92
79
-
`.withSession('first-unconstrained')` directs the first query in the Session (whether read or write) to any database instance. This could be either the primary database instance or a read replica (note that write queries will be forwarded to the primary database instance). This means that:
80
-
81
-
- The Session may immediately start using read replicas.
82
-
- D1 serves the first read query with a version of the database that is as up-to-date as the query needs it to be.
83
-
- Subsequent queries in the Session have sequential consistency.
84
-
85
-
A possible use-case may be displaying blog articles on a website. It is not crucial to display the latest blog article, and therefore the user can start the Session by reading from a read replica. Subsequent queries can also go to the read replica.
93
+
- D1 serves the first read query may immediately start using read replicas. This will be a version of the database that is as up-to-date as the query needs it to be.
94
+
- Possible use-case: Displaying blog articles on a website. It is not crucial to display the latest blog article, and therefore the user can start the Session by reading from a read replica. Subsequent queries can also go to the read replica.
95
+
- Refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
86
96
87
-
#### Example of using `first-unconstrained`
97
+
#### Example of a D1 Session without constraints
88
98
89
99
Suppose you want to develop a feature for displaying “likes” on a social network application.
To start a new D1 Session with the first query reading from the primary database instance, pass the parameter `first-primary` to `.withSession()`.
120
+
You can start a new D1 Session by forcing the first read query to go to the primary database instance. Subsequent read queries may read from the read replica.
121
+
122
+
Use this option if you need to first read from the latest version of the database in the primary database instance.
123
+
124
+
To do this, pass the parameter `first-primary` to `withSession()`.
108
125
109
126
```ts
110
127
// synchronous
111
-
let sess =env.DB_D1.withSession('first-primary')
112
-
conststmt=sess.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
128
+
let session =env.DB_D1.withSession('first-primary')
129
+
conststmt= session
130
+
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
113
131
// wait for Session condition, queries within batch have casual consistency
114
-
constresult=awaitsess.run()
132
+
constresult=awaitsession.run()
115
133
```
116
134
117
-
`.withSession('first-primary')` directs the first query in the Session (whether read or write) to the primary database instance. This means that:
118
-
119
135
- D1 serves the first read query of the Session with the latest version of the database, which is the primary database instance.
120
136
- Subsequent queries in the Session may use read replicas.
121
-
- Subsequent queries in the Session have sequential consistency.
122
-
123
-
A possible use-case may be reading the score-table of an ongoing sports tournament. It is important for the first read to read the latest data from the primary database instance, but may not need to display subsequent score updates immediately (and therefore suitable to use read replicas for subsequent queries within the same Session).
137
+
- Possible use-case: Reading the score-table of an ongoing sports tournament. It is important for the first read to read the latest data from the primary database instance, but may not need to display subsequent score updates immediately (and therefore suitable to use read replicas for subsequent queries within the same Session).
138
+
- Refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
124
139
125
140
#### Example of using `first-primary`
126
141
@@ -133,38 +148,58 @@ Then, when opening an individual electricity bill statement, we can continue usi
When continuing an existing Session started with `withSession`, you can provide a `bookmark` parameter from an existing Session.
171
+
You can continue an existing Session from a previous Session.
172
+
173
+
Use this option if you need to read from the specific version of the database which you last read from your previous Session.
174
+
175
+
To do this, pass the `bookmark` string to `withSession()`.
151
176
152
177
```ts
153
178
// synchronous
154
-
let session =env.DB_D1.withSession('00000004-00000002-00004ec6-242180193730387d6b7156959e056db7')
155
-
conststmt=session.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
179
+
let session =env.DB_D1.withSession('bookmark_string')
180
+
conststmt= session
181
+
.prepare(`SELECT * FROM Customers WHERE CompanyName = 'Bs Beverages'`)
156
182
// wait for Session condition, queries within batch have casual consistency
157
183
constresult=awaitsession.run()
158
184
```
159
185
160
-
Starting a Session with a `bookmark` continues an existing Session using the `bookmark` as the reference point. This ensures you read from a version of the database which is at least as up-to-date as the provided `bookmark`.
186
+
- Starting a Session with a `bookmark` continues an existing Session using the `bookmark` as the reference point. This ensures you read from a version of the database which is at least as up-to-date as the provided `bookmark`.
187
+
- Possible use-case: Reading the score-table of an ongoing sports tournament (a continuation of the example from [Start a D1 Session with a constraint](/d1/features/read-replication/#start-a-d1-session-with-a-constraint)). A user may have started a web browser session and already has a `bookmark`. D1 can serve subsequent read queries within the same Session which is logically consistent, by using the `bookmark`.
188
+
- Refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
189
+
190
+
#### Example of using `bookmark`
191
+
192
+
193
+
194
+
```ts
195
+
//TBC
196
+
```
197
+
198
+
### Check if read replication is enabled
161
199
162
-
- Subsequent queries in the Session have sequential consistency.
163
-
- You can return the last encountered `bookmark` for a given Session using `session.getBookmark()`.
200
+
### Disable read replication
164
201
165
-
A potential use-case may be reading the score-table of an ongoing sports tournament (a continuation of the example from [Start a D1 Session with a constraint](/d1/features/read-replication/#start-a-d1-session-with-a-constraint)). A user may have started a web browser session and already has a `bookmark`. D1 can serve subsequent read queries within the same Session which is logically consistent, by using the `bookmark`.
166
202
167
-
For more information on Sessions API, refer to the [D1 Workers Binding API documentation](/d1/worker-api/d1-database#withsession).
- Directs the first query in the Session (whether read or write) to the primary database instance. Use this option if you need to start the Session with the most up-to-date data from the primary database instance.
257
+
- Directs the first query in the Session (whether read or write) to the primary database instance. Use this option if you need to start the Session with the most up-to-date data from the primary database instance.
258
+
- Subsequent queries in the Session may use read replicas.
259
+
- Subsequent queries in the Session have sequential consistency.
- Directs the first query in the Session (whether read or write) to any database instance. Use this option if you do not need to start the Session with the most up-to-date data, and wish to prioritize minimizing query latency from the very start of the Session.
262
+
- Directs the first query in the Session (whether read or write) to any database instance. Use this option if you do not need to start the Session with the most up-to-date data, and wish to prioritize minimizing query latency from the very start of the Session.
263
+
- Subsequent queries in the Session have sequential consistency.
264
+
- This is the default behavior when no parameters are provided.
- A [`bookmark`](/d1/reference/time-travel/#bookmarks) from an existing D1 Session. This allows you to continue the existing Session using the `bookmark` as a reference point.
264
-
265
-
- If no parameters are provided, `withSession` defaults to using the `first-unconstrained` parameter.
268
+
- Subsequent queries in the Session have sequential consistency.
269
+
- You can return the last encountered `bookmark` for a given Session using [`session.getBookmark()`](/d1/worker-api/d1-database/#getbookmark).
0 commit comments