Spaces:
Sleeping
Sleeping
Commit ·
777b311
1
Parent(s): 6e40730
fix: resolve CastError by making ApiKey model NeDB-compatible
Browse files- backend/db.js +3 -1
- backend/models/ApiKey.js +75 -37
backend/db.js
CHANGED
|
@@ -9,12 +9,14 @@ const db = {
|
|
| 9 |
users: new Datastore({ filename: path.join(dbDir, 'users.db'), autoload: true }),
|
| 10 |
sessions: new Datastore({ filename: path.join(dbDir, 'sessions.db'), autoload: true }),
|
| 11 |
messages: new Datastore({ filename: path.join(dbDir, 'messages.db'), autoload: true }),
|
| 12 |
-
announcements: new Datastore({ filename: path.join(dbDir, 'announcements.db'), autoload: true })
|
|
|
|
| 13 |
};
|
| 14 |
|
| 15 |
// Ensure indices
|
| 16 |
db.users.ensureIndex({ fieldName: 'email', unique: true });
|
| 17 |
db.users.ensureIndex({ fieldName: 'githubId', unique: true, sparse: true });
|
|
|
|
| 18 |
|
| 19 |
const connectDB = async () => {
|
| 20 |
console.log('Neural Archive: Local NeDB Interface ACTIVE ✅');
|
|
|
|
| 9 |
users: new Datastore({ filename: path.join(dbDir, 'users.db'), autoload: true }),
|
| 10 |
sessions: new Datastore({ filename: path.join(dbDir, 'sessions.db'), autoload: true }),
|
| 11 |
messages: new Datastore({ filename: path.join(dbDir, 'messages.db'), autoload: true }),
|
| 12 |
+
announcements: new Datastore({ filename: path.join(dbDir, 'announcements.db'), autoload: true }),
|
| 13 |
+
apikeys: new Datastore({ filename: path.join(dbDir, 'apikeys.db'), autoload: true })
|
| 14 |
};
|
| 15 |
|
| 16 |
// Ensure indices
|
| 17 |
db.users.ensureIndex({ fieldName: 'email', unique: true });
|
| 18 |
db.users.ensureIndex({ fieldName: 'githubId', unique: true, sparse: true });
|
| 19 |
+
db.apikeys.ensureIndex({ fieldName: 'key', unique: true });
|
| 20 |
|
| 21 |
const connectDB = async () => {
|
| 22 |
console.log('Neural Archive: Local NeDB Interface ACTIVE ✅');
|
backend/models/ApiKey.js
CHANGED
|
@@ -1,39 +1,77 @@
|
|
| 1 |
-
const
|
| 2 |
const crypto = require('crypto');
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { db } = require('../db');
|
| 2 |
const crypto = require('crypto');
|
| 3 |
|
| 4 |
+
class ApiKey {
|
| 5 |
+
constructor(keyData) {
|
| 6 |
+
Object.assign(this, keyData);
|
| 7 |
+
this.id = keyData._id;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
static async find(query) {
|
| 11 |
+
return new Promise((resolve, reject) => {
|
| 12 |
+
db.apikeys.find(query, (err, docs) => {
|
| 13 |
+
if (err) reject(err);
|
| 14 |
+
else resolve(docs.map(doc => new ApiKey(doc)));
|
| 15 |
+
});
|
| 16 |
+
});
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
static async findOne(query) {
|
| 20 |
+
return new Promise((resolve, reject) => {
|
| 21 |
+
db.apikeys.findOne(query, (err, doc) => {
|
| 22 |
+
if (err) reject(err);
|
| 23 |
+
else resolve(doc ? new ApiKey(doc) : null);
|
| 24 |
+
});
|
| 25 |
+
});
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
static async findById(id) {
|
| 29 |
+
return new Promise((resolve, reject) => {
|
| 30 |
+
db.apikeys.findOne({ _id: id }, (err, doc) => {
|
| 31 |
+
if (err) reject(err);
|
| 32 |
+
else resolve(doc ? new ApiKey(doc) : null);
|
| 33 |
+
});
|
| 34 |
+
});
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
static async create(keyData) {
|
| 38 |
+
keyData.isActive = keyData.isActive !== undefined ? keyData.isActive : true;
|
| 39 |
+
keyData.createdAt = keyData.createdAt || new Date();
|
| 40 |
+
keyData.lastUsed = keyData.lastUsed || null;
|
| 41 |
+
|
| 42 |
+
return new Promise((resolve, reject) => {
|
| 43 |
+
db.apikeys.insert(keyData, (err, newDoc) => {
|
| 44 |
+
if (err) reject(err);
|
| 45 |
+
else resolve(new ApiKey(newDoc));
|
| 46 |
+
});
|
| 47 |
+
});
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
static generateKey() {
|
| 51 |
+
const token = crypto.randomBytes(24).toString('hex');
|
| 52 |
+
return `sk-cdx-${token}`;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
async save() {
|
| 56 |
+
return new Promise((resolve, reject) => {
|
| 57 |
+
const data = { ...this };
|
| 58 |
+
const id = data._id;
|
| 59 |
+
delete data._id;
|
| 60 |
+
db.apikeys.update({ _id: id }, { $set: data }, (err) => {
|
| 61 |
+
if (err) reject(err);
|
| 62 |
+
else resolve(this);
|
| 63 |
+
});
|
| 64 |
+
});
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
async deleteOne() {
|
| 68 |
+
return new Promise((resolve, reject) => {
|
| 69 |
+
db.apikeys.remove({ _id: this._id }, {}, (err) => {
|
| 70 |
+
if (err) reject(err);
|
| 71 |
+
else resolve();
|
| 72 |
+
});
|
| 73 |
+
});
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
module.exports = ApiKey;
|