Spaces:
Sleeping
Sleeping
Andrew
commited on
Commit
·
b346517
1
Parent(s):
cf90a38
feat(db): add userTokens collection and multi-provider user index
Browse files- src/lib/server/database.ts +30 -1
src/lib/server/database.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { MongoMemoryServer } from "mongodb-memory-server";
|
|
| 16 |
import { logger } from "$lib/server/logger";
|
| 17 |
import { building } from "$app/environment";
|
| 18 |
import type { TokenCache } from "$lib/types/TokenCache";
|
|
|
|
| 19 |
import { onExit } from "./exitHandler";
|
| 20 |
import { fileURLToPath } from "url";
|
| 21 |
import { dirname, join } from "path";
|
|
@@ -131,6 +132,7 @@ export class Database {
|
|
| 131 |
const migrationResults = db.collection<MigrationResult>("migrationResults");
|
| 132 |
const semaphores = db.collection<Semaphore>("semaphores");
|
| 133 |
const tokenCaches = db.collection<TokenCache>("tokens");
|
|
|
|
| 134 |
const tools = db.collection("tools");
|
| 135 |
const configCollection = db.collection<ConfigKey>("config");
|
| 136 |
|
|
@@ -150,6 +152,7 @@ export class Database {
|
|
| 150 |
migrationResults,
|
| 151 |
semaphores,
|
| 152 |
tokenCaches,
|
|
|
|
| 153 |
tools,
|
| 154 |
config: configCollection,
|
| 155 |
};
|
|
@@ -174,6 +177,7 @@ export class Database {
|
|
| 174 |
messageEvents,
|
| 175 |
semaphores,
|
| 176 |
tokenCaches,
|
|
|
|
| 177 |
config,
|
| 178 |
} = this.getCollections();
|
| 179 |
|
|
@@ -235,7 +239,29 @@ export class Database {
|
|
| 235 |
.createIndex({ userId: 1 }, { unique: true, sparse: true })
|
| 236 |
.catch((e) => logger.error(e));
|
| 237 |
settings.createIndex({ assistants: 1 }).catch((e) => logger.error(e));
|
| 238 |
-
users
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
users
|
| 240 |
.createIndex({ sessionId: 1 }, { unique: true, sparse: true })
|
| 241 |
.catch((e) => logger.error(e));
|
|
@@ -271,6 +297,9 @@ export class Database {
|
|
| 271 |
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 5 * 60 })
|
| 272 |
.catch((e) => logger.error(e));
|
| 273 |
tokenCaches.createIndex({ tokenHash: 1 }).catch((e) => logger.error(e));
|
|
|
|
|
|
|
|
|
|
| 274 |
// Tools removed: skipping tools indexes
|
| 275 |
|
| 276 |
conversations
|
|
|
|
| 16 |
import { logger } from "$lib/server/logger";
|
| 17 |
import { building } from "$app/environment";
|
| 18 |
import type { TokenCache } from "$lib/types/TokenCache";
|
| 19 |
+
import type { UserToken } from "$lib/types/UserToken";
|
| 20 |
import { onExit } from "./exitHandler";
|
| 21 |
import { fileURLToPath } from "url";
|
| 22 |
import { dirname, join } from "path";
|
|
|
|
| 132 |
const migrationResults = db.collection<MigrationResult>("migrationResults");
|
| 133 |
const semaphores = db.collection<Semaphore>("semaphores");
|
| 134 |
const tokenCaches = db.collection<TokenCache>("tokens");
|
| 135 |
+
const userTokens = db.collection<UserToken>("userTokens");
|
| 136 |
const tools = db.collection("tools");
|
| 137 |
const configCollection = db.collection<ConfigKey>("config");
|
| 138 |
|
|
|
|
| 152 |
migrationResults,
|
| 153 |
semaphores,
|
| 154 |
tokenCaches,
|
| 155 |
+
userTokens,
|
| 156 |
tools,
|
| 157 |
config: configCollection,
|
| 158 |
};
|
|
|
|
| 177 |
messageEvents,
|
| 178 |
semaphores,
|
| 179 |
tokenCaches,
|
| 180 |
+
userTokens,
|
| 181 |
config,
|
| 182 |
} = this.getCollections();
|
| 183 |
|
|
|
|
| 239 |
.createIndex({ userId: 1 }, { unique: true, sparse: true })
|
| 240 |
.catch((e) => logger.error(e));
|
| 241 |
settings.createIndex({ assistants: 1 }).catch((e) => logger.error(e));
|
| 242 |
+
users
|
| 243 |
+
.createIndex(
|
| 244 |
+
{ hfUserId: 1 },
|
| 245 |
+
{
|
| 246 |
+
unique: true,
|
| 247 |
+
name: "hfUserId_1",
|
| 248 |
+
partialFilterExpression: { hfUserId: { $exists: true } },
|
| 249 |
+
}
|
| 250 |
+
)
|
| 251 |
+
.catch((e) => logger.error(e));
|
| 252 |
+
users
|
| 253 |
+
.createIndex(
|
| 254 |
+
{ authProvider: 1, authId: 1 },
|
| 255 |
+
{
|
| 256 |
+
unique: true,
|
| 257 |
+
name: "authProvider_1_authId_1",
|
| 258 |
+
partialFilterExpression: {
|
| 259 |
+
authProvider: { $exists: true },
|
| 260 |
+
authId: { $exists: true },
|
| 261 |
+
},
|
| 262 |
+
}
|
| 263 |
+
)
|
| 264 |
+
.catch((e) => logger.error(e));
|
| 265 |
users
|
| 266 |
.createIndex({ sessionId: 1 }, { unique: true, sparse: true })
|
| 267 |
.catch((e) => logger.error(e));
|
|
|
|
| 297 |
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 5 * 60 })
|
| 298 |
.catch((e) => logger.error(e));
|
| 299 |
tokenCaches.createIndex({ tokenHash: 1 }).catch((e) => logger.error(e));
|
| 300 |
+
userTokens
|
| 301 |
+
.createIndex({ userId: 1, provider: 1 }, { unique: true })
|
| 302 |
+
.catch((e) => logger.error(e));
|
| 303 |
// Tools removed: skipping tools indexes
|
| 304 |
|
| 305 |
conversations
|