Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated sample to 0.16.1 version #97

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ Read also [introduction article on my blog](https://event-driven.io/en/introduct
You can use Pongo syntax with explicit typing about supported syntax:

```ts
import { pongoClient } from "@event-driven-io/pongo";
import { v7 as uuid } from "uuid";
import { pongoClient, ObjectId } from "@event-driven-io/pongo";

type User = { name: string; age: number };

Expand All @@ -35,7 +34,7 @@ const pongoDb = pongo.db();
const users = pongoDb.collection<User>("users");
const roger = { name: "Roger", age: 30 };
const anita = { name: "Anita", age: 25 };
const cruella = { _id: uuid(), name: "Cruella", age: 40 };
const cruella = { _id: ObjectId(), name: "Cruella", age: 40 };

// Inserting
await users.insertOne(roger);
Expand All @@ -60,8 +59,7 @@ const usersFromDb = await users.find({ age: { $lt: 40 } });
Or use MongoDB compliant shim:

```ts
import { MongoClient } from "@event-driven-io/pongo/shim";
import { v7 as uuid } from "uuid";
import { MongoClient, ObjectId } from "@event-driven-io/pongo/shim";

type User = { name: string; age: number };

Expand All @@ -74,7 +72,7 @@ const pongoDb = pongoClient.db();
const users = pongoDb.collection<User>("users");
const roger = { name: "Roger", age: 30 };
const anita = { name: "Anita", age: 25 };
const cruella = { _id: uuid(), name: "Cruella", age: 40 };
const cruella = { _id: ObjectId(), name: "Cruella", age: 40 };

// Inserting
await users.insertOne(roger);
Expand Down
16 changes: 8 additions & 8 deletions samples/simple-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion samples/simple-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
"dist"
],
"dependencies": {
"@event-driven-io/pongo": "0.15.0"
"@event-driven-io/pongo": "0.16.1"
}
}
10 changes: 5 additions & 5 deletions samples/simple-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pongoClient } from '@event-driven-io/pongo';
import { v7 as uuid } from 'uuid';
import { prettyJson } from '@event-driven-io/dumbo';
import { ObjectId, pongoClient } from '@event-driven-io/pongo';

type User = { _id?: string; name: string; age: number };

Expand All @@ -13,7 +13,7 @@ const pongoDb = pongo.db();
const users = pongoDb.collection<User>('users');
const roger = { name: 'Roger', age: 30 };
const anita = { name: 'Anita', age: 25 };
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };
const cruella = { _id: ObjectId(), name: 'Cruella', age: 40 };

// Inserting
await users.insertOne(roger);
Expand All @@ -30,10 +30,10 @@ await users.deleteOne({ _id: cruella._id });

// Finding by Id
const anitaFromDb = await users.findOne({ _id: anitaId });
console.log(JSON.stringify(anitaFromDb));
console.log(prettyJson(anitaFromDb));

// Finding more
const usersFromDB = await users.find({ age: { $lt: 40 } });
console.log(JSON.stringify(usersFromDB));
console.log(prettyJson(usersFromDB));

await pongo.close();
7 changes: 4 additions & 3 deletions samples/simple-ts/src/shim.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { prettyJson } from '@event-driven-io/dumbo';
import { MongoClient } from '@event-driven-io/pongo/shim';

type User = { name: string; age: number };
Expand All @@ -12,7 +13,7 @@ const pongoDb = pongoClient.db('postgres');
const users = pongoDb.collection<User>('users');
const roger = { name: 'Roger', age: 30 };
const anita = { name: 'Anita', age: 25 };
//const cruella = { _id: uuid(), name: 'Cruella', age: 40 };
//const cruella = { _id: ObjectId(), name: 'Cruella', age: 40 };

// Inserting
await users.insertOne(roger);
Expand All @@ -29,10 +30,10 @@ await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });

// Finding by Id
const anitaFromDb = await users.findOne({ _id: anitaId });
console.log(anitaFromDb);
console.log(prettyJson(anitaFromDb));

// Finding more
const usersFromDB = await users.find({ age: { $lt: 40 } }).toArray();
console.log(usersFromDB);
console.log(prettyJson(usersFromDB));

await pongoClient.close();
10 changes: 5 additions & 5 deletions samples/simple-ts/src/typedClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pongoClient } from '@event-driven-io/pongo';
import { v7 as uuid } from 'uuid';
import { prettyJson } from '@event-driven-io/dumbo';
import { ObjectId, pongoClient } from '@event-driven-io/pongo';
import config from './pongo.config';

const connectionString =
Expand All @@ -14,7 +14,7 @@ const pongoDb = pongo.database;
const users = pongoDb.users;
const roger = { name: 'Roger', age: 30 };
const anita = { name: 'Anita', age: 25 };
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };
const cruella = { _id: ObjectId(), name: 'Cruella', age: 40 };

// Inserting
await users.insertOne(roger);
Expand All @@ -31,10 +31,10 @@ await users.deleteOne({ _id: cruella._id });

// Finding by Id
const anitaFromDb = await users.findOne({ _id: anitaId });
console.log(anitaFromDb);
console.log(prettyJson(anitaFromDb));

// Finding more
const usersFromDB = await users.find({ age: { $lt: 40 } });
console.log(usersFromDB);
console.log(prettyJson(usersFromDB));

await pongo.close();
10 changes: 4 additions & 6 deletions src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ Read also [introduction article on my blog](https://event-driven.io/en/introduct
You can use Pongo syntax with explicit typing about supported syntax:

```ts
import { pongoClient } from '@event-driven-io/pongo';
import { v7 as uuid } from 'uuid';
import { pongoClient, ObjectId } from '@event-driven-io/pongo';

type User = { name: string; age: number };

Expand All @@ -33,7 +32,7 @@ const pongoDb = pongo.db();
const users = pongoDb.collection<User>('users');
const roger = { name: 'Roger', age: 30 };
const anita = { name: 'Anita', age: 25 };
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };
const cruella = { _id: ObjectId(), name: 'Cruella', age: 40 };

// Inserting
await users.insertOne(roger);
Expand All @@ -58,8 +57,7 @@ const usersFromDb = await users.find({ age: { $lt: 40 } });
Or use MongoDB compliant shim:

```ts
import { MongoClient } from '@event-driven-io/pongo/shim';
import { v7 as uuid } from 'uuid';
import { MongoClient, ObjectId } from '@event-driven-io/pongo/shim';

type User = { name: string; age: number };

Expand All @@ -72,7 +70,7 @@ const pongoDb = pongoClient.db();
const users = pongoDb.collection<User>('users');
const roger = { name: 'Roger', age: 30 };
const anita = { name: 'Anita', age: 25 };
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };
const cruella = { _id: ObjectId(), name: 'Cruella', age: 40 };

// Inserting
await users.insertOne(roger);
Expand Down
33 changes: 16 additions & 17 deletions src/packages/pongo/src/e2e/postgres.optimistic-concurrency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
import assert from 'assert';
import console from 'console';
import { after, before, beforeEach, describe, it } from 'node:test';
import { v7 as uuid } from 'uuid';
import {
ObjectId,
pongoClient,
pongoSchema,
type ObjectId,
type PongoClient,
type PongoCollection,
type PongoDb,
Expand Down Expand Up @@ -67,7 +66,7 @@ void describe('MongoDB Compatibility Tests', () => {

beforeEach(() => {
user = {
_id: uuid(),
_id: ObjectId(),
name: 'Anita',
age: 25,
};
Expand Down Expand Up @@ -156,7 +155,7 @@ void describe('MongoDB Compatibility Tests', () => {
void describe('insertMany', () => {
void it('inserts a document with id', async () => {
// Given
const otherUser = { ...user, _id: uuid() };
const otherUser = { ...user, _id: ObjectId() };

// When
const pongoInsertResult = await users.insertMany([user, otherUser]);
Expand Down Expand Up @@ -185,7 +184,7 @@ void describe('MongoDB Compatibility Tests', () => {
const nonDefaultVersion = 495n;
const otherUser = {
...user,
_id: uuid(),
_id: ObjectId(),
_version: nonDefaultVersion,
};
// When
Expand Down Expand Up @@ -225,7 +224,7 @@ void describe('MongoDB Compatibility Tests', () => {
name: 'Cruella',
age: 40,
};
const otherUser = { ...user, _id: uuid() };
const otherUser = { ...user, _id: ObjectId() };
// When
const pongoInsertResult = await users.insertMany([
userWithTheSameId,
Expand Down Expand Up @@ -369,8 +368,8 @@ void describe('MongoDB Compatibility Tests', () => {

void describe('updateMany', () => {
void it('updates documents and expected version', async () => {
const otherUser = { ...user, _id: uuid() };
await users.insertMany([user, otherUser, { ...user, _id: uuid() }]);
const otherUser = { ...user, _id: ObjectId() };
await users.insertMany([user, otherUser, { ...user, _id: ObjectId() }]);

// When
const updateResult = await users.updateMany(
Expand Down Expand Up @@ -403,7 +402,7 @@ void describe('MongoDB Compatibility Tests', () => {
});

void it('overrides documents version with autoincremented document version', async () => {
const otherUser = { ...user, _id: uuid() };
const otherUser = { ...user, _id: ObjectId() };
await users.insertMany([{ ...user }, otherUser]);

// When
Expand Down Expand Up @@ -594,8 +593,8 @@ void describe('MongoDB Compatibility Tests', () => {

void describe('deleteMany', () => {
void it('deletes documents and expected version', async () => {
const otherUser = { ...user, _id: uuid() };
await users.insertMany([user, otherUser, { ...user, _id: uuid() }]);
const otherUser = { ...user, _id: ObjectId() };
await users.insertMany([user, otherUser, { ...user, _id: ObjectId() }]);

// When
const deleteResult = await users.deleteMany({
Expand All @@ -616,7 +615,7 @@ void describe('MongoDB Compatibility Tests', () => {
});

void it('overrides documents version with autoincremented document version', async () => {
const otherUser = { ...user, _id: uuid() };
const otherUser = { ...user, _id: ObjectId() };
await users.insertMany([{ ...user }, otherUser]);

// When
Expand All @@ -639,7 +638,7 @@ void describe('MongoDB Compatibility Tests', () => {

void describe('Handle Operations', () => {
void it('should NOT insert a new document if it does not exist and expected DOCUMENT_EXISTS', async () => {
const nonExistingId = uuid() as unknown as ObjectId;
const nonExistingId = ObjectId() as unknown as ObjectId;

const newDoc: User = { name: 'John', age: 25 };

Expand All @@ -659,7 +658,7 @@ void describe('MongoDB Compatibility Tests', () => {
});

void it('should NOT insert a new document if it does not exist and expected is numeric value', async () => {
const nonExistingId = uuid() as unknown as ObjectId;
const nonExistingId = ObjectId() as unknown as ObjectId;

const newDoc: User = { name: 'John', age: 25 };

Expand All @@ -679,7 +678,7 @@ void describe('MongoDB Compatibility Tests', () => {
});

void it('should replace an existing document when expected version matches', async () => {
const existingDoc: User = { _id: uuid(), name: 'John', age: 25 };
const existingDoc: User = { _id: ObjectId(), name: 'John', age: 25 };
const updatedDoc: User = { _id: existingDoc._id!, name: 'John', age: 30 };

const pongoInsertResult = await users.insertOne(existingDoc);
Expand Down Expand Up @@ -712,7 +711,7 @@ void describe('MongoDB Compatibility Tests', () => {
});

void it('should NOT replace an existing document when expected DOCUMENT_DOES_NOT_EXIST', async () => {
const existingDoc: User = { _id: uuid(), name: 'John', age: 25 };
const existingDoc: User = { _id: ObjectId(), name: 'John', age: 25 };
const updatedDoc: User = { _id: existingDoc._id!, name: 'John', age: 30 };

const pongoInsertResult = await users.insertOne(existingDoc);
Expand Down Expand Up @@ -745,7 +744,7 @@ void describe('MongoDB Compatibility Tests', () => {
});

void it('should NOT replace an existing document when expected version is mismatched ', async () => {
const existingDoc: User = { _id: uuid(), name: 'John', age: 25 };
const existingDoc: User = { _id: ObjectId(), name: 'John', age: 25 };
const updatedDoc: User = { _id: existingDoc._id!, name: 'John', age: 30 };

const pongoInsertResult = await users.insertOne(existingDoc);
Expand Down