@@ -55,79 +55,79 @@ import { sql } from 'drizzle-orm/sql';
55
55
import { Pool } from ' pg' ;
56
56
57
57
export const users = pgTable (' users' , {
58
- id: serial (' id' ).primaryKey (),
59
- fullName: text (' full_name' ).notNull (),
60
- phone: varchar (' phone' , { length: 20 }).notNull (),
61
- role: text <' user' | ' admin' >(' role' ).default (' user' ).notNull (),
62
- cityId: integer (' city_id' ).references (() => cities .id ),
63
- createdAt: timestamp (' created_at' ).defaultNow ().notNull (),
64
- updatedAt: timestamp (' updated_at' ).defaultNow ().notNull (),
58
+ id: serial (' id' ).primaryKey (),
59
+ fullName: text (' full_name' ).notNull (),
60
+ phone: varchar (' phone' , { length: 20 }).notNull (),
61
+ role: text <' user' | ' admin' >(' role' ).default (' user' ).notNull (),
62
+ cityId: integer (' city_id' ).references (() => cities .id ),
63
+ createdAt: timestamp (' created_at' ).defaultNow ().notNull (),
64
+ updatedAt: timestamp (' updated_at' ).defaultNow ().notNull (),
65
65
});
66
66
67
67
export type User = InferModel <typeof users >;
68
68
export type NewUser = InferModel <typeof users , ' insert' >;
69
69
70
70
export const cities = pgTable (' cities' , {
71
- id: serial (' id' ).primaryKey (),
72
- name: text (' name' ).notNull (),
71
+ id: serial (' id' ).primaryKey (),
72
+ name: text (' name' ).notNull (),
73
73
});
74
74
75
75
export type City = InferModel <typeof cities >;
76
76
export type NewCity = InferModel <typeof cities , ' insert' >;
77
77
78
78
const pool = new Pool ({
79
- connectionString: ' postgres://user:password@host:port/db' ,
79
+ connectionString: ' postgres://user:password@host:port/db' ,
80
80
});
81
81
82
82
const db = drizzle (pool );
83
83
84
84
// Insert
85
85
const newUser: NewUser = {
86
- fullName: ' John Doe' ,
87
- phone: ' +123456789' ,
86
+ fullName: ' John Doe' ,
87
+ phone: ' +123456789' ,
88
88
};
89
89
const insertedUsers /* : User */ = await db .insert (users ).values (newUser ).returning ();
90
90
const insertedUser = insertedUsers [0 ]! ;
91
91
92
92
const newCity: NewCity = {
93
- name: ' New York' ,
93
+ name: ' New York' ,
94
94
};
95
95
const insertedCities /* : City */ = await db .insert (cities ).values (newCity ).returning ();
96
96
const insertedCity = insertedCities [0 ]! ;
97
97
98
98
// Update
99
99
const updateResult /* : { updated: Date }[] */ = await db .update (users )
100
- .set ({ cityId: insertedCity .id , updatedAt: new Date () })
101
- .where (eq (users .id , insertedUser .id ))
102
- .returning ({ updated: users .updatedAt });
100
+ .set ({ cityId: insertedCity .id , updatedAt: new Date () })
101
+ .where (eq (users .id , insertedUser .id ))
102
+ .returning ({ updated: users .updatedAt });
103
103
104
104
// Select
105
105
const allUsers /* : User[] */ = await db .select (users );
106
106
107
107
// Select custom fields
108
108
const upperCaseNames /* : { id: number; name: string }[] */ = await db .select (users )
109
- .fields ({
110
- id: users .id ,
111
- name: sql ` upper(${users .fullName }) ` .as <string >(),
112
- });
109
+ .fields ({
110
+ id: users .id ,
111
+ name: sql ` upper(${users .fullName }) ` .as <string >(),
112
+ });
113
113
114
114
// Joins
115
115
// You wouldn't BELIEVE how SMART the result type is! 😱
116
116
const allUsersWithCities = await db .select (users )
117
- .fields ({
118
- user: {
119
- id: users .id ,
120
- name: users .fullName ,
121
- },
122
- cityId: cities .id ,
123
- cityName: cities .name ,
124
- })
125
- .leftJoin (cities , eq (users .cityId , cities .id ));
117
+ .fields ({
118
+ user: {
119
+ id: users .id ,
120
+ name: users .fullName ,
121
+ },
122
+ cityId: cities .id ,
123
+ cityName: cities .name ,
124
+ })
125
+ .leftJoin (cities , eq (users .cityId , cities .id ));
126
126
127
127
// Delete
128
128
const deletedNames /* : { name: string }[] */ = await db .delete (users )
129
- .where (eq (users .id , insertedUser .id ))
130
- .returning ({ name: users .fullName });
129
+ .where (eq (users .id , insertedUser .id ))
130
+ .returning ({ name: users .fullName });
131
131
```
132
132
133
133
See [ PostgreSQL docs] ( ./drizzle-orm/src/pg-core/README.md ) for full API reference.
0 commit comments