@prisma/client#EnumDataType TypeScript Examples

The following examples show how to use @prisma/client#EnumDataType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: delete-auto-number.ts    From amplication with Apache License 2.0 6 votes vote down vote up
async function main() {
  const client = new PrismaClient();
  await client.entityField.deleteMany({
    where: {
      dataType: EnumDataType.AutoNumber
    }
  });
  await client.$disconnect();
}
Example #2
Source File: entity.resolver.spec.ts    From amplication with Apache License 2.0 6 votes vote down vote up
EXAMPLE_ENTITY_FIELD: EntityField = {
  id: EXAMPLE_ENTITY_FIELD_ID,
  permanentId: 'examplePermanentId',
  createdAt: new Date(),
  updatedAt: new Date(),
  name: EXAMPLE_NAME,
  displayName: EXAMPLE_DISPLAY_NAME,
  dataType: EnumDataType.SingleLineText,
  required: false,
  unique: false,
  searchable: true,
  description: 'exampleDescription',
  properties: {}
}
Example #3
Source File: add-related-fields.ts    From amplication with Apache License 2.0 4 votes vote down vote up
// For every existing lookup field create a related field
async function main() {
  const client = new PrismaClient();
  // Find all lookup fields in the database
  const lookupFields = await client.entityField.findMany({
    where: {
      dataType: EnumDataType.Lookup,
      entityVersion: {
        versionNumber: 0
      }
    },
    include: {
      entityVersion: {
        include: {
          entity: {
            include: {
              app: {
                include: {
                  workspace: {
                    include: {
                      users: true
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  });
  console.info(`Found ${lookupFields.length} fields`);
  // Filter out fields that have relatedFieldId already or their related entity id is corrupt
  const fieldsToUpdate = lookupFields.filter(field => {
    const properties = (field.properties as unknown) as types.Lookup;
    return (
      !properties.relatedFieldId && !properties.relatedEntityId.startsWith('[')
    );
  });
  console.info(`Attempting to update ${fieldsToUpdate.length} fields`);
  await Promise.all(
    fieldsToUpdate.map(async field => {
      const properties = (field.properties as unknown) as types.Lookup;
      const { entity } = field.entityVersion;
      const [user] = entity.app.workspace.users;
      console.info(`Adding related field for ${field.id}...`);
      let relatedFieldName = camelCase(entity.name);
      let relatedFieldDisplayName = entity.name;

      // Find fields with the desired related field name
      const existingFieldWithName = await client.entityField.findFirst({
        where: {
          OR: [
            { name: relatedFieldName },
            {
              displayName: relatedFieldDisplayName
            }
          ],
          entityVersion: {
            entityId: properties.relatedEntityId,
            versionNumber: 0
          }
        }
      });

      // In case name exists use a generated name
      if (
        existingFieldWithName?.name === relatedFieldName &&
        existingFieldWithName?.displayName === relatedFieldDisplayName
      ) {
        const name = `a${cuid().replace(/[^A-z0-9]/g, '')}`;
        relatedFieldName = name;
        relatedFieldDisplayName = name;
      }

      // Lock the entity
      await client.entity.update({
        where: {
          id: entity.id
        },
        data: {
          lockedByUser: {
            connect: {
              id: user.id
            }
          },
          lockedAt: new Date()
        }
      });

      // Lock the related entity
      await client.entity.update({
        where: {
          id: properties.relatedEntityId
        },
        data: {
          lockedByUser: {
            connect: {
              id: user.id
            }
          },
          lockedAt: new Date()
        }
      });

      let relatedField;
      // Create the related field
      try {
        relatedField = await client.entityField.create({
          data: {
            unique: false,
            required: false,
            searchable: false,
            description: '',
            name: relatedFieldName,
            displayName: relatedFieldDisplayName,
            dataType: EnumDataType.Lookup,
            entityVersion: {
              connect: {
                // eslint-disable-next-line @typescript-eslint/naming-convention
                entityId_versionNumber: {
                  entityId: properties.relatedEntityId,
                  versionNumber: 0
                }
              }
            },
            properties: {
              allowMultipleSelection: !properties.allowMultipleSelection,
              relatedEntityId: field.entityVersion.entity.id,
              relatedFieldId: field.permanentId
            }
          }
        });
      } catch (error) {
        relatedField = await client.entityField.create({
          data: {
            unique: false,
            required: false,
            searchable: false,
            description: '',
            name: cuid(),
            displayName: cuid(),
            dataType: EnumDataType.Lookup,
            entityVersion: {
              connect: {
                // eslint-disable-next-line @typescript-eslint/naming-convention
                entityId_versionNumber: {
                  entityId: properties.relatedEntityId,
                  versionNumber: 0
                }
              }
            },
            properties: {
              allowMultipleSelection: !properties.allowMultipleSelection,
              relatedEntityId: field.entityVersion.entity.id,
              relatedFieldId: field.permanentId
            }
          }
        });
      }
      // Update the field with the related field id
      await client.entityField.update({
        where: { id: field.id },
        data: {
          properties: {
            ...properties,
            relatedFieldId: relatedField.permanentId
          }
        }
      });
    })
  );

  await client.$disconnect();
}