querier-ts 2.0.1 → 2.1.0
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 -81
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,30 +24,6 @@ const usersQuery = Query.from<User>(users);
|
|
|
24
24
|
|
|
25
25
|
---
|
|
26
26
|
|
|
27
|
-
### Getting results
|
|
28
|
-
|
|
29
|
-
#### `all()`
|
|
30
|
-
|
|
31
|
-
Returns all results.
|
|
32
|
-
|
|
33
|
-
#### `first()`
|
|
34
|
-
|
|
35
|
-
Returns the first result.
|
|
36
|
-
|
|
37
|
-
#### `last()`
|
|
38
|
-
|
|
39
|
-
Returns the last result.
|
|
40
|
-
|
|
41
|
-
#### `count()`
|
|
42
|
-
|
|
43
|
-
Returns the number of results.
|
|
44
|
-
|
|
45
|
-
#### `exists()`
|
|
46
|
-
|
|
47
|
-
Returns a boolean indicating whether any results exist.
|
|
48
|
-
|
|
49
|
-
---
|
|
50
|
-
|
|
51
27
|
### Filtering data
|
|
52
28
|
|
|
53
29
|
#### `where(condition)`
|
|
@@ -103,82 +79,48 @@ const filteredUsers = Query.from(users)
|
|
|
103
79
|
|
|
104
80
|
---
|
|
105
81
|
|
|
106
|
-
### Selecting
|
|
82
|
+
### Selecting data
|
|
107
83
|
|
|
108
84
|
#### `select(columns)`
|
|
109
85
|
|
|
110
|
-
Defines which columns should be selected.
|
|
111
|
-
It can be combined with `scalar()`, `column()`, or `values()`.
|
|
86
|
+
Defines which columns should be selected, returning a new query with rows containing only the selected columns.
|
|
112
87
|
|
|
113
|
-
Accepts
|
|
88
|
+
Accepts multiple column names.
|
|
114
89
|
|
|
115
90
|
```ts
|
|
116
|
-
.select('id')
|
|
91
|
+
.select('id', 'email')
|
|
117
92
|
```
|
|
118
93
|
|
|
119
94
|
---
|
|
120
95
|
|
|
121
|
-
#### `
|
|
122
|
-
|
|
123
|
-
Returns the value of the first property of the first result.
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
const firstId = Query.from(users).scalar();
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
You can combine it with `select()` to retrieve a specific property:
|
|
130
|
-
|
|
131
|
-
```ts
|
|
132
|
-
const firstEmail = Query.from(users)
|
|
133
|
-
.select('email')
|
|
134
|
-
.scalar();
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Returns `false` if no value is found.
|
|
138
|
-
|
|
139
|
-
---
|
|
140
|
-
|
|
141
|
-
#### `column()`
|
|
96
|
+
#### `map(callback)`
|
|
142
97
|
|
|
143
|
-
|
|
98
|
+
Transforms each row into a new object using a callback function.
|
|
144
99
|
|
|
145
|
-
|
|
146
|
-
const ids = Query.from(users).column();
|
|
147
|
-
```
|
|
100
|
+
Like `select()`, this method is intended for data projection, but it is more flexible and better suited for complex transformations:
|
|
148
101
|
|
|
149
|
-
You can specify a column:
|
|
150
102
|
|
|
151
103
|
```ts
|
|
152
|
-
const
|
|
104
|
+
const authorsWithFirstPostQuery = Query.from(authors)
|
|
105
|
+
.map(({ posts, ...author }) => ({
|
|
106
|
+
...author
|
|
107
|
+
firstPost: posts[0],
|
|
108
|
+
}));
|
|
153
109
|
```
|
|
154
110
|
|
|
155
|
-
|
|
111
|
+
When dealing with simples scenarios, prefer `select()`:
|
|
156
112
|
|
|
157
113
|
```ts
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
```ts
|
|
170
|
-
const data = Query.from(users)
|
|
171
|
-
.select(['id', 'email'])
|
|
172
|
-
.values();
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
Example output:
|
|
176
|
-
|
|
177
|
-
```ts
|
|
178
|
-
[
|
|
179
|
-
[1, 'john@icloud.com'],
|
|
180
|
-
[2, 'mary@gmail.com']
|
|
181
|
-
]
|
|
114
|
+
// ⚠️ More verbose than necessary
|
|
115
|
+
const query = Query.from(users)
|
|
116
|
+
.map((user) => ({
|
|
117
|
+
id: user.id,
|
|
118
|
+
email: user.email,
|
|
119
|
+
}));
|
|
120
|
+
|
|
121
|
+
// ✅ More concise and expressive
|
|
122
|
+
const query = Query.from()
|
|
123
|
+
.select('id', 'email');
|
|
182
124
|
```
|
|
183
125
|
|
|
184
126
|
---
|
|
@@ -238,3 +180,100 @@ const secondId = Query.from(users)
|
|
|
238
180
|
```
|
|
239
181
|
|
|
240
182
|
> Passing a non-integer or a negative number will throw an `InvalidArgumentError`.
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
### Getting results
|
|
187
|
+
|
|
188
|
+
#### `all()`
|
|
189
|
+
|
|
190
|
+
Returns all results.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
#### `first()`
|
|
195
|
+
|
|
196
|
+
Returns the first result.
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
#### `last()`
|
|
201
|
+
|
|
202
|
+
Returns the last result.
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
#### `count()`
|
|
207
|
+
|
|
208
|
+
Returns the number of results.
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
#### `exists()`
|
|
213
|
+
|
|
214
|
+
Returns a boolean indicating whether any results exist.
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
#### `scalar()`
|
|
219
|
+
|
|
220
|
+
Returns the value of the first property of the first result.
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
const firstId = Query.from(users).scalar();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
You can combine it with `select()` to retrieve a specific property:
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
const firstEmail = Query.from(users)
|
|
230
|
+
.select('email')
|
|
231
|
+
.scalar();
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Returns `false` if no value is found.
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
#### `column()`
|
|
239
|
+
|
|
240
|
+
Returns the values of the first property from all results by default.
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
const ids = Query.from(users).column();
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
You can specify a column:
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
const emails = Query.from(users).column('email');
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Or combine it with `select()`:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
const emails = Query.from(users)
|
|
256
|
+
.select('email')
|
|
257
|
+
.column();
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
#### `values()`
|
|
263
|
+
|
|
264
|
+
Returns all results as arrays of values.
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
const data = Query.from(users)
|
|
268
|
+
.select(['id', 'email'])
|
|
269
|
+
.values();
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Example output:
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
[
|
|
276
|
+
[1, 'john@icloud.com'],
|
|
277
|
+
[2, 'mary@gmail.com']
|
|
278
|
+
]
|
|
279
|
+
```
|