Skip to content

Commit

Permalink
Fixed inserted by _id when _id is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Aug 8, 2024
1 parent df2de79 commit 31b3aa7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/packages/pongo/src/core/collection/pongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export const pongoCollection = <
): Promise<PongoInsertOneResult> => {
await createCollection(options);

const _id = uuid();
const _id = (document._id as string | undefined | null) ?? uuid();

const result = await command(
SqlFor.insertOne({ _id, ...document } as OptionalUnlessRequiredId<T>),
SqlFor.insertOne({ ...document, _id } as OptionalUnlessRequiredId<T>),
options,
);

Expand All @@ -108,15 +108,15 @@ export const pongoCollection = <
): Promise<PongoInsertManyResult> => {
await createCollection(options);

const rows = documents.map(
(doc) =>
({
_id: uuid(),
...doc,
}) as OptionalUnlessRequiredId<T>,
);
const rows = documents.map((doc) => ({
...doc,
_id: (doc._id as string | undefined | null) ?? uuid(),
}));

const result = await command(SqlFor.insertMany(rows), options);
const result = await command(
SqlFor.insertMany(rows as OptionalUnlessRequiredId<T>[]),
options,
);

return {
acknowledged: result.rowCount === rows.length,
Expand Down
8 changes: 4 additions & 4 deletions src/packages/pongo/src/core/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ export declare type EnhancedOmit<TRecordOrUnion, KeyUnion> =
: never;
export declare type OptionalUnlessRequiredId<TSchema> = TSchema extends {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_id: any;
_id: string | ObjectId;
}
? TSchema
: OptionalId<TSchema>;

export declare type WithId<TSchema> = EnhancedOmit<TSchema, '_id'> & {
_id: InferIdType<TSchema>;
_id: string | ObjectId;
};

export type WithoutId<T> = Omit<T, '_id'>;
Expand All @@ -189,11 +189,11 @@ export declare interface Document {
}

export declare type OptionalId<TSchema> = EnhancedOmit<TSchema, '_id'> & {
_id?: InferIdType<TSchema>;
_id?: string | ObjectId;
};

export declare interface ObjectIdLike {
__id?: string;
__id?: string | ObjectId;
}

export declare type NonObjectIdLikeDocument = {
Expand Down
40 changes: 40 additions & 0 deletions src/packages/pongo/src/e2e/postgres.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type User = {
tags?: string[];
};

type UserWithoutId = {
name: string;
age: number;
address?: Address;
tags?: string[];
};

void describe('MongoDB Compatibility Tests', () => {
let postgres: StartedPostgreSqlContainer;
let postgresConnectionString: string;
Expand Down Expand Up @@ -65,6 +72,39 @@ void describe('MongoDB Compatibility Tests', () => {
});

void describe('Insert Operations', () => {
void it('should insert a document with id into both PostgreSQL and MongoDB', async () => {
const pongoCollection = pongoDb.collection<User>('insertOne');
const mongoCollection = mongoDb.collection<User>('shiminsertOne');
const _id = new Date().toISOString();
const doc: User = {
_id: new Date().toISOString(),
name: 'Anita',
age: 25,
};
const pongoInsertResult = await pongoCollection.insertOne(doc);
const mongoInsertResult = await mongoCollection.insertOne(doc);
assert(pongoInsertResult.insertedId);
assert(mongoInsertResult.insertedId);
const pongoDoc = await pongoCollection.findOne({
_id: pongoInsertResult.insertedId,
});
const mongoDoc = await mongoCollection.findOne({
_id: mongoInsertResult.insertedId,
});
assert.ok(pongoDoc);
assert.ok(mongoDoc);
assert.deepStrictEqual(
{
name: pongoDoc.name,
age: pongoDoc.age,
},
{
name: mongoDoc.name,
age: mongoDoc.age,
},
);
});

void it('should insert a document into both PostgreSQL and MongoDB', async () => {
const pongoCollection = pongoDb.collection<User>('insertOne');
const mongoCollection = mongoDb.collection<User>('shiminsertOne');
Expand Down

0 comments on commit 31b3aa7

Please sign in to comment.