mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-14 02:20:12 +01:00
a126b5f5aa
* WIP converted all sqlite models into prisma calls * modify db setup and fix ApiKey model calls in admin.js * renaming function params to be consistent * converted adminEndpoints to utilize prisma orm * converted chatEndpoints to utilize prisma orm * converted inviteEndpoints to utilize prisma orm * converted systemEndpoints to utilize prisma orm * converted workspaceEndpoints to utilize prisma orm * converting sql queries to prisma calls * fixed default param bug for orderBy and limit * fixed typo for workspace chats * fixed order of deletion to account for sql relations * fix invite CRUD and workspace management CRUD * fixed CRUD for api keys * created prisma setup scripts/docs for understanding how to use prisma * prisma dependency change * removing unneeded console.logs * removing unneeded sql escape function * linting and creating migration script * migration from depreciated sqlite script update * removing unneeded migrations in prisma folder * create backup of old sqlite db and use transactions to ensure all operations complete successfully * adding migrations to gitignore * updated PRISMA.md docs for info on how to use sqlite migration script * comment changes * adding back migrations folder to repo * Reviewing SQL and prisma integraiton on fresh repo * update inline key replacement * ensure migration script executes and maps foreign_keys regardless of db ordering * run migration endpoint * support new prisma backend * bump version * change migration call --------- Co-authored-by: timothycarambat <rambat1010@gmail.com>
99 lines
2.4 KiB
JavaScript
99 lines
2.4 KiB
JavaScript
const { Telemetry } = require("./telemetry");
|
|
const prisma = require("../utils/prisma");
|
|
|
|
const ApiKey = {
|
|
tablename: "api_keys",
|
|
writable: [],
|
|
|
|
makeSecret: () => {
|
|
const uuidAPIKey = require("uuid-apikey");
|
|
return uuidAPIKey.create().apiKey;
|
|
},
|
|
|
|
create: async function (createdByUserId = null) {
|
|
try {
|
|
const apiKey = await prisma.api_keys.create({
|
|
data: {
|
|
secret: this.makeSecret(),
|
|
createdBy: createdByUserId,
|
|
},
|
|
});
|
|
|
|
await Telemetry.sendTelemetry("api_key_created");
|
|
return { apiKey, error: null };
|
|
} catch (error) {
|
|
console.error("FAILED TO CREATE API KEY.", error.message);
|
|
return { apiKey: null, error: error.message };
|
|
}
|
|
},
|
|
|
|
get: async function (clause = {}) {
|
|
try {
|
|
const apiKey = await prisma.api_keys.findFirst({ where: clause });
|
|
return apiKey;
|
|
} catch (error) {
|
|
console.error("FAILED TO GET API KEY.", error.message);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
count: async function (clause = {}) {
|
|
try {
|
|
const count = await prisma.api_keys.count({ where: clause });
|
|
return count;
|
|
} catch (error) {
|
|
console.error("FAILED TO COUNT API KEYS.", error.message);
|
|
return 0;
|
|
}
|
|
},
|
|
|
|
delete: async function (clause = {}) {
|
|
try {
|
|
await prisma.api_keys.deleteMany({ where: clause });
|
|
return true;
|
|
} catch (error) {
|
|
console.error("FAILED TO DELETE API KEY.", error.message);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
where: async function (clause = {}, limit) {
|
|
try {
|
|
const apiKeys = await prisma.api_keys.findMany({
|
|
where: clause,
|
|
take: limit,
|
|
});
|
|
return apiKeys;
|
|
} catch (error) {
|
|
console.error("FAILED TO GET API KEYS.", error.message);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
whereWithUser: async function (clause = {}, limit) {
|
|
try {
|
|
const { User } = require("./user");
|
|
const apiKeys = await this.where(clause, limit);
|
|
|
|
for (const apiKey of apiKeys) {
|
|
if (!apiKey.createdBy) continue;
|
|
const user = await User.get({ id: apiKey.createdBy });
|
|
if (!user) continue;
|
|
|
|
apiKey.createdBy = {
|
|
id: user.id,
|
|
username: user.username,
|
|
role: user.role,
|
|
};
|
|
}
|
|
|
|
return apiKeys;
|
|
} catch (error) {
|
|
console.error("FAILED TO GET API KEYS WITH USER.", error.message);
|
|
return [];
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = { ApiKey };
|