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

Examples of using models #1

Open
adelsz opened this issue Mar 28, 2023 · 2 comments
Open

Examples of using models #1

adelsz opened this issue Mar 28, 2023 · 2 comments

Comments

@adelsz
Copy link

adelsz commented Mar 28, 2023

The README has plenty of details on how to build models, but no examples of how to use/call them after they have been built.

@shfrmn
Copy link
Owner

shfrmn commented Mar 29, 2023

I can add examples like this for each chapter, or is there something else I should illustrate?

README.md
..........

Map all queries to an entity

Now, let's map the results of all queries to Album class.

models/Album/index.ts

import {createModel, mapWithEntity} from "pgtyped-model"
import {pool} from "./connection"
import * as queries from "./Album.queries"
// Return type of all queries will change to `Album[]`.
export const AlbumModel = createModel({
  queries,
  connection: pool,
  camelCaseColumnNames: true,
  collectDefault: mapWithEntity(Album),
})

Each query from the original SQL file will be exposed as a function with return type Album[].

business-logic-dir/Album.ts

import {AlbumModel} from "../models/Album"

async function doSomething(artistIds: string[]) {
  //    albums: Album[]
  const albums = await AlbumModel.listAlbums({artistIds})
}

..........

@adelsz
Copy link
Author

adelsz commented Mar 29, 2023

Yes, that will be great in my opinion.
Showing a snippet of the queries file would probably also make sense, for those unfamiliar with pgTyped.
That way, one can fully trace the lifecycle of the query query definition -> model definition -> usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants