@whenmoon-afk/memory-mcp 2.1.2 → 2.2.1
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/LICENSE +21 -21
- package/README.md +153 -3
- package/dist/database/better-sqlite3-driver.d.ts +38 -0
- package/dist/database/better-sqlite3-driver.d.ts.map +1 -0
- package/dist/database/better-sqlite3-driver.js +82 -0
- package/dist/database/better-sqlite3-driver.js.map +1 -0
- package/dist/database/connection.d.ts +5 -5
- package/dist/database/connection.d.ts.map +1 -1
- package/dist/database/connection.js +7 -4
- package/dist/database/connection.js.map +1 -1
- package/dist/database/db-driver.d.ts +76 -0
- package/dist/database/db-driver.d.ts.map +1 -0
- package/dist/database/db-driver.js +9 -0
- package/dist/database/db-driver.js.map +1 -0
- package/dist/database/driver-factory.d.ts +51 -0
- package/dist/database/driver-factory.d.ts.map +1 -0
- package/dist/database/driver-factory.js +63 -0
- package/dist/database/driver-factory.js.map +1 -0
- package/dist/database/schema.d.ts +5 -5
- package/dist/database/schema.d.ts.map +1 -1
- package/dist/database/schema.js.map +1 -1
- package/dist/database/sqljs-driver.d.ts +50 -0
- package/dist/database/sqljs-driver.d.ts.map +1 -0
- package/dist/database/sqljs-driver.js +108 -0
- package/dist/database/sqljs-driver.js.map +1 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/search/semantic-search.d.ts +2 -2
- package/dist/search/semantic-search.d.ts.map +1 -1
- package/dist/search/semantic-search.js.map +1 -1
- package/dist/tools/memory-forget.d.ts +2 -2
- package/dist/tools/memory-forget.d.ts.map +1 -1
- package/dist/tools/memory-forget.js.map +1 -1
- package/dist/tools/memory-recall.d.ts +2 -2
- package/dist/tools/memory-recall.d.ts.map +1 -1
- package/dist/tools/memory-recall.js.map +1 -1
- package/dist/tools/memory-store.d.ts +2 -2
- package/dist/tools/memory-store.d.ts.map +1 -1
- package/dist/tools/memory-store.js.map +1 -1
- package/install.js +47 -5
- package/package.json +3 -2
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 WhenMoon-afk
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 WhenMoon-afk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -83,6 +83,150 @@ npm run build
|
|
|
83
83
|
|
|
84
84
|
---
|
|
85
85
|
|
|
86
|
+
### Claude Code Installation
|
|
87
|
+
|
|
88
|
+
For users of Claude Code (terminal-based Claude), use the `claude mcp add` command:
|
|
89
|
+
|
|
90
|
+
**Global Memory (user-wide, persists across all projects):**
|
|
91
|
+
```bash
|
|
92
|
+
claude mcp add memory -s user -- npx -y @whenmoon-afk/memory-mcp
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Per-Project Memory (project-specific, stored in project directory):**
|
|
96
|
+
```bash
|
|
97
|
+
claude mcp add memory -s local -e MEMORY_DB_PATH=./memory.db -- npx -y @whenmoon-afk/memory-mcp
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Verify installation:**
|
|
101
|
+
```bash
|
|
102
|
+
claude mcp list
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
> **Tip:** Use `--scope user` for personal knowledge that spans projects. Use `--scope local` with `MEMORY_DB_PATH=./memory.db` for codebase-specific context that stays with the repository.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Docker Deployment
|
|
110
|
+
|
|
111
|
+
Run the Memory MCP server in a containerized environment with persistent storage.
|
|
112
|
+
|
|
113
|
+
### Prerequisites
|
|
114
|
+
- Docker installed ([Get Docker](https://docs.docker.com/get-docker/))
|
|
115
|
+
- Docker Compose (included with Docker Desktop)
|
|
116
|
+
|
|
117
|
+
### Quick Start with Docker Compose
|
|
118
|
+
|
|
119
|
+
**1. Clone the repository:**
|
|
120
|
+
```bash
|
|
121
|
+
git clone https://github.com/WhenMoon-afk/claude-memory-mcp.git
|
|
122
|
+
cd claude-memory-mcp
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**2. Start the container:**
|
|
126
|
+
```bash
|
|
127
|
+
docker-compose up -d
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
This will:
|
|
131
|
+
- Build the Docker image with Node.js 20
|
|
132
|
+
- Create a persistent volume at `./data/` for the database
|
|
133
|
+
- Start the MCP server in detached mode
|
|
134
|
+
|
|
135
|
+
**3. View logs:**
|
|
136
|
+
```bash
|
|
137
|
+
docker-compose logs -f memory-mcp
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**4. Stop the container:**
|
|
141
|
+
```bash
|
|
142
|
+
docker-compose down
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Manual Docker Build
|
|
146
|
+
|
|
147
|
+
**Build the image:**
|
|
148
|
+
```bash
|
|
149
|
+
docker build -t memory-mcp:latest .
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**Run the container:**
|
|
153
|
+
```bash
|
|
154
|
+
# Create data directory
|
|
155
|
+
mkdir -p ./data
|
|
156
|
+
|
|
157
|
+
# Run container with volume mount
|
|
158
|
+
docker run -d \
|
|
159
|
+
--name memory-mcp \
|
|
160
|
+
-v "$(pwd)/data:/data" \
|
|
161
|
+
-e MEMORY_DB_PATH=/data/memory.db \
|
|
162
|
+
-e DEFAULT_TTL_DAYS=90 \
|
|
163
|
+
memory-mcp:latest
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**Interact with the running container:**
|
|
167
|
+
```bash
|
|
168
|
+
# View logs
|
|
169
|
+
docker logs -f memory-mcp
|
|
170
|
+
|
|
171
|
+
# Execute commands inside container
|
|
172
|
+
docker exec -it memory-mcp /bin/bash
|
|
173
|
+
|
|
174
|
+
# Stop container
|
|
175
|
+
docker stop memory-mcp
|
|
176
|
+
|
|
177
|
+
# Remove container
|
|
178
|
+
docker rm memory-mcp
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Volume Persistence
|
|
182
|
+
|
|
183
|
+
The database is stored in a Docker volume mapped to `./data/` on your host:
|
|
184
|
+
|
|
185
|
+
| Location | Path |
|
|
186
|
+
|----------|------|
|
|
187
|
+
| **Host** | `./data/memory.db` |
|
|
188
|
+
| **Container** | `/data/memory.db` |
|
|
189
|
+
|
|
190
|
+
**To backup your database:**
|
|
191
|
+
```bash
|
|
192
|
+
cp ./data/memory.db ./memory-backup-$(date +%Y%m%d).db
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
**To restore from backup:**
|
|
196
|
+
```bash
|
|
197
|
+
cp ./memory-backup-20250105.db ./data/memory.db
|
|
198
|
+
docker-compose restart
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Environment Variables (Docker)
|
|
202
|
+
|
|
203
|
+
Configure via `docker-compose.yml` or `-e` flags:
|
|
204
|
+
|
|
205
|
+
| Variable | Default | Description |
|
|
206
|
+
|----------|---------|-------------|
|
|
207
|
+
| `MEMORY_DB_PATH` | `/data/memory.db` | Database file location inside container |
|
|
208
|
+
| `DEFAULT_TTL_DAYS` | `90` | Default memory expiration (days) |
|
|
209
|
+
| `MEMORY_DB_DRIVER` | `better-sqlite3` | Database driver (better-sqlite3 or sqljs) |
|
|
210
|
+
| `NODE_ENV` | `production` | Node environment |
|
|
211
|
+
|
|
212
|
+
### Docker Notes
|
|
213
|
+
|
|
214
|
+
- **No HTTP Port**: This is a stdio-based MCP server. Communication happens via stdin/stdout, not HTTP requests.
|
|
215
|
+
- **Native Dependencies**: Uses multi-stage build to compile better-sqlite3 native bindings
|
|
216
|
+
- **Resource Limits**: Default docker-compose.yml sets 512MB memory limit (adjustable)
|
|
217
|
+
- **Auto-restart**: Container restarts automatically unless explicitly stopped
|
|
218
|
+
|
|
219
|
+
### Using with MCP Clients
|
|
220
|
+
|
|
221
|
+
When running in Docker, you'll need to configure your MCP client to communicate with the containerized server. The exact method depends on your client:
|
|
222
|
+
|
|
223
|
+
- **For local development**: Use `docker exec` to run commands
|
|
224
|
+
- **For production**: Consider using a container orchestration platform (Kubernetes, ECS, etc.)
|
|
225
|
+
|
|
226
|
+
> **Tip**: For most use cases, the **NPM package installation** (Option 1) is simpler and more suitable than Docker, since MCP servers typically run locally and communicate directly with the AI client via stdio.
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
86
230
|
## Integrate with Your MCP Client
|
|
87
231
|
|
|
88
232
|
Add to your client's MCP config file:
|
|
@@ -298,11 +442,17 @@ All other dependencies are dev-only (TypeScript, testing, linting).
|
|
|
298
442
|
|
|
299
443
|
## Environment Variables
|
|
300
444
|
|
|
301
|
-
|
|
|
302
|
-
|
|
303
|
-
| `MEMORY_DB_PATH` |
|
|
445
|
+
| Variable | Default | Description |
|
|
446
|
+
|----------|---------|-------------|
|
|
447
|
+
| `MEMORY_DB_PATH` | Platform-specific* | Database file location |
|
|
448
|
+
| `MEMORY_DB_DRIVER` | `better-sqlite3` | Database driver (`better-sqlite3` or `sqljs`) |
|
|
304
449
|
| `DEFAULT_TTL_DAYS` | `90` | Default time-to-live for memories (days) |
|
|
305
450
|
|
|
451
|
+
*Default database locations (when installed via `npx`):
|
|
452
|
+
- **macOS:** `~/.claude-memories/memory.db`
|
|
453
|
+
- **Windows:** `%APPDATA%\claude-memories\memory.db`
|
|
454
|
+
- **Linux:** `~/.local/share/claude-memories/memory.db`
|
|
455
|
+
|
|
306
456
|
---
|
|
307
457
|
|
|
308
458
|
### Security
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BetterSqlite3Driver - Adapter for better-sqlite3
|
|
3
|
+
*
|
|
4
|
+
* Wraps better-sqlite3's Database to implement the DbDriver interface.
|
|
5
|
+
* This adapter allows the codebase to depend on the interface rather than
|
|
6
|
+
* the concrete better-sqlite3 types, enabling future database driver swaps.
|
|
7
|
+
*/
|
|
8
|
+
import Database from 'better-sqlite3';
|
|
9
|
+
import type { DbDriver, PreparedStatement } from './db-driver.js';
|
|
10
|
+
/**
|
|
11
|
+
* Adapter for better-sqlite3 Database
|
|
12
|
+
* Implements DbDriver interface
|
|
13
|
+
*/
|
|
14
|
+
export declare class BetterSqlite3Driver implements DbDriver {
|
|
15
|
+
private db;
|
|
16
|
+
constructor(db: Database.Database);
|
|
17
|
+
prepare<T = unknown>(sql: string): PreparedStatement<T>;
|
|
18
|
+
exec(sql: string): void;
|
|
19
|
+
pragma(pragma: string): unknown;
|
|
20
|
+
transaction<T>(fn: () => T): () => T;
|
|
21
|
+
close(): void;
|
|
22
|
+
/**
|
|
23
|
+
* Get the underlying better-sqlite3 Database instance
|
|
24
|
+
* Only exposed for migration purposes - avoid using in business logic
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
getUnderlying(): Database.Database;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a BetterSqlite3Driver from a file path
|
|
32
|
+
*
|
|
33
|
+
* @param path Database file path
|
|
34
|
+
* @param options better-sqlite3 options
|
|
35
|
+
* @returns Driver instance
|
|
36
|
+
*/
|
|
37
|
+
export declare function createBetterSqlite3Driver(path: string, options?: Database.Options): BetterSqlite3Driver;
|
|
38
|
+
//# sourceMappingURL=better-sqlite3-driver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"better-sqlite3-driver.d.ts","sourceRoot":"","sources":["../../src/database/better-sqlite3-driver.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,EAAE,QAAQ,EAAE,iBAAiB,EAAa,MAAM,gBAAgB,CAAC;AA+B7E;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,QAAQ;IACtC,OAAO,CAAC,EAAE;gBAAF,EAAE,EAAE,QAAQ,CAAC,QAAQ;IAEzC,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC;IAKvD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIvB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAI/B,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC;IAIpC,KAAK,IAAI,IAAI;IAIb;;;;;OAKG;IACH,aAAa,IAAI,QAAQ,CAAC,QAAQ;CAGnC;AAED;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO,GACzB,mBAAmB,CAGrB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BetterSqlite3Driver - Adapter for better-sqlite3
|
|
3
|
+
*
|
|
4
|
+
* Wraps better-sqlite3's Database to implement the DbDriver interface.
|
|
5
|
+
* This adapter allows the codebase to depend on the interface rather than
|
|
6
|
+
* the concrete better-sqlite3 types, enabling future database driver swaps.
|
|
7
|
+
*/
|
|
8
|
+
import Database from 'better-sqlite3';
|
|
9
|
+
/**
|
|
10
|
+
* Wrapper for better-sqlite3's Statement
|
|
11
|
+
* Implements PreparedStatement interface
|
|
12
|
+
*/
|
|
13
|
+
class StatementWrapper {
|
|
14
|
+
stmt;
|
|
15
|
+
constructor(stmt) {
|
|
16
|
+
this.stmt = stmt;
|
|
17
|
+
}
|
|
18
|
+
run(...params) {
|
|
19
|
+
const result = this.stmt.run(...params);
|
|
20
|
+
return {
|
|
21
|
+
changes: result.changes,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
get(...params) {
|
|
25
|
+
return this.stmt.get(...params);
|
|
26
|
+
}
|
|
27
|
+
all(...params) {
|
|
28
|
+
return this.stmt.all(...params);
|
|
29
|
+
}
|
|
30
|
+
pluck() {
|
|
31
|
+
// better-sqlite3's pluck() returns a modified Statement
|
|
32
|
+
const pluckedStmt = this.stmt.pluck();
|
|
33
|
+
return new StatementWrapper(pluckedStmt);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Adapter for better-sqlite3 Database
|
|
38
|
+
* Implements DbDriver interface
|
|
39
|
+
*/
|
|
40
|
+
export class BetterSqlite3Driver {
|
|
41
|
+
db;
|
|
42
|
+
constructor(db) {
|
|
43
|
+
this.db = db;
|
|
44
|
+
}
|
|
45
|
+
prepare(sql) {
|
|
46
|
+
const stmt = this.db.prepare(sql);
|
|
47
|
+
return new StatementWrapper(stmt);
|
|
48
|
+
}
|
|
49
|
+
exec(sql) {
|
|
50
|
+
this.db.exec(sql);
|
|
51
|
+
}
|
|
52
|
+
pragma(pragma) {
|
|
53
|
+
return this.db.pragma(pragma);
|
|
54
|
+
}
|
|
55
|
+
transaction(fn) {
|
|
56
|
+
return this.db.transaction(fn);
|
|
57
|
+
}
|
|
58
|
+
close() {
|
|
59
|
+
this.db.close();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get the underlying better-sqlite3 Database instance
|
|
63
|
+
* Only exposed for migration purposes - avoid using in business logic
|
|
64
|
+
*
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
getUnderlying() {
|
|
68
|
+
return this.db;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a BetterSqlite3Driver from a file path
|
|
73
|
+
*
|
|
74
|
+
* @param path Database file path
|
|
75
|
+
* @param options better-sqlite3 options
|
|
76
|
+
* @returns Driver instance
|
|
77
|
+
*/
|
|
78
|
+
export function createBetterSqlite3Driver(path, options) {
|
|
79
|
+
const db = new Database(path, options);
|
|
80
|
+
return new BetterSqlite3Driver(db);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=better-sqlite3-driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"better-sqlite3-driver.js","sourceRoot":"","sources":["../../src/database/better-sqlite3-driver.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAGtC;;;GAGG;AACH,MAAM,gBAAgB;IACA;IAApB,YAAoB,IAAwB;QAAxB,SAAI,GAAJ,IAAI,CAAoB;IAAG,CAAC;IAEhD,GAAG,CAAC,GAAG,MAAiB;QACtB,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;QACxC,OAAO;YACL,OAAO,EAAE,MAAM,CAAC,OAAO;SACxB,CAAC;IACJ,CAAC;IAED,GAAG,CAAC,GAAG,MAAiB;QACtB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAkB,CAAC;IACnD,CAAC;IAED,GAAG,CAAC,GAAG,MAAiB;QACtB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAQ,CAAC;IACzC,CAAC;IAED,KAAK;QACH,wDAAwD;QACxD,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QACtC,OAAO,IAAI,gBAAgB,CAAI,WAAW,CAAC,CAAC;IAC9C,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IACV;IAApB,YAAoB,EAAqB;QAArB,OAAE,GAAF,EAAE,CAAmB;IAAG,CAAC;IAE7C,OAAO,CAAc,GAAW;QAC9B,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAClC,OAAO,IAAI,gBAAgB,CAAI,IAAI,CAAC,CAAC;IACvC,CAAC;IAED,IAAI,CAAC,GAAW;QACd,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,MAAc;QACnB,OAAO,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAED,WAAW,CAAI,EAAW;QACxB,OAAO,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;IACjC,CAAC;IAED,KAAK;QACH,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC;IAED;;;;;OAKG;IACH,aAAa;QACX,OAAO,IAAI,CAAC,EAAE,CAAC;IACjB,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,yBAAyB,CACvC,IAAY,EACZ,OAA0B;IAE1B,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACvC,OAAO,IAAI,mBAAmB,CAAC,EAAE,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Database connection management and utilities
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
4
|
+
import type { DbDriver } from './db-driver.js';
|
|
5
5
|
import { type DatabaseStats } from './schema.js';
|
|
6
6
|
/**
|
|
7
7
|
* Get or create database connection
|
|
8
8
|
*/
|
|
9
|
-
export declare function getDatabase(path: string):
|
|
9
|
+
export declare function getDatabase(path: string): DbDriver;
|
|
10
10
|
/**
|
|
11
11
|
* Close database connection
|
|
12
12
|
*/
|
|
@@ -14,7 +14,7 @@ export declare function closeDatabase(): void;
|
|
|
14
14
|
/**
|
|
15
15
|
* Execute a transaction
|
|
16
16
|
*/
|
|
17
|
-
export declare function transaction<T>(db:
|
|
17
|
+
export declare function transaction<T>(db: DbDriver, fn: () => T): T;
|
|
18
18
|
/**
|
|
19
19
|
* Serialize metadata to JSON string
|
|
20
20
|
*/
|
|
@@ -39,9 +39,9 @@ export interface PruneResult {
|
|
|
39
39
|
deleted_count: number;
|
|
40
40
|
total_pruned: number;
|
|
41
41
|
}
|
|
42
|
-
export declare function pruneMemories(db:
|
|
42
|
+
export declare function pruneMemories(db: DbDriver, olderThanDays?: number, dryRun?: boolean): PruneResult;
|
|
43
43
|
/**
|
|
44
44
|
* Get database statistics
|
|
45
45
|
*/
|
|
46
|
-
export declare function getStats(db:
|
|
46
|
+
export declare function getStats(db: DbDriver): DatabaseStats;
|
|
47
47
|
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/database/connection.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/database/connection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/C,OAAO,EAKL,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;AAKrB;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAoClD;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAKpC;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAG3D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAE3E;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAMzE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,MAAM,GAAE,MAAc,GAAG,MAAM,CAIzD;AAED;;GAEG;AACH,wBAAgB,GAAG,IAAI,MAAM,CAE5B;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,aAAa,CAC3B,EAAE,EAAE,QAAQ,EACZ,aAAa,GAAE,MAAU,EACzB,MAAM,GAAE,OAAe,GACtB,WAAW,CAwEb;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,EAAE,EAAE,QAAQ,GAAG,aAAa,CAEpD"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Database connection management and utilities
|
|
3
3
|
*/
|
|
4
|
-
import Database from 'better-sqlite3';
|
|
5
4
|
import { existsSync, mkdirSync } from 'fs';
|
|
6
5
|
import { dirname } from 'path';
|
|
6
|
+
import { createDriverFromEnv } from './driver-factory.js';
|
|
7
7
|
import { initializeSchema, createViews, optimizeDatabase, getDatabaseStats, } from './schema.js';
|
|
8
8
|
import { DatabaseError } from '../types/index.js';
|
|
9
9
|
let dbInstance = null;
|
|
@@ -20,9 +20,12 @@ export function getDatabase(path) {
|
|
|
20
20
|
if (!existsSync(dir)) {
|
|
21
21
|
mkdirSync(dir, { recursive: true });
|
|
22
22
|
}
|
|
23
|
-
// Open database
|
|
24
|
-
dbInstance =
|
|
25
|
-
|
|
23
|
+
// Open database using driver factory (respects MEMORY_DB_DRIVER env var)
|
|
24
|
+
dbInstance = createDriverFromEnv({
|
|
25
|
+
path,
|
|
26
|
+
options: {
|
|
27
|
+
verbose: process.env['NODE_ENV'] === 'development' ? () => { } : undefined,
|
|
28
|
+
},
|
|
26
29
|
});
|
|
27
30
|
// Initialize schema
|
|
28
31
|
initializeSchema(dbInstance);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/database/connection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/database/connection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EACL,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,gBAAgB,GAEjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,IAAI,UAAU,GAAoB,IAAI,CAAC;AAEvC;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,IAAI,CAAC;QACH,0BAA0B;QAC1B,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;QAC1B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACrB,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACtC,CAAC;QAED,yEAAyE;QACzE,UAAU,GAAG,mBAAmB,CAAC;YAC/B,IAAI;YACJ,OAAO,EAAE;gBACP,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,aAAa,CAAC,CAAC,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,SAAS;aAC1E;SACF,CAAC,CAAC;QAEH,oBAAoB;QACpB,gBAAgB,CAAC,UAAU,CAAC,CAAC;QAE7B,eAAe;QACf,WAAW,CAAC,UAAU,CAAC,CAAC;QAExB,WAAW;QACX,gBAAgB,CAAC,UAAU,CAA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E,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database driver interface - Abstraction layer for SQLite operations
|
|
3
|
+
*
|
|
4
|
+
* This interface decouples the application from better-sqlite3's concrete types,
|
|
5
|
+
* allowing for alternative implementations (e.g., libsql, better-sqlite3-multiple-ciphers)
|
|
6
|
+
* without changing business logic.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Result returned by .run() operations (INSERT/UPDATE/DELETE)
|
|
10
|
+
*/
|
|
11
|
+
export interface RunResult {
|
|
12
|
+
/**
|
|
13
|
+
* Number of rows affected by the operation
|
|
14
|
+
*/
|
|
15
|
+
changes: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepared statement interface - mirrors better-sqlite3's Statement API
|
|
19
|
+
*/
|
|
20
|
+
export interface PreparedStatement<T = unknown> {
|
|
21
|
+
/**
|
|
22
|
+
* Execute statement and return run result
|
|
23
|
+
*/
|
|
24
|
+
run(...params: unknown[]): RunResult;
|
|
25
|
+
/**
|
|
26
|
+
* Execute statement and return single row (or undefined if no match)
|
|
27
|
+
*/
|
|
28
|
+
get(...params: unknown[]): T | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Execute statement and return all matching rows
|
|
31
|
+
*/
|
|
32
|
+
all(...params: unknown[]): T[];
|
|
33
|
+
/**
|
|
34
|
+
* Return a statement that extracts only the first column of each row
|
|
35
|
+
* Used for COUNT queries and scalar selects
|
|
36
|
+
*/
|
|
37
|
+
pluck(): PreparedStatement<T>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Main database driver interface
|
|
41
|
+
*/
|
|
42
|
+
export interface DbDriver {
|
|
43
|
+
/**
|
|
44
|
+
* Prepare a SQL statement for execution
|
|
45
|
+
*
|
|
46
|
+
* @param sql SQL statement with ? placeholders
|
|
47
|
+
* @returns Prepared statement object
|
|
48
|
+
*/
|
|
49
|
+
prepare<T = unknown>(sql: string): PreparedStatement<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Execute multi-statement SQL (no parameter binding)
|
|
52
|
+
* Used for schema creation, migrations, and DDL operations
|
|
53
|
+
*
|
|
54
|
+
* @param sql Multi-statement SQL string
|
|
55
|
+
*/
|
|
56
|
+
exec(sql: string): void;
|
|
57
|
+
/**
|
|
58
|
+
* Set a SQLite PRAGMA
|
|
59
|
+
*
|
|
60
|
+
* @param pragma PRAGMA statement (e.g., "journal_mode = WAL")
|
|
61
|
+
* @returns Pragma value if query pragma, undefined if setting pragma
|
|
62
|
+
*/
|
|
63
|
+
pragma(pragma: string): unknown;
|
|
64
|
+
/**
|
|
65
|
+
* Create a transaction function wrapper
|
|
66
|
+
*
|
|
67
|
+
* @param fn Function to execute in transaction
|
|
68
|
+
* @returns Callable transaction function
|
|
69
|
+
*/
|
|
70
|
+
transaction<T>(fn: () => T): () => T;
|
|
71
|
+
/**
|
|
72
|
+
* Close the database connection
|
|
73
|
+
*/
|
|
74
|
+
close(): void;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=db-driver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-driver.d.ts","sourceRoot":"","sources":["../../src/database/db-driver.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,OAAO;IAC5C;;OAEG;IACH,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAC;IAErC;;OAEG;IACH,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC;IAEzC;;OAEG;IACH,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC;IAE/B;;;OAGG;IACH,KAAK,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB;;;;;OAKG;IACH,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC;IAExD;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAExB;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAEhC;;;;;OAKG;IACH,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC;IAErC;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;CACf"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database driver interface - Abstraction layer for SQLite operations
|
|
3
|
+
*
|
|
4
|
+
* This interface decouples the application from better-sqlite3's concrete types,
|
|
5
|
+
* allowing for alternative implementations (e.g., libsql, better-sqlite3-multiple-ciphers)
|
|
6
|
+
* without changing business logic.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=db-driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-driver.js","sourceRoot":"","sources":["../../src/database/db-driver.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Driver Factory - Selects database driver based on environment configuration
|
|
3
|
+
*
|
|
4
|
+
* Supports pluggable database drivers through the DbDriver interface.
|
|
5
|
+
* Selection is controlled by MEMORY_DB_DRIVER environment variable.
|
|
6
|
+
*/
|
|
7
|
+
import type { DbDriver } from './db-driver.js';
|
|
8
|
+
import type Database from 'better-sqlite3';
|
|
9
|
+
/**
|
|
10
|
+
* Supported database driver types
|
|
11
|
+
*/
|
|
12
|
+
export type DriverType = 'better-sqlite3' | 'sqljs';
|
|
13
|
+
/**
|
|
14
|
+
* Driver configuration options
|
|
15
|
+
*/
|
|
16
|
+
export interface DriverConfig {
|
|
17
|
+
/**
|
|
18
|
+
* Database file path (for file-based drivers like better-sqlite3)
|
|
19
|
+
*/
|
|
20
|
+
path: string;
|
|
21
|
+
/**
|
|
22
|
+
* Driver-specific options (e.g., better-sqlite3 Database.Options)
|
|
23
|
+
*/
|
|
24
|
+
options?: Database.Options;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create database driver based on type
|
|
28
|
+
*
|
|
29
|
+
* @param type - Driver type ('better-sqlite3' or 'sqljs')
|
|
30
|
+
* @param config - Driver configuration
|
|
31
|
+
* @returns DbDriver instance
|
|
32
|
+
* @throws Error if driver type is unsupported or driver initialization fails
|
|
33
|
+
*/
|
|
34
|
+
export declare function createDriver(type: DriverType, config: DriverConfig): DbDriver;
|
|
35
|
+
/**
|
|
36
|
+
* Get driver type from environment variable
|
|
37
|
+
*
|
|
38
|
+
* @returns Driver type from MEMORY_DB_DRIVER env var, defaults to 'better-sqlite3'
|
|
39
|
+
*/
|
|
40
|
+
export declare function getDriverTypeFromEnv(): DriverType;
|
|
41
|
+
/**
|
|
42
|
+
* Create database driver from environment configuration
|
|
43
|
+
*
|
|
44
|
+
* Convenience function that reads MEMORY_DB_DRIVER from environment
|
|
45
|
+
* and creates the appropriate driver instance.
|
|
46
|
+
*
|
|
47
|
+
* @param config - Driver configuration
|
|
48
|
+
* @returns DbDriver instance
|
|
49
|
+
*/
|
|
50
|
+
export declare function createDriverFromEnv(config: DriverConfig): DbDriver;
|
|
51
|
+
//# sourceMappingURL=driver-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver-factory.d.ts","sourceRoot":"","sources":["../../src/database/driver-factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG/C,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,gBAAgB,GAAG,OAAO,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC;CAC5B;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,GAAG,QAAQ,CAgB7E;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,IAAI,UAAU,CAoBjD;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,YAAY,GAAG,QAAQ,CAMlE"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Driver Factory - Selects database driver based on environment configuration
|
|
3
|
+
*
|
|
4
|
+
* Supports pluggable database drivers through the DbDriver interface.
|
|
5
|
+
* Selection is controlled by MEMORY_DB_DRIVER environment variable.
|
|
6
|
+
*/
|
|
7
|
+
import { createBetterSqlite3Driver } from './better-sqlite3-driver.js';
|
|
8
|
+
import { createSqlJsDriver } from './sqljs-driver.js';
|
|
9
|
+
/**
|
|
10
|
+
* Create database driver based on type
|
|
11
|
+
*
|
|
12
|
+
* @param type - Driver type ('better-sqlite3' or 'sqljs')
|
|
13
|
+
* @param config - Driver configuration
|
|
14
|
+
* @returns DbDriver instance
|
|
15
|
+
* @throws Error if driver type is unsupported or driver initialization fails
|
|
16
|
+
*/
|
|
17
|
+
export function createDriver(type, config) {
|
|
18
|
+
switch (type) {
|
|
19
|
+
case 'better-sqlite3':
|
|
20
|
+
return createBetterSqlite3Driver(config.path, config.options);
|
|
21
|
+
case 'sqljs':
|
|
22
|
+
// SqlJsDriver is a stub - throws informative error
|
|
23
|
+
return createSqlJsDriver();
|
|
24
|
+
default:
|
|
25
|
+
throw new Error(`Unsupported driver type: ${type}\n` +
|
|
26
|
+
'Supported drivers: better-sqlite3, sqljs\n' +
|
|
27
|
+
'Set MEMORY_DB_DRIVER environment variable to select driver.');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get driver type from environment variable
|
|
32
|
+
*
|
|
33
|
+
* @returns Driver type from MEMORY_DB_DRIVER env var, defaults to 'better-sqlite3'
|
|
34
|
+
*/
|
|
35
|
+
export function getDriverTypeFromEnv() {
|
|
36
|
+
const envDriver = process.env['MEMORY_DB_DRIVER'];
|
|
37
|
+
if (!envDriver) {
|
|
38
|
+
return 'better-sqlite3'; // Default
|
|
39
|
+
}
|
|
40
|
+
const normalized = envDriver.toLowerCase().trim();
|
|
41
|
+
if (normalized === 'better-sqlite3' || normalized === 'sqljs') {
|
|
42
|
+
return normalized;
|
|
43
|
+
}
|
|
44
|
+
console.error(`[WARN] Unknown MEMORY_DB_DRIVER value: "${envDriver}". ` +
|
|
45
|
+
'Using default: better-sqlite3. ' +
|
|
46
|
+
'Valid options: better-sqlite3, sqljs');
|
|
47
|
+
return 'better-sqlite3';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create database driver from environment configuration
|
|
51
|
+
*
|
|
52
|
+
* Convenience function that reads MEMORY_DB_DRIVER from environment
|
|
53
|
+
* and creates the appropriate driver instance.
|
|
54
|
+
*
|
|
55
|
+
* @param config - Driver configuration
|
|
56
|
+
* @returns DbDriver instance
|
|
57
|
+
*/
|
|
58
|
+
export function createDriverFromEnv(config) {
|
|
59
|
+
const driverType = getDriverTypeFromEnv();
|
|
60
|
+
console.error(`[INFO] Using database driver: ${driverType}`);
|
|
61
|
+
return createDriver(driverType, config);
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=driver-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver-factory.js","sourceRoot":"","sources":["../../src/database/driver-factory.ts"],"names":[],"m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