drizzle-orm 0.17.1-fd9b1de → 0.17.2-121a8a5
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 +36 -34
- package/package.json +1 -1
- package/version.d.ts.map +1 -1
- package/version.js +1 -1
- package/version.js.map +1 -1
package/README.md
CHANGED
|
@@ -45,8 +45,7 @@ npm install -D drizzle-kit
|
|
|
45
45
|
|
|
46
46
|
## Feature showcase (PostgreSQL)
|
|
47
47
|
|
|
48
|
-
> **Note
|
|
49
|
-
> Don't forget to install `pg` and `@types/pg` packages for this example to work.
|
|
48
|
+
> **Note**: don't forget to install `pg` and `@types/pg` packages for this example to work.
|
|
50
49
|
|
|
51
50
|
```typescript
|
|
52
51
|
import { eq } from 'drizzle-orm/expressions';
|
|
@@ -56,79 +55,82 @@ import { sql } from 'drizzle-orm/sql';
|
|
|
56
55
|
import { Pool } from 'pg';
|
|
57
56
|
|
|
58
57
|
export const users = pgTable('users', {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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(),
|
|
66
65
|
});
|
|
67
66
|
|
|
68
67
|
export type User = InferModel<typeof users>;
|
|
69
68
|
export type NewUser = InferModel<typeof users, 'insert'>;
|
|
70
69
|
|
|
71
70
|
export const cities = pgTable('cities', {
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
id: serial('id').primaryKey(),
|
|
72
|
+
name: text('name').notNull(),
|
|
74
73
|
});
|
|
75
74
|
|
|
76
75
|
export type City = InferModel<typeof cities>;
|
|
77
76
|
export type NewCity = InferModel<typeof cities, 'insert'>;
|
|
78
77
|
|
|
79
78
|
const pool = new Pool({
|
|
80
|
-
|
|
79
|
+
connectionString: 'postgres://user:password@host:port/db',
|
|
81
80
|
});
|
|
82
81
|
|
|
83
82
|
const db = drizzle(pool);
|
|
84
83
|
|
|
85
84
|
// Insert
|
|
86
85
|
const newUser: NewUser = {
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
fullName: 'John Doe',
|
|
87
|
+
phone: '+123456789',
|
|
89
88
|
};
|
|
90
89
|
const insertedUsers /* : User */ = await db.insert(users).values(newUser).returning();
|
|
91
90
|
const insertedUser = insertedUsers[0]!;
|
|
92
91
|
|
|
93
92
|
const newCity: NewCity = {
|
|
94
|
-
|
|
93
|
+
name: 'New York',
|
|
95
94
|
};
|
|
96
95
|
const insertedCities /* : City */ = await db.insert(cities).values(newCity).returning();
|
|
97
96
|
const insertedCity = insertedCities[0]!;
|
|
98
97
|
|
|
99
98
|
// Update
|
|
100
99
|
const updateResult /* : { updated: Date }[] */ = await db.update(users)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
.set({ cityId: insertedCity.id, updatedAt: new Date() })
|
|
101
|
+
.where(eq(users.id, insertedUser.id))
|
|
102
|
+
.returning({ updated: users.updatedAt });
|
|
104
103
|
|
|
105
104
|
// Select
|
|
106
105
|
const allUsers /* : User[] */ = await db.select(users);
|
|
107
106
|
|
|
108
107
|
// Select custom fields
|
|
109
108
|
const upperCaseNames /* : { id: number; name: string }[] */ = await db.select(users)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
.fields({
|
|
110
|
+
id: users.id,
|
|
111
|
+
name: sql`upper(${users.fullName})`.as<string>(),
|
|
112
|
+
});
|
|
114
113
|
|
|
115
114
|
// Joins
|
|
116
115
|
// You wouldn't BELIEVE how SMART the result type is! 😱
|
|
117
116
|
const allUsersWithCities = await db.select(users)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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));
|
|
127
126
|
|
|
128
127
|
// Delete
|
|
129
128
|
const deletedNames /* : { name: string }[] */ = await db.delete(users)
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
.where(eq(users.id, insertedUser.id))
|
|
130
|
+
.returning({ name: users.fullName });
|
|
132
131
|
```
|
|
133
132
|
|
|
134
|
-
See
|
|
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)
|
package/package.json
CHANGED
package/version.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,IAAI,CAAC;AACtC,eAAO,MAAM,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,IAAI,CAAC;AACtC,eAAO,MAAM,UAAU,EAAE,MAA0C,CAAC"}
|
package/version.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.npmVersion = exports.compatibilityVersion = void 0;
|
|
4
4
|
exports.compatibilityVersion = 2;
|
|
5
|
-
exports.npmVersion = require('
|
|
5
|
+
exports.npmVersion = require('./package.json').version;
|
|
6
6
|
//# sourceMappingURL=version.js.map
|
package/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG,CAAC,CAAC;AACzB,QAAA,UAAU,GAAW,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG,CAAC,CAAC;AACzB,QAAA,UAAU,GAAW,OAAO,CAAC,gBAAgB,CAAC,CAAC,OAAO,CAAC"}
|