drizzle-orm 0.10.43 → 0.10.44
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 +439 -0
- package/builders/highLvlBuilders/selectRequestBuilder.js +4 -4
- package/builders/joinBuilders/builders/abstractJoinBuilder.d.ts +3 -1
- package/builders/joinBuilders/builders/abstractJoinBuilder.js +6 -1
- package/builders/joinBuilders/builders/selectWithFiveJoins.d.ts +2 -1
- package/builders/joinBuilders/builders/selectWithFiveJoins.js +2 -2
- package/builders/joinBuilders/builders/selectWithFourJoins.d.ts +2 -1
- package/builders/joinBuilders/builders/selectWithFourJoins.js +6 -6
- package/builders/joinBuilders/builders/selectWithJoin.d.ts +2 -1
- package/builders/joinBuilders/builders/selectWithJoin.js +6 -6
- package/builders/joinBuilders/builders/selectWithThreeJoins.d.ts +2 -1
- package/builders/joinBuilders/builders/selectWithThreeJoins.js +6 -6
- package/builders/joinBuilders/builders/selectWithTwoJoins.d.ts +2 -1
- package/builders/joinBuilders/builders/selectWithTwoJoins.js +6 -6
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
# DrizzleORM
|
|
2
|
+
|
|
3
|
+
**DrizzleORM** is an ORM framework for
|
|
4
|
+
[TypeScript](https://www.typescriptlang.org/).
|
|
5
|
+
It offers you several levels of Database communication:
|
|
6
|
+
* Typesafe Table View approach
|
|
7
|
+
* Typesafe Query Builder
|
|
8
|
+
* Simple SQL query execution
|
|
9
|
+
|
|
10
|
+
Drizzle ORM is highly influenced by [Exposed](https://github.com/JetBrains/Exposed) and Jetbrains development methodology
|
|
11
|
+
|
|
12
|
+
## Supported Databases
|
|
13
|
+
|
|
14
|
+
* PostgreSQL
|
|
15
|
+
|
|
16
|
+
## Links
|
|
17
|
+
|
|
18
|
+
In Progress
|
|
19
|
+
|
|
20
|
+
## Installing
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install drizzle-orm drizzle-kit
|
|
24
|
+
```
|
|
25
|
+
#### **In Progress**
|
|
26
|
+
```bash
|
|
27
|
+
yarn add drizzle-orm drizzle-kit
|
|
28
|
+
bower install drizzle-orm drizzle-kit
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Connecting to database
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { DbConnector } from "drizzle-orm";
|
|
35
|
+
|
|
36
|
+
// connect via postgresql connection url
|
|
37
|
+
const db = await new DbConnector()
|
|
38
|
+
.connectionString("postgres://user:password@host:port/db")
|
|
39
|
+
.connect();
|
|
40
|
+
|
|
41
|
+
// or by params
|
|
42
|
+
const db = await new DbConnector()
|
|
43
|
+
.params({
|
|
44
|
+
host: '0.0.0.0',
|
|
45
|
+
port: 5432,
|
|
46
|
+
user: 'user',
|
|
47
|
+
password: 'password',
|
|
48
|
+
db: 'optional_db_name'
|
|
49
|
+
}).connect();
|
|
50
|
+
```
|
|
51
|
+
## Project structure
|
|
52
|
+
- tables folder
|
|
53
|
+
- migrations folder
|
|
54
|
+
|
|
55
|
+
## Create tables
|
|
56
|
+
### Users Table
|
|
57
|
+
---
|
|
58
|
+
```typescript
|
|
59
|
+
|
|
60
|
+
export const rolesEnum = createEnum({ alias: 'test-enum', values: ['user', 'guest', 'admin'] });
|
|
61
|
+
|
|
62
|
+
export default class UsersTable extends AbstractTable<UsersTable> {
|
|
63
|
+
public id = this.serial('id').primaryKey();
|
|
64
|
+
public fullName = this.text('full_name');
|
|
65
|
+
|
|
66
|
+
public phone = this.varchar('phone', { size: 256 });
|
|
67
|
+
public media = this.jsonb<string[]>('media');
|
|
68
|
+
public decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();
|
|
69
|
+
public bigIntField = this.bigint('test1', 'max_bytes_53');
|
|
70
|
+
public role = this.type(rolesEnum, 'name_in_table').notNull();
|
|
71
|
+
|
|
72
|
+
public createdAt = this.timestamp('created_at').notNull();
|
|
73
|
+
|
|
74
|
+
public createdAtWithTimezone = this.timestamptz('created_at_time_zone');
|
|
75
|
+
|
|
76
|
+
public updatedAt = this.timestamp('updated_at').defaultValue(Defaults.CURRENT_TIMESTAMP);
|
|
77
|
+
public isArchived = this.bool('is_archived').defaultValue(false);
|
|
78
|
+
|
|
79
|
+
public phoneFullNameIndex = this.index([this.phone, this.fullName]);
|
|
80
|
+
public phoneIndex = this.uniqueIndex(this.phone);
|
|
81
|
+
|
|
82
|
+
public tableName(): string {
|
|
83
|
+
return 'users';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
### Cities Table
|
|
88
|
+
---
|
|
89
|
+
```typescript
|
|
90
|
+
interface CityMeta {
|
|
91
|
+
population: number,
|
|
92
|
+
connection: string,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export default class CitiesTable extends AbstractTable<CitiesTable> {
|
|
96
|
+
public id = this.serial('id').primaryKey();
|
|
97
|
+
|
|
98
|
+
public foundationDate = this.timestamp('name').notNull();
|
|
99
|
+
public location = this.varchar('page', { size: 256 });
|
|
100
|
+
|
|
101
|
+
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'CASCADE' });
|
|
102
|
+
|
|
103
|
+
public metadata = this.jsonb<CityMeta>('metadata');
|
|
104
|
+
|
|
105
|
+
public tableName(): string {
|
|
106
|
+
return 'cities';
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
### User Groups Table
|
|
111
|
+
---
|
|
112
|
+
```typescript
|
|
113
|
+
export default class UserGroupsTable extends AbstractTable<UserGroupsTable> {
|
|
114
|
+
public id = this.serial('id').primaryKey();
|
|
115
|
+
|
|
116
|
+
public name = this.varchar('name');
|
|
117
|
+
public description = this.varchar('description');
|
|
118
|
+
|
|
119
|
+
public tableName(): string {
|
|
120
|
+
return 'user_groups';
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
### User to User Groups Table
|
|
125
|
+
---
|
|
126
|
+
#### Many to many connection between Users and User Groups
|
|
127
|
+
```typescript
|
|
128
|
+
export default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {
|
|
129
|
+
public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, { onDelete: 'CASCADE' });
|
|
130
|
+
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'CASCADE' });
|
|
131
|
+
|
|
132
|
+
public manyToManyIndex = this.index([this.groupId, this.userId]);
|
|
133
|
+
|
|
134
|
+
public tableName(): string {
|
|
135
|
+
return 'users_to_user_groups';
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## CRUD
|
|
141
|
+
### **SELECT**
|
|
142
|
+
---
|
|
143
|
+
```typescript
|
|
144
|
+
const db = await new DbConnector()
|
|
145
|
+
.connectionString('postgresql://postgres@127.0.0.1/drizzle')
|
|
146
|
+
.connect();
|
|
147
|
+
|
|
148
|
+
const usersTable = new UsersTable(db);
|
|
149
|
+
|
|
150
|
+
// select all
|
|
151
|
+
const allSelect = await usersTable.select().all();
|
|
152
|
+
|
|
153
|
+
// select first
|
|
154
|
+
const firstSelect = await usersTable.select().findOne();
|
|
155
|
+
```
|
|
156
|
+
#### **Sorting and Filtering**
|
|
157
|
+
---
|
|
158
|
+
##### Select all records from `Users` where phone is `"hello"`
|
|
159
|
+
```typescript
|
|
160
|
+
const eqSelect = await usersTable.select().where(
|
|
161
|
+
eq(usersTable.phone, 'hello')
|
|
162
|
+
).all();
|
|
163
|
+
```
|
|
164
|
+
##### Select all records from `Users` where **both** phone is `"hello"` **and** phone is `"hello"`
|
|
165
|
+
```typescript
|
|
166
|
+
const andSelect = await usersTable.select().where(
|
|
167
|
+
and([
|
|
168
|
+
eq(usersTable.phone, 'hello'),
|
|
169
|
+
eq(usersTable.phone, 'hello')
|
|
170
|
+
]),
|
|
171
|
+
).all();
|
|
172
|
+
```
|
|
173
|
+
##### Select all records from `Users` where **either** phone is `"hello"` **or** phone is `"hello"`
|
|
174
|
+
```typescript
|
|
175
|
+
const orSelect = await usersTable.select().where(
|
|
176
|
+
or([eq(usersTable.phone, 'hello')]),
|
|
177
|
+
).all();
|
|
178
|
+
```
|
|
179
|
+
##### Select all records from `Users` using **LIMIT** and **OFFSET**
|
|
180
|
+
```typescript
|
|
181
|
+
const limitOffsetSelect = await usersTable.select().limit(10).offset(10).all();
|
|
182
|
+
```
|
|
183
|
+
##### Select all records from `Users` where `phone` contains `"hello"`
|
|
184
|
+
```typescript
|
|
185
|
+
const likeSelect = await usersTable.select().where(
|
|
186
|
+
like(usersTable.phone, '%hello%')
|
|
187
|
+
).all();
|
|
188
|
+
```
|
|
189
|
+
##### Select all records from `Users` where `phone` equals to some of values from array
|
|
190
|
+
```typescript
|
|
191
|
+
const inArraySelect = usersTable.select().where(
|
|
192
|
+
inArray(usersTable.phone, ['hello'])
|
|
193
|
+
).all();
|
|
194
|
+
```
|
|
195
|
+
##### Select all records from `Users` where `phone` greater(**>**) than `"hello"`
|
|
196
|
+
```typescript
|
|
197
|
+
const greaterSelect = usersTable.select().where(
|
|
198
|
+
greater(usersTable.phone, 'hello')
|
|
199
|
+
).all();
|
|
200
|
+
```
|
|
201
|
+
##### Select all records from `Users` where `phone` less(**<**) than `"hello"`
|
|
202
|
+
```typescript
|
|
203
|
+
const lessSelect = usersTable.select().where(
|
|
204
|
+
less(usersTable.phone, 'hello')
|
|
205
|
+
).all();
|
|
206
|
+
```
|
|
207
|
+
##### Select all records from `Users` where `phone` greater or equals(**>=**) than `"hello"`
|
|
208
|
+
```typescript
|
|
209
|
+
const greaterEqSelect = usersTable.select().where(
|
|
210
|
+
greaterEq(usersTable.phone, 'hello')
|
|
211
|
+
).all();
|
|
212
|
+
```
|
|
213
|
+
##### Select all records from `Users` where `phone` less or equals(**<=**)
|
|
214
|
+
```typescript
|
|
215
|
+
const lessEqSelect = usersTable.select().where(
|
|
216
|
+
lessEq(usersTable.phone, 'hello')
|
|
217
|
+
).all();
|
|
218
|
+
```
|
|
219
|
+
##### Select all records from `Users` where `phone` is **NULL**
|
|
220
|
+
```typescript
|
|
221
|
+
const isNullSelect = usersTable.select().where(
|
|
222
|
+
isNull(usersTable.phone)
|
|
223
|
+
).all();
|
|
224
|
+
```
|
|
225
|
+
##### Select all records from `Users` where `phone` not equals to `"hello"`
|
|
226
|
+
```typescript
|
|
227
|
+
const notEqSelect = usersTable.select().where(
|
|
228
|
+
notEq(usersTable.phone, 'hello')
|
|
229
|
+
).all();
|
|
230
|
+
```
|
|
231
|
+
##### Select all records from `Users` ordered by `phone` in ascending order
|
|
232
|
+
```typescript
|
|
233
|
+
const ordered = await usersTable.select().orderBy((table) => table.phone, Order.ASC).all();
|
|
234
|
+
```
|
|
235
|
+
#### **Partial Selecting**
|
|
236
|
+
```typescript
|
|
237
|
+
const partialSelect = await usersTable.select({
|
|
238
|
+
mappedId: usersTable.id,
|
|
239
|
+
mappedPhone: usersTable.phone,
|
|
240
|
+
}).all();
|
|
241
|
+
|
|
242
|
+
// Usage
|
|
243
|
+
const { mappedId, mappedPhone } = partialSelect;
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
### **Update**
|
|
248
|
+
---
|
|
249
|
+
##### Update `fullName` to `newName` in `Users` where phone is `"hello"`
|
|
250
|
+
```typescript
|
|
251
|
+
await usersTable.update()
|
|
252
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
253
|
+
.set({ fullName: 'newName' })
|
|
254
|
+
.execute();
|
|
255
|
+
```
|
|
256
|
+
##### Update `fullName` to `newName` in `Users` where phone is `"hello"` returning updated `User` model
|
|
257
|
+
```typescript
|
|
258
|
+
await usersTable.update()
|
|
259
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
260
|
+
.set({ fullName: 'newName' })
|
|
261
|
+
.all();
|
|
262
|
+
```
|
|
263
|
+
##### Update `fullName` to `newName` in `Users` where phone is `"hello"` returning updated `User` model
|
|
264
|
+
```typescript
|
|
265
|
+
await usersTable.update()
|
|
266
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
267
|
+
.set({ fullName: 'newName' })
|
|
268
|
+
.findOne();
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### **Delete**
|
|
272
|
+
##### Delete `user` where phone is `"hello"`
|
|
273
|
+
```typescript
|
|
274
|
+
await usersTable.delete()
|
|
275
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
276
|
+
.execute();
|
|
277
|
+
```
|
|
278
|
+
##### Delete `user` where phone is `"hello"` returning updated `User` model
|
|
279
|
+
```typescript
|
|
280
|
+
await usersTable.delete()
|
|
281
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
282
|
+
.all();
|
|
283
|
+
```
|
|
284
|
+
##### Delete `user` where phone is `"hello"` returning updated `User` model
|
|
285
|
+
```typescript
|
|
286
|
+
await usersTable.delete()
|
|
287
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
288
|
+
.findOne();
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### **Insert**
|
|
292
|
+
##### Insert `user` with required fields
|
|
293
|
+
```typescript
|
|
294
|
+
await usersTable.insert({
|
|
295
|
+
test: 1,
|
|
296
|
+
createdAt: new Date(),
|
|
297
|
+
}).execute();
|
|
298
|
+
```
|
|
299
|
+
##### Insert `user` with required fields and get all rows as array
|
|
300
|
+
```typescript
|
|
301
|
+
const user = await usersTable.insert({
|
|
302
|
+
test: 1,
|
|
303
|
+
createdAt: new Date(),
|
|
304
|
+
}).all();
|
|
305
|
+
```
|
|
306
|
+
##### Insert `user` with required fields and get inserted entity
|
|
307
|
+
```typescript
|
|
308
|
+
const user = await usersTable.insert({
|
|
309
|
+
test: 1,
|
|
310
|
+
createdAt: new Date(),
|
|
311
|
+
}).findOne();
|
|
312
|
+
```
|
|
313
|
+
##### Insert many `users` with required fields and get all inserted entities
|
|
314
|
+
```typescript
|
|
315
|
+
const users = await usersTable.insertMany([{
|
|
316
|
+
test: 1,
|
|
317
|
+
createdAt: new Date(),
|
|
318
|
+
}, {
|
|
319
|
+
test: 2,
|
|
320
|
+
createdAt: new Date(),
|
|
321
|
+
}]).all();
|
|
322
|
+
```
|
|
323
|
+
##### Insert many `users` with required fields and get all inserted entities. If such user already exists - update `phone` field
|
|
324
|
+
```typescript
|
|
325
|
+
await usersTable.insertMany([{
|
|
326
|
+
test: 1,
|
|
327
|
+
createdAt: new Date(),
|
|
328
|
+
}, {
|
|
329
|
+
test: 2,
|
|
330
|
+
createdAt: new Date(),
|
|
331
|
+
}])
|
|
332
|
+
.onConflict(
|
|
333
|
+
(table) => table.phoneIndex,
|
|
334
|
+
{ phone: 'confilctUpdate' },
|
|
335
|
+
).all();
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Joins
|
|
339
|
+
### Join One-To-Many Tables
|
|
340
|
+
##### Join Cities with Users and map to city object with full user
|
|
341
|
+
```typescript
|
|
342
|
+
const usersTable = new UsersTable(db);
|
|
343
|
+
const citiesTable = new CitiesTable(db);
|
|
344
|
+
|
|
345
|
+
const userWithCities = await citiesTable.select()
|
|
346
|
+
.where(eq(citiesTable.id, 1))
|
|
347
|
+
.leftJoin(UsersTable,
|
|
348
|
+
(city) => city.userId,
|
|
349
|
+
(users) => users.id)
|
|
350
|
+
.execute();
|
|
351
|
+
|
|
352
|
+
const citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Join Many-To-Many Tables
|
|
356
|
+
##### Join User Groups with Users, using many-to-many table and map response to get user object with groups array
|
|
357
|
+
```typescript
|
|
358
|
+
const usersWithUserGroups = await usersToUserGroupsTable.select()
|
|
359
|
+
.where(eq(userGroupsTable.id, 1))
|
|
360
|
+
.leftJoin(UsersTable,
|
|
361
|
+
(userToGroup) => userToGroup.userId,
|
|
362
|
+
(users) => users.id)
|
|
363
|
+
.leftJoin(UsersToUserGroupsTable, UserGroupsTable,
|
|
364
|
+
(userToGroup) => userToGroup.groupId,
|
|
365
|
+
(users) => users.id)
|
|
366
|
+
.execute();
|
|
367
|
+
|
|
368
|
+
const userGroupWithUsers = usersWithUserGroups.group({
|
|
369
|
+
one: (_, dbUser, dbUserGroup) => dbUser!,
|
|
370
|
+
many: (_, dbUser, dbUserGroup) => dbUserGroup!,
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const userWithGroups: ExtractModel<UsersTable> & { groups: ExtractModel<UserGroupsTable>[] } = {
|
|
374
|
+
...userGroupWithUsers.one,
|
|
375
|
+
groups: userGroupWithUsers.many,
|
|
376
|
+
};
|
|
377
|
+
```
|
|
378
|
+
##### Join User Groups with Users, using many-to-many table and map response to get user group object with users array
|
|
379
|
+
```typescript
|
|
380
|
+
const usersWithUserGroups = await usersToUserGroupsTable.select()
|
|
381
|
+
.where(eq(userGroupsTable.id, 1))
|
|
382
|
+
.leftJoin(UsersTable,
|
|
383
|
+
(userToGroup) => userToGroup.userId,
|
|
384
|
+
(users) => users.id)
|
|
385
|
+
.leftJoin(UsersToUserGroupsTable, UserGroupsTable,
|
|
386
|
+
(userToGroup) => userToGroup.groupId,
|
|
387
|
+
(users) => users.id)
|
|
388
|
+
.execute();
|
|
389
|
+
|
|
390
|
+
const userGroupWithUsers = usersWithUserGroups.group({
|
|
391
|
+
one: (_, dbUser, dbUserGroup) => dbUserGroup!,
|
|
392
|
+
many: (_, dbUser, dbUserGroup) => dbUser!,
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
const userWithGroups: ExtractModel<UserGroupsTable> & { users: ExtractModel<UsersTable>[] } = {
|
|
396
|
+
...userGroupWithUsers.one,
|
|
397
|
+
users: userGroupWithUsers.many,
|
|
398
|
+
};
|
|
399
|
+
```
|
|
400
|
+
### Join using partial field select
|
|
401
|
+
##### Join Cities with Users getting only needed fields form request
|
|
402
|
+
```typescript
|
|
403
|
+
await citiesTable.select({
|
|
404
|
+
id: citiesTable.id,
|
|
405
|
+
userId: citiesTable.userId,
|
|
406
|
+
})
|
|
407
|
+
.where(eq(citiesTable.id, 1))
|
|
408
|
+
.leftJoin(UsersTable,
|
|
409
|
+
(city) => city.userId,
|
|
410
|
+
(users) => users.id,
|
|
411
|
+
{
|
|
412
|
+
id: usersTable.id,
|
|
413
|
+
})
|
|
414
|
+
.execute();
|
|
415
|
+
|
|
416
|
+
const citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
## Migrations
|
|
421
|
+
#### To run migrations generated by drizzle-kit you could use `Migrator` class
|
|
422
|
+
##### Provide drizzle-kit config path
|
|
423
|
+
```typescript
|
|
424
|
+
await drizzle.migrator(db).migrate('src/drizzle.config.yaml');
|
|
425
|
+
```
|
|
426
|
+
##### Another possibility is to provide object with path to folder with migrations
|
|
427
|
+
```typescript
|
|
428
|
+
await drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
## Raw query usage
|
|
433
|
+
#### If you have some complex queries to execute and drizzle-orm can't handle them yet, then you could use `rawQuery` execution
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
##### Execute custom raw query
|
|
437
|
+
```typescript
|
|
438
|
+
const res: QueryResult<any> = await db.session().execute('SELECT * FROM users WHERE user.id = $1', [1]);
|
|
439
|
+
```
|
|
@@ -99,7 +99,7 @@ class SelectTRB extends abstractRequestBuilder_1.default {
|
|
|
99
99
|
const toColumn = to(toTable);
|
|
100
100
|
const join = new __1.JoinWith(toTable.tableName(), toTable.mapServiceToDb())
|
|
101
101
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.INNER_JOIN);
|
|
102
|
-
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial);
|
|
102
|
+
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial, this._logger);
|
|
103
103
|
}
|
|
104
104
|
leftJoin(table, from, to, partial) {
|
|
105
105
|
const toTable = this._table.db.create(table);
|
|
@@ -107,7 +107,7 @@ class SelectTRB extends abstractRequestBuilder_1.default {
|
|
|
107
107
|
const toColumn = to(toTable);
|
|
108
108
|
const join = new __1.JoinWith(toTable.tableName(), toTable.mapServiceToDb())
|
|
109
109
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.LEFT_JOIN);
|
|
110
|
-
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial);
|
|
110
|
+
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial, this._logger);
|
|
111
111
|
}
|
|
112
112
|
rightJoin(table, from, to, partial) {
|
|
113
113
|
const toTable = this._table.db.create(table);
|
|
@@ -115,7 +115,7 @@ class SelectTRB extends abstractRequestBuilder_1.default {
|
|
|
115
115
|
const toColumn = to(toTable);
|
|
116
116
|
const join = new __1.JoinWith(toTable.tableName(), toTable.mapServiceToDb())
|
|
117
117
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.RIGHT_JOIN);
|
|
118
|
-
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial);
|
|
118
|
+
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial, this._logger);
|
|
119
119
|
}
|
|
120
120
|
fullJoin(table, from, to, partial) {
|
|
121
121
|
const toTable = this._table.db.create(table);
|
|
@@ -123,7 +123,7 @@ class SelectTRB extends abstractRequestBuilder_1.default {
|
|
|
123
123
|
const toColumn = to(toTable);
|
|
124
124
|
const join = new __1.JoinWith(toTable.tableName(), toTable.mapServiceToDb())
|
|
125
125
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.FULL_JOIN);
|
|
126
|
-
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial);
|
|
126
|
+
return new selectWithJoin_1.default(this._table, this._session, this._filter, join, this.props, this.__orderBy, this.__order, this.__distinct, this.__partial, partial, this._logger);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
exports.default = SelectTRB;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BaseLogger from 'orm/src/logger/abstractLogger';
|
|
1
2
|
import { QueryResult } from 'pg';
|
|
2
3
|
import { AbstractColumn } from '../../../columns/column';
|
|
3
4
|
import ColumnType from '../../../columns/types/columnType';
|
|
@@ -19,10 +20,11 @@ export default abstract class AbstractJoined<TTable extends AbstractTable<TTable
|
|
|
19
20
|
protected _orderBy?: AbstractColumn<ColumnType, boolean, boolean>;
|
|
20
21
|
protected _order?: Order;
|
|
21
22
|
protected _partial?: TPartial;
|
|
23
|
+
protected _logger?: BaseLogger;
|
|
22
24
|
constructor(table: TTable, filter: Expr, session: ISession, props: {
|
|
23
25
|
limit?: number;
|
|
24
26
|
offset?: number;
|
|
25
|
-
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial);
|
|
27
|
+
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, logger?: BaseLogger);
|
|
26
28
|
limit: (limit: number) => this;
|
|
27
29
|
offset: (offset: number) => this;
|
|
28
30
|
execute: () => Promise<TRes>;
|
|
@@ -26,7 +26,7 @@ const builderError_1 = __importStar(require("../../../errors/builderError"));
|
|
|
26
26
|
const responseMapper_1 = __importDefault(require("../../../mappers/responseMapper"));
|
|
27
27
|
const select_1 = __importDefault(require("../../lowLvlBuilders/selects/select"));
|
|
28
28
|
class AbstractJoined {
|
|
29
|
-
constructor(table, filter, session, props, orderBy, order, distinct, tablePartial) {
|
|
29
|
+
constructor(table, filter, session, props, orderBy, order, distinct, tablePartial, logger) {
|
|
30
30
|
this.limit = (limit) => {
|
|
31
31
|
this._props.limit = limit;
|
|
32
32
|
return this;
|
|
@@ -54,6 +54,10 @@ class AbstractJoined {
|
|
|
54
54
|
catch (e) {
|
|
55
55
|
throw new builderError_1.default(builderError_1.BuilderType.JOINED_SELECT, this._table.tableName(), Object.values(this._table.mapServiceToDb()), e, this._session, this._filter);
|
|
56
56
|
}
|
|
57
|
+
if (this._logger) {
|
|
58
|
+
this._logger.info(`Selecting from ${this._table.tableName()} using query:\n ${query}`);
|
|
59
|
+
this._logger.info(`Values for query:\n ${values}`);
|
|
60
|
+
}
|
|
57
61
|
const result = await this._session.execute(query, values);
|
|
58
62
|
return this.mapResponse(result);
|
|
59
63
|
};
|
|
@@ -65,6 +69,7 @@ class AbstractJoined {
|
|
|
65
69
|
this._orderBy = orderBy;
|
|
66
70
|
this._distinct = distinct;
|
|
67
71
|
this._partial = tablePartial;
|
|
72
|
+
this._logger = logger;
|
|
68
73
|
}
|
|
69
74
|
fullOrPartial(mappedServiceToDb, result, partial, joinId) {
|
|
70
75
|
if (partial) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BaseLogger from 'orm/src/logger/abstractLogger';
|
|
1
2
|
import { QueryResult } from 'pg';
|
|
2
3
|
import { AbstractColumn } from '../../../columns/column';
|
|
3
4
|
import ColumnType from '../../../columns/types/columnType';
|
|
@@ -23,7 +24,7 @@ export default class SelectTRBWithFiveJoins<TTable extends AbstractTable<TTable>
|
|
|
23
24
|
constructor(table: TTable, session: ISession, filter: Expr, join1: Join<TTable1>, join2: Join<TTable2>, join3: Join<TTable3>, join4: Join<TTable4>, join5: Join<TTable5>, props: {
|
|
24
25
|
limit?: number;
|
|
25
26
|
offset?: number;
|
|
26
|
-
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, joinedPartial2?: TPartial3, joinedPartial3?: TPartial4, joinedPartial4?: TPartial5);
|
|
27
|
+
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, joinedPartial2?: TPartial3, joinedPartial3?: TPartial4, joinedPartial4?: TPartial5, logger?: BaseLogger);
|
|
27
28
|
protected joins(): Array<{
|
|
28
29
|
join: Join<any>;
|
|
29
30
|
partial?: {
|
|
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const selectResponseFiveJoins_1 = __importDefault(require("../responses/selectResponseFiveJoins"));
|
|
7
7
|
const abstractJoinBuilder_1 = __importDefault(require("./abstractJoinBuilder"));
|
|
8
8
|
class SelectTRBWithFiveJoins extends abstractJoinBuilder_1.default {
|
|
9
|
-
constructor(table, session, filter, join1, join2, join3, join4, join5, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, joinedPartial2, joinedPartial3, joinedPartial4) {
|
|
10
|
-
super(table, filter, session, props, orderBy, order, distinct, tablePartial);
|
|
9
|
+
constructor(table, session, filter, join1, join2, join3, join4, join5, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, joinedPartial2, joinedPartial3, joinedPartial4, logger) {
|
|
10
|
+
super(table, filter, session, props, orderBy, order, distinct, tablePartial, logger);
|
|
11
11
|
this._join1 = join1;
|
|
12
12
|
this._join2 = join2;
|
|
13
13
|
this._join3 = join3;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BaseLogger from 'orm/src/logger/abstractLogger';
|
|
1
2
|
import { QueryResult } from 'pg';
|
|
2
3
|
import { AbstractColumn } from '../../../columns/column';
|
|
3
4
|
import ColumnType from '../../../columns/types/columnType';
|
|
@@ -23,7 +24,7 @@ export default class SelectTRBWithFourJoins<TTable extends AbstractTable<TTable>
|
|
|
23
24
|
constructor(table: TTable, session: ISession, filter: Expr, join1: Join<TTable1>, join2: Join<TTable2>, join3: Join<TTable3>, join4: Join<TTable4>, props: {
|
|
24
25
|
limit?: number;
|
|
25
26
|
offset?: number;
|
|
26
|
-
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, joinedPartial2?: TPartial3, joinedPartial3?: TPartial4);
|
|
27
|
+
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, joinedPartial2?: TPartial3, joinedPartial3?: TPartial4, logger?: BaseLogger);
|
|
27
28
|
innerJoin<InputTable extends AbstractTable<InputTable>, TColumn extends ColumnType, TToColumn extends ColumnType, IToTable extends AbstractTable<IToTable>, IToPartial extends PartialFor<IToTable> = {}>(fromTable: {
|
|
28
29
|
new (db: DB): InputTable;
|
|
29
30
|
}, table: {
|
|
@@ -9,8 +9,8 @@ const selectResponseFourJoins_1 = __importDefault(require("../responses/selectRe
|
|
|
9
9
|
const abstractJoinBuilder_1 = __importDefault(require("./abstractJoinBuilder"));
|
|
10
10
|
const selectWithFiveJoins_1 = __importDefault(require("./selectWithFiveJoins"));
|
|
11
11
|
class SelectTRBWithFourJoins extends abstractJoinBuilder_1.default {
|
|
12
|
-
constructor(table, session, filter, join1, join2, join3, join4, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, joinedPartial2, joinedPartial3) {
|
|
13
|
-
super(table, filter, session, props, orderBy, order, distinct, tablePartial);
|
|
12
|
+
constructor(table, session, filter, join1, join2, join3, join4, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, joinedPartial2, joinedPartial3, logger) {
|
|
13
|
+
super(table, filter, session, props, orderBy, order, distinct, tablePartial, logger);
|
|
14
14
|
this._join1 = join1;
|
|
15
15
|
this._join2 = join2;
|
|
16
16
|
this._join3 = join3;
|
|
@@ -27,7 +27,7 @@ class SelectTRBWithFourJoins extends abstractJoinBuilder_1.default {
|
|
|
27
27
|
const toColumn = to(toTable);
|
|
28
28
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
29
29
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.INNER_JOIN);
|
|
30
|
-
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial);
|
|
30
|
+
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial, this._logger);
|
|
31
31
|
}
|
|
32
32
|
leftJoin(fromTable, table, from, to, partial) {
|
|
33
33
|
const toTable = this._table.db.create(table);
|
|
@@ -36,7 +36,7 @@ class SelectTRBWithFourJoins extends abstractJoinBuilder_1.default {
|
|
|
36
36
|
const toColumn = to(toTable);
|
|
37
37
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
38
38
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.LEFT_JOIN);
|
|
39
|
-
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial);
|
|
39
|
+
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial, this._logger);
|
|
40
40
|
}
|
|
41
41
|
rightJoin(fromTable, table, from, to, partial) {
|
|
42
42
|
const toTable = this._table.db.create(table);
|
|
@@ -45,7 +45,7 @@ class SelectTRBWithFourJoins extends abstractJoinBuilder_1.default {
|
|
|
45
45
|
const toColumn = to(toTable);
|
|
46
46
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
47
47
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.RIGHT_JOIN);
|
|
48
|
-
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial);
|
|
48
|
+
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial, this._logger);
|
|
49
49
|
}
|
|
50
50
|
fullJoin(fromTable, table, from, to, partial) {
|
|
51
51
|
const toTable = this._table.db.create(table);
|
|
@@ -54,7 +54,7 @@ class SelectTRBWithFourJoins extends abstractJoinBuilder_1.default {
|
|
|
54
54
|
const toColumn = to(toTable);
|
|
55
55
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
56
56
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.FULL_JOIN);
|
|
57
|
-
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial);
|
|
57
|
+
return new selectWithFiveJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, this._join4, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, this._joinedPartial3, partial, this._logger);
|
|
58
58
|
}
|
|
59
59
|
joins() {
|
|
60
60
|
return [{ join: this._join1, partial: this._joinedPartial, id: 1 },
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BaseLogger from 'orm/src/logger/abstractLogger';
|
|
1
2
|
import { QueryResult } from 'pg';
|
|
2
3
|
import { AbstractColumn } from '../../../columns/column';
|
|
3
4
|
import ColumnType from '../../../columns/types/columnType';
|
|
@@ -17,7 +18,7 @@ export default class SelectTRBWithJoin<TTable extends AbstractTable<TTable>, TTa
|
|
|
17
18
|
constructor(table: TTable, session: ISession, filter: Expr, join: Join<TTable1>, props: {
|
|
18
19
|
limit?: number;
|
|
19
20
|
offset?: number;
|
|
20
|
-
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1);
|
|
21
|
+
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, logger?: BaseLogger);
|
|
21
22
|
innerJoin<InputTable extends AbstractTable<InputTable>, TColumn extends ColumnType, TToColumn extends ColumnType, IToTable extends AbstractTable<IToTable>, IToPartial extends PartialFor<IToTable> = {}>(fromTable: {
|
|
22
23
|
new (db: DB): InputTable;
|
|
23
24
|
}, table: {
|
|
@@ -10,8 +10,8 @@ const abstractJoinBuilder_1 = __importDefault(require("./abstractJoinBuilder"));
|
|
|
10
10
|
const selectWithTwoJoins_1 = __importDefault(require("./selectWithTwoJoins"));
|
|
11
11
|
// eslint-disable-next-line max-len
|
|
12
12
|
class SelectTRBWithJoin extends abstractJoinBuilder_1.default {
|
|
13
|
-
constructor(table, session, filter, join, props, orderBy, order, distinct, tablePartial, joinedPartial) {
|
|
14
|
-
super(table, filter, session, props, orderBy, order, distinct, tablePartial);
|
|
13
|
+
constructor(table, session, filter, join, props, orderBy, order, distinct, tablePartial, joinedPartial, logger) {
|
|
14
|
+
super(table, filter, session, props, orderBy, order, distinct, tablePartial, logger);
|
|
15
15
|
this._join = join;
|
|
16
16
|
this._joinedPartial = joinedPartial;
|
|
17
17
|
}
|
|
@@ -22,7 +22,7 @@ class SelectTRBWithJoin extends abstractJoinBuilder_1.default {
|
|
|
22
22
|
const toColumn = to(toTable);
|
|
23
23
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
24
24
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.INNER_JOIN);
|
|
25
|
-
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial);
|
|
25
|
+
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial, this._logger);
|
|
26
26
|
}
|
|
27
27
|
leftJoin(fromTable, table, from, to, partial) {
|
|
28
28
|
const toTable = this._table.db.create(table);
|
|
@@ -31,7 +31,7 @@ class SelectTRBWithJoin extends abstractJoinBuilder_1.default {
|
|
|
31
31
|
const toColumn = to(toTable);
|
|
32
32
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
33
33
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.LEFT_JOIN);
|
|
34
|
-
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial);
|
|
34
|
+
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial, this._logger);
|
|
35
35
|
}
|
|
36
36
|
rightJoin(fromTable, table, from, to, partial) {
|
|
37
37
|
const toTable = this._table.db.create(table);
|
|
@@ -40,7 +40,7 @@ class SelectTRBWithJoin extends abstractJoinBuilder_1.default {
|
|
|
40
40
|
const toColumn = to(toTable);
|
|
41
41
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
42
42
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.RIGHT_JOIN);
|
|
43
|
-
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial);
|
|
43
|
+
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial, this._logger);
|
|
44
44
|
}
|
|
45
45
|
fullJoin(fromTable, table, from, to, partial) {
|
|
46
46
|
const toTable = this._table.db.create(table);
|
|
@@ -49,7 +49,7 @@ class SelectTRBWithJoin extends abstractJoinBuilder_1.default {
|
|
|
49
49
|
const toColumn = to(toTable);
|
|
50
50
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
51
51
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.FULL_JOIN);
|
|
52
|
-
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial);
|
|
52
|
+
return new selectWithTwoJoins_1.default(this._table, this._session, this._filter, this._join, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, partial, this._logger);
|
|
53
53
|
}
|
|
54
54
|
joins() {
|
|
55
55
|
return [{ join: this._join, partial: this._joinedPartial, id: 1 }];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BaseLogger from 'orm/src/logger/abstractLogger';
|
|
1
2
|
import { QueryResult } from 'pg';
|
|
2
3
|
import { AbstractColumn } from '../../../columns/column';
|
|
3
4
|
import ColumnType from '../../../columns/types/columnType';
|
|
@@ -21,7 +22,7 @@ export default class SelectTRBWithThreeJoins<TTable extends AbstractTable<TTable
|
|
|
21
22
|
constructor(table: TTable, session: ISession, filter: Expr, join1: Join<TTable1>, join2: Join<TTable2>, join3: Join<TTable3>, props: {
|
|
22
23
|
limit?: number;
|
|
23
24
|
offset?: number;
|
|
24
|
-
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, joinedPartial2?: TPartial3);
|
|
25
|
+
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, joinedPartial2?: TPartial3, logger?: BaseLogger);
|
|
25
26
|
innerJoin<InputTable extends AbstractTable<InputTable>, TColumn extends ColumnType, TToColumn extends ColumnType, IToTable extends AbstractTable<IToTable>, IToPartial extends PartialFor<IToTable> = {}>(fromTable: {
|
|
26
27
|
new (db: DB): InputTable;
|
|
27
28
|
}, table: {
|
|
@@ -9,8 +9,8 @@ const selectResponseThreeJoins_1 = __importDefault(require("../responses/selectR
|
|
|
9
9
|
const abstractJoinBuilder_1 = __importDefault(require("./abstractJoinBuilder"));
|
|
10
10
|
const selectWithFourJoins_1 = __importDefault(require("./selectWithFourJoins"));
|
|
11
11
|
class SelectTRBWithThreeJoins extends abstractJoinBuilder_1.default {
|
|
12
|
-
constructor(table, session, filter, join1, join2, join3, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, joinedPartial2) {
|
|
13
|
-
super(table, filter, session, props, orderBy, order, distinct, tablePartial);
|
|
12
|
+
constructor(table, session, filter, join1, join2, join3, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, joinedPartial2, logger) {
|
|
13
|
+
super(table, filter, session, props, orderBy, order, distinct, tablePartial, logger);
|
|
14
14
|
this._join1 = join1;
|
|
15
15
|
this._join2 = join2;
|
|
16
16
|
this._join3 = join3;
|
|
@@ -25,7 +25,7 @@ class SelectTRBWithThreeJoins extends abstractJoinBuilder_1.default {
|
|
|
25
25
|
const toColumn = to(toTable);
|
|
26
26
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
27
27
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.INNER_JOIN);
|
|
28
|
-
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial);
|
|
28
|
+
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial, this._logger);
|
|
29
29
|
}
|
|
30
30
|
leftJoin(fromTable, table, from, to, partial) {
|
|
31
31
|
const toTable = this._table.db.create(table);
|
|
@@ -34,7 +34,7 @@ class SelectTRBWithThreeJoins extends abstractJoinBuilder_1.default {
|
|
|
34
34
|
const toColumn = to(toTable);
|
|
35
35
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
36
36
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.LEFT_JOIN);
|
|
37
|
-
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial);
|
|
37
|
+
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial, this._logger);
|
|
38
38
|
}
|
|
39
39
|
rightJoin(fromTable, table, from, to, partial) {
|
|
40
40
|
const toTable = this._table.db.create(table);
|
|
@@ -43,7 +43,7 @@ class SelectTRBWithThreeJoins extends abstractJoinBuilder_1.default {
|
|
|
43
43
|
const toColumn = to(toTable);
|
|
44
44
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
45
45
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.RIGHT_JOIN);
|
|
46
|
-
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial);
|
|
46
|
+
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial, this._logger);
|
|
47
47
|
}
|
|
48
48
|
fullJoin(fromTable, table, from, to, partial) {
|
|
49
49
|
const toTable = this._table.db.create(table);
|
|
@@ -52,7 +52,7 @@ class SelectTRBWithThreeJoins extends abstractJoinBuilder_1.default {
|
|
|
52
52
|
const toColumn = to(toTable);
|
|
53
53
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
54
54
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.FULL_JOIN);
|
|
55
|
-
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial);
|
|
55
|
+
return new selectWithFourJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, this._join3, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, this._joinedPartial2, partial, this._logger);
|
|
56
56
|
}
|
|
57
57
|
mapResponse(result) {
|
|
58
58
|
const parent = this._join1.mappedServiceToDb;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BaseLogger from 'orm/src/logger/abstractLogger';
|
|
1
2
|
import { QueryResult } from 'pg';
|
|
2
3
|
import { AbstractColumn } from '../../../columns/column';
|
|
3
4
|
import ColumnType from '../../../columns/types/columnType';
|
|
@@ -19,7 +20,7 @@ export default class SelectTRBWithTwoJoins<TTable extends AbstractTable<TTable>,
|
|
|
19
20
|
constructor(table: TTable, session: ISession, filter: Expr, join1: Join<TTable1>, join2: Join<TTable2>, props: {
|
|
20
21
|
limit?: number;
|
|
21
22
|
offset?: number;
|
|
22
|
-
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2);
|
|
23
|
+
}, orderBy?: AbstractColumn<ColumnType, boolean, boolean>, order?: Order, distinct?: AbstractColumn<ColumnType, boolean, boolean>, tablePartial?: TPartial, joinedPartial?: TPartial1, joinedPartial1?: TPartial2, logger?: BaseLogger);
|
|
23
24
|
innerJoin<InputTable extends AbstractTable<InputTable>, TColumn extends ColumnType, TToColumn extends ColumnType, IToTable extends AbstractTable<IToTable>, IToPartial extends PartialFor<IToTable> = {}>(fromTable: {
|
|
24
25
|
new (db: DB): InputTable;
|
|
25
26
|
}, table: {
|
|
@@ -9,8 +9,8 @@ const selectResponseTwoJoins_1 = __importDefault(require("../responses/selectRes
|
|
|
9
9
|
const abstractJoinBuilder_1 = __importDefault(require("./abstractJoinBuilder"));
|
|
10
10
|
const selectWithThreeJoins_1 = __importDefault(require("./selectWithThreeJoins"));
|
|
11
11
|
class SelectTRBWithTwoJoins extends abstractJoinBuilder_1.default {
|
|
12
|
-
constructor(table, session, filter, join1, join2, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1) {
|
|
13
|
-
super(table, filter, session, props, orderBy, order, distinct, tablePartial);
|
|
12
|
+
constructor(table, session, filter, join1, join2, props, orderBy, order, distinct, tablePartial, joinedPartial, joinedPartial1, logger) {
|
|
13
|
+
super(table, filter, session, props, orderBy, order, distinct, tablePartial, logger);
|
|
14
14
|
this._join1 = join1;
|
|
15
15
|
this._join2 = join2;
|
|
16
16
|
this._joinedPartial = joinedPartial;
|
|
@@ -23,7 +23,7 @@ class SelectTRBWithTwoJoins extends abstractJoinBuilder_1.default {
|
|
|
23
23
|
const toColumn = to(toTable);
|
|
24
24
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
25
25
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.INNER_JOIN);
|
|
26
|
-
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial);
|
|
26
|
+
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial, this._logger);
|
|
27
27
|
}
|
|
28
28
|
leftJoin(fromTable, table, from, to, partial) {
|
|
29
29
|
const toTable = this._table.db.create(table);
|
|
@@ -32,7 +32,7 @@ class SelectTRBWithTwoJoins extends abstractJoinBuilder_1.default {
|
|
|
32
32
|
const toColumn = to(toTable);
|
|
33
33
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
34
34
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.LEFT_JOIN);
|
|
35
|
-
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial);
|
|
35
|
+
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial, this._logger);
|
|
36
36
|
}
|
|
37
37
|
rightJoin(fromTable, table, from, to, partial) {
|
|
38
38
|
const toTable = this._table.db.create(table);
|
|
@@ -41,7 +41,7 @@ class SelectTRBWithTwoJoins extends abstractJoinBuilder_1.default {
|
|
|
41
41
|
const toColumn = to(toTable);
|
|
42
42
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
43
43
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.RIGHT_JOIN);
|
|
44
|
-
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial);
|
|
44
|
+
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial, this._logger);
|
|
45
45
|
}
|
|
46
46
|
fullJoin(fromTable, table, from, to, partial) {
|
|
47
47
|
const toTable = this._table.db.create(table);
|
|
@@ -50,7 +50,7 @@ class SelectTRBWithTwoJoins extends abstractJoinBuilder_1.default {
|
|
|
50
50
|
const toColumn = to(toTable);
|
|
51
51
|
const join = new joinWith_1.default(toTable.tableName(), toTable.mapServiceToDb())
|
|
52
52
|
.columns(fromColumn, toColumn).joinStrategy(join_1.JoinStrategy.FULL_JOIN);
|
|
53
|
-
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial);
|
|
53
|
+
return new selectWithThreeJoins_1.default(this._table, this._session, this._filter, this._join1, this._join2, join, this._props, this._orderBy, this._order, this._distinct, this._partial, this._joinedPartial, this._joinedPartial1, partial, this._logger);
|
|
54
54
|
}
|
|
55
55
|
mapResponse(result) {
|
|
56
56
|
const parent = this._join1.mappedServiceToDb;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drizzle-orm",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.44",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -34,5 +34,6 @@
|
|
|
34
34
|
"ts": "tsc",
|
|
35
35
|
"lint": "eslint ./src --ext .ts",
|
|
36
36
|
"run": "ts-node src/tables/newAbstractTable.ts"
|
|
37
|
-
}
|
|
37
|
+
},
|
|
38
|
+
"readme": "# DrizzleORM\n\n**DrizzleORM** is an ORM framework for \n[TypeScript](https://www.typescriptlang.org/).\nIt offers you several levels of Database communication:\n* Typesafe Table View approach \n* Typesafe Query Builder\n* Simple SQL query execution\n\nDrizzle ORM is highly influenced by [Exposed](https://github.com/JetBrains/Exposed) and Jetbrains development methodology\n\n## Supported Databases\n\n* PostgreSQL\n\n## Links\n\nIn Progress\n\n## Installing\n\n```bash\nnpm install drizzle-orm drizzle-kit\n```\n#### **In Progress**\n```bash\nyarn add drizzle-orm drizzle-kit\nbower install drizzle-orm drizzle-kit\n```\n\n## Connecting to database\n\n```tsx\nimport { DbConnector } from \"drizzle-orm\";\n\n// connect via postgresql connection url\nconst db = await new DbConnector()\n\t.connectionString(\"postgres://user:password@host:port/db\")\n\t.connect();\n\n// or by params\nconst db = await new DbConnector()\n\t.params({\n\t\thost: '0.0.0.0',\n\t\tport: 5432,\n\t\tuser: 'user',\n\t\tpassword: 'password',\n\t\tdb: 'optional_db_name'\n\t}).connect();\n```\n## Project structure\n- tables folder\n- migrations folder\n\n## Create tables\n### Users Table\n---\n```typescript\n\nexport const rolesEnum = createEnum({ alias: 'test-enum', values: ['user', 'guest', 'admin'] });\n\nexport default class UsersTable extends AbstractTable<UsersTable> {\n public id = this.serial('id').primaryKey();\n public fullName = this.text('full_name');\n\n public phone = this.varchar('phone', { size: 256 });\n public media = this.jsonb<string[]>('media');\n public decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();\n public bigIntField = this.bigint('test1', 'max_bytes_53');\n public role = this.type(rolesEnum, 'name_in_table').notNull();\n\n public createdAt = this.timestamp('created_at').notNull();\n\n public createdAtWithTimezone = this.timestamptz('created_at_time_zone');\n\n public updatedAt = this.timestamp('updated_at').defaultValue(Defaults.CURRENT_TIMESTAMP);\n public isArchived = this.bool('is_archived').defaultValue(false);\n\n public phoneFullNameIndex = this.index([this.phone, this.fullName]);\n public phoneIndex = this.uniqueIndex(this.phone);\n\n public tableName(): string {\n return 'users';\n }\n}\n```\n### Cities Table\n---\n```typescript\ninterface CityMeta {\n population: number,\n connection: string,\n}\n\nexport default class CitiesTable extends AbstractTable<CitiesTable> {\n public id = this.serial('id').primaryKey();\n\n public foundationDate = this.timestamp('name').notNull();\n public location = this.varchar('page', { size: 256 });\n\n public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'CASCADE' });\n\n public metadata = this.jsonb<CityMeta>('metadata');\n\n public tableName(): string {\n return 'cities';\n }\n}\n```\n### User Groups Table\n---\n```typescript\nexport default class UserGroupsTable extends AbstractTable<UserGroupsTable> {\n public id = this.serial('id').primaryKey();\n\n public name = this.varchar('name');\n public description = this.varchar('description');\n\n public tableName(): string {\n return 'user_groups';\n }\n}\n```\n### User to User Groups Table\n---\n#### Many to many connection between Users and User Groups\n```typescript\nexport default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {\n public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, { onDelete: 'CASCADE' });\n public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'CASCADE' });\n\n public manyToManyIndex = this.index([this.groupId, this.userId]);\n\n public tableName(): string {\n return 'users_to_user_groups';\n }\n}\n```\n\n## CRUD\n### **SELECT**\n---\n```typescript\nconst db = await new DbConnector()\n .connectionString('postgresql://postgres@127.0.0.1/drizzle')\n .connect();\n\nconst usersTable = new UsersTable(db);\n\n// select all\nconst allSelect = await usersTable.select().all();\n\n// select first\nconst firstSelect = await usersTable.select().findOne();\n```\n#### **Sorting and Filtering**\n---\n##### Select all records from `Users` where phone is `\"hello\"`\n```typescript\nconst eqSelect = await usersTable.select().where(\n eq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where **both** phone is `\"hello\"` **and** phone is `\"hello\"`\n```typescript\nconst andSelect = await usersTable.select().where(\n and([\n eq(usersTable.phone, 'hello'),\n eq(usersTable.phone, 'hello')\n ]),\n).all();\n```\n##### Select all records from `Users` where **either** phone is `\"hello\"` **or** phone is `\"hello\"`\n```typescript\nconst orSelect = await usersTable.select().where(\n or([eq(usersTable.phone, 'hello')]),\n).all();\n```\n##### Select all records from `Users` using **LIMIT** and **OFFSET**\n```typescript\nconst limitOffsetSelect = await usersTable.select().limit(10).offset(10).all();\n```\n##### Select all records from `Users` where `phone` contains `\"hello\"`\n```typescript\nconst likeSelect = await usersTable.select().where(\n like(usersTable.phone, '%hello%')\n).all();\n```\n##### Select all records from `Users` where `phone` equals to some of values from array\n```typescript\nconst inArraySelect = usersTable.select().where(\n inArray(usersTable.phone, ['hello'])\n).all();\n```\n##### Select all records from `Users` where `phone` greater(**>**) than `\"hello\"`\n```typescript\nconst greaterSelect = usersTable.select().where(\n greater(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` less(**<**) than `\"hello\"`\n```typescript\nconst lessSelect = usersTable.select().where(\n less(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` greater or equals(**>=**) than `\"hello\"`\n```typescript\nconst greaterEqSelect = usersTable.select().where(\n greaterEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` less or equals(**<=**) \n```typescript\nconst lessEqSelect = usersTable.select().where(\n lessEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` is **NULL**\n```typescript\nconst isNullSelect = usersTable.select().where(\n isNull(usersTable.phone)\n).all();\n```\n##### Select all records from `Users` where `phone` not equals to `\"hello\"`\n```typescript\nconst notEqSelect = usersTable.select().where(\n notEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` ordered by `phone` in ascending order\n```typescript\nconst ordered = await usersTable.select().orderBy((table) => table.phone, Order.ASC).all();\n```\n#### **Partial Selecting**\n ```typescript\n const partialSelect = await usersTable.select({\n mappedId: usersTable.id,\n mappedPhone: usersTable.phone,\n }).all();\n\n // Usage\n const { mappedId, mappedPhone } = partialSelect;\n ```\n\n\n### **Update**\n---\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"`\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .execute();\n```\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .all();\n```\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .findOne();\n```\n\n### **Delete**\n##### Delete `user` where phone is `\"hello\"`\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .execute();\n```\n##### Delete `user` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .all();\n```\n##### Delete `user` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .findOne();\n```\n\n### **Insert**\n##### Insert `user` with required fields\n```typescript\nawait usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).execute();\n```\n##### Insert `user` with required fields and get all rows as array\n```typescript\nconst user = await usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).all();\n```\n##### Insert `user` with required fields and get inserted entity\n```typescript\nconst user = await usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).findOne();\n```\n##### Insert many `users` with required fields and get all inserted entities\n```typescript\nconst users = await usersTable.insertMany([{\n test: 1,\n createdAt: new Date(),\n }, {\n test: 2,\n createdAt: new Date(),\n }]).all();\n```\n##### Insert many `users` with required fields and get all inserted entities. If such user already exists - update `phone` field\n```typescript\nawait usersTable.insertMany([{\n test: 1,\n createdAt: new Date(),\n }, {\n test: 2,\n createdAt: new Date(),\n }])\n .onConflict(\n (table) => table.phoneIndex,\n { phone: 'confilctUpdate' },\n ).all();\n```\n\n## Joins\n### Join One-To-Many Tables\n##### Join Cities with Users and map to city object with full user\n```typescript\nconst usersTable = new UsersTable(db);\nconst citiesTable = new CitiesTable(db);\n\n const userWithCities = await citiesTable.select()\n .where(eq(citiesTable.id, 1))\n .leftJoin(UsersTable,\n (city) => city.userId,\n (users) => users.id)\n .execute();\n\nconst citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));\n```\n\n### Join Many-To-Many Tables\n##### Join User Groups with Users, using many-to-many table and map response to get user object with groups array\n```typescript\n const usersWithUserGroups = await usersToUserGroupsTable.select()\n .where(eq(userGroupsTable.id, 1))\n .leftJoin(UsersTable,\n (userToGroup) => userToGroup.userId,\n (users) => users.id)\n .leftJoin(UsersToUserGroupsTable, UserGroupsTable,\n (userToGroup) => userToGroup.groupId,\n (users) => users.id)\n .execute();\n\n const userGroupWithUsers = usersWithUserGroups.group({\n one: (_, dbUser, dbUserGroup) => dbUser!,\n many: (_, dbUser, dbUserGroup) => dbUserGroup!,\n });\n\n const userWithGroups: ExtractModel<UsersTable> & { groups: ExtractModel<UserGroupsTable>[] } = {\n ...userGroupWithUsers.one,\n groups: userGroupWithUsers.many,\n };\n```\n##### Join User Groups with Users, using many-to-many table and map response to get user group object with users array\n```typescript\n const usersWithUserGroups = await usersToUserGroupsTable.select()\n .where(eq(userGroupsTable.id, 1))\n .leftJoin(UsersTable,\n (userToGroup) => userToGroup.userId,\n (users) => users.id)\n .leftJoin(UsersToUserGroupsTable, UserGroupsTable,\n (userToGroup) => userToGroup.groupId,\n (users) => users.id)\n .execute();\n\n const userGroupWithUsers = usersWithUserGroups.group({\n one: (_, dbUser, dbUserGroup) => dbUserGroup!,\n many: (_, dbUser, dbUserGroup) => dbUser!,\n });\n\n const userWithGroups: ExtractModel<UserGroupsTable> & { users: ExtractModel<UsersTable>[] } = {\n ...userGroupWithUsers.one,\n users: userGroupWithUsers.many,\n };\n```\n### Join using partial field select\n##### Join Cities with Users getting only needed fields form request\n```typescript\nawait citiesTable.select({\n id: citiesTable.id,\n userId: citiesTable.userId,\n })\n .where(eq(citiesTable.id, 1))\n .leftJoin(UsersTable,\n (city) => city.userId,\n (users) => users.id,\n {\n id: usersTable.id,\n })\n .execute();\n\nconst citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));\n```\n\n\n## Migrations\n#### To run migrations generated by drizzle-kit you could use `Migrator` class\n##### Provide drizzle-kit config path\n```typescript\nawait drizzle.migrator(db).migrate('src/drizzle.config.yaml');\n```\n##### Another possibility is to provide object with path to folder with migrations\n```typescript\nawait drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });\n```\n\n\n## Raw query usage\n#### If you have some complex queries to execute and drizzle-orm can't handle them yet, then you could use `rawQuery` execution\n\n\n##### Execute custom raw query\n```typescript\nconst res: QueryResult<any> = await db.session().execute('SELECT * FROM users WHERE user.id = $1', [1]);\n```"
|
|
38
39
|
}
|