mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-21 01:16:49 +00:00
feat(infisical-pg: added services for project, project key and environment
This commit is contained in:
@@ -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" });
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user