Skip to content

Commit 5c80a7f

Browse files
committed
Fixed updates
It appeared that I made a shameless error with test assertions and didn't catch the basic scenario. This commit fixes it, enhances also other operators and adds additional test coverage. Fixes #21
1 parent d87fbcd commit 5c80a7f

File tree

5 files changed

+120
-35
lines changed

5 files changed

+120
-35
lines changed

src/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo-core",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"engines": {

src/packages/pongo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"scripts": {

src/packages/pongo/src/e2e/compatibilityTest.e2e.spec.ts

+103-20
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void describe('MongoDB Compatibility Tests', () => {
9898
},
9999
);
100100
});
101+
101102
void it('should insert many documents into both PostgreSQL and MongoDB', async () => {
102103
const pongoCollection = pongoDb.collection<User>('insertMany');
103104
const mongoCollection = mongoDb.collection<User>('insertMany');
@@ -162,14 +163,57 @@ void describe('MongoDB Compatibility Tests', () => {
162163
_id: mongoInsertResult.insertedId,
163164
});
164165

166+
assert.equal(mongoDoc?.age, 31);
165167
assert.deepStrictEqual(
166168
{
167169
name: pongoDoc!.name,
168-
age: 31,
170+
age: pongoDoc!.age,
169171
},
170172
{
171173
name: mongoDoc!.name,
172-
age: 31,
174+
age: mongoDoc!.age,
175+
},
176+
);
177+
});
178+
179+
void it('should update a multiple properties in document in both PostgreSQL and MongoDB', async () => {
180+
const pongoCollection = pongoDb.collection<User>('updateOneMultiple');
181+
const mongoCollection = mongoDb.collection<User>('updateOneMultiple');
182+
const doc = { name: 'Roger', age: 30 };
183+
184+
const pongoInsertResult = await pongoCollection.insertOne(doc);
185+
const mongoInsertResult = await mongoCollection.insertOne(doc);
186+
187+
const update = { $set: { age: 31, tags: [] } };
188+
189+
await pongoCollection.updateOne(
190+
{ _id: pongoInsertResult.insertedId },
191+
update,
192+
);
193+
await mongoCollection.updateOne(
194+
{ _id: mongoInsertResult.insertedId },
195+
update,
196+
);
197+
198+
const pongoDoc = await pongoCollection.findOne({
199+
_id: pongoInsertResult.insertedId,
200+
});
201+
const mongoDoc = await mongoCollection.findOne({
202+
_id: mongoInsertResult.insertedId,
203+
});
204+
205+
assert.equal(mongoDoc?.age, 31);
206+
assert.deepEqual(mongoDoc?.tags, []);
207+
assert.deepStrictEqual(
208+
{
209+
name: pongoDoc!.name,
210+
age: pongoDoc!.age,
211+
tags: pongoDoc!.tags,
212+
},
213+
{
214+
name: mongoDoc!.name,
215+
age: mongoDoc!.age,
216+
tags: mongoDoc!.tags,
173217
},
174218
);
175219
});
@@ -218,11 +262,11 @@ void describe('MongoDB Compatibility Tests', () => {
218262
assert.deepStrictEqual(
219263
pongoDocs.map((doc) => ({
220264
name: doc.name,
221-
age: 31,
265+
age: doc.age,
222266
})),
223267
mongoDocs.map((doc) => ({
224268
name: doc.name,
225-
age: 31,
269+
age: doc.age,
226270
})),
227271
);
228272
});
@@ -235,10 +279,11 @@ void describe('MongoDB Compatibility Tests', () => {
235279
const pongoInsertResult = await pongoCollection.insertOne(doc);
236280
const mongoInsertResult = await mongoCollection.insertOne(doc);
237281

238-
await pongoCollection.updateOne(
282+
const { matchedCount } = await pongoCollection.updateOne(
239283
{ _id: pongoInsertResult.insertedId },
240284
{ $unset: { address: '' } },
241285
);
286+
assert.equal(matchedCount, 1);
242287
await mongoCollection.updateOne(
243288
{ _id: mongoInsertResult.insertedId },
244289
{ $unset: { address: '' } },
@@ -275,10 +320,11 @@ void describe('MongoDB Compatibility Tests', () => {
275320

276321
const update = { $inc: { age: 1 } };
277322

278-
await pongoCollection.updateOne(
323+
const { matchedCount } = await pongoCollection.updateOne(
279324
{ _id: pongoInsertResult.insertedId },
280325
update,
281326
);
327+
assert.equal(matchedCount, 1);
282328
await mongoCollection.updateOne(
283329
{ _id: mongoInsertResult.insertedId },
284330
update,
@@ -306,21 +352,39 @@ void describe('MongoDB Compatibility Tests', () => {
306352
void it('should update a document in both PostgreSQL and MongoDB using $push', async () => {
307353
const pongoCollection = pongoDb.collection<User>('testCollection');
308354
const mongoCollection = mongoDb.collection<User>('testCollection');
309-
const doc = { name: 'Roger', age: 30, tags: ['tag1'] };
355+
const doc = { name: 'Roger', age: 30 };
310356

311357
const pongoInsertResult = await pongoCollection.insertOne(doc);
312358
const mongoInsertResult = await mongoCollection.insertOne(doc);
359+
let pongoDoc = await pongoCollection.findOne({
360+
_id: pongoInsertResult.insertedId,
361+
});
362+
// Push to non existing
363+
let updateResult = await pongoCollection.updateOne(
364+
{ _id: pongoInsertResult.insertedId },
365+
{ $push: { tags: 'tag1' } },
366+
);
367+
assert.equal(updateResult.matchedCount, 1);
368+
await mongoCollection.updateOne(
369+
{ _id: mongoInsertResult.insertedId },
370+
{ $push: { tags: 'tag1' } },
371+
);
372+
pongoDoc = await pongoCollection.findOne({
373+
_id: pongoInsertResult.insertedId,
374+
});
313375

314-
await pongoCollection.updateOne(
376+
// Push to existing
377+
updateResult = await pongoCollection.updateOne(
315378
{ _id: pongoInsertResult.insertedId },
316379
{ $push: { tags: 'tag2' } },
317380
);
381+
assert.equal(updateResult.matchedCount, 1);
318382
await mongoCollection.updateOne(
319383
{ _id: mongoInsertResult.insertedId },
320384
{ $push: { tags: 'tag2' } },
321385
);
322386

323-
const pongoDoc = await pongoCollection.findOne({
387+
pongoDoc = await pongoCollection.findOne({
324388
_id: pongoInsertResult.insertedId,
325389
});
326390
const mongoDoc = await mongoCollection.findOne({
@@ -351,7 +415,10 @@ void describe('MongoDB Compatibility Tests', () => {
351415
const pongoInsertResult = await pongoCollection.insertOne(doc);
352416
const mongoInsertResult = await mongoCollection.insertOne(doc);
353417

354-
await pongoCollection.deleteOne({ _id: pongoInsertResult.insertedId });
418+
const { deletedCount } = await pongoCollection.deleteOne({
419+
_id: pongoInsertResult.insertedId,
420+
});
421+
assert.equal(deletedCount, 1);
355422
await mongoCollection.deleteOne({ _id: mongoInsertResult.insertedId });
356423

357424
const pongoDoc = await pongoCollection.findOne({
@@ -475,8 +542,12 @@ void describe('MongoDB Compatibility Tests', () => {
475542
});
476543

477544
void it('should find documents with a nested property filter in both PostgreSQL and MongoDB', async () => {
478-
const pongoCollection = pongoDb.collection<User>('testCollection');
479-
const mongoCollection = mongoDb.collection<User>('testCollection');
545+
const pongoCollection = pongoDb.collection<User>(
546+
'findWithNestedProperty',
547+
);
548+
const mongoCollection = mongoDb.collection<User>(
549+
'findWithNestedProperty',
550+
);
480551

481552
const docs = [
482553
{
@@ -522,8 +593,12 @@ void describe('MongoDB Compatibility Tests', () => {
522593
});
523594

524595
void it('should find documents with multiple nested property filters in both PostgreSQL and MongoDB', async () => {
525-
const pongoCollection = pongoDb.collection<User>('testCollection');
526-
const mongoCollection = mongoDb.collection<User>('testCollection');
596+
const pongoCollection = pongoDb.collection<User>(
597+
'findWithMultipleNestedProperties',
598+
);
599+
const mongoCollection = mongoDb.collection<User>(
600+
'findWithMultipleNestedProperties',
601+
);
527602

528603
const docs = [
529604
{
@@ -625,8 +700,8 @@ void describe('MongoDB Compatibility Tests', () => {
625700
});
626701

627702
void it('should find documents with an array filter in both PostgreSQL and MongoDB', async () => {
628-
const pongoCollection = pongoDb.collection<User>('testCollection');
629-
const mongoCollection = mongoDb.collection<User>('testCollection');
703+
const pongoCollection = pongoDb.collection<User>('findWithArrayFilter');
704+
const mongoCollection = mongoDb.collection<User>('findWithArrayFilter');
630705

631706
const docs = [
632707
{ name: 'Anita', age: 25, tags: ['tag1', 'tag2'] },
@@ -652,8 +727,12 @@ void describe('MongoDB Compatibility Tests', () => {
652727
});
653728

654729
void it('should find documents with multiple array filters in both PostgreSQL and MongoDB', async () => {
655-
const pongoCollection = pongoDb.collection<User>('testCollection');
656-
const mongoCollection = mongoDb.collection<User>('testCollection');
730+
const pongoCollection = pongoDb.collection<User>(
731+
'findWithMultipleArrayFilters',
732+
);
733+
const mongoCollection = mongoDb.collection<User>(
734+
'findWithMultipleArrayFilters',
735+
);
657736

658737
const docs = [
659738
{ name: 'Anita', age: 25, tags: ['tag1', 'tag2'] },
@@ -714,8 +793,12 @@ void describe('MongoDB Compatibility Tests', () => {
714793
});
715794

716795
void it('should find documents with a nested array element match filter in both PostgreSQL and MongoDB', async () => {
717-
const pongoCollection = pongoDb.collection<User>('testCollection');
718-
const mongoCollection = mongoDb.collection<User>('testCollection');
796+
const pongoCollection = pongoDb.collection<User>(
797+
'findWithElemMatchFilter',
798+
);
799+
const mongoCollection = mongoDb.collection<User>(
800+
'findWithElemMatchFilter',
801+
);
719802

720803
const docs = [
721804
{

src/packages/pongo/src/postgres/update/index.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ export const buildUpdateQuery = <T>(update: PongoUpdate<T>): SQL =>
1919
}, sql('data'));
2020

2121
export const buildSetQuery = <T>(set: $set<T>, currentUpdateQuery: SQL): SQL =>
22-
sql(
23-
'jsonb_set(%s, %L, data || %L)',
24-
currentUpdateQuery,
25-
'{}',
26-
JSON.stringify(set),
27-
);
22+
sql('%s || %L::jsonb', currentUpdateQuery, JSON.stringify(set));
2823

2924
export const buildUnsetQuery = <T>(
3025
unset: $unset<T>,
3126
currentUpdateQuery: SQL,
32-
): SQL => sql('%s - %L', currentUpdateQuery, Object.keys(unset).join(', '));
27+
): SQL =>
28+
sql(
29+
'%s - %L',
30+
currentUpdateQuery,
31+
Object.keys(unset)
32+
.map((k) => `{${k}}`)
33+
.join(', '),
34+
);
3335

3436
export const buildIncQuery = <T>(
3537
inc: $inc<T>,
3638
currentUpdateQuery: SQL,
3739
): SQL => {
3840
for (const [key, value] of Object.entries(inc)) {
3941
currentUpdateQuery = sql(
40-
"jsonb_set(%s, '{%s}', to_jsonb((data->>'%s')::numeric + %L))",
42+
"jsonb_set(%s, '{%s}', to_jsonb((data->>'%s')::numeric + %L), true)",
4143
currentUpdateQuery,
4244
key,
4345
key,
@@ -53,11 +55,11 @@ export const buildPushQuery = <T>(
5355
): SQL => {
5456
for (const [key, value] of Object.entries(push)) {
5557
currentUpdateQuery = sql(
56-
"jsonb_set(%s, '{%s}', (COALESCE(data->'%s', '[]'::jsonb) || '[%s]'::jsonb))",
58+
"jsonb_set(%s, '{%s}', (coalesce(data->'%s', '[]'::jsonb) || %L::jsonb), true)",
5759
currentUpdateQuery,
5860
key,
5961
key,
60-
JSON.stringify(value),
62+
JSON.stringify([value]),
6163
);
6264
}
6365
return currentUpdateQuery;

0 commit comments

Comments
 (0)