nestjs-drizzle-crud 2.1.1 → 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 +45 -0
- 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)
|
|
@@ -520,6 +521,50 @@ Enable per-project via `defaults.softDelete` or per-entity via `forFeature` conf
|
|
|
520
521
|
|
|
521
522
|
---
|
|
522
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
|
+
|
|
523
568
|
## Bulk operations
|
|
524
569
|
|
|
525
570
|
```typescript
|