mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-16 01:05:40 +00:00
73 lines
3.0 KiB
TypeScript
73 lines
3.0 KiB
TypeScript
import { Knex } from "knex";
|
|
|
|
import { TDbClient } from "@app/db";
|
|
import { DynamicSecretLeasesSchema, TableName } from "@app/db/schemas";
|
|
import { DatabaseError } from "@app/lib/errors";
|
|
import { ormify, selectAllTableCols } from "@app/lib/knex";
|
|
|
|
export type TDynamicSecretLeaseDALFactory = ReturnType<typeof dynamicSecretLeaseDALFactory>;
|
|
|
|
export const dynamicSecretLeaseDALFactory = (db: TDbClient) => {
|
|
const orm = ormify(db, TableName.DynamicSecretLease);
|
|
|
|
const countLeasesForDynamicSecret = async (dynamicSecretId: string, tx?: Knex) => {
|
|
try {
|
|
const doc = await (tx || db)(TableName.DynamicSecretLease).count("*").where({ dynamicSecretId }).first();
|
|
return parseInt(doc || "0", 10);
|
|
} catch (error) {
|
|
throw new DatabaseError({ error, name: "DynamicSecretCountLeases" });
|
|
}
|
|
};
|
|
|
|
const findById = async (id: string, tx?: Knex) => {
|
|
try {
|
|
const doc = await (tx || db)(TableName.DynamicSecretLease)
|
|
.where({ [`${TableName.DynamicSecretLease}.id` as "id"]: id })
|
|
.first()
|
|
.join(
|
|
TableName.DynamicSecret,
|
|
`${TableName.DynamicSecretLease}.dynamicSecretId`,
|
|
`${TableName.DynamicSecret}.id`
|
|
)
|
|
.select(selectAllTableCols(TableName.DynamicSecretLease))
|
|
.select(
|
|
db.ref("id").withSchema(TableName.DynamicSecret).as("dynId"),
|
|
db.ref("name").withSchema(TableName.DynamicSecret).as("dynName"),
|
|
db.ref("version").withSchema(TableName.DynamicSecret).as("dynVersion"),
|
|
db.ref("type").withSchema(TableName.DynamicSecret).as("dynType"),
|
|
db.ref("defaultTTL").withSchema(TableName.DynamicSecret).as("dynDefaultTTL"),
|
|
db.ref("maxTTL").withSchema(TableName.DynamicSecret).as("dynMaxTTL"),
|
|
db.ref("encryptedInput").withSchema(TableName.DynamicSecret).as("dynEncryptedInput"),
|
|
db.ref("folderId").withSchema(TableName.DynamicSecret).as("dynFolderId"),
|
|
db.ref("status").withSchema(TableName.DynamicSecret).as("dynStatus"),
|
|
db.ref("statusDetails").withSchema(TableName.DynamicSecret).as("dynStatusDetails"),
|
|
db.ref("createdAt").withSchema(TableName.DynamicSecret).as("dynCreatedAt"),
|
|
db.ref("updatedAt").withSchema(TableName.DynamicSecret).as("dynUpdatedAt")
|
|
);
|
|
if (!doc) return;
|
|
|
|
return {
|
|
...DynamicSecretLeasesSchema.parse(doc),
|
|
dynamicSecret: {
|
|
id: doc.dynId,
|
|
name: doc.dynName,
|
|
version: doc.dynVersion,
|
|
type: doc.dynType,
|
|
defaultTTL: doc.dynDefaultTTL,
|
|
maxTTL: doc.dynMaxTTL,
|
|
encryptedInput: doc.dynEncryptedInput,
|
|
folderId: doc.dynFolderId,
|
|
status: doc.dynStatus,
|
|
statusDetails: doc.dynStatusDetails,
|
|
createdAt: doc.dynCreatedAt,
|
|
updatedAt: doc.dynUpdatedAt
|
|
}
|
|
};
|
|
} catch (error) {
|
|
throw new DatabaseError({ error, name: "DynamicSecretLeaseFindById" });
|
|
}
|
|
};
|
|
|
|
return { ...orm, findById, countLeasesForDynamicSecret };
|
|
};
|