CRUD

This topic describes how to perform CRUD operations with your generated Teo frontend client. CRUD is an acronym that stands for:

Refer to the Teo Client API reference documentation for detailed explanations of each method.

Example server

All examples are based on the following server schema definition:

schema.teo
connector {
  provider: .sqlite,
  url: "sqlite::memory:"
}
 
server {
  bind: ("0.0.0.0", 5100)
}
 
client ts {
  provider: .typeScript,
  dest: "../hello-client/",
  package: true,
  host: .string("http://127.0.0.1:5001"),
  gitCommit: true
}
 
model Artist {
  @id @autoIncrement @readonly
  id: Int
  name: String
  age: Int?
  @relation(through: ArtistAndSong, local: .artist, foreign: .song)
  songs: Song[]
}
 
model Song {
  @id @autoIncrement @readonly
  id: Int
  name: String
  @default(false)
  published: Bool
  @relation(through: ArtistAndSong, local: .song, foreign: .artist)
  artists: Artist[]
}
 
@id([.artistId, .songId])
model ArtistAndSong {
  @foreignKey
  artistId: Int
  @foreignKey
  songId: Int
  @relation(fields: .artistId, references: .id)
  artist: Artist
  @relation(fields: .songId, references: .id)
  song: Song
}

Use the teo generate client command to generate the TypeScript library and navigate to it.

Create

Create a single record

The following query creates a single artist with two fields:

const { data: artist } = await teo.artist.create({
  create: {
    name: 'Michael Ong',
    age: 20,
  },
})
Hide HTTP response
{
  "id": 1,
  "name": "Michael Ong",
  "age": 20
}

Create multiple records

The following createMany query creates multiple artists.

const { data: artists } = await teo.artist.createMany({
  create: [
    {
      name: 'Charlene Ong',
      age: 20,
    },
    {
      name: 'Charlie Law',
      age: 24,
    },
  ],
})
Hide HTTP response
[
  {
    "id": 2,
    "name": "Charlene Ong",
    "age": 20
  },
  {
    "id": 3,
    "name": "Charlie Law",
    "age": 24
  }
]

Create nested records

The following createMany query create an artist with related songs.

const { data: artist } = await teo.artist.create({
  create: {
    name: 'Angela Peterson',
    age: 23,
    songs: {
      create: [
        {
          name: 'Forever in Safe',
          published: true,
        },
        {
          name: 'Fairy Tale',
          published: true,
        }
      ]
    }
  },
  include: {
    songs: true,
  }
})
Hide HTTP response
{
  "id": 4,
  "name": "Angela Peterson",
  "age": 23,
  "songs": [
    {
      "id": 1,
      "name": "Forever in Safe",
      "published": true
    },
    {
      "id": 2,
      "name": "Fairy Tale",
      "published": true
    }
  ]
}

Read

Get record by ID or unique identifier

The following queries return a single record by unique identifier or ID:

// by unique identifier
const { data: artist } = await teo.artist.findUnique({
  where: {
    name: 'Michael Ong',
  }
})
// by id
const { data: artist } = await teo.artist.findUnique({
  where: {
    id: 1,
  }
})
Hide HTTP response
{
  "id": 1,
  "name": "Michael Ong",
  "age": 20
}

Get matched records

The following query return filtered records. Without filtering and pagination arguments, findMany returns all records.

const result = await teo.artist.findMany({
  where: {
    age: { lte: 20 }
  }
})
Hide HTTP response
{
  "meta": {
    "count": 2
  },
  "data": [
    {
      "id": 1,
      "name": "Michael Ong",
      "age": 20
    },
    {
      "id": 2,
      "name": "Charlene Ong",
      "age": 20
    }
  ]
}

Get the first matched record

The following query returns the first matched record.

const { data: artist } = await teo.artist.findFirst({
  where: {
    age: { lte: 20 }
  }
})
Hide HTTP response
{
  "id": 1,
  "name": "Michael Ong",
  "age": 20
}

Update

Update a single record

The following query updates a matched record.

const { data: artist } = await teo.artist.update({
  where: {
    id: 1
  },
  update: {
    age: 21
  }
})
Hide HTTP response
{
  "id": 1,
  "name": "Michael Ong",
  "age": 21
}

Update multiple records

The following query updates 0 or more matched records.

const { data: artists } = await teo.artist.updateMany({
  where: {
    age: { lte: 21 }
  },
  update: {
    age: 20
  }
})
Hide HTTP response
[
  {
    "id": 1,
    "name": "Michael Ong",
    "age": 20
  },
  {
    "id": 2,
    "name": "Charlene Ong",
    "age": 20
  }
]

Update or create a record

The following query updates matched unique record if it exists otherwise create a new one.

// you can run this more than once and the result is the same
const { data: artist } = await teo.artist.upsert({
  where: {
    name: "Tony Justin"
  },
  update: {
    name: "Tony Justin",
    age: 26
  },
  create: {
    name: "Tony Justin",
    age: 26
  }
})
Hide HTTP response
{
  "id": 5,
  "name": "Tony Justin",
  "age": 26
}

Atomic updation

A number field can be updated with atomic updator.

const { data: artist } = await teo.artist.update({
  where: {
    name: "Tony Justin"
  },
  update: {
    age: { increment: 1 }
  },
})
Hide HTTP response
{
  "id": 5,
  "name": "Tony Justin",
  "age": 27
}

Connect and disconnect records

The following query connects records.

const { data: artist } = await teo.artist.update({
  where: {
    name: "Tony Justin"
  },
  update: {
    songs: {
      connect: {
        id: 1
      }
    }
  },
  include: {
    songs: true
  }
})
Hide HTTP response
{
  "id": 5,
  "name": "Tony Justin",
  "age": 27,
  "songs": [
    {
      "id": 1,
      "name": "Forever in Safe",
      "published": true
    }
  ]
}

The following query disconnects records.

const { data: artist } = await teo.artist.update({
  where: {
    name: "Tony Justin"
  },
  update: {
    songs: {
      disconnect: {
        id: 1
      }
    }
  },
  include: {
    songs: true
  }
})
Hide HTTP response
{
  "id": 5,
  "name": "Tony Justin",
  "age": 27,
  "songs": []
}

Delete

Delete a single record

This query deletes a single record.

const { data: artist } = await teo.artist.delete({
  where: {
    id: 3
  }
})
Hide HTTP response
{
  "id": 3,
  "name": "Charlie Law",
  "age": 24
}

Delete multiple records

This query deletes matched records. Without filtering and pagination arguments, deleteMany deletes all records.

const { data: artists } = await teo.artist.deleteMany({
  where: {
    id: { lte: 2 }
  }
})
Hide HTTP response
[
  {
    "id": 1,
    "name": "Michael Ong",
    "age": 20
  },
  {
    "id": 2,
    "name": "Charlene Ong",
    "age": 20
  }
]