fixed mongodb type bug

This commit is contained in:
k8w 2022-01-11 21:16:25 +08:00
parent 5afb16e2bb
commit 6707825188
15 changed files with 56 additions and 47 deletions

View File

@ -19,6 +19,6 @@
"typescript": "^4.4.3"
},
"dependencies": {
"tsrpc": "^3.0.9"
"tsrpc": "^3.1.7"
}
}

View File

@ -23,7 +23,7 @@
"typescript": "^4.5.4"
},
"dependencies": {
"mongodb": "^4.2.2",
"tsrpc": "^3.1.4"
"mongodb": "^4.3.0",
"tsrpc": "^3.1.7"
}
}

View File

@ -1,3 +1,4 @@
import { Document, ObjectId } from "mongodb";
import { ApiCall } from "tsrpc";
import { Global } from "../models/Global";
import { ReqAddPost, ResAddPost } from "../shared/protocols/PtlAddPost";

View File

@ -5,7 +5,7 @@ import { ReqDelPost, ResDelPost } from "../shared/protocols/PtlDelPost";
export async function ApiDelPost(call: ApiCall<ReqDelPost, ResDelPost>) {
let op = await Global.collection('Post').deleteOne({
_id: new ObjectId(call.req._id)
_id: call.req._id
})
call.succ({});

View File

@ -5,7 +5,7 @@ import { ReqGetPost, ResGetPost } from "../shared/protocols/PtlGetPost";
export async function ApiGetPost(call: ApiCall<ReqGetPost, ResGetPost>) {
let op = await Global.collection('Post').findOne({
_id: new ObjectId(call.req._id)
_id: call.req._id
});
if (!op) {
@ -16,7 +16,7 @@ export async function ApiGetPost(call: ApiCall<ReqGetPost, ResGetPost>) {
call.succ({
post: {
...op,
_id: op._id.toHexString()
_id: op._id
}
})
}

View File

@ -7,7 +7,7 @@ export async function ApiUpdatePost(call: ApiCall<ReqUpdatePost, ResUpdatePost>)
let { _id, ...update } = call.req.update;
let op = await Global.collection('Post').updateOne({
_id: new ObjectId(_id)
_id: _id
}, {
$set: {
...update,

View File

@ -1,7 +1,7 @@
import { Collection, Db, MongoClient } from "mongodb";
import { Logger } from "tsrpc";
import { DbPost } from "../shared/db/DbPost";
import { BackConfig } from "./BackConfig";
import { DbPost } from "./dbItems/DbPost";
export class Global {

View File

@ -1,7 +0,0 @@
import { ObjectId } from "mongodb";
import { Overwrite } from "tsrpc";
import { Post } from "../../shared/protocols/models/Post";
export type DbPost = Overwrite<Post, {
_id: ObjectId
}>

View File

@ -1,5 +1,7 @@
export interface Post {
_id: string;
import { ObjectId } from "mongodb";
export interface DbPost {
_id: ObjectId;
author: string;
title: string;
content: string;

View File

@ -1,7 +1,7 @@
import { Post } from "./models/Post";
import { DbPost } from "../db/DbPost";
export interface ReqAddPost {
newPost: Omit<Post, '_id' | 'create' | 'update' | 'visitedNum'>;
newPost: Omit<DbPost, '_id' | 'create' | 'update' | 'visitedNum'>;
}
export interface ResAddPost {

View File

@ -1,5 +1,7 @@
import { ObjectId } from "bson";
export interface ReqDelPost {
_id: string;
_id: ObjectId;
}
export interface ResDelPost {

View File

@ -1,9 +1,10 @@
import { Post } from "./models/Post";
import { ObjectId } from "bson";
import { DbPost } from "../db/DbPost";
export interface ReqGetPost {
_id: string;
_id: ObjectId;
}
export interface ResGetPost {
post: Post;
post: DbPost;
}

View File

@ -1,7 +1,7 @@
import { Post } from "./models/Post";
import { DbPost } from "../db/DbPost";
export interface ReqUpdatePost {
update: { _id: string } & Partial<Pick<Post, 'title' | 'content'>>;
update: Pick<DbPost, '_id'> & Partial<Pick<DbPost, 'title' | 'content'>>;
}
export interface ResUpdatePost {

View File

@ -29,7 +29,7 @@ export interface ServiceType {
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 16,
"version": 17,
"services": [
{
"id": 0,
@ -62,7 +62,7 @@ export const serviceProto: ServiceProto<ServiceType> = {
"type": {
"target": {
"type": "Reference",
"target": "models/Post/Post"
"target": "../db/DbPost/DbPost"
},
"keys": [
"_id",
@ -75,14 +75,15 @@ export const serviceProto: ServiceProto<ServiceType> = {
}
]
},
"models/Post/Post": {
"../db/DbPost/DbPost": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "_id",
"type": {
"type": "String"
"type": "Reference",
"target": "?mongodb/ObjectId"
}
},
{
@ -181,7 +182,8 @@ export const serviceProto: ServiceProto<ServiceType> = {
"id": 0,
"name": "_id",
"type": {
"type": "String"
"type": "Reference",
"target": "?bson/ObjectId"
}
}
]
@ -196,7 +198,8 @@ export const serviceProto: ServiceProto<ServiceType> = {
"id": 0,
"name": "_id",
"type": {
"type": "String"
"type": "Reference",
"target": "?bson/ObjectId"
}
}
]
@ -209,7 +212,7 @@ export const serviceProto: ServiceProto<ServiceType> = {
"name": "post",
"type": {
"type": "Reference",
"target": "models/Post/Post"
"target": "../db/DbPost/DbPost"
}
}
]
@ -224,28 +227,26 @@ export const serviceProto: ServiceProto<ServiceType> = {
"type": "Intersection",
"members": [
{
"id": 1,
"id": 4,
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "_id",
"type": {
"type": "String"
}
}
]
"target": {
"type": "Reference",
"target": "../db/DbPost/DbPost"
},
"keys": [
"_id"
],
"type": "Pick"
}
},
{
"id": 3,
"id": 5,
"type": {
"type": "Partial",
"target": {
"target": {
"type": "Reference",
"target": "models/Post/Post"
"target": "../db/DbPost/DbPost"
},
"keys": [
"title",

View File

@ -0,0 +1,9 @@
// TSRPC would decode ObjectId as string in frontend.
declare module 'mongodb' {
export type ObjectId = string;
export type ObjectID = string;
}
declare module 'bson' {
export type ObjectId = string;
export type ObjectID = string;
}