Skip to content

@map attribute is not properly used on types during serialization #596

@robertjpayne

Description

@robertjpayne

When using types to define type-safe json the @Map attribute while allowed in .zmodel does nothing at runtime:

Example .zmodel

datasource db {
  provider = "postgresql"
  url = "postgres://postgres:postgres@localhost:5432/zmodel-tests"
}

type UserPosition {
  discipline String @map("role_category")
  role String @map("role")
  name String @map("name")
}

model User {
  id String @id @default(uuid(7))
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")
  firstName String @map("first_name")
  lastName String @map("last_name")
  position UserPosition @json @map("position")

  @@map("users")
}

While the client code correctly is generated to use discipline the @map key is entirely ignored and the json is written to the database as per the field names rather than the specified value in @map.

Example main.ts:

import { ZenStackClient } from "@zenstackhq/orm"
import { PostgresDialect } from "@zenstackhq/orm/dialects/postgres"
import { schema } from "../lib/schema"
import { Pool } from "pg"

export const db = new ZenStackClient(schema, {
  log: ["query"],
  dialect: new PostgresDialect({
    pool: new Pool({
      connectionString: "postgres://postgres:postgres@localhost:5432/zmodel-tests",
    }),
  }),
})

const user = await db.user.create({
  data: {
    firstName: "John",
    lastName: "Doe",
    email: "[email protected]",
    password: "password",
    position: {
      discipline: "Software Engineering",
      role: "Senior Software Engineer",
      name: "John Doe",
    },
  },
})

Result in the database:

Image

Understandably this may not be fixable (or even easily fixable) but until it is perhaps the generator/zmodel linting can show an error that @Map isn't supported on types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions