Skip to content

Commit

Permalink
Removed upsert function
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Sep 29, 2024
1 parent 16896d5 commit ca81e7f
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 65 deletions.
28 changes: 0 additions & 28 deletions src/packages/pongo/src/core/collection/pongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
type ReplaceOneOptions,
type UpdateManyOptions,
type UpdateOneOptions,
type UpsertOneOptions,
type WithoutId,
type WithVersion,
} from '..';
Expand Down Expand Up @@ -204,28 +203,6 @@ export const pongoCollection = <
{ operationName: 'updateOne', collectionName, errors },
);
},
upsertOne: async (
filter: PongoFilter<T>,
update: PongoUpdate<T>,
options?: UpsertOneOptions,
): Promise<PongoUpdateResult> => {
await ensureCollectionCreated(options);

const result = await command<UpdateSqlResult>(
SqlFor.upsertOne(filter, update, options),
options,
);

return operationResult<PongoUpdateResult>(
{
successful: result.rows[0]!.modified === 1n,
modifiedCount: Number(result.rows[0]!.modified),
matchedCount: Number(result.rows[0]!.matched),
nextExpectedVersion: result.rows[0]!.version,
},
{ operationName: 'upsertOne', collectionName, errors },
);
},
replaceOne: async (
filter: PongoFilter<T>,
document: WithoutId<T>,
Expand Down Expand Up @@ -508,11 +485,6 @@ export type PongoCollectionSQLBuilder = {
update: PongoUpdate<T>,
options?: UpdateOneOptions,
) => SQL;
upsertOne: <T>(
filter: PongoFilter<T>,
update: PongoUpdate<T>,
options?: UpsertOneOptions,
) => SQL;
replaceOne: <T>(
filter: PongoFilter<T>,
document: WithoutId<T>,
Expand Down
9 changes: 0 additions & 9 deletions src/packages/pongo/src/core/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ export type UpdateOneOptions = {
expectedVersion?: Exclude<ExpectedDocumentVersion, 'DOCUMENT_DOES_NOT_EXIST'>;
} & CollectionOperationOptions;

export type UpsertOneOptions = {
expectedVersion?: ExpectedDocumentVersion;
} & CollectionOperationOptions;

export type UpdateManyOptions = {
expectedVersion?: Extract<
ExpectedDocumentVersion,
Expand Down Expand Up @@ -139,11 +135,6 @@ export interface PongoCollection<T extends PongoDocument> {
update: PongoUpdate<T>,
options?: UpdateOneOptions,
): Promise<PongoUpdateResult>;
upsertOne(
filter: PongoFilter<T>,
update: PongoUpdate<T>,
options?: UpsertOneOptions,
): Promise<PongoUpdateResult>;
replaceOne(
filter: PongoFilter<T>,
document: WithoutId<T>,
Expand Down
28 changes: 0 additions & 28 deletions src/packages/pongo/src/postgres/sqlBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
type PongoUpdate,
type ReplaceOneOptions,
type UpdateOneOptions,
type UpsertOneOptions,
type WithoutId,
} from '../../core';
import { constructFilterQuery } from './filter';
Expand Down Expand Up @@ -124,33 +123,6 @@ export const postgresSQLBuilder = (
collectionName,
);
},
upsertOne: <T>(
filter: PongoFilter<T>,
update: PongoUpdate<T>,
options?: UpsertOneOptions,
): SQL => {
const expectedVersionUpdate = options?.expectedVersion
? { _version: expectedVersionValue(options.expectedVersion) }
: {};

const filterQuery = constructFilterQuery<T>({
...expectedVersionUpdate,
...filter,
});
const updateQuery = buildUpdateQuery(update);

return sql(
`WITH cte AS (
SELECT _id FROM %I %s LIMIT 1
)
UPDATE %I SET data = %s FROM cte WHERE %I._id = cte._id;`,
collectionName,
where(filterQuery),
collectionName,
updateQuery,
collectionName,
);
},
replaceOne: <T>(
filter: PongoFilter<T>,
document: WithoutId<T>,
Expand Down

0 comments on commit ca81e7f

Please sign in to comment.