nestjs-drizzle-crud 2.1.0 → 2.1.2
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 +62 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,6 +34,7 @@ DrizzleCrudModule.forFeature([{ service: UsersService, table: users }]);
|
|
|
34
34
|
- [Relations](#relations)
|
|
35
35
|
- [Primary keys (serial / uuid)](#primary-keys-serial--uuid)
|
|
36
36
|
- [Soft delete](#soft-delete)
|
|
37
|
+
- [Timestamps](#timestamps)
|
|
37
38
|
- [Bulk operations](#bulk-operations)
|
|
38
39
|
- [Transactions](#transactions)
|
|
39
40
|
- [Full-text search (PostgreSQL)](#full-text-search-postgresql)
|
|
@@ -63,22 +64,32 @@ DrizzleCrudModule.forFeature([{ service: UsersService, table: users }]);
|
|
|
63
64
|
## Installation
|
|
64
65
|
|
|
65
66
|
```bash
|
|
67
|
+
pnpm add nestjs-drizzle-crud
|
|
68
|
+
# or
|
|
66
69
|
npm install nestjs-drizzle-crud
|
|
70
|
+
# or
|
|
71
|
+
yarn add nestjs-drizzle-crud
|
|
67
72
|
```
|
|
68
73
|
|
|
69
74
|
Peer dependencies (install the ones you use):
|
|
70
75
|
|
|
71
76
|
```bash
|
|
72
77
|
# always
|
|
73
|
-
|
|
78
|
+
pnpm add @nestjs/common @nestjs/core drizzle-orm reflect-metadata
|
|
74
79
|
|
|
75
80
|
# PostgreSQL (also required if you use `connectionString` with dialect 'postgresql')
|
|
76
|
-
|
|
81
|
+
pnpm add postgres
|
|
77
82
|
|
|
78
83
|
# MySQL
|
|
79
|
-
|
|
84
|
+
pnpm add mysql2
|
|
80
85
|
```
|
|
81
86
|
|
|
87
|
+
> Using npm or yarn? Swap `pnpm add` for `npm install` / `yarn add`.
|
|
88
|
+
|
|
89
|
+
> **pnpm note:** `postgres`/`mysql2` are optional peer dependencies. pnpm enforces
|
|
90
|
+
> peer deps strictly, so if you build the connection from a `connectionString`
|
|
91
|
+
> make sure the matching driver is installed in your app.
|
|
92
|
+
|
|
82
93
|
> `postgres` is an **optional** peer dependency. It's only needed when you let the
|
|
83
94
|
> module build the connection from a `connectionString` for the `postgresql` dialect.
|
|
84
95
|
> If you pass a pre-built `db` instead, you don't need it.
|
|
@@ -137,13 +148,13 @@ import type { User } from '../db/schema';
|
|
|
137
148
|
|
|
138
149
|
export interface CreateUserDto { name: string; email: string }
|
|
139
150
|
export interface UpdateUserDto { name?: string; email?: string }
|
|
140
|
-
export interface
|
|
151
|
+
export interface UserFilterDto { name?: string; email?: string }
|
|
141
152
|
|
|
142
153
|
export class UsersService extends SqlBaseCrudService<
|
|
143
154
|
User,
|
|
144
155
|
CreateUserDto,
|
|
145
156
|
UpdateUserDto,
|
|
146
|
-
|
|
157
|
+
UserFilterDto
|
|
147
158
|
> {}
|
|
148
159
|
```
|
|
149
160
|
|
|
@@ -289,7 +300,7 @@ Add custom behaviour by overriding hooks (see [Lifecycle hooks](#lifecycle-hooks
|
|
|
289
300
|
or add your own methods:
|
|
290
301
|
|
|
291
302
|
```typescript
|
|
292
|
-
export class UsersService extends SqlBaseCrudService<User, CreateUserDto, UpdateUserDto,
|
|
303
|
+
export class UsersService extends SqlBaseCrudService<User, CreateUserDto, UpdateUserDto, UserFilterDto> {
|
|
293
304
|
findByEmail(email: string) {
|
|
294
305
|
return this.findOne({ email } as Partial<User>);
|
|
295
306
|
}
|
|
@@ -510,6 +521,50 @@ Enable per-project via `defaults.softDelete` or per-entity via `forFeature` conf
|
|
|
510
521
|
|
|
511
522
|
---
|
|
512
523
|
|
|
524
|
+
## Timestamps
|
|
525
|
+
|
|
526
|
+
There are two ways to manage `created_at` / `updated_at`.
|
|
527
|
+
|
|
528
|
+
### Package-managed (convenient)
|
|
529
|
+
|
|
530
|
+
Enable `defaults.timestamps` (or per-entity `timestamps`). On `create()` the
|
|
531
|
+
service stamps both columns; on `update()`/`softDelete()` it stamps `updated_at`:
|
|
532
|
+
|
|
533
|
+
```typescript
|
|
534
|
+
DrizzleCrudModule.forRoot({
|
|
535
|
+
dialect: 'postgresql',
|
|
536
|
+
connectionString: process.env.DATABASE_URL,
|
|
537
|
+
schema,
|
|
538
|
+
defaults: { timestamps: true }, // uses columns created_at / updated_at
|
|
539
|
+
});
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
> Caveat: this uses the **application** clock and only applies to writes that go
|
|
543
|
+
> **through the package** — raw SQL, migrations or other code paths won't set
|
|
544
|
+
> the values.
|
|
545
|
+
|
|
546
|
+
### Schema/DB-managed (recommended)
|
|
547
|
+
|
|
548
|
+
For authoritative timestamps (database time, every write path), define them in
|
|
549
|
+
your Drizzle schema and **leave the package's `timestamps` disabled**:
|
|
550
|
+
|
|
551
|
+
```typescript
|
|
552
|
+
import { timestamp } from 'drizzle-orm/pg-core';
|
|
553
|
+
|
|
554
|
+
created_at: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
|
|
555
|
+
updated_at: timestamp('updated_at', { withTimezone: true })
|
|
556
|
+
.defaultNow()
|
|
557
|
+
.notNull()
|
|
558
|
+
.$onUpdate(() => new Date()),
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
`defaultNow()` lets the database set `created_at` on insert; `$onUpdate()` makes
|
|
562
|
+
Drizzle bump `updated_at` on every update (including the package's own
|
|
563
|
+
`update()`). For `updated_at` that's authoritative even for raw SQL, add a
|
|
564
|
+
Postgres trigger / MySQL `ON UPDATE CURRENT_TIMESTAMP`.
|
|
565
|
+
|
|
566
|
+
---
|
|
567
|
+
|
|
513
568
|
## Bulk operations
|
|
514
569
|
|
|
515
570
|
```typescript
|
|
@@ -608,7 +663,7 @@ Concise, accurate facts for code generation. Prefer these over guessing.
|
|
|
608
663
|
|
|
609
664
|
**A service is an empty subclass — do NOT inject the db or pass `dialect`/`db`:**
|
|
610
665
|
```typescript
|
|
611
|
-
export class XService extends SqlBaseCrudService<X, CreateXDto, UpdateXDto,
|
|
666
|
+
export class XService extends SqlBaseCrudService<X, CreateXDto, UpdateXDto, XFilterDto> {}
|
|
612
667
|
```
|
|
613
668
|
|
|
614
669
|
**Rules / gotchas:**
|