Skip to content

Commit

Permalink
Fix join in JoinedEntryRevision (#14)
Browse files Browse the repository at this point in the history
* Fix join in JoinedEntryRevision

* Return saved revision as default for getEntry
  • Loading branch information
imsitnikov authored Oct 30, 2023
1 parent 94a318b commit dc585ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
25 changes: 14 additions & 11 deletions src/db/presentations/joined-entry-revision/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Knex} from 'knex';
import {TransactionOrKnex} from 'objection';
import {TransactionOrKnex, raw} from 'objection';
import {Model} from '../..';
import {Entry} from '../../models/new/entry';
import {RevisionModel} from '../../models/new/revision';
Expand Down Expand Up @@ -43,21 +43,24 @@ export const selectedColumns = [
export interface JoinRevisionArgs {
revId?: RevisionModel['revId'];
branch?: 'saved' | 'published';
isPublishFallback?: boolean;
}

export const joinRevision =
({revId, branch, isPublishFallback}: JoinRevisionArgs) =>
({revId, branch}: JoinRevisionArgs) =>
(builder: Knex.JoinClause) => {
if (revId) {
builder.on(`${Entry.tableName}.entryId`, `${RevisionModel.tableName}.entryId`);
} else if (branch === 'published') {
builder.on(`${Entry.tableName}.publishedId`, `${RevisionModel.tableName}.revId`);
} else {
} else if (branch === 'saved') {
builder.on(`${Entry.tableName}.savedId`, `${RevisionModel.tableName}.revId`);
if (isPublishFallback) {
builder.orOn(`${Entry.tableName}.publishedId`, `${RevisionModel.tableName}.revId`);
}
} else {
builder.on(
raw(
`COALESCE(${Entry.tableName}.published_id, ${Entry.tableName}.saved_id)`,
) as unknown as string,
`${RevisionModel.tableName}.revId`,
);
}
};

Expand All @@ -75,11 +78,11 @@ export class JoinedEntryRevision extends Model {

static find({
where,
joinRevisionArgs,
joinRevisionArgs = {},
trx,
}: {
where: Record<string, unknown> | ((builder: Knex.QueryBuilder) => void);
joinRevisionArgs: JoinRevisionArgs;
joinRevisionArgs?: JoinRevisionArgs;
trx: TransactionOrKnex;
}) {
return JoinedEntryRevision.query(trx)
Expand All @@ -93,11 +96,11 @@ export class JoinedEntryRevision extends Model {

static findOne({
where,
joinRevisionArgs,
joinRevisionArgs = {},
trx,
}: {
where: Record<string, unknown> | ((builder: Knex.QueryBuilder) => void);
joinRevisionArgs: JoinRevisionArgs;
joinRevisionArgs?: JoinRevisionArgs;
trx: TransactionOrKnex;
}) {
return JoinedEntryRevision.query(trx)
Expand Down
6 changes: 0 additions & 6 deletions src/services/entry/actions/copy-to-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => {

builder.whereIn(`${Entry.tableName}.entryId`, entryIds);
},
joinRevisionArgs: {
isPublishFallback: true,
},
trx: Entry.replica,
});

Expand Down Expand Up @@ -269,9 +266,6 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => {

builder.whereIn(`${Entry.tableName}.entryId`, Array.from(mapEntryIdsWithOldIds.keys()));
},
joinRevisionArgs: {
isPublishFallback: true,
},
trx: transactionTrx,
});

Expand Down
2 changes: 1 addition & 1 deletion src/services/new/entry/get-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const getEntry = async (
{ctx, trx, skipValidation = false}: ServiceArgs,
args: GetEntryArgs,
) => {
const {entryId, revId, branch, includePermissionsInfo, includeLinks} = args;
const {entryId, revId, branch = 'saved', includePermissionsInfo, includeLinks} = args;

logInfo(ctx, 'GET_ENTRY_REQUEST', {
entryId: Utils.encodeId(entryId),
Expand Down

0 comments on commit dc585ba

Please sign in to comment.