Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 282 additions & 0 deletions packages/cli/test/ts-schema-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,286 @@ model User {
expect(schemaLite!.models.User.fields.id.attributes).toBeUndefined();
expect(schemaLite!.models.User.fields.email.attributes).toBeUndefined();
});

it('supports ignorable fields for @updatedAt', async () => {
const { schema } = await generateTsSchema(`
model User {
id String @id @default(uuid())
name String
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt(ignore: [email])
posts Post[]

@@map('users')
}

model Post {
id String @id @default(cuid())
title String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String
}
`);

expect(schema).toMatchObject({
provider: {
type: 'sqlite'
},
models: {
User: {
name: 'User',
fields: {
id: {
name: 'id',
type: 'String',
id: true,
attributes: [
{
name: '@id'
},
{
name: '@default',
args: [
{
name: 'value',
value: {
kind: 'call',
function: 'uuid'
}
}
]
}
],
default: {
kind: 'call',
function: 'uuid'
}
},
name: {
name: 'name',
type: 'String'
},
email: {
name: 'email',
type: 'String',
unique: true,
attributes: [
{
name: '@unique'
}
]
},
createdAt: {
name: 'createdAt',
type: 'DateTime',
attributes: [
{
name: '@default',
args: [
{
name: 'value',
value: {
kind: 'call',
function: 'now'
}
}
]
}
],
default: {
kind: 'call',
function: 'now'
}
},
updatedAt: {
name: 'updatedAt',
type: 'DateTime',
updatedAt: {
ignore: [
'email'
]
},
attributes: [
{
name: '@updatedAt',
args: [
{
name: 'ignore',
value: {
kind: 'array',
items: [
{
kind: 'field',
field: 'email'
}
]
}
}
]
}
]
},
posts: {
name: 'posts',
type: 'Post',
array: true,
relation: {
opposite: 'author'
}
}
},
attributes: [
{
name: '@@map',
args: [
{
name: 'name',
value: {
kind: 'literal',
value: 'users'
}
}
]
}
],
idFields: [
'id'
],
uniqueFields: {
id: {
type: 'String'
},
email: {
type: 'String'
}
}
},
Post: {
name: 'Post',
fields: {
id: {
name: 'id',
type: 'String',
id: true,
attributes: [
{
name: '@id'
},
{
name: '@default',
args: [
{
name: 'value',
value: {
kind: 'call',
function: 'cuid'
}
}
]
}
],
default: {
kind: 'call',
function: 'cuid'
}
},
title: {
name: 'title',
type: 'String'
},
published: {
name: 'published',
type: 'Boolean',
attributes: [
{
name: '@default',
args: [
{
name: 'value',
value: {
kind: 'literal',
value: false
}
}
]
}
],
default: false
},
author: {
name: 'author',
type: 'User',
attributes: [
{
name: '@relation',
args: [
{
name: 'fields',
value: {
kind: 'array',
items: [
{
kind: 'field',
field: 'authorId'
}
]
}
},
{
name: 'references',
value: {
kind: 'array',
items: [
{
kind: 'field',
field: 'id'
}
]
}
},
{
name: 'onDelete',
value: {
kind: 'literal',
value: 'Cascade'
}
}
]
}
],
relation: {
opposite: 'posts',
fields: [
'authorId'
],
references: [
'id'
],
onDelete: 'Cascade'
}
},
authorId: {
name: 'authorId',
type: 'String',
foreignKeyFor: [
'author'
]
}
},
idFields: [
'id'
],
uniqueFields: {
id: {
type: 'String'
}
}
}
},
authType: 'User',
plugins: {}
});
})
});
6 changes: 5 additions & 1 deletion packages/language/res/stdlib.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,12 @@ attribute @omit()

/**
* Automatically stores the time when a record was last updated.
*
* @param ignore: A list of field names that are not considered when the ORM client is determining whether any
* updates have been made to a record. An update that only contains ignored fields does not change the
* timestamp.
*/
attribute @updatedAt() @@@targetField([DateTimeField]) @@@prisma
attribute @updatedAt(_ ignore: FieldReference[]?) @@@targetField([DateTimeField]) @@@prisma

/**
* Add full text index (MySQL only).
Expand Down
3 changes: 2 additions & 1 deletion packages/orm/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
SchemaDef,
TypeDefFieldIsArray,
TypeDefFieldIsOptional,
UpdatedAtInfo,
} from '../schema';
import type {
AtLeast,
Expand Down Expand Up @@ -985,7 +986,7 @@ type OptionalFieldsForCreate<Schema extends SchemaDef, Model extends GetModels<S
? Key
: FieldIsArray<Schema, Model, Key> extends true
? Key
: GetModelField<Schema, Model, Key>['updatedAt'] extends true
: GetModelField<Schema, Model, Key>['updatedAt'] extends (true | UpdatedAtInfo)
? Key
: never]: GetModelField<Schema, Model, Key>;
};
Expand Down
13 changes: 11 additions & 2 deletions packages/orm/src/client/crud/operations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,11 +994,20 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
const autoUpdatedFields: string[] = [];
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
if (fieldDef.updatedAt) {
const ignoredFields = new Set(typeof fieldDef.updatedAt === 'boolean' ? [] : fieldDef.updatedAt.ignore);
const hasNonIgnoredFields = Object.keys(data).some((field) => (
(
isScalarField(this.schema, modelDef.name, field) ||
isForeignKeyField(this.schema, modelDef.name, field)
) && !ignoredFields.has(field)
));
if (finalData === data) {
finalData = clone(data);
}
finalData[fieldName] = this.dialect.transformPrimitive(new Date(), 'DateTime', false);
autoUpdatedFields.push(fieldName);
if (hasNonIgnoredFields) {
finalData[fieldName] = this.dialect.transformPrimitive(new Date(), 'DateTime', false);
autoUpdatedFields.push(fieldName);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/schema/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ export type RelationInfo = {
onUpdate?: CascadeAction;
};

export type UpdatedAtInfo = {
ignore?: readonly string[];
};

export type FieldDef = {
name: string;
type: string;
id?: boolean;
array?: boolean;
optional?: boolean;
unique?: boolean;
updatedAt?: boolean;
updatedAt?: boolean | UpdatedAtInfo;
attributes?: readonly AttributeApplication[];
default?: MappedBuiltinType | Expression | readonly unknown[];
omit?: boolean;
Expand Down Expand Up @@ -282,7 +286,7 @@ export type FieldHasDefault<
Field extends GetModelFields<Schema, Model>,
> = GetModelField<Schema, Model, Field>['default'] extends object | number | string | boolean
? true
: GetModelField<Schema, Model, Field>['updatedAt'] extends true
: GetModelField<Schema, Model, Field>['updatedAt'] extends (true | UpdatedAtInfo)
? true
: GetModelField<Schema, Model, Field>['relation'] extends { hasDefault: true }
? true
Expand Down
Loading
Loading