@eldoy/webdb 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Document database API backed by CouchDB, exposed through a Mongo-style client.
|
|
|
5
5
|
### Installation
|
|
6
6
|
```sh
|
|
7
7
|
npm i @eldoy/webdb
|
|
8
|
-
|
|
8
|
+
```
|
|
9
9
|
|
|
10
10
|
### Usage
|
|
11
11
|
|
|
@@ -83,6 +83,125 @@ await db.compact('user')
|
|
|
83
83
|
await db.drop()
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
+
### Mango Query Options
|
|
87
|
+
|
|
88
|
+
WebDB queries map directly to CouchDB Mango selectors. All Mango operators and options work as expected through `find()` and `batch()`.
|
|
89
|
+
|
|
90
|
+
#### Comparison Operators
|
|
91
|
+
Mango supports standard comparison operators inside selectors:
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
$eq equal
|
|
95
|
+
$ne not equal
|
|
96
|
+
$gt greater than
|
|
97
|
+
$gte greater than or equal
|
|
98
|
+
$lt less than
|
|
99
|
+
$lte less than or equal
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Example:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
await db('user').find({
|
|
106
|
+
age: { $gte: 18 }
|
|
107
|
+
})
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Logical Operators
|
|
111
|
+
|
|
112
|
+
Combine conditions using logical operators:
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
$and
|
|
116
|
+
$or
|
|
117
|
+
$not
|
|
118
|
+
$nor
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Example:
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
await db('user').find({
|
|
125
|
+
$or: [{ role: 'admin' }, { active: true }]
|
|
126
|
+
})
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### Sorting
|
|
130
|
+
|
|
131
|
+
Sorting requires an index on every field in the sort specification:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
await db('user').index([['created']])
|
|
135
|
+
|
|
136
|
+
await db('user').find(
|
|
137
|
+
{},
|
|
138
|
+
{ sort: [{ created: 'asc' }] }
|
|
139
|
+
)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Limiting
|
|
143
|
+
|
|
144
|
+
Limit returned documents:
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
await db('user').find({}, { limit: 10 })
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### Projection (fields)
|
|
151
|
+
|
|
152
|
+
Return only selected fields:
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
await db('user').find(
|
|
156
|
+
{},
|
|
157
|
+
{ fields: ['name', 'email'] }
|
|
158
|
+
)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### Date Handling
|
|
162
|
+
|
|
163
|
+
CouchDB stores dates as strings.
|
|
164
|
+
Using ISO-8601 timestamps (`new Date().toISOString()`) enables:
|
|
165
|
+
|
|
166
|
+
* correct lexical comparison
|
|
167
|
+
* correct sorting
|
|
168
|
+
* correct `$gt` / `$lt` range queries
|
|
169
|
+
|
|
170
|
+
Example:
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
await db('log').find({
|
|
174
|
+
created: { $gt: "2024-01-01T00:00:00.000Z" }
|
|
175
|
+
})
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
ISO strings compare and sort in true chronological order.
|
|
179
|
+
|
|
180
|
+
#### Batch Queries
|
|
181
|
+
|
|
182
|
+
`batch()` supports all Mango options:
|
|
183
|
+
|
|
184
|
+
* `size` (page size)
|
|
185
|
+
* `limit`
|
|
186
|
+
* `sort`
|
|
187
|
+
* `fields`
|
|
188
|
+
* standard selectors
|
|
189
|
+
|
|
190
|
+
Example:
|
|
191
|
+
|
|
192
|
+
```js
|
|
193
|
+
await db('user').batch(
|
|
194
|
+
{ created: { $gt: cutoff } },
|
|
195
|
+
{ size: 100, sort: [{ created: 'asc' }] },
|
|
196
|
+
async function (docs) {
|
|
197
|
+
// process chunk
|
|
198
|
+
}
|
|
199
|
+
)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
All Mango selectors and options work identically in both `find()` and `batch()`.
|
|
203
|
+
|
|
204
|
+
|
|
86
205
|
### Notes on Indexing, Sorting, and Mango Queries
|
|
87
206
|
|
|
88
207
|
**1. Selector fields and indexes**
|