drizzle-orm 0.9.13 → 0.9.17
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 +399 -0
- package/builders/lowLvlBuilders/create.js +1 -1
- package/columns/column.d.ts +18 -26
- package/columns/column.js +14 -30
- package/columns/index.d.ts +1 -1
- package/columns/index.js +2 -1
- package/columns/types/pgBigInt.d.ts +9 -2
- package/columns/types/pgBigInt.js +17 -5
- package/columns/types/pgBigSerial.d.ts +15 -0
- package/columns/types/pgBigSerial.js +29 -0
- package/columns/types/pgSerial.d.ts +8 -0
- package/columns/types/pgSerial.js +15 -0
- package/columns/types/pgTimestamptz.d.ts +8 -0
- package/columns/types/pgTimestamptz.js +15 -0
- package/db/index.d.ts +1 -0
- package/db/index.js +3 -1
- package/docs/cases/simple_join.js +1 -0
- package/docs/cases/simple_select.js +1 -1
- package/docs/tables/citiesTable.d.ts +1 -1
- package/docs/tables/citiesTable.js +2 -2
- package/docs/tables/userGroupsTable.d.ts +1 -1
- package/docs/tables/userGroupsTable.js +1 -1
- package/docs/tables/usersTable.d.ts +10 -9
- package/docs/tables/usersTable.js +7 -5
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/migrator/migrator.d.ts +6 -11
- package/migrator/migrator.js +56 -67
- package/package.json +1 -1
- package/serializer/serializer.d.ts +12 -0
- package/serializer/serializer.js +168 -1
- package/tables/abstractTable.d.ts +18 -94
- package/tables/abstractTable.js +38 -31
- package/tables/migrationsTable.d.ts +3 -4
- package/tables/migrationsTable.js +4 -5
- package/test.js +8 -1
- package/docs/tables/citiesToUsers.d.ts +0 -7
- package/docs/tables/citiesToUsers.js +0 -18
package/README.md
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
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().first();
|
|
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
|
+
|
|
236
|
+
### **Update**
|
|
237
|
+
---
|
|
238
|
+
##### Update `fullName` to `newName` in `Users` where phone is `"hello"`
|
|
239
|
+
```typescript
|
|
240
|
+
await usersTable.update()
|
|
241
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
242
|
+
.set({ fullName: 'newName' })
|
|
243
|
+
.execute();
|
|
244
|
+
```
|
|
245
|
+
##### Update `fullName` to `newName` in `Users` where phone is `"hello"` returning updated `User` model
|
|
246
|
+
```typescript
|
|
247
|
+
await usersTable.update()
|
|
248
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
249
|
+
.set({ fullName: 'newName' })
|
|
250
|
+
.all();
|
|
251
|
+
```
|
|
252
|
+
##### Update `fullName` to `newName` in `Users` where phone is `"hello"` returning updated `User` model
|
|
253
|
+
```typescript
|
|
254
|
+
await usersTable.update()
|
|
255
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
256
|
+
.set({ fullName: 'newName' })
|
|
257
|
+
.first();
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### **Delete**
|
|
261
|
+
##### Delete `user` where phone is `"hello"`
|
|
262
|
+
```typescript
|
|
263
|
+
await usersTable.delete()
|
|
264
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
265
|
+
.execute();
|
|
266
|
+
```
|
|
267
|
+
##### Delete `user` where phone is `"hello"` returning updated `User` model
|
|
268
|
+
```typescript
|
|
269
|
+
await usersTable.delete()
|
|
270
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
271
|
+
.all();
|
|
272
|
+
```
|
|
273
|
+
##### Delete `user` where phone is `"hello"` returning updated `User` model
|
|
274
|
+
```typescript
|
|
275
|
+
await usersTable.delete()
|
|
276
|
+
.where(eq(usersTable.phone, 'hello'))
|
|
277
|
+
.first();
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### **Insert**
|
|
281
|
+
##### Insert `user` with required fields
|
|
282
|
+
```typescript
|
|
283
|
+
await usersTable.insert({
|
|
284
|
+
test: 1,
|
|
285
|
+
createdAt: new Date(),
|
|
286
|
+
}).execute();
|
|
287
|
+
```
|
|
288
|
+
##### Insert `user` with required fields and get all rows as array
|
|
289
|
+
```typescript
|
|
290
|
+
const user = await usersTable.insert({
|
|
291
|
+
test: 1,
|
|
292
|
+
createdAt: new Date(),
|
|
293
|
+
}).all();
|
|
294
|
+
```
|
|
295
|
+
##### Insert `user` with required fields and get inserted entity
|
|
296
|
+
```typescript
|
|
297
|
+
const user = await usersTable.insert({
|
|
298
|
+
test: 1,
|
|
299
|
+
createdAt: new Date(),
|
|
300
|
+
}).first();
|
|
301
|
+
```
|
|
302
|
+
##### Insert many `users` with required fields and get all inserted entities
|
|
303
|
+
```typescript
|
|
304
|
+
const users = await usersTable.insertMany([{
|
|
305
|
+
test: 1,
|
|
306
|
+
createdAt: new Date(),
|
|
307
|
+
}, {
|
|
308
|
+
test: 2,
|
|
309
|
+
createdAt: new Date(),
|
|
310
|
+
}]).all();
|
|
311
|
+
```
|
|
312
|
+
##### Insert many `users` with required fields and get all inserted entities. If such user already exists - update `phone` field
|
|
313
|
+
```typescript
|
|
314
|
+
await usersTable.insertMany([{
|
|
315
|
+
test: 1,
|
|
316
|
+
createdAt: new Date(),
|
|
317
|
+
}, {
|
|
318
|
+
test: 2,
|
|
319
|
+
createdAt: new Date(),
|
|
320
|
+
}])
|
|
321
|
+
.onConflict(
|
|
322
|
+
(table) => table.phoneIndex,
|
|
323
|
+
{ phone: 'confilctUpdate' },
|
|
324
|
+
).all();
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Joins
|
|
328
|
+
### Join One-To-Many Tables
|
|
329
|
+
##### Join Cities with Users and map to city object with full user
|
|
330
|
+
```typescript
|
|
331
|
+
const usersTable = new UsersTable(db);
|
|
332
|
+
const citiesTable = new CitiesTable(db);
|
|
333
|
+
|
|
334
|
+
const userWithCities = await citiesTable.select()
|
|
335
|
+
.where(eq(citiesTable.id, 1))
|
|
336
|
+
.leftJoin(UsersTable,
|
|
337
|
+
(city) => city.userId,
|
|
338
|
+
(users) => users.id)
|
|
339
|
+
.execute();
|
|
340
|
+
|
|
341
|
+
const citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Join Many-To-Many Tables
|
|
345
|
+
##### Join User Groups with Users, using many-to-many table and map response to get user object with groups array
|
|
346
|
+
```typescript
|
|
347
|
+
const usersWithUserGroups = await usersToUserGroupsTable.select()
|
|
348
|
+
.where(eq(userGroupsTable.id, 1))
|
|
349
|
+
.leftJoin(UsersTable,
|
|
350
|
+
(userToGroup) => userToGroup.userId,
|
|
351
|
+
(users) => users.id)
|
|
352
|
+
.leftJoin(UserGroupsTable,
|
|
353
|
+
(userToGroup) => userToGroup.groupId,
|
|
354
|
+
(users) => users.id)
|
|
355
|
+
.execute();
|
|
356
|
+
|
|
357
|
+
const userGroupWithUsers = usersWithUserGroups.group({
|
|
358
|
+
one: (_, dbUser, dbUserGroup) => dbUser!,
|
|
359
|
+
many: (_, dbUser, dbUserGroup) => dbUserGroup!,
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const userWithGroups: ExtractModel<UsersTable> & { groups: ExtractModel<UserGroupsTable>[] } = {
|
|
363
|
+
...userGroupWithUsers.one,
|
|
364
|
+
groups: userGroupWithUsers.many,
|
|
365
|
+
};
|
|
366
|
+
```
|
|
367
|
+
##### Join User Groups with Users, using many-to-many table and map response to get user group object with users array
|
|
368
|
+
```typescript
|
|
369
|
+
const usersWithUserGroups = await usersToUserGroupsTable.select()
|
|
370
|
+
.where(eq(userGroupsTable.id, 1))
|
|
371
|
+
.leftJoin(UsersTable,
|
|
372
|
+
(userToGroup) => userToGroup.userId,
|
|
373
|
+
(users) => users.id)
|
|
374
|
+
.leftJoin(UserGroupsTable,
|
|
375
|
+
(userToGroup) => userToGroup.groupId,
|
|
376
|
+
(users) => users.id)
|
|
377
|
+
.execute();
|
|
378
|
+
|
|
379
|
+
const userGroupWithUsers = usersWithUserGroups.group({
|
|
380
|
+
one: (_, dbUser, dbUserGroup) => dbUserGroup!,
|
|
381
|
+
many: (_, dbUser, dbUserGroup) => dbUser!,
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const userWithGroups: ExtractModel<UserGroupsTable> & { users: ExtractModel<UsersTable>[] } = {
|
|
385
|
+
...userGroupWithUsers.one,
|
|
386
|
+
users: userGroupWithUsers.many,
|
|
387
|
+
};
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Migrations
|
|
391
|
+
#### To run migrations generated by drizzle-kit you could use `Migtator` class
|
|
392
|
+
##### Provide drizzle-kit config path
|
|
393
|
+
```typescript
|
|
394
|
+
await drizzle.migrator(db).migrate('src/drizzle.config.yaml');
|
|
395
|
+
```
|
|
396
|
+
##### Provide object with path to folder with migrations
|
|
397
|
+
```typescript
|
|
398
|
+
await drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });
|
|
399
|
+
```
|
|
@@ -38,7 +38,7 @@ class Create {
|
|
|
38
38
|
}
|
|
39
39
|
this.columnsBuilder.push(ecranate_1.ecranate(column.getColumnName()));
|
|
40
40
|
this.columnsBuilder.push(' ');
|
|
41
|
-
this.columnsBuilder.push(column.
|
|
41
|
+
this.columnsBuilder.push(column.getColumnType().getDbName());
|
|
42
42
|
this.columnsBuilder.push(' ');
|
|
43
43
|
this.columnsBuilder.push(column.getDefaultValue() != null ? `DEFAULT ${column.getColumnType().insertStrategy(column.getDefaultValue())}` : '');
|
|
44
44
|
this.columnsBuilder.push(column.isNullableFlag ? '' : ' NOT NULL');
|
package/columns/column.d.ts
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
+
import { PgTime, PgTimestamp } from '.';
|
|
1
2
|
import DB from '../db/db';
|
|
2
3
|
import { AbstractTable } from '../tables';
|
|
3
4
|
import ColumnType from './types/columnType';
|
|
4
|
-
|
|
5
|
-
export declare enum
|
|
6
|
-
|
|
7
|
-
CASCADE = "ON DELETE CASCADE"
|
|
8
|
-
}
|
|
9
|
-
export declare enum OnUpdate {
|
|
10
|
-
RESTRICT = "ON UPDATE RESTRICT",
|
|
11
|
-
CASCADE = "ON UPDATE RESTRICT"
|
|
5
|
+
import PgTimestamptz from './types/pgTimestamptz';
|
|
6
|
+
export declare enum Defaults {
|
|
7
|
+
CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP"
|
|
12
8
|
}
|
|
9
|
+
declare type PgTimes = PgTimestamptz | PgTime | PgTimestamp;
|
|
10
|
+
export declare type ExtractColumnType<T extends ColumnType> = T extends ColumnType<infer TCodeType> ? T extends PgTimes ? TCodeType | Defaults : TCodeType : never;
|
|
13
11
|
export declare abstract class AbstractColumn<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false> {
|
|
14
|
-
isNullableFlag:
|
|
15
|
-
autoIncrementType: TAutoIncrement;
|
|
12
|
+
isNullableFlag: boolean;
|
|
16
13
|
primaryKeyName?: string;
|
|
17
14
|
uniqueKeyName?: string;
|
|
18
15
|
protected onDelete?: string;
|
|
@@ -21,10 +18,9 @@ export declare abstract class AbstractColumn<T extends ColumnType, TNullable ext
|
|
|
21
18
|
protected parentTableName: string;
|
|
22
19
|
protected columnType: T;
|
|
23
20
|
protected columnName: string;
|
|
24
|
-
protected autoIncrementFlag: boolean;
|
|
25
21
|
protected defaultParam: any;
|
|
26
22
|
protected referenced: AbstractColumn<T, boolean, boolean>;
|
|
27
|
-
constructor(parent: AbstractTable<any>, columnName: string, columnType: T
|
|
23
|
+
constructor(parent: AbstractTable<any>, columnName: string, columnType: T);
|
|
28
24
|
getOnDelete: () => string | undefined;
|
|
29
25
|
getOnUpdate: () => string | undefined;
|
|
30
26
|
getAlias: () => string;
|
|
@@ -33,38 +29,34 @@ export declare abstract class AbstractColumn<T extends ColumnType, TNullable ext
|
|
|
33
29
|
abstract foreignKey<ITable extends AbstractTable<ITable>>(table: {
|
|
34
30
|
new (db: DB): ITable;
|
|
35
31
|
}, callback: (table: ITable) => AbstractColumn<T, boolean, boolean>, onConstraint: {
|
|
36
|
-
onDelete?: 'CASCADE' | 'RESTRICT';
|
|
37
|
-
onUpdate?: 'CASCADE' | 'RESTRICT';
|
|
32
|
+
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
33
|
+
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
38
34
|
}): AbstractColumn<T, TNullable, TAutoIncrement>;
|
|
39
35
|
defaultValue: (value: ExtractColumnType<T>) => this;
|
|
40
|
-
abstract autoIncrement(): AbstractColumn<T, boolean, boolean>;
|
|
41
36
|
abstract primaryKey(): AbstractColumn<T, boolean, boolean>;
|
|
42
|
-
abstract serial(): AbstractColumn<T, boolean, boolean>;
|
|
43
37
|
unique: () => this;
|
|
44
|
-
|
|
38
|
+
abstract notNull(): AbstractColumn<T, boolean, boolean>;
|
|
45
39
|
getColumnName: () => string;
|
|
46
40
|
getReferenced: () => AbstractColumn<T, boolean, boolean>;
|
|
47
41
|
getColumnType: () => T;
|
|
48
42
|
getDefaultValue: () => any;
|
|
49
43
|
}
|
|
50
44
|
export declare class Column<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false> extends AbstractColumn<T, TNullable, TAutoIncrement> {
|
|
51
|
-
constructor(parent: AbstractTable<any>, columnName: string, columnType: T
|
|
52
|
-
|
|
45
|
+
constructor(parent: AbstractTable<any>, columnName: string, columnType: T);
|
|
46
|
+
notNull(): Column<T, TAutoIncrement extends true ? true : TNullable extends true ? false : true, TAutoIncrement>;
|
|
53
47
|
primaryKey(): Column<T, TAutoIncrement extends true ? true : false, TAutoIncrement>;
|
|
54
48
|
foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => Column<T, boolean, boolean>, onConstraint?: {
|
|
55
|
-
onDelete?: 'CASCADE' | 'RESTRICT';
|
|
56
|
-
onUpdate?: 'CASCADE' | 'RESTRICT';
|
|
49
|
+
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
50
|
+
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
57
51
|
}): Column<T, TNullable, TAutoIncrement>;
|
|
58
|
-
autoIncrement(): IndexedColumn<T, true, true>;
|
|
59
52
|
}
|
|
60
53
|
export declare class IndexedColumn<T extends ColumnType, TNullable extends boolean = true, TAutoIncrement extends boolean = false> extends AbstractColumn<T, TNullable, TAutoIncrement> {
|
|
61
54
|
constructor(parent: AbstractTable<any>, columnName: string, columnType: T, nullable: TNullable);
|
|
62
|
-
|
|
55
|
+
notNull(): IndexedColumn<T, TAutoIncrement extends true ? true : TNullable extends true ? false : true, TAutoIncrement>;
|
|
63
56
|
primaryKey(): IndexedColumn<T, TAutoIncrement extends true ? true : false, TAutoIncrement>;
|
|
64
57
|
foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => IndexedColumn<T, boolean, boolean>, onConstraint?: {
|
|
65
|
-
onDelete?: 'CASCADE' | 'RESTRICT';
|
|
66
|
-
onUpdate?: 'CASCADE' | 'RESTRICT';
|
|
58
|
+
onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
59
|
+
onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT';
|
|
67
60
|
}): IndexedColumn<T, TNullable, TAutoIncrement>;
|
|
68
|
-
autoIncrement(): IndexedColumn<T, true, true>;
|
|
69
61
|
}
|
|
70
62
|
export {};
|
package/columns/column.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IndexedColumn = exports.Column = exports.AbstractColumn = exports.
|
|
4
|
-
var
|
|
5
|
-
(function (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
})(OnDelete = exports.OnDelete || (exports.OnDelete = {}));
|
|
9
|
-
var OnUpdate;
|
|
10
|
-
(function (OnUpdate) {
|
|
11
|
-
OnUpdate["RESTRICT"] = "ON UPDATE RESTRICT";
|
|
12
|
-
OnUpdate["CASCADE"] = "ON UPDATE RESTRICT";
|
|
13
|
-
})(OnUpdate = exports.OnUpdate || (exports.OnUpdate = {}));
|
|
3
|
+
exports.IndexedColumn = exports.Column = exports.AbstractColumn = exports.Defaults = void 0;
|
|
4
|
+
var Defaults;
|
|
5
|
+
(function (Defaults) {
|
|
6
|
+
Defaults["CURRENT_TIMESTAMP"] = "CURRENT_TIMESTAMP";
|
|
7
|
+
})(Defaults = exports.Defaults || (exports.Defaults = {}));
|
|
14
8
|
// eslint-disable-next-line max-len
|
|
15
9
|
class AbstractColumn {
|
|
16
|
-
constructor(parent, columnName, columnType
|
|
17
|
-
this.
|
|
10
|
+
constructor(parent, columnName, columnType) {
|
|
11
|
+
this.isNullableFlag = true;
|
|
18
12
|
this.defaultParam = null;
|
|
19
13
|
this.getOnDelete = () => this.onDelete;
|
|
20
14
|
this.getOnUpdate = () => this.onUpdate;
|
|
@@ -29,7 +23,6 @@ class AbstractColumn {
|
|
|
29
23
|
this.uniqueKeyName = this.columnName;
|
|
30
24
|
return this;
|
|
31
25
|
};
|
|
32
|
-
this.isAutoIncrement = () => this.autoIncrementFlag;
|
|
33
26
|
this.getColumnName = () => this.columnName;
|
|
34
27
|
this.getReferenced = () => this.referenced;
|
|
35
28
|
this.getColumnType = () => this.columnType;
|
|
@@ -38,17 +31,16 @@ class AbstractColumn {
|
|
|
38
31
|
this.columnName = columnName;
|
|
39
32
|
this.parentTableName = parent.tableName();
|
|
40
33
|
this.parent = parent;
|
|
41
|
-
this.isNullableFlag = nullable;
|
|
42
34
|
}
|
|
43
35
|
}
|
|
44
36
|
exports.AbstractColumn = AbstractColumn;
|
|
45
37
|
// eslint-disable-next-line max-len
|
|
46
38
|
class Column extends AbstractColumn {
|
|
47
|
-
constructor(parent, columnName, columnType
|
|
48
|
-
super(parent, columnName, columnType
|
|
39
|
+
constructor(parent, columnName, columnType) {
|
|
40
|
+
super(parent, columnName, columnType);
|
|
49
41
|
}
|
|
50
|
-
|
|
51
|
-
this.
|
|
42
|
+
notNull() {
|
|
43
|
+
this.isNullableFlag = false;
|
|
52
44
|
return this;
|
|
53
45
|
}
|
|
54
46
|
primaryKey() {
|
|
@@ -63,19 +55,15 @@ class Column extends AbstractColumn {
|
|
|
63
55
|
this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
|
|
64
56
|
return this;
|
|
65
57
|
}
|
|
66
|
-
autoIncrement() {
|
|
67
|
-
this.autoIncrementFlag = true;
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
58
|
}
|
|
71
59
|
exports.Column = Column;
|
|
72
60
|
// eslint-disable-next-line max-len
|
|
73
61
|
class IndexedColumn extends AbstractColumn {
|
|
74
62
|
constructor(parent, columnName, columnType, nullable) {
|
|
75
|
-
super(parent, columnName, columnType
|
|
63
|
+
super(parent, columnName, columnType);
|
|
76
64
|
}
|
|
77
|
-
|
|
78
|
-
this.
|
|
65
|
+
notNull() {
|
|
66
|
+
this.isNullableFlag = false;
|
|
79
67
|
return this;
|
|
80
68
|
}
|
|
81
69
|
primaryKey() {
|
|
@@ -90,9 +78,5 @@ class IndexedColumn extends AbstractColumn {
|
|
|
90
78
|
this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
|
|
91
79
|
return this;
|
|
92
80
|
}
|
|
93
|
-
autoIncrement() {
|
|
94
|
-
this.autoIncrementFlag = true;
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
81
|
}
|
|
98
82
|
exports.IndexedColumn = IndexedColumn;
|
package/columns/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Column } from './column';
|
|
1
|
+
export { Column, ExtractColumnType, Defaults } from './column';
|
|
2
2
|
export { default as PgBigDecimal } from './types/pgBigDecimal';
|
|
3
3
|
export { default as PgBigInt } from './types/pgBigInt';
|
|
4
4
|
export { default as PgBoolean } from './types/pgBoolean';
|
package/columns/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PgVarChar = exports.PgTimestamp = exports.PgTime = exports.PgText = exports.PgJsonb = exports.PgInteger = exports.PgBoolean = exports.PgBigInt = exports.PgBigDecimal = exports.Column = void 0;
|
|
3
|
+
exports.PgVarChar = exports.PgTimestamp = exports.PgTime = exports.PgText = exports.PgJsonb = exports.PgInteger = exports.PgBoolean = exports.PgBigInt = exports.PgBigDecimal = exports.Defaults = exports.Column = void 0;
|
|
4
4
|
var column_1 = require("./column");
|
|
5
5
|
Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return column_1.Column; } });
|
|
6
|
+
Object.defineProperty(exports, "Defaults", { enumerable: true, get: function () { return column_1.Defaults; } });
|
|
6
7
|
var pgBigDecimal_1 = require("./types/pgBigDecimal");
|
|
7
8
|
Object.defineProperty(exports, "PgBigDecimal", { enumerable: true, get: function () { return pgBigDecimal_1.default; } });
|
|
8
9
|
var pgBigInt_1 = require("./types/pgBigInt");
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import ColumnType from './columnType';
|
|
2
|
-
export default class
|
|
2
|
+
export default class PgBigInt53 extends ColumnType<number> {
|
|
3
3
|
dbName: string;
|
|
4
4
|
constructor();
|
|
5
|
-
getDbName()
|
|
5
|
+
getDbName: () => string;
|
|
6
6
|
insertStrategy: (value: number) => string;
|
|
7
7
|
selectStrategy(value: string): number | undefined;
|
|
8
8
|
}
|
|
9
|
+
export declare class PgBigInt64 extends ColumnType<bigint> {
|
|
10
|
+
dbName: string;
|
|
11
|
+
constructor();
|
|
12
|
+
getDbName: () => string;
|
|
13
|
+
insertStrategy: (value: bigint) => string;
|
|
14
|
+
selectStrategy(value: string): bigint | undefined;
|
|
15
|
+
}
|
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PgBigInt64 = void 0;
|
|
4
|
+
// eslint-disable-next-line max-classes-per-file
|
|
3
5
|
const columnType_1 = require("./columnType");
|
|
4
|
-
class
|
|
6
|
+
class PgBigInt53 extends columnType_1.default {
|
|
5
7
|
constructor() {
|
|
6
8
|
super();
|
|
9
|
+
this.getDbName = () => this.dbName;
|
|
7
10
|
this.insertStrategy = (value) => `${value}`;
|
|
8
11
|
this.dbName = 'BIGINT';
|
|
9
12
|
}
|
|
10
|
-
getDbName() {
|
|
11
|
-
return this.dbName;
|
|
12
|
-
}
|
|
13
13
|
selectStrategy(value) {
|
|
14
14
|
return value ? parseInt(value, 10) : undefined;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
exports.default =
|
|
17
|
+
exports.default = PgBigInt53;
|
|
18
|
+
class PgBigInt64 extends columnType_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.getDbName = () => this.dbName;
|
|
22
|
+
this.insertStrategy = (value) => `${value}`;
|
|
23
|
+
this.dbName = 'BIGINT';
|
|
24
|
+
}
|
|
25
|
+
selectStrategy(value) {
|
|
26
|
+
return value ? BigInt(value) : undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.PgBigInt64 = PgBigInt64;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ColumnType from './columnType';
|
|
2
|
+
export default class PgBigSerial53 extends ColumnType<number> {
|
|
3
|
+
dbName: string;
|
|
4
|
+
constructor();
|
|
5
|
+
getDbName: () => string;
|
|
6
|
+
insertStrategy: (value: number) => string;
|
|
7
|
+
selectStrategy(value: string): number | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare class PgBigSerial64 extends ColumnType<bigint> {
|
|
10
|
+
dbName: string;
|
|
11
|
+
constructor();
|
|
12
|
+
getDbName: () => string;
|
|
13
|
+
insertStrategy: (value: bigint) => string;
|
|
14
|
+
selectStrategy(value: string): bigint | undefined;
|
|
15
|
+
}
|