@nauth-toolkit/storage-database 0.1.3 → 0.1.6
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 +4 -142
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,147 +1,9 @@
|
|
|
1
1
|
# @nauth-toolkit/storage-database
|
|
2
2
|
|
|
3
|
-
Database storage adapter for nauth-toolkit using TypeORM
|
|
3
|
+
Database storage adapter for nauth-toolkit using TypeORM
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Preview Release Notice
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
- ✅ **Multi-server compatible** - All servers share the same database
|
|
12
|
-
- ✅ **No separate connection** - Uses your existing TypeORM DataSource
|
|
13
|
-
- ✅ **Atomic operations** - Database-level increment/decrement for thread-safety
|
|
14
|
-
- ✅ **TTL support** - Automatic expiration via `expiresAt` column
|
|
15
|
-
- ✅ **PostgreSQL & MySQL** - Works with both database types
|
|
16
|
-
- ✅ **Zero external dependencies** - No Redis required
|
|
17
|
-
|
|
18
|
-
## Installation
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
yarn add @nauth-toolkit/storage-database
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
### 1. Include Storage Entities
|
|
27
|
-
|
|
28
|
-
Storage entities (`RateLimit`, `StorageLock`) must be included in your TypeORM configuration:
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import { getNAuthEntities, getNAuthStorageEntities } from '@nauth-toolkit/database-typeorm-postgres';
|
|
32
|
-
// or
|
|
33
|
-
// import { getNAuthEntities, getNAuthStorageEntities } from '@nauth-toolkit/database-typeorm-mysql';
|
|
34
|
-
|
|
35
|
-
@Module({
|
|
36
|
-
imports: [
|
|
37
|
-
TypeOrmModule.forRoot({
|
|
38
|
-
type: 'postgres', // or 'mysql'
|
|
39
|
-
// ... your DB config
|
|
40
|
-
entities: [
|
|
41
|
-
...getNAuthEntities(), // Core entities (User, Session, etc.)
|
|
42
|
-
...getNAuthStorageEntities(), // Storage entities (RateLimit, StorageLock)
|
|
43
|
-
],
|
|
44
|
-
}),
|
|
45
|
-
AuthModule.forRoot({
|
|
46
|
-
jwt: { ... },
|
|
47
|
-
storageAdapter: new DatabaseStorageAdapter(),
|
|
48
|
-
}),
|
|
49
|
-
],
|
|
50
|
-
})
|
|
51
|
-
export class AppModule {}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### 2. Configure Storage Adapter
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
import { DatabaseStorageAdapter } from '@nauth-toolkit/storage-database';
|
|
58
|
-
|
|
59
|
-
AuthModule.forRoot({
|
|
60
|
-
jwt: { ... },
|
|
61
|
-
storageAdapter: new DatabaseStorageAdapter(),
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
The adapter automatically injects `RateLimitRepository` and `StorageLockRepository` via NestJS dependency injection.
|
|
66
|
-
|
|
67
|
-
## Database Tables
|
|
68
|
-
|
|
69
|
-
The adapter creates two tables:
|
|
70
|
-
|
|
71
|
-
### `nauth_rate_limits`
|
|
72
|
-
|
|
73
|
-
Stores rate limit counters and key-value pairs.
|
|
74
|
-
|
|
75
|
-
**Schema:**
|
|
76
|
-
- `id`: Primary key
|
|
77
|
-
- `key`: VARCHAR(255) UNIQUE - Storage key
|
|
78
|
-
- `value`: TEXT - Stored value (counter or JSON)
|
|
79
|
-
- `expiresAt`: TIMESTAMP - Expiration time
|
|
80
|
-
- `createdAt`: TIMESTAMP
|
|
81
|
-
- `updatedAt`: TIMESTAMP
|
|
82
|
-
|
|
83
|
-
**Indexes:**
|
|
84
|
-
- `idx_rate_limits_key` (UNIQUE) on `key`
|
|
85
|
-
- `idx_rate_limits_expires` on `expiresAt`
|
|
86
|
-
|
|
87
|
-
### `nauth_storage_locks`
|
|
88
|
-
|
|
89
|
-
Stores distributed locks for concurrent operations.
|
|
90
|
-
|
|
91
|
-
**Schema:**
|
|
92
|
-
- `id`: Primary key
|
|
93
|
-
- `key`: VARCHAR(255) UNIQUE - Lock key
|
|
94
|
-
- `value`: TEXT - Lock value
|
|
95
|
-
- `expiresAt`: TIMESTAMP - Lock expiration
|
|
96
|
-
- `createdAt`: TIMESTAMP
|
|
97
|
-
|
|
98
|
-
**Indexes:**
|
|
99
|
-
- `idx_storage_locks_key` (UNIQUE) on `key`
|
|
100
|
-
- `idx_storage_locks_expires` on `expiresAt`
|
|
101
|
-
|
|
102
|
-
## Cleanup
|
|
103
|
-
|
|
104
|
-
Expired records are cleaned up via:
|
|
105
|
-
|
|
106
|
-
1. **Automatic expiration** - Queries check `expiresAt` and delete expired records
|
|
107
|
-
2. **Scheduled cleanup** - Run `adapter.cleanup()` periodically or set up a database-level scheduled job
|
|
108
|
-
|
|
109
|
-
**PostgreSQL (pg_cron):**
|
|
110
|
-
```sql
|
|
111
|
-
SELECT cron.schedule(
|
|
112
|
-
'cleanup-expired-storage',
|
|
113
|
-
'*/5 * * * *', -- Every 5 minutes
|
|
114
|
-
'DELETE FROM nauth_rate_limits WHERE expiresAt < NOW(); DELETE FROM nauth_storage_locks WHERE expiresAt < NOW();'
|
|
115
|
-
);
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
**MySQL (EVENT SCHEDULER):**
|
|
119
|
-
```sql
|
|
120
|
-
CREATE EVENT cleanup_expired_storage
|
|
121
|
-
ON SCHEDULE EVERY 5 MINUTE
|
|
122
|
-
DO
|
|
123
|
-
DELETE FROM nauth_rate_limits WHERE expiresAt < NOW();
|
|
124
|
-
DELETE FROM nauth_storage_locks WHERE expiresAt < NOW();
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
## Performance Considerations
|
|
128
|
-
|
|
129
|
-
- **Atomic operations** use database-level `UPDATE ... SET value = value + 1` for optimal concurrency
|
|
130
|
-
- **Indexes** on `key` and `expiresAt` ensure fast lookups
|
|
131
|
-
- **Hash/list operations** stored as JSON in the `value` column
|
|
132
|
-
- **Cleanup** should run periodically to prevent table bloat
|
|
133
|
-
|
|
134
|
-
## Comparison with Other Adapters
|
|
135
|
-
|
|
136
|
-
| Feature | MemoryStorageAdapter | DatabaseStorageAdapter | RedisStorageAdapter |
|
|
137
|
-
|---------|---------------------|------------------------|---------------------|
|
|
138
|
-
| Multi-server | ❌ | ✅ | ✅ |
|
|
139
|
-
| Persistence | ❌ | ✅ | ✅ (optional) |
|
|
140
|
-
| Performance | ⚡⚡⚡ | ⚡⚡ | ⚡⚡⚡ |
|
|
141
|
-
| External dependency | ❌ | ❌ | ✅ (Redis) |
|
|
142
|
-
| Setup complexity | 🟢 Low | 🟡 Medium | 🟡 Medium |
|
|
143
|
-
|
|
144
|
-
## License
|
|
145
|
-
|
|
146
|
-
MIT
|
|
7
|
+
**This is a preview release for internal testing. Do not use in production yet.**
|
|
147
8
|
|
|
9
|
+
This package is part of nauth-toolkit and is currently in early access/preview. Features and APIs may change between releases. For production use, please wait for the stable v1.0 release.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nauth-toolkit/storage-database",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Database storage adapter for nauth-toolkit using TypeORM",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"format:check": "prettier --check \"src/**/*.ts\""
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@nauth-toolkit/core": "^0.1.
|
|
17
|
+
"@nauth-toolkit/core": "^0.1.6",
|
|
18
18
|
"typeorm": "^0.3.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public",
|
|
32
|
-
"tag": "
|
|
32
|
+
"tag": "latest"
|
|
33
33
|
},
|
|
34
34
|
"license": "UNLICENSED",
|
|
35
35
|
"keywords": [
|