mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 05:27:52 +00:00
feat: added increment and decrement ops in update knex orm
This commit is contained in:
@@ -104,24 +104,68 @@ export const ormify = <DbOps extends object, Tname extends keyof Tables>(db: Kne
|
|||||||
throw new DatabaseError({ error, name: "Create" });
|
throw new DatabaseError({ error, name: "Create" });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
updateById: async (id: string, data: Tables[Tname]["update"], tx?: Knex) => {
|
updateById: async (
|
||||||
|
id: string,
|
||||||
|
{
|
||||||
|
$incr,
|
||||||
|
$decr,
|
||||||
|
...data
|
||||||
|
}: Tables[Tname]["update"] & {
|
||||||
|
$incr?: { [x in keyof Partial<Tables[Tname]["base"]>]: number };
|
||||||
|
$decr?: { [x in keyof Partial<Tables[Tname]["base"]>]: number };
|
||||||
|
},
|
||||||
|
tx?: Knex
|
||||||
|
) => {
|
||||||
try {
|
try {
|
||||||
const [res] = await (tx || db)(tableName)
|
const query = (tx || db)(tableName)
|
||||||
.where({ id } as never)
|
.where({ id } as never)
|
||||||
.update(data as never)
|
.update(data as never)
|
||||||
.returning("*");
|
.returning("*");
|
||||||
return res;
|
if ($incr) {
|
||||||
|
Object.entries($incr).forEach(([incrementField, incrementValue]) => {
|
||||||
|
void query.increment(incrementField, incrementValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if ($decr) {
|
||||||
|
Object.entries($decr).forEach(([incrementField, incrementValue]) => {
|
||||||
|
void query.increment(incrementField, incrementValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const [docs] = await query;
|
||||||
|
return docs;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new DatabaseError({ error, name: "Update by id" });
|
throw new DatabaseError({ error, name: "Update by id" });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
update: async (filter: TFindFilter<Tables[Tname]["base"]>, data: Tables[Tname]["update"], tx?: Knex) => {
|
update: async (
|
||||||
|
filter: TFindFilter<Tables[Tname]["base"]>,
|
||||||
|
{
|
||||||
|
$incr,
|
||||||
|
$decr,
|
||||||
|
...data
|
||||||
|
}: Tables[Tname]["update"] & {
|
||||||
|
$incr?: { [x in keyof Partial<Tables[Tname]["base"]>]: number };
|
||||||
|
$decr?: { [x in keyof Partial<Tables[Tname]["base"]>]: number };
|
||||||
|
},
|
||||||
|
tx?: Knex
|
||||||
|
) => {
|
||||||
try {
|
try {
|
||||||
const res = await (tx || db)(tableName)
|
const query = (tx || db)(tableName)
|
||||||
.where(buildFindFilter(filter))
|
.where(buildFindFilter(filter))
|
||||||
.update(data as never)
|
.update(data as never)
|
||||||
.returning("*");
|
.returning("*");
|
||||||
return res;
|
// increment and decrement operation in update
|
||||||
|
if ($incr) {
|
||||||
|
Object.entries($incr).forEach(([incrementField, incrementValue]) => {
|
||||||
|
void query.increment(incrementField, incrementValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if ($decr) {
|
||||||
|
Object.entries($decr).forEach(([incrementField, incrementValue]) => {
|
||||||
|
void query.increment(incrementField, incrementValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return await query;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new DatabaseError({ error, name: "Update" });
|
throw new DatabaseError({ error, name: "Update" });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user