mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 16:40:30 +00:00
134 lines
4.8 KiB
TypeScript
134 lines
4.8 KiB
TypeScript
import { TDbClient } from "@app/db";
|
|
import { ProjectsSchema, TableName } from "@app/db/schemas";
|
|
import { DatabaseError } from "@app/lib/errors";
|
|
import { ormify, selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
|
|
|
|
export type TProjectDALFactory = ReturnType<typeof projectDALFactory>;
|
|
|
|
export const projectDALFactory = (db: TDbClient) => {
|
|
const projectOrm = ormify(db, TableName.Project);
|
|
|
|
const findAllProjects = async (userId: string) => {
|
|
try {
|
|
const workspaces = await db(TableName.ProjectMembership)
|
|
.where({ userId })
|
|
.join(TableName.Project, `${TableName.ProjectMembership}.projectId`, `${TableName.Project}.id`)
|
|
.leftJoin(TableName.Environment, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
|
|
.select(
|
|
selectAllTableCols(TableName.Project),
|
|
db.ref("id").withSchema(TableName.Project).as("_id"),
|
|
db.ref("id").withSchema(TableName.Environment).as("envId"),
|
|
db.ref("slug").withSchema(TableName.Environment).as("envSlug"),
|
|
db.ref("name").withSchema(TableName.Environment).as("envName")
|
|
)
|
|
.orderBy("createdAt", "asc", "last");
|
|
const nestedWorkspaces = sqlNestRelationships({
|
|
data: workspaces,
|
|
key: "id",
|
|
parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }),
|
|
childrenMapper: [
|
|
{
|
|
key: "envId",
|
|
label: "environments" as const,
|
|
mapper: ({ envId: id, envSlug: slug, envName: name }) => ({
|
|
id,
|
|
slug,
|
|
name
|
|
})
|
|
}
|
|
]
|
|
});
|
|
|
|
return nestedWorkspaces.map((workspace) => ({
|
|
...workspace,
|
|
organization: workspace.id
|
|
}));
|
|
} catch (error) {
|
|
throw new DatabaseError({ error, name: "Find all projects" });
|
|
}
|
|
};
|
|
|
|
const findAllProjectsByIdentity = async (identityId: string) => {
|
|
try {
|
|
const workspaces = await db(TableName.IdentityProjectMembership)
|
|
.where({ identityId })
|
|
.join(TableName.Project, `${TableName.IdentityProjectMembership}.projectId`, `${TableName.Project}.id`)
|
|
.leftJoin(TableName.Environment, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
|
|
.select(
|
|
selectAllTableCols(TableName.Project),
|
|
db.ref("id").withSchema(TableName.Project).as("_id"),
|
|
db.ref("id").withSchema(TableName.Environment).as("envId"),
|
|
db.ref("slug").withSchema(TableName.Environment).as("envSlug"),
|
|
db.ref("name").withSchema(TableName.Environment).as("envName")
|
|
)
|
|
.orderBy("createdAt", "asc", "last");
|
|
|
|
const nestedWorkspaces = sqlNestRelationships({
|
|
data: workspaces,
|
|
key: "id",
|
|
parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }),
|
|
childrenMapper: [
|
|
{
|
|
key: "envId",
|
|
label: "environments" as const,
|
|
mapper: ({ envId: id, envSlug: slug, envName: name }) => ({
|
|
id,
|
|
slug,
|
|
name
|
|
})
|
|
}
|
|
]
|
|
});
|
|
|
|
// We need to add the organization field, as it's required for one of our API endpoint responses.
|
|
return nestedWorkspaces.map((workspace) => ({
|
|
...workspace,
|
|
organization: workspace.id
|
|
}));
|
|
} catch (error) {
|
|
throw new DatabaseError({ error, name: "Find all projects by identity" });
|
|
}
|
|
};
|
|
|
|
const findProjectById = async (id: string) => {
|
|
try {
|
|
const workspaces = await db(TableName.ProjectMembership)
|
|
.where(`${TableName.Project}.id`, id)
|
|
.join(TableName.Project, `${TableName.ProjectMembership}.projectId`, `${TableName.Project}.id`)
|
|
.join(TableName.Environment, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
|
|
.select(
|
|
selectAllTableCols(TableName.Project),
|
|
db.ref("id").withSchema(TableName.Project).as("_id"),
|
|
db.ref("id").withSchema(TableName.Environment).as("envId"),
|
|
db.ref("slug").withSchema(TableName.Environment).as("envSlug"),
|
|
db.ref("name").withSchema(TableName.Environment).as("envName")
|
|
);
|
|
return sqlNestRelationships({
|
|
data: workspaces,
|
|
key: "id",
|
|
parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }),
|
|
childrenMapper: [
|
|
{
|
|
key: "envId",
|
|
label: "environments" as const,
|
|
mapper: ({ envId, envSlug: slug, envName: name }) => ({
|
|
id: envId,
|
|
slug,
|
|
name
|
|
})
|
|
}
|
|
]
|
|
})?.[0];
|
|
} catch (error) {
|
|
throw new DatabaseError({ error, name: "Find all projects" });
|
|
}
|
|
};
|
|
|
|
return {
|
|
...projectOrm,
|
|
findAllProjects,
|
|
findAllProjectsByIdentity,
|
|
findProjectById
|
|
};
|
|
};
|