我有两个相关模型:
1.- 角色实体
import { Column, Entity, BaseEntity, OneToMany, PrimaryColumn } from "typeorm";
import { Field, ObjectType } from "type-graphql";
import { UserEntity } from "./user.entity";
@ObjectType()
@Entity({
name: "tb_roles"
})
export class RoleEntity extends BaseEntity {
@Field()
@PrimaryColumn({
name: "id",
type: "character varying",
length: 5
})
id!: string;
@Field()
@Column({
name: "description",
type: "character varying",
nullable: true
})
description!: string
@Field(() => [UserEntity])
@OneToMany(() => UserEntity, user => user.role)
users!: UserEntity[];
}
Run Code Online (Sandbox Code Playgroud)
2.- 用户实体
import {Field, ObjectType} from "type-graphql"; …Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的简单 TypeORM 实体:
import { Entity, PrimaryGeneratorColumn, Column } from 'typeorm';
@Entity()
export class Employee {
@PrimaryGeneratedColumn()
id: number;
@Column()
Name: string;
}
Run Code Online (Sandbox Code Playgroud)
只要我不手动接触数据库,一切都可以正常工作。如果我手动输入记录,实体将无法检查id表中的最后一个当前记录并抛出 error duplicate key value violates unique constraint。
例如:
Employee Entity自己创建两条记录。id 2id 3Employee Entity运行时,它不会 = 创建另一个记录,因为它想使用id 3,但我已经手动添加了。duplicate key value violates unique constraintEmployee Entity再次运行时,它会起作用,因为它尝试在已经可用的情况下保存另一条记录id 4。那么我如何确保检查Employee Entity数据库中的最后一个 id,然后使用增量保存下一条记录id?
谢谢
我有一个数字数组,我需要使用计算样本标准差jq。
标准差公式示例(学分):\n
我尝试将代码拆分为多个部分(长度、平均值),但我的尝试均无效,因为我不知道如何将所有数据合并为单个sqrtandmap操作:
# Example of data input\n_data="[73,73,76,77,81,100]"\n\n_length=$(echo "$_data" | jq --raw-output \'length\')\n_mean=$(echo "$_data" | jq --raw-output \'add/length\')\n\n_standard_deviation=$(echo "$_data" \\\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 | jq --raw-output \\\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 --arg length "$_length" \\\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0--arg mean "$_mean" \\\n\xc2\xa0 \xc2\xa0 \xc2\xa0 …Run Code Online (Sandbox Code Playgroud) 作为参考从 Javascript 中的对象中删除空白属性,如何使其兼容 Typescript?
JS 函数(嵌套对象 | ES10):
function removeEmpty(obj) {
return Object.fromEntries(
Object.entries(obj)
.filter(([_, v]) => v != null)
.map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过Exclude<T, null>,但它不适用于嵌套对象,并且我认为这不是正确的实用程序。
请注意,返回的类型应删除null类型但保留undefined。
示例/预期行为:
type TestType = {
a?: {
b?: {
c: string;
} | null;
z?: {
x: string;
};
} | null;
};
const testObj: TestType = {
a: {
b: null,
z: { …Run Code Online (Sandbox Code Playgroud) typeorm ×2
typescript ×2
graphql ×1
javascript ×1
jq ×1
json ×1
nestjs ×1
null ×1
orm ×1
typegraphql ×1
undefined ×1