feat(infisical-pg: added services for project, project key and environment

This commit is contained in:
Akhil Mohan
2024-01-27 12:38:22 +05:30
parent 53bb3bc610
commit 76e5d61da5
33 changed files with 1469 additions and 28 deletions
+22 -10
View File
@@ -5,6 +5,8 @@ import { TableName } from "@app/db/schemas";
import { DatabaseError } from "../errors";
export * from "./join";
export const withTransaction = <K extends object>(db: Knex, dal: K) => ({
transaction: async <T>(cb: (tx: Knex) => Promise<T>) =>
db.transaction(async (trx) => {
@@ -53,19 +55,29 @@ export const ormify = <DbOps extends object, Tname extends TableName>(
},
create: async (data: Tables[Tname]["insert"], tx?: Knex) => {
try {
const [user] = await (tx || db)(tableName).insert(data).returning("*");
return user;
const [res] = await (tx || db)(tableName).insert(data).returning("*");
return res;
} catch (error) {
throw new DatabaseError({ error, name: "Create" });
}
},
insertMany: async (data: readonly Tables[Tname]["insert"][], tx?: Knex) => {
try {
const res = await (tx || db)(tableName)
.insert(data as any)
.returning("*");
return res;
} catch (error) {
throw new DatabaseError({ error, name: "Create" });
}
},
updateById: async (id: string, data: Tables[Tname]["update"], tx?: Knex) => {
try {
const [user] = await (tx || db)(tableName)
const [res] = await (tx || db)(tableName)
.where({ id } as any)
.update(data as any)
.returning("*");
return user;
return res;
} catch (error) {
throw new DatabaseError({ error, name: "Update by id" });
}
@@ -76,30 +88,30 @@ export const ormify = <DbOps extends object, Tname extends TableName>(
tx?: Knex
) => {
try {
const user = await (tx || db)(tableName)
const res = await (tx || db)(tableName)
.where(filter)
.update(data as any)
.returning("*");
return user;
return res;
} catch (error) {
throw new DatabaseError({ error, name: "Update" });
}
},
deleteById: async (id: string, tx?: Knex) => {
try {
const [user] = await (tx || db)(tableName)
const [res] = await (tx || db)(tableName)
.where({ id } as any)
.delete()
.returning("*");
return user;
return res;
} catch (error) {
throw new DatabaseError({ error, name: "Delete by id" });
}
},
delete: async (filter: Partial<Tables[Tname]["base"]>, tx?: Knex) => {
try {
const user = await (tx || db)(tableName).where(filter).delete().returning("*");
return user;
const res = await (tx || db)(tableName).where(filter).delete().returning("*");
return res;
} catch (error) {
throw new DatabaseError({ error, name: "Delete" });
}
+28
View File
@@ -0,0 +1,28 @@
export const mergeOneToManyRelation = <
T extends Record<string, any>,
Pk extends keyof T,
P extends Record<string, any>,
C extends Record<string, any>,
Ck extends string = "child"
>(
data: T[],
key: Pk,
parentMapper: (arg: T) => P,
childMapper: (arg: T) => C,
childKey: Ck
) => {
const recordFirstVisitIndex: Record<string, number> = {};
const groupedRecord: (P & Record<Ck, C[]>)[] = [];
for (let i = 0; i < data.length; i += 1) {
const pk = data[i][key];
const row = data[i];
if (typeof recordFirstVisitIndex[pk] === "undefined") {
const parent = parentMapper(row) as any;
parent[childKey] = [];
groupedRecord.push(parent);
recordFirstVisitIndex[pk] = i;
}
groupedRecord[recordFirstVisitIndex[pk]][childKey].push(childMapper(row));
}
return groupedRecord;
};
+8
View File
@@ -1,3 +1,11 @@
import { ActorType } from "@app/services/auth/auth-type";
export type TProjectPermission = {
actor: ActorType;
actorId: string;
projectId: string;
};
export type RequiredKeys<T> = {
[K in keyof T]-?: undefined extends T[K] ? never : K;
}[keyof T];