event-driven-io / Pongo

Pongo - Mongo but on Postgres and with strong consistency benefits
https://event-driven-io.github.io/Pongo/
MIT License
1.21k stars 30 forks source link

Added PongoSchema that automatically generates cliend and database typed properties #73

Closed oskardudycz closed 2 months ago

oskardudycz commented 2 months ago

Now, you'll be able to define schema like:

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

const schema = pongoSchema.client({
  database: pongoSchema.db({
    users: pongoSchema.collection<User>('users'),
    customers: pongoSchema.collection<Customer>('customers'),
  }),
});

And pass it to the client, getting the typed version.

const typedClient = pongoClient(postgresConnectionString, {
  schema: { definition: schema },
});
// 👇 client have the same database as we defined above, and the collection
const users = typedClient.database.users;

const doc: User = {
  _id: randomUUUID(),
  name: 'Anita',
  age: 25,
};
const inserted = await users.insertOne(doc);

// 👇 yup, the collection is fully typed!
const pongoDoc = await users.findOne({
  name: 'Anita'
});

That's also the next step to being able to migrate schema upfront. The schema will be the basis to know what should be migrated.

It's a follow-up to https://github.com/event-driven-io/Pongo/pull/71, https://github.com/event-driven-io/Pongo/pull/72.

There'll be needed next adding the migrations based on schema, and command-line tooling.