mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 18:27:19 +00:00
fix: apply project identity offset/limit separate from left joins
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
import { Knex } from "knex";
|
import { Knex } from "knex";
|
||||||
|
|
||||||
import { TDbClient } from "@app/db";
|
import { TDbClient } from "@app/db";
|
||||||
import { TableName } from "@app/db/schemas";
|
import { TableName, TIdentities } from "@app/db/schemas";
|
||||||
import { DatabaseError } from "@app/lib/errors";
|
import { DatabaseError } from "@app/lib/errors";
|
||||||
import { ormify, sqlNestRelationships } from "@app/lib/knex";
|
import { ormify, selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
|
||||||
import { TListProjectIdentityDTO } from "@app/services/identity-project/identity-project-types";
|
import { OrderByDirection } from "@app/lib/types";
|
||||||
|
import { ProjectIdentityOrderBy, TListProjectIdentityDTO } from "@app/services/identity-project/identity-project-types";
|
||||||
|
|
||||||
export type TIdentityProjectDALFactory = ReturnType<typeof identityProjectDALFactory>;
|
export type TIdentityProjectDALFactory = ReturnType<typeof identityProjectDALFactory>;
|
||||||
|
|
||||||
@@ -117,18 +118,38 @@ export const identityProjectDALFactory = (db: TDbClient) => {
|
|||||||
tx?: Knex
|
tx?: Knex
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
|
// TODO: scott - optimize, there's redundancy here with project membership and the below query
|
||||||
|
const fetchIdentitySubquery = (tx || db.replicaNode())(TableName.Identity)
|
||||||
|
.where((qb) => {
|
||||||
|
if (filter.search) {
|
||||||
|
void qb.whereILike(`${TableName.Identity}.name`, `%${filter.search}%`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.join(
|
||||||
|
TableName.IdentityProjectMembership,
|
||||||
|
`${TableName.IdentityProjectMembership}.identityId`,
|
||||||
|
`${TableName.Identity}.id`
|
||||||
|
)
|
||||||
|
.where(`${TableName.IdentityProjectMembership}.projectId`, projectId)
|
||||||
|
.offset(filter.offset ?? 0)
|
||||||
|
.limit(filter.limit ?? 100)
|
||||||
|
.orderBy(
|
||||||
|
`${TableName.Identity}.${filter.orderBy ?? ProjectIdentityOrderBy.Name}`,
|
||||||
|
filter.orderDirection ?? OrderByDirection.ASC
|
||||||
|
)
|
||||||
|
.select(selectAllTableCols(TableName.Identity))
|
||||||
|
.as(TableName.Identity); // required for subqueries
|
||||||
|
|
||||||
const query = (tx || db.replicaNode())(TableName.IdentityProjectMembership)
|
const query = (tx || db.replicaNode())(TableName.IdentityProjectMembership)
|
||||||
.where(`${TableName.IdentityProjectMembership}.projectId`, projectId)
|
.where(`${TableName.IdentityProjectMembership}.projectId`, projectId)
|
||||||
.join(TableName.Project, `${TableName.IdentityProjectMembership}.projectId`, `${TableName.Project}.id`)
|
.join(TableName.Project, `${TableName.IdentityProjectMembership}.projectId`, `${TableName.Project}.id`)
|
||||||
.join(TableName.Identity, `${TableName.IdentityProjectMembership}.identityId`, `${TableName.Identity}.id`)
|
.join<TIdentities, TIdentities>(fetchIdentitySubquery, (bd) => {
|
||||||
|
bd.on(`${TableName.IdentityProjectMembership}.identityId`, `${TableName.Identity}.id`);
|
||||||
|
})
|
||||||
.where((qb) => {
|
.where((qb) => {
|
||||||
if (filter.identityId) {
|
if (filter.identityId) {
|
||||||
void qb.where("identityId", filter.identityId);
|
void qb.where("identityId", filter.identityId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.search) {
|
|
||||||
void qb.whereILike(`${TableName.Identity}.name`, `%${filter.search}%`);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.join(
|
.join(
|
||||||
TableName.IdentityProjectMembershipRole,
|
TableName.IdentityProjectMembershipRole,
|
||||||
@@ -166,10 +187,7 @@ export const identityProjectDALFactory = (db: TDbClient) => {
|
|||||||
db.ref("name").as("projectName").withSchema(TableName.Project)
|
db.ref("name").as("projectName").withSchema(TableName.Project)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (filter.limit) {
|
// TODO: scott - joins seem to reorder identities so need to order again, for the sake of urgency will optimize at a later point
|
||||||
void query.offset(filter.offset ?? 0).limit(filter.limit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filter.orderBy) {
|
if (filter.orderBy) {
|
||||||
switch (filter.orderBy) {
|
switch (filter.orderBy) {
|
||||||
case "name":
|
case "name":
|
||||||
|
|||||||
Reference in New Issue
Block a user