Skip to content

Commit

Permalink
Updated usage of uuid in README and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Oct 9, 2024
1 parent 7f871fd commit fe8f77e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
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
2 changes: 1 addition & 1 deletion samples/simple-ts/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,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 Down
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

0 comments on commit fe8f77e

Please sign in to comment.