drizzle-orm 0.17.0 → 0.17.1-4673c8e
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 +93 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,9 +25,9 @@ Drizzle ORM is being battle-tested on production projects by multiple teams 🚀
|
|
|
25
25
|
- Auto-inferring of TS types for DB models for selections and insertions separately
|
|
26
26
|
- Zero dependencies
|
|
27
27
|
|
|
28
|
-
##
|
|
28
|
+
## Supported databases
|
|
29
29
|
|
|
30
|
-
| Database |
|
|
30
|
+
| Database | Status | |
|
|
31
31
|
|:------------|:-------:|:---|
|
|
32
32
|
| PostgreSQL | ✅ | [Docs](./drizzle-orm/src/pg-core/README.md)|
|
|
33
33
|
| MySQL | ✅ |[Docs](./drizzle-orm/src/mysql-core/README.md)|
|
|
@@ -43,4 +43,94 @@ npm install drizzle-orm
|
|
|
43
43
|
npm install -D drizzle-kit
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
## Feature showcase (PostgreSQL)
|
|
47
|
+
|
|
48
|
+
> **Note**: don't forget to install `pg` and `@types/pg` packages for this example to work.
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { eq } from 'drizzle-orm/expressions';
|
|
52
|
+
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
53
|
+
import { InferModel, integer, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core';
|
|
54
|
+
import { sql } from 'drizzle-orm/sql';
|
|
55
|
+
import { Pool } from 'pg';
|
|
56
|
+
|
|
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(),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
export type User = InferModel<typeof users>;
|
|
68
|
+
export type NewUser = InferModel<typeof users, 'insert'>;
|
|
69
|
+
|
|
70
|
+
export const cities = pgTable('cities', {
|
|
71
|
+
id: serial('id').primaryKey(),
|
|
72
|
+
name: text('name').notNull(),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
export type City = InferModel<typeof cities>;
|
|
76
|
+
export type NewCity = InferModel<typeof cities, 'insert'>;
|
|
77
|
+
|
|
78
|
+
const pool = new Pool({
|
|
79
|
+
connectionString: 'postgres://user:password@host:port/db',
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const db = drizzle(pool);
|
|
83
|
+
|
|
84
|
+
// Insert
|
|
85
|
+
const newUser: NewUser = {
|
|
86
|
+
fullName: 'John Doe',
|
|
87
|
+
phone: '+123456789',
|
|
88
|
+
};
|
|
89
|
+
const insertedUsers /* : User */ = await db.insert(users).values(newUser).returning();
|
|
90
|
+
const insertedUser = insertedUsers[0]!;
|
|
91
|
+
|
|
92
|
+
const newCity: NewCity = {
|
|
93
|
+
name: 'New York',
|
|
94
|
+
};
|
|
95
|
+
const insertedCities /* : City */ = await db.insert(cities).values(newCity).returning();
|
|
96
|
+
const insertedCity = insertedCities[0]!;
|
|
97
|
+
|
|
98
|
+
// Update
|
|
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 });
|
|
103
|
+
|
|
104
|
+
// Select
|
|
105
|
+
const allUsers /* : User[] */ = await db.select(users);
|
|
106
|
+
|
|
107
|
+
// Select custom fields
|
|
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
|
+
});
|
|
113
|
+
|
|
114
|
+
// Joins
|
|
115
|
+
// You wouldn't BELIEVE how SMART the result type is! 😱
|
|
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));
|
|
126
|
+
|
|
127
|
+
// Delete
|
|
128
|
+
const deletedNames /* : { name: string }[] */ = await db.delete(users)
|
|
129
|
+
.where(eq(users.id, insertedUser.id))
|
|
130
|
+
.returning({ name: users.fullName });
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**See full docs for further reference:**
|
|
134
|
+
- [PostgreSQL](./drizzle-orm/src/pg-core/README.md)
|
|
135
|
+
- [MySQL](./drizzle-orm/src/mysql-core/README.md)
|
|
136
|
+
- [SQLite](./drizzle-orm/src/sqlite-core/README.md)
|