Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (filter.where) {
const whereStrings = this.queryWhereBuilder(filter.where, tableName);
if (whereStrings.length > 0) {
whereStrings.map(whereString => {
if (whereString) {
query.andWhere(whereString);
}
});
}
}
if (filter.whereOr) {
const whereStringOrs = this.queryWhereBuilder(filter.whereOr, tableName);
if (whereStringOrs.length > 0) {
query.andWhere(
new Brackets(qb => {
whereStringOrs.map(whereStringOr => {
if (whereStringOr) {
qb.orWhere(whereStringOr);
}
});
}),
);
}
}
// resolve order by
if (filter.order && filter.order.length > 0) {
let isNewOrder: boolean = true;
filter.order.forEach(val => {
const split = val.split(' ');
protected formatPredicate(entityType: Function, predicate: { [field: string]: any }): Brackets {
// field names should be alphanumeric
const predicateFieldPatt = /^[\w\d]+$/;
const predciateFields = Object.keys(predicate);
const condition: { fieldName: string, value: any }[] = [];
for (let i = 0; i < predciateFields.length; i++) {
const fieldName = predciateFields[i];
if (!predicateFieldPatt.test(fieldName)) {
throw new Error('Invalid field name');
}
condition.push({ fieldName, value: predicate[fieldName] });
}
return new Brackets(qb => {
condition.forEach(({ fieldName, value }) => qb.andWhere(
`${fieldName} = :${fieldName}`,
{ [fieldName]: value },
));
});
}
const { term, facetIds, collectionId } = input;
qb.where('1 = 1');
if (term && term.length > this.minTermLength) {
// Note: SQLite does not natively have fulltext search capabilities,
// so we just use a weighted LIKE match
qb.addSelect(
`
CASE WHEN sku LIKE :like_term THEN 10 ELSE 0 END +
CASE WHEN productName LIKE :like_term THEN 3 ELSE 0 END +
CASE WHEN productVariantName LIKE :like_term THEN 2 ELSE 0 END +
CASE WHEN description LIKE :like_term THEN 1 ELSE 0 END`,
'score',
)
.andWhere(
new Brackets(qb1 => {
qb1.where('sku LIKE :like_term')
.orWhere('productName LIKE :like_term')
.orWhere('productVariantName LIKE :like_term')
.orWhere('description LIKE :like_term');
}),
)
.setParameters({ term, like_term: `%${term}%` });
}
if (facetIds) {
for (const id of facetIds) {
const placeholder = '_' + id;
qb.andWhere(`(',' || facetValueIds || ',') LIKE :${placeholder}`, {
[placeholder]: `%,${id},%`,
});
}
}
builder.orWhere(new typeorm_1.Brackets((qb) => {
for (let i = 0; i < parsed.or.length; i++) {
this.setAndWhere(parsed.or[i], `or${i}`, qb);
}
}));
}
else if (parsed.or.length === 1) {
this.setAndWhere(parsed.or[0], `or0`, builder);
builder.orWhere(new typeorm_1.Brackets((qb) => {
for (let i = 0; i < filters.length; i++) {
this.setAndWhere(filters[i], `filter${i}`, qb);
}
}));
}
else {
builder.andWhere(new typeorm_1.Brackets((qb) => {
for (let i = 0; i < filters.length; i++) {
this.setAndWhere(filters[i], `filter${i}`, qb);
}
}));
builder.orWhere(new typeorm_1.Brackets((qb) => {
for (let i = 0; i < parsed.or.length; i++) {
this.setAndWhere(parsed.or[i], `or${i}`, qb);
}
}));
}
}
else if (hasOr) {
for (let i = 0; i < parsed.or.length; i++) {
this.setOrWhere(parsed.or[i], `or${i}`, builder);
}
}
export const urlSlugMatchesClause = (relationName: string, urlSlug: string) => {
return new Brackets(qb =>
qb.where(`LOWER(REPLACE(${relationName}.urlSlug, '-', '_')) = LOWER(REPLACE(:urlSlug, '-', '_'))`,
{ urlSlug })
)
}
async findUsableDatabases(user: ContextUser): Promise {
const groupWhereStmt = new Brackets(qb => qb.where('moldb.group_id is NULL')
.orWhere('moldb.group_id = ANY(:usableGroupIds)', { usableGroupIds: user.groupIds ?? [] }));
const query = this.queryWhere(user,
new Brackets(qb => qb.where('moldb.archived = false').andWhere(groupWhereStmt))
);
return await query.getMany();
}
return new Brackets(rootQb => {
for (let i = 0; i < sortOptions.length; i++) {
const subKeySetComparison = new Brackets(qb => {
const subKeySet = key.slice(0, i + 1);
for (let j = 0; j < subKeySet.length; j++) {
const { sort, order } = sortOptions[j];
const cursorKey = subKeySet[j];
const paramterName = `${direction}__${sort}`;
let equality: string;
if (j === i) {
equality = order === 'ASC' ? eq[0] : eq[1];
} else {
equality = order === 'ASC' ? eq[2] : eq[3];
}
qb.andWhere(`"${sort}" ${equality} :${paramterName}`, { [paramterName]: cursorKey });
}
});
async function createUserProfileObject(user: User, deviceId: string): Promise {
const data: Array<{ type: string; last_date: string; cnt: number; }> = await Order.queryBuilder("ordr_createUserProfileObject")
.select("context.type", "type")
.addSelect("MAX(ordr_createUserProfileObject.createdDate)", "last_date")
.addSelect("COUNT(*)", "cnt")
.leftJoin("ordr_createUserProfileObject.contexts", "context")
.where("context.userId = :userId", { userId: user.id })
.andWhere(new Brackets(qb => {
qb.where("ordr_createUserProfileObject.status = :status", { status: "completed" })
.orWhere(
new Brackets(qb2 => {
qb2.where("ordr_createUserProfileObject.status IN (:statuses)", { statuses: ["pending", "opened"] })
.andWhere("ordr_createUserProfileObject.expirationDate > :date", { date: new Date() });
})
);
}))
.groupBy("context.type")
.getRawMany();
const stats: UserStats = {
earn_count: 0,
spend_count: 0
};