typeorm#EntitySchema TypeScript Examples

The following examples show how to use typeorm#EntitySchema. 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: TokenTypeORM.ts    From node-experience with MIT License 6 votes vote down vote up
TokenSchema = new EntitySchema<Token>({
    name: 'Token',
    target: Token,
    tableName: 'tokens',
    columns: {
        _id: {
            type: 'uuid',
            primary: true,
            unique: true
        },
        hash: {
            type: String
        },
        expires: {
            type: Number
        },
        payload: {
            type: 'json'
        },
        blackListed: {
            type: 'bool'
        }
    }
})
Example #2
Source File: typeorm.spec.ts    From ucast with Apache License 2.0 6 votes vote down vote up
async function configureORM() {
  class User {
    id!: number
    name!: string
    projects!: Project[]
  }

  class Project {
    id!: number
    name!: string
    user!: User
  }

  const UserSchema = new EntitySchema<User>({
    name: 'User',
    target: User,
    columns: {
      id: { primary: true, type: 'int', generated: true },
      name: { type: 'varchar' },
    },
    relations: {
      projects: {
        target: 'Project',
        type: 'one-to-many',
        inverseSide: 'user'
      }
    }
  })

  const ProjectSchema = new EntitySchema<Project>({
    name: 'Project',
    target: Project,
    columns: {
      id: { primary: true, type: 'int', generated: true },
      name: { type: 'varchar' },
    },
    relations: {
      user: { target: 'User', type: 'many-to-one' }
    }
  })

  const conn = await createConnection({
    type: 'sqlite',
    database: ':memory:',
    entities: [UserSchema, ProjectSchema]
  })

  return { User, Project, conn }
}
Example #3
Source File: manager.ts    From typeorm-extension with MIT License 6 votes vote down vote up
set<O extends Record<string, any>>(
        entity: ObjectType<O> | EntitySchema<O>,
        factoryFn: FactoryCallback<O>,
    ) : SeederFactoryConfig {
        const name = getEntityName(entity);

        this.items[name] = {
            factoryFn,
            entity,
        };

        return this.items[name];
    }
Example #4
Source File: manager.ts    From typeorm-extension with MIT License 6 votes vote down vote up
get<O extends Record<string, any>>(
        entity: ObjectType<O> | EntitySchema<O>,
    ) : SeederFactory<O> {
        const name = getEntityName(entity);

        if (!hasOwnProperty(this.items, name)) {
            throw new Error(`No seeder factory is registered for the entity: ${name}`);
        }

        return new SeederFactory({
            factoryFn: this.items[name].factoryFn,
            entity,
            name,
        });
    }
Example #5
Source File: utils.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function setSeederFactory<O extends Record<string, any>>(
    entity: ObjectType<O> | EntitySchema<O>,
    factoryFn: FactoryCallback<O>,
) : SeederFactoryConfig {
    const manager = useSeederFactoryManager();
    return manager.set(entity, factoryFn);
}
Example #6
Source File: utils.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function useSeederFactory<O extends Record<string, any>>(
    entity: ObjectType<O> | EntitySchema<O>,
) {
    const manager = useSeederFactoryManager();
    return manager.get(entity);
}
Example #7
Source File: entity.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function getEntityName<O>(entity: ObjectType<O> | EntitySchema<O>) : string {
    if (typeof entity === 'function') {
        return entity.name;
    }

    if (InstanceChecker.isEntitySchema(entity)) {
        return entity.options.name;
    }

    return new (entity as any)().constructor.name;
}
Example #8
Source File: BaseSqlRepository.ts    From node-experience with MIT License 5 votes vote down vote up
constructor(@unmanaged() entityName: string, @unmanaged() schema: EntitySchema)
    {
        this.entityName = entityName;
        this.repository = getRepository<T>(schema);
    }
Example #9
Source File: FileTypeORM.ts    From node-experience with MIT License 5 votes vote down vote up
FileSchema = new EntitySchema<File>({
    name: 'File',
    target: File,
    tableName: 'files',
    columns: {
        _id: {
            type: 'uuid',
            primary: true,
            unique: true
        },
        name: {
            type: String
        },
        originalName: {
            type: String
        },
        mimeType: {
            type: String
        },
        path: {
            type: String
        },
        extension: {
            type: String
        },
        size: {
            type: Number
        },
        version: {
            type: Number
        },
        isPublic: {
            type: Boolean
        },
        createdAt: {
            name: 'createdAt',
            type: 'timestamp with time zone',
            createDate: true
        },
        updatedAt: {
            name: 'updatedAt',
            type: 'timestamp with time zone',
            updateDate: true
        }
    }
})
Example #10
Source File: ItemTypeORM.ts    From node-experience with MIT License 5 votes vote down vote up
ItemSchema = new EntitySchema<Item>({
    name: 'Item',
    target: Item,
    tableName: 'items',
    columns: {
        _id: {
            type: 'uuid',
            primary: true,
            unique: true
        },
        name: {
            type: String
        },
        type: {
            type: Number
        },
        createdAt: {
            name: 'createdAt',
            type: 'timestamp with time zone',
            createDate: true
        },
        updatedAt: {
            name: 'updatedAt',
            type: 'timestamp with time zone',
            updateDate: true
        }
    },
    relations: {
        createdBy: {
            type: 'many-to-one',
            target: 'User',
            joinColumn: true,
            eager: true
        },
        lastModifiedBy: {
            type: 'many-to-one',
            target: 'User',
            joinColumn: true,
            eager: true
        }
    }
})
Example #11
Source File: NotificationTypeORM.ts    From node-experience with MIT License 5 votes vote down vote up
TypeNotificationSchema = new EntitySchema<TypeNotification>({
    name: 'TypeNotification',
    target: TypeNotification,
    tableName: 'notifications',
    columns: {
        _id: {
            type: 'uuid',
            primary: true,
            unique: true
        },
        name: {
            type: String
        },
        emailTemplatePath: {
            type: String,
            nullable: true
        },
        senderName: {
            type: String,
            nullable: true
        },
        from: {
            type: String,
            nullable: true
        },
        to: {
            type: String,
            nullable: true
        },
        cc: {
            type: String,
            nullable: true
        },
        subject: {
            type: String,
            nullable: true
        },
        description: {
            type: String,
            nullable: true
        },
        url: {
            type: String,
            nullable: true
        },
        type: {
            type: 'enum',
            enum: TypeNotificationEnum,
            default: TypeNotificationEnum.EMAIL
        }
    }
})
Example #12
Source File: RoleTypeORM.ts    From node-experience with MIT License 5 votes vote down vote up
RoleSchema = new EntitySchema<Role>({
    name: 'Role',
    target: Role,
    tableName: 'roles',
    columns: {
        _id: {
            type: 'uuid',
            primary: true,
            unique: true
        },
        name: {
            type: String
        },
        slug: {
            type: String,
            unique: true
        },
        enable: {
            type: 'bool',
            default: true
        },
        ofSystem: {
            type: 'bool',
            default: false
        },
        permissions: {
            type: 'simple-array',
            nullable: true
        },
        createdAt: {
            name: 'createdAt',
            type: 'timestamp with time zone',
            createDate: true
        },
        updatedAt: {
            name: 'updatedAt',
            type: 'timestamp with time zone',
            updateDate: true
        }
    }
})
Example #13
Source File: UserTypeORM.ts    From node-experience with MIT License 4 votes vote down vote up
UserSchema = new EntitySchema<User>({
    name: 'User',
    target: User,
    tableName: 'users',
    columns: {
        _id: {
            type: 'uuid',
            primary: true,
            unique: true
        },
        firstName: {
            type: String
        },
        lastName: {
            type: String
        },
        email: {
            type: String,
            unique: true
        },
        birthday: {
            type: String
        },
        documentType: {
            type: String
        },
        documentNumber: {
            type: String,
            unique: true
        },
        gender: {
            type: String
        },
        phone: {
            type: String
        },
        country: {
            type: String
        },
        address: {
            type: String
        },
        password: {
            type: String,
            transformer: {
                from(val: string)
                {
                    return val;
                },
                to(val: Record<string, string>)
                {
                    return val.value;
                }
            }
        },
        permissions: {
            type: 'simple-array',
            nullable: true
        },
        enable: {
            type: 'bool',
            default: true
        },
        verify: {
            type: 'bool',
            default: false
        },
        isSuperAdmin: {
            type: 'bool',
            default: false
        },
        confirmationToken: {
            type: String,
            nullable: true
        },
        passwordRequestedAt: {
            type: Date,
            nullable: true
        },
        createdAt: {
            name: 'createdAt',
            type: 'timestamp with time zone',
            createDate: true
        },
        updatedAt: {
            name: 'updatedAt',
            type: 'timestamp with time zone',
            updateDate: true
        }
    },
    relations: {
        roles: {
            type: 'many-to-many',
            target: 'Role',
            eager: true,
            joinTable: {
                name: 'users_has_roles',
                joinColumn: {
                    name: 'user_id'
                },
                inverseJoinColumn: {
                    name: 'role_id'
                }
            }
        }
    }
})