Skip to content

Commit

Permalink
Added peer dependency to pg and uuid types
Browse files Browse the repository at this point in the history
Adjusted also README to show proper samples
  • Loading branch information
oskardudycz committed Jul 9, 2024
1 parent 02260b5 commit ac7df28
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ type User = { name: string; age: number };
const connectionString =
"postgresql://dbuser:[email protected]:3211/mydb";

const pongoClient = pongoClient(connectionString);
const pongoDb = pongoClient.db();
const pongo = pongoClient(connectionString);
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 };

// Inserting
await pongoCollection.insertOne(roger);
await pongoCollection.insertOne(cruella);
await users.insertOne(roger);
await users.insertOne(cruella);

const { insertedId } = await pongoCollection.insertOne(anita);
const { insertedId } = await users.insertOne(anita);
const anitaId = insertedId;

// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });

// Deleting
await pongoCollection.deleteOne({ _id: cruella._id });
await users.deleteOne({ _id: cruella._id });

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

// Finding more
const users = await pongoCollection.find({ age: { $lt: 40 } });
users = await users.find({ age: { $lt: 40 } });
```

Or use MongoDB compliant shim:
Expand All @@ -77,23 +77,23 @@ const anita = { name: "Anita", age: 25 };
const cruella = { _id: uuid(), name: "Cruella", age: 40 };

// Inserting
await pongoCollection.insertOne(roger);
await pongoCollection.insertOne(cruella);
await users.insertOne(roger);
await users.insertOne(cruella);

const { insertedId } = await pongoCollection.insertOne(anita);
const { insertedId } = await users.insertOne(anita);
const anitaId = insertedId;

// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });

// Deleting
await pongoCollection.deleteOne({ _id: cruella._id });
await users.deleteOne({ _id: cruella._id });

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

// Finding more
const users = await pongoCollection.find({ age: { $lt: 40 } }).toArray();
users = await users.find({ age: { $lt: 40 } }).toArray();
```

## How does it work?
Expand All @@ -114,12 +114,9 @@ CREATE TABLE IF NOT EXISTS "YourCollectionName" (
**E.g. the MongoDB update syntax:**

```ts
const pongoCollection = pongoDb.collection<User>("users");
const users = pongoDb.collection<User>("users");

await pongoCollection.updateOne(
{ _id: someId },
{ $push: { tags: "character" } }
);
await users.updateOne({ _id: someId }, { $push: { tags: "character" } });
```

will be translated to:
Expand All @@ -133,7 +130,7 @@ WHERE _id = '137ef052-e41c-428b-b606-1c8070a47eda';
**Or for query:**

```ts
const result = await pongoCollection
const result = await users
.find({ "address.history": { $elemMatch: { street: "Elm St" } } })
.toArray();
```
Expand Down
37 changes: 17 additions & 20 deletions src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ type User = { name: string; age: number };
const connectionString =
'postgresql://dbuser:[email protected]:3211/mydb';

const pongoClient = pongoClient(connectionString);
const pongoDb = pongoClient.db();
const pongo = pongoClient(connectionString);
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 };

// Inserting
await pongoCollection.insertOne(roger);
await pongoCollection.insertOne(cruella);
await users.insertOne(roger);
await users.insertOne(cruella);

const { insertedId } = await pongoCollection.insertOne(anita);
const { insertedId } = await users.insertOne(anita);
const anitaId = insertedId;

// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });

// Deleting
await pongoCollection.deleteOne({ _id: cruella._id });
await users.deleteOne({ _id: cruella._id });

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

// Finding more
const users = await pongoCollection.find({ age: { $lt: 40 } });
const users = await users.find({ age: { $lt: 40 } });
```

Or use MongoDB compliant shim:
Expand All @@ -75,23 +75,23 @@ const anita = { name: 'Anita', age: 25 };
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };

// Inserting
await pongoCollection.insertOne(roger);
await pongoCollection.insertOne(cruella);
await users.insertOne(roger);
await users.insertOne(cruella);

const { insertedId } = await pongoCollection.insertOne(anita);
const { insertedId } = await users.insertOne(anita);
const anitaId = insertedId;

// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });

// Deleting
await pongoCollection.deleteOne({ _id: cruella._id });
await users.deleteOne({ _id: cruella._id });

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

// Finding more
const users = await pongoCollection.find({ age: { $lt: 40 } }).toArray();
const users = await users.find({ age: { $lt: 40 } }).toArray();
```

## How does it work?
Expand All @@ -112,12 +112,9 @@ CREATE TABLE IF NOT EXISTS "YourCollectionName" (
**E.g. the MongoDB update syntax:**

```typescript
const pongoCollection = pongoDb.collection<User>('users');
const users = pongoDb.collection<User>('users');

await pongoCollection.updateOne(
{ _id: someId },
{ $push: { tags: 'character' } },
);
await users.updateOne({ _id: someId }, { $push: { tags: 'character' } });
```

will be translated to:
Expand All @@ -131,7 +128,7 @@ WHERE _id = '137ef052-e41c-428b-b606-1c8070a47eda';
**Or for query:**

```typescript
const result = await pongoCollection
const result = await users
.find({ 'address.history': { $elemMatch: { street: 'Elm St' } } })
.toArray();
```
Expand Down
9 changes: 5 additions & 4 deletions src/packages/pongo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
"dist"
],
"peerDependencies": {
"@types/uuid": "^9.0.8",
"@types/pg": "^8.11.6",
"@types/pg-format": "^1.0.5",
"@types/mongodb": "^4.0.7",
"pg": "^8.12.0",
"pg-format": "^1.0.4",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/uuid": "^9.0.8",
"@types/node": "20.11.30",
"@types/pg": "^8.11.6",
"@types/pg-format": "^1.0.5"
"@types/node": "20.11.30"
}
}
4 changes: 3 additions & 1 deletion src/packages/pongo/src/main/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export interface PongoCollection<T> {

export type WithId<T> = T & { _id: string };

export type WithoutId<T> = Omit<T, '_id'>;

export type PongoFilter<T> = {
[P in keyof T]?: T[P] | PongoFilterOperator<T[P]>;
[P in keyof T]?: WithId<T[P]> | PongoFilterOperator<T[P]>;
};

export type PongoFilterOperator<T> = {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/pongo/src/mongo/mongoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MongoClient {
await this.pongoClient.close();
}

db(dbName: string): Db {
db(dbName?: string): Db {
return new Db(this.pongoClient.db(dbName));
}
}

0 comments on commit ac7df28

Please sign in to comment.