首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeORM丢弃where子句

TypeORM丢弃where子句
EN

Stack Overflow用户
提问于 2020-12-05 04:06:05
回答 1查看 30关注 0票数 0

我正在扩展和改进由另一个(不可用的)开发人员构建的API。它使用TypeORM,我们使用PostgreSQL数据库。

这是我打算修改的文件开头的“函数框架”(不知道正确的术语):

代码语言:javascript
复制
type Handler = RequestHandler<ParamsDictionary, any, any, Query>;

const createNew = <TReq, TParent, TChild, TRes = DataNode.DataNode>(
    parentEntityClass: ObjectType<TParent>,
    childPropertyName: string,
    parentPropertyName: string,
    reqValidator: (body: TReq) => boolean,
    constraintSelector: (body: TReq) => string | number | Date | ObjectID,
    dataMapper: (body: TReq, res: Response) => Promise<TChild>,
    responseSerializer?: (
        entity: TChild,
        req?: Request<ParamsDictionary, any, any, Query>
    ) => Promise<TRes>
): Handler => asyncHandler(async (req, res) => {
    const logger = req.getLogger();
    const db = req.getDbContext();

    const reqBody = RequestUtility.getJsonAndValidate<TReq>(
        req,
        reqValidator,
        logger
    );

    const child = await dataMapper(reqBody, res);

    if (parentEntityClass) {
        const parent = await QueryUtility.findOneOrFail(
            db,
            parentEntityClass,
            constraintSelector(reqBody)
        );

        const childsParentWrapped: { [k: string]: any } = {};

        logger.debug(`checking if child refs parent ${JSON.stringify({
            childProps: Object.getOwnPropertyNames(child),
            prop: parentPropertyName
        })}`);

        if (Object.getOwnPropertyNames(child).includes(parentPropertyName) ||
            Object.getOwnPropertyNames(child).includes(`__${parentPropertyName}__`)) {
            childsParentWrapped[parentPropertyName] = Promise.resolve(parent);

            Object.assign(child, childsParentWrapped);
        }

        if (Object.getOwnPropertyNames(parent).includes(childPropertyName)) {
            await QueryUtility.addChild(db, parentEntityClass, childPropertyName, parent, child);
        } else {
            await db.manager.save(child);
        }
    } else {
        await db.manager.save(child);
    }

    const result = await responseSerializer(child, req);

    res.json(result);
});

这是我打算修改的函数(同一文件的另一部分)。

代码语言:javascript
复制
type NewFile = { fileName: string};
type CreatedNewFileResponse = { uploadUrl: string; file: DataNode.DataNode };

export const createNewFile: Handler = createNew<NewFile, EUser, EFile, CreatedNewFileResponse>(
    EUser,
    "",
    "user",
    (body) => body.fileName ? true : false,
    (body) => null,
    async (body, res) => {
        const newFile = new EFile();
        const fileType = body.fileName.toLowerCase().split(/\.(?=[^\.]+$)/);
        if (['jpg', 'jpeg', 'gif', 'bmp', 'png'].includes(fileType[1])){
            newFile.fileName = body.fileName;
            newFile.fileType = 'image';
            newFile.storageProvider = "internal-storage";
            newFile.isMarkedForDeletion = false;
            const id = JSON.parse(JSON.stringify(res.locals.jwtPayload.id));
            newFile.user = Promise.resolve(
                        await getRepository(EUser).findOne({where: { id } })    
                        );
            return newFile;

        } else if (['avi', 'wmv', 'mp4', 'mkv', 'flv', 'mov'].includes(fileType[1])) {
            newFile.fileName = body.fileName;
            newFile.fileType = 'video';
            newFile.storageProvider = "internal-storage";
            newFile.isMarkedForDeletion = false;
            const id = JSON.parse(JSON.stringify(res.locals.jwtPayload.id));
            newFile.user = Promise.resolve(
                        await getRepository(EUser).findOne({where: { id } })    
                        );

            return newFile;

        } else if (['ogg', 'wma', 'wave', 'mp3', 'wav', 'm4a'].includes(fileType[1])) {
            newFile.fileName = body.fileName;
            newFile.fileType = 'audio';
            newFile.storageProvider = "internal-storage";
            newFile.isMarkedForDeletion = false;
            const id = JSON.parse(JSON.stringify(res.locals.jwtPayload.id));
            newFile.user = Promise.resolve(
                        await getRepository(EUser).findOne({where: { id } })    
                        );

            return newFile;

        } else {
            throw errors.create(BadRequestError);
        }
    },
    async (newFile, req) => {
        const minioContext = req.getMinioContext();
        let uploadUrl = await minioContext.client.presignedUrl(
            "PUT",
            minioContext.bucket,
            newFile.fileName,
            7 * 24 * 60 * 60,
            { query: {}, headers: { host: req.hostname } }
        );
        uploadUrl = uploadUrl.replace(
            `${minioContext.config.host}:${minioContext.config.port}`,
            req.hostname
        ).replace("localhost", `${minioContext.config.minioUrl}`).replace("http", "https")

        return {
            uploadUrl,
            file: await DataNode.fromFile(newFile)
        };
    }
);

该函数起作用(就像在中一样,它没有错误地服务于它的目的),并在数据库上插入预期的行。但是,在声明newFile.user = JSON.parse(JSON.stringify(res.locals.jwtPayload.id));的代码行上,尽管它确实检索了正确的信息(它从JWT返回id ),但正如我们在PostgreSQL日志中看到的那样,该信息被丢弃了,利用了Users表中的第一行(这意味着,不使用我们检索到的id ):

代码语言:javascript
复制
2020-12-04 18:39:20.395 UTC [690] DETAIL:  parameters: $1 = '1'
2020-12-04 18:39:20.405 UTC [690] LOG:  execute <unnamed>: SELECT "User"."id" AS "User_id", "User"."created" AS "User_created", "User"."updatedAt" AS "User_updatedAt", "User"."username" AS "User_username", "User"."password" AS "User_password", "User"."role" AS "User_role", "User"."firstName" AS "User_firstName", "User"."lastName" AS "User_lastName", "User"."email" AS "User_email" FROM "Users" "User" WHERE "User"."id" = $1 LIMIT 1
2020-12-04 18:39:20.405 UTC [690] DETAIL:  parameters: $1 = '1'
2020-12-04 18:39:20.413 UTC [690] LOG:  statement: SELECT "User"."id" AS "User_id", "User"."created" AS "User_created", "User"."updatedAt" AS "User_updatedAt", "User"."username" AS "User_username", "User"."password" AS "User_password", "User"."role" AS "User_role", "User"."firstName" AS "User_firstName", "User"."lastName" AS "User_lastName", "User"."email" AS "User_email" FROM "Users" "User" LIMIT 1
2020-12-04 18:39:20.420 UTC [690] LOG:  statement: START TRANSACTION
2020-12-04 18:39:20.429 UTC [690] LOG:  execute <unnamed>: INSERT INTO "Files"("fileName", "fileType", "storageProvider", "isMarkedForDeletion", "userId") VALUES ($1, $2, $3, $4, $5) RETURNING "id", "created", "updatedAt"
2020-12-04 18:39:20.429 UTC [690] DETAIL:  parameters: $1 = 'test.jpg', $2 = 'image', $3 = 'internal-storage', $4 = 'f', $5 = '2'
2020-12-04 18:39:20.433 UTC [690] LOG:  statement: COMMIT

正如您在日志片段的第2行中所看到的,where子句得到了遵守。但是,当我们到达要将信息插入到数据库中的部分(代码片段的第4行)时,where子句将被丢弃。

我不知道是否应该单独调试它。任何帮助我们都深表感谢。

EN

回答 1

Stack Overflow用户

发布于 2020-12-05 05:48:29

解决了它。

我最终把"user“改成了"”。

不知何故,这解决了问题,现在到达我的postgres的查询包括我想要的userId。

不管怎样,谢谢你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65150019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档