From 67078251886b1827f4ba2495785cd54500e11412 Mon Sep 17 00:00:00 2001 From: k8w Date: Tue, 11 Jan 2022 21:16:25 +0800 Subject: [PATCH] fixed mongodb type bug --- examples/first-api/backend/package.json | 4 +- examples/mongodb-crud/backend/package.json | 6 +-- .../backend/src/api/ApiAddPost.ts | 1 + .../backend/src/api/ApiDelPost.ts | 2 +- .../backend/src/api/ApiGetPost.ts | 4 +- .../backend/src/api/ApiUpdatePost.ts | 2 +- .../mongodb-crud/backend/src/models/Global.ts | 2 +- .../backend/src/models/dbItems/DbPost.ts | 7 ---- .../models/Post.ts => db/DbPost.ts} | 6 ++- .../src/shared/protocols/PtlAddPost.ts | 4 +- .../src/shared/protocols/PtlDelPost.ts | 4 +- .../src/shared/protocols/PtlGetPost.ts | 7 ++-- .../src/shared/protocols/PtlUpdatePost.ts | 4 +- .../src/shared/protocols/serviceProto.ts | 41 ++++++++++--------- examples/mongodb-crud/frontend/src/env.d.ts | 9 ++++ 15 files changed, 56 insertions(+), 47 deletions(-) delete mode 100644 examples/mongodb-crud/backend/src/models/dbItems/DbPost.ts rename examples/mongodb-crud/backend/src/shared/{protocols/models/Post.ts => db/DbPost.ts} (71%) create mode 100644 examples/mongodb-crud/frontend/src/env.d.ts diff --git a/examples/first-api/backend/package.json b/examples/first-api/backend/package.json index ebc10a1..070b3ba 100644 --- a/examples/first-api/backend/package.json +++ b/examples/first-api/backend/package.json @@ -19,6 +19,6 @@ "typescript": "^4.4.3" }, "dependencies": { - "tsrpc": "^3.0.9" + "tsrpc": "^3.1.7" } -} \ No newline at end of file +} diff --git a/examples/mongodb-crud/backend/package.json b/examples/mongodb-crud/backend/package.json index 44b0e74..091c0ee 100644 --- a/examples/mongodb-crud/backend/package.json +++ b/examples/mongodb-crud/backend/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/examples/mongodb-crud/backend/src/api/ApiAddPost.ts b/examples/mongodb-crud/backend/src/api/ApiAddPost.ts index 3491e1a..d041370 100644 --- a/examples/mongodb-crud/backend/src/api/ApiAddPost.ts +++ b/examples/mongodb-crud/backend/src/api/ApiAddPost.ts @@ -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"; diff --git a/examples/mongodb-crud/backend/src/api/ApiDelPost.ts b/examples/mongodb-crud/backend/src/api/ApiDelPost.ts index 0c654a3..ca49a32 100644 --- a/examples/mongodb-crud/backend/src/api/ApiDelPost.ts +++ b/examples/mongodb-crud/backend/src/api/ApiDelPost.ts @@ -5,7 +5,7 @@ import { ReqDelPost, ResDelPost } from "../shared/protocols/PtlDelPost"; export async function ApiDelPost(call: ApiCall) { let op = await Global.collection('Post').deleteOne({ - _id: new ObjectId(call.req._id) + _id: call.req._id }) call.succ({}); diff --git a/examples/mongodb-crud/backend/src/api/ApiGetPost.ts b/examples/mongodb-crud/backend/src/api/ApiGetPost.ts index 53232d6..d959ace 100644 --- a/examples/mongodb-crud/backend/src/api/ApiGetPost.ts +++ b/examples/mongodb-crud/backend/src/api/ApiGetPost.ts @@ -5,7 +5,7 @@ import { ReqGetPost, ResGetPost } from "../shared/protocols/PtlGetPost"; export async function ApiGetPost(call: ApiCall) { 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) { call.succ({ post: { ...op, - _id: op._id.toHexString() + _id: op._id } }) } \ No newline at end of file diff --git a/examples/mongodb-crud/backend/src/api/ApiUpdatePost.ts b/examples/mongodb-crud/backend/src/api/ApiUpdatePost.ts index ef2d66b..dcb6af6 100644 --- a/examples/mongodb-crud/backend/src/api/ApiUpdatePost.ts +++ b/examples/mongodb-crud/backend/src/api/ApiUpdatePost.ts @@ -7,7 +7,7 @@ export async function ApiUpdatePost(call: ApiCall) let { _id, ...update } = call.req.update; let op = await Global.collection('Post').updateOne({ - _id: new ObjectId(_id) + _id: _id }, { $set: { ...update, diff --git a/examples/mongodb-crud/backend/src/models/Global.ts b/examples/mongodb-crud/backend/src/models/Global.ts index ff5f945..7804839 100644 --- a/examples/mongodb-crud/backend/src/models/Global.ts +++ b/examples/mongodb-crud/backend/src/models/Global.ts @@ -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 { diff --git a/examples/mongodb-crud/backend/src/models/dbItems/DbPost.ts b/examples/mongodb-crud/backend/src/models/dbItems/DbPost.ts deleted file mode 100644 index ca4b8f3..0000000 --- a/examples/mongodb-crud/backend/src/models/dbItems/DbPost.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ObjectId } from "mongodb"; -import { Overwrite } from "tsrpc"; -import { Post } from "../../shared/protocols/models/Post"; - -export type DbPost = Overwrite \ No newline at end of file diff --git a/examples/mongodb-crud/backend/src/shared/protocols/models/Post.ts b/examples/mongodb-crud/backend/src/shared/db/DbPost.ts similarity index 71% rename from examples/mongodb-crud/backend/src/shared/protocols/models/Post.ts rename to examples/mongodb-crud/backend/src/shared/db/DbPost.ts index 67b21e2..c318b7c 100644 --- a/examples/mongodb-crud/backend/src/shared/protocols/models/Post.ts +++ b/examples/mongodb-crud/backend/src/shared/db/DbPost.ts @@ -1,5 +1,7 @@ -export interface Post { - _id: string; +import { ObjectId } from "mongodb"; + +export interface DbPost { + _id: ObjectId; author: string; title: string; content: string; diff --git a/examples/mongodb-crud/backend/src/shared/protocols/PtlAddPost.ts b/examples/mongodb-crud/backend/src/shared/protocols/PtlAddPost.ts index c4f367e..c2af637 100644 --- a/examples/mongodb-crud/backend/src/shared/protocols/PtlAddPost.ts +++ b/examples/mongodb-crud/backend/src/shared/protocols/PtlAddPost.ts @@ -1,7 +1,7 @@ -import { Post } from "./models/Post"; +import { DbPost } from "../db/DbPost"; export interface ReqAddPost { - newPost: Omit; + newPost: Omit; } export interface ResAddPost { diff --git a/examples/mongodb-crud/backend/src/shared/protocols/PtlDelPost.ts b/examples/mongodb-crud/backend/src/shared/protocols/PtlDelPost.ts index 026fec7..03d02a8 100644 --- a/examples/mongodb-crud/backend/src/shared/protocols/PtlDelPost.ts +++ b/examples/mongodb-crud/backend/src/shared/protocols/PtlDelPost.ts @@ -1,5 +1,7 @@ +import { ObjectId } from "bson"; + export interface ReqDelPost { - _id: string; + _id: ObjectId; } export interface ResDelPost { diff --git a/examples/mongodb-crud/backend/src/shared/protocols/PtlGetPost.ts b/examples/mongodb-crud/backend/src/shared/protocols/PtlGetPost.ts index a6d169f..d10c33a 100644 --- a/examples/mongodb-crud/backend/src/shared/protocols/PtlGetPost.ts +++ b/examples/mongodb-crud/backend/src/shared/protocols/PtlGetPost.ts @@ -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; } diff --git a/examples/mongodb-crud/backend/src/shared/protocols/PtlUpdatePost.ts b/examples/mongodb-crud/backend/src/shared/protocols/PtlUpdatePost.ts index 20ccc71..c81797f 100644 --- a/examples/mongodb-crud/backend/src/shared/protocols/PtlUpdatePost.ts +++ b/examples/mongodb-crud/backend/src/shared/protocols/PtlUpdatePost.ts @@ -1,7 +1,7 @@ -import { Post } from "./models/Post"; +import { DbPost } from "../db/DbPost"; export interface ReqUpdatePost { - update: { _id: string } & Partial>; + update: Pick & Partial>; } export interface ResUpdatePost { diff --git a/examples/mongodb-crud/backend/src/shared/protocols/serviceProto.ts b/examples/mongodb-crud/backend/src/shared/protocols/serviceProto.ts index 6b3a764..c96478a 100644 --- a/examples/mongodb-crud/backend/src/shared/protocols/serviceProto.ts +++ b/examples/mongodb-crud/backend/src/shared/protocols/serviceProto.ts @@ -29,7 +29,7 @@ export interface ServiceType { } export const serviceProto: ServiceProto = { - "version": 16, + "version": 17, "services": [ { "id": 0, @@ -62,7 +62,7 @@ export const serviceProto: ServiceProto = { "type": { "target": { "type": "Reference", - "target": "models/Post/Post" + "target": "../db/DbPost/DbPost" }, "keys": [ "_id", @@ -75,14 +75,15 @@ export const serviceProto: ServiceProto = { } ] }, - "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 = { "id": 0, "name": "_id", "type": { - "type": "String" + "type": "Reference", + "target": "?bson/ObjectId" } } ] @@ -196,7 +198,8 @@ export const serviceProto: ServiceProto = { "id": 0, "name": "_id", "type": { - "type": "String" + "type": "Reference", + "target": "?bson/ObjectId" } } ] @@ -209,7 +212,7 @@ export const serviceProto: ServiceProto = { "name": "post", "type": { "type": "Reference", - "target": "models/Post/Post" + "target": "../db/DbPost/DbPost" } } ] @@ -224,28 +227,26 @@ export const serviceProto: ServiceProto = { "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", diff --git a/examples/mongodb-crud/frontend/src/env.d.ts b/examples/mongodb-crud/frontend/src/env.d.ts new file mode 100644 index 0000000..95a50dc --- /dev/null +++ b/examples/mongodb-crud/frontend/src/env.d.ts @@ -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; +} \ No newline at end of file