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" "typescript": "^4.4.3"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.0.9" "tsrpc": "^3.1.7"
} }
} }

View File

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

View File

@ -1,3 +1,4 @@
import { Document, ObjectId } from "mongodb";
import { ApiCall } from "tsrpc"; import { ApiCall } from "tsrpc";
import { Global } from "../models/Global"; import { Global } from "../models/Global";
import { ReqAddPost, ResAddPost } from "../shared/protocols/PtlAddPost"; 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>) { export async function ApiDelPost(call: ApiCall<ReqDelPost, ResDelPost>) {
let op = await Global.collection('Post').deleteOne({ let op = await Global.collection('Post').deleteOne({
_id: new ObjectId(call.req._id) _id: call.req._id
}) })
call.succ({}); call.succ({});

View File

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

View File

@ -1,7 +1,7 @@
import { Collection, Db, MongoClient } from "mongodb"; import { Collection, Db, MongoClient } from "mongodb";
import { Logger } from "tsrpc"; import { Logger } from "tsrpc";
import { DbPost } from "../shared/db/DbPost";
import { BackConfig } from "./BackConfig"; import { BackConfig } from "./BackConfig";
import { DbPost } from "./dbItems/DbPost";
export class Global { 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 { import { ObjectId } from "mongodb";
_id: string;
export interface DbPost {
_id: ObjectId;
author: string; author: string;
title: string; title: string;
content: string; content: string;

View File

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

View File

@ -1,5 +1,7 @@
import { ObjectId } from "bson";
export interface ReqDelPost { export interface ReqDelPost {
_id: string; _id: ObjectId;
} }
export interface ResDelPost { 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 { export interface ReqGetPost {
_id: string; _id: ObjectId;
} }
export interface ResGetPost { 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 { export interface ReqUpdatePost {
update: { _id: string } & Partial<Pick<Post, 'title' | 'content'>>; update: Pick<DbPost, '_id'> & Partial<Pick<DbPost, 'title' | 'content'>>;
} }
export interface ResUpdatePost { export interface ResUpdatePost {

View File

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