Compare commits

...

8 Commits

Author SHA1 Message Date
k8w
cb46f856f7 readme 2022-04-02 14:15:29 +08:00
k8w
ad022ba3f8 upload allow json 2022-02-26 15:33:45 +08:00
k8w
3ae8b81da4 mongodb crud 2022-02-26 11:32:37 +08:00
k8w
3e87af42e1 npm update 2022-02-26 11:29:21 +08:00
k8w
3f4082593e MongoDB OptionalId<xxx> 2022-01-12 15:37:53 +08:00
k8w
57f0129fde tsrpc@3.1.8 2022-01-11 21:38:21 +08:00
k8w
6707825188 fixed mongodb type bug 2022-01-11 21:16:25 +08:00
k8w
5afb16e2bb ObjectID -> ObjectId 2021-12-29 22:48:25 +08:00
30 changed files with 124 additions and 111 deletions

View File

@ -15,5 +15,4 @@ Start local frontend server:
``` ```
cd <example-dir>/frontend cd <example-dir>/frontend
npm install npm install
npm run dev
``` ```

View File

@ -15,10 +15,10 @@
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"tsrpc-cli": "^2.0.8", "tsrpc-cli": "^2.4.0",
"typescript": "^4.4.3" "typescript": "^4.5.5"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.0.9" "tsrpc": "^3.2.0"
} }
} }

View File

@ -7,15 +7,15 @@
"build": "webpack --mode=production" "build": "webpack --mode=production"
}, },
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.1.0",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"typescript": "^4.4.3", "typescript": "^4.5.5",
"webpack": "^5.57.1", "webpack": "^5.69.1",
"webpack-cli": "^4.9.0", "webpack-cli": "^4.9.2",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.3"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.7" "tsrpc-browser": "^3.2.0"
} }
} }

View File

@ -14,11 +14,11 @@
"devDependencies": { "devDependencies": {
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^10.2.1", "ts-node": "^10.5.0",
"tsrpc-cli": "^2.0.8", "tsrpc-cli": "^2.4.0",
"typescript": "^4.4.3" "typescript": "^4.5.5"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.0.9" "tsrpc": "^3.2.0"
} }
} }

View File

@ -7,16 +7,16 @@
"build": "webpack --mode=production" "build": "webpack --mode=production"
}, },
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.1.0",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"typescript": "^4.4.3", "typescript": "^4.5.5",
"webpack": "^5.57.1", "webpack": "^5.69.1",
"webpack-cli": "^4.9.0", "webpack-cli": "^4.9.2",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.3"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.7" "tsrpc-browser": "^3.2.0"
}, },
"browserslist": [ "browserslist": [
"defaults" "defaults"

View File

@ -15,10 +15,10 @@
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"tsrpc-cli": "^2.2.0", "tsrpc-cli": "^2.4.0",
"typescript": "^4.5.2" "typescript": "^4.5.5"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.1.2" "tsrpc": "^3.2.0"
} }
} }

View File

@ -9,7 +9,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.1.2", "tsrpc-browser": "^3.2.0",
"tsrpc-miniapp": "^3.1.2" "tsrpc-miniapp": "^3.2.0"
} }
} }

View File

@ -15,10 +15,10 @@
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"tsrpc-cli": "^2.2.0", "tsrpc-cli": "^2.4.0",
"typescript": "^4.5.2" "typescript": "^4.5.5"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.1.2" "tsrpc": "^3.2.0"
} }
} }

View File

@ -4,7 +4,7 @@
"uuid": "cd1e2fff-2ab5-4407-b917-e60b25207723", "uuid": "cd1e2fff-2ab5-4407-b917-e60b25207723",
"version": "3.3.2", "version": "3.3.2",
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.1.2", "tsrpc-browser": "^3.2.0",
"tsrpc-miniapp": "^3.1.2" "tsrpc-miniapp": "^3.2.0"
} }
} }

View File

@ -15,10 +15,10 @@
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"tsrpc-cli": "^2.0.8", "tsrpc-cli": "^2.4.0",
"typescript": "^4.4.3" "typescript": "^4.5.5"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.0.9" "tsrpc": "^3.2.0"
} }
} }

View File

@ -6,7 +6,8 @@ import { serviceProto } from "./shared/protocols/serviceProto";
// Create the Server // Create the Server
const server = new HttpServer(serviceProto, { const server = new HttpServer(serviceProto, {
port: 3000, port: 3000,
cors: '*' cors: '*',
json: true
}); });
// Flow: Serve static files // Flow: Serve static files

View File

@ -7,15 +7,15 @@
"build": "webpack --mode=production" "build": "webpack --mode=production"
}, },
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.1.0",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"typescript": "^4.4.3", "typescript": "^4.5.5",
"webpack": "^5.57.1", "webpack": "^5.69.1",
"webpack-cli": "^4.9.0", "webpack-cli": "^4.9.2",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.3"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.7" "tsrpc-browser": "^3.2.0"
} }
} }

View File

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

View File

@ -4,5 +4,8 @@ module.exports = {
], ],
timeout: 999999, timeout: 999999,
exit: true, exit: true,
'preserve-symlinks': true 'preserve-symlinks': true,
spec: [
'./test/**/*.test.ts'
]
} }

View File

@ -9,20 +9,21 @@
"api": "tsrpc api", "api": "tsrpc api",
"doc": "tsrpc doc", "doc": "tsrpc doc",
"dev": "tsrpc dev", "dev": "tsrpc dev",
"build": "tsrpc build" "build": "tsrpc build",
"test": "mocha"
}, },
"devDependencies": { "devDependencies": {
"@types/mocha": "^8.2.3", "@types/mocha": "^8.2.3",
"@types/mongodb": "^3.6.20", "@types/mongodb": "^3.6.20",
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"mocha": "^9.1.2", "mocha": "^9.2.1",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^10.2.1", "ts-node": "^10.5.0",
"tsrpc-cli": "^2.0.8", "tsrpc-cli": "^2.4.0",
"typescript": "^4.4.3" "typescript": "^4.5.5"
}, },
"dependencies": { "dependencies": {
"mongodb": "^3.7.2", "mongodb": "^4.4.0",
"tsrpc": "^3.0.9" "tsrpc": "^3.2.0"
} }
} }

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

@ -1,11 +1,11 @@
import { ObjectID } from 'mongodb'; import { ObjectId } from 'mongodb';
import { ApiCall } from "tsrpc"; import { ApiCall } from "tsrpc";
import { Global } from "../models/Global"; import { Global } from "../models/Global";
import { ReqDelPost, ResDelPost } from "../shared/protocols/PtlDelPost"; 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

@ -1,11 +1,11 @@
import { ObjectID } from 'mongodb'; import { ObjectId } from 'mongodb';
import { ApiCall } from "tsrpc"; import { ApiCall } from "tsrpc";
import { Global } from "../models/Global"; import { Global } from "../models/Global";
import { ReqGetPost, ResGetPost } from "../shared/protocols/PtlGetPost"; 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

@ -1,16 +1,16 @@
import { ObjectID } from 'mongodb'; import { ObjectId } from 'mongodb';
import { ApiCall } from "tsrpc"; import { ApiCall } from "tsrpc";
import { Global } from "../models/Global"; import { Global } from "../models/Global";
import { ReqUpdatePost, ResUpdatePost } from "../shared/protocols/PtlUpdatePost"; import { ReqUpdatePost, ResUpdatePost } from "../shared/protocols/PtlUpdatePost";
export async function ApiUpdatePost(call: ApiCall<ReqUpdatePost, ResUpdatePost>) { export async function ApiUpdatePost(call: ApiCall<ReqUpdatePost, ResUpdatePost>) {
let { _id, ...update } = call.req.update; let { _id, ...rest } = 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, ...rest,
update: { update: {
uid: 'xxx', uid: 'xxx',
time: new Date() time: new Date()

View File

@ -1,4 +1,4 @@
export const BackConfig = { export const BackConfig = {
// Please replace by your db // Please replace by your db
mongoDb: 'mongodb://username:password@xxx.com:27017/test?authSource=admin', mongoDb: 'mongodb+srv://test:test@tsrpc-example.0gzai.mongodb.net/tsrpc-example?retryWrites=true&w=majority',
} }

View File

@ -1,7 +1,7 @@
import { Collection, Db, MongoClient } from "mongodb"; import { Collection, Db, MongoClient, OptionalId } 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 {
@ -14,7 +14,7 @@ export class Global {
this.db = client.db(); this.db = client.db();
} }
static collection<T extends keyof DbCollectionType>(col: T): Collection<DbCollectionType[T]> { static collection<T extends keyof DbCollectionType>(col: T): Collection<OptionalId<DbCollectionType[T]>> {
return this.db.collection(col); return this.db.collection(col);
} }

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

@ -7,16 +7,16 @@
"build": "webpack --mode=production" "build": "webpack --mode=production"
}, },
"devDependencies": { "devDependencies": {
"copy-webpack-plugin": "^9.0.1", "copy-webpack-plugin": "^9.1.0",
"html-webpack-plugin": "^5.3.2", "html-webpack-plugin": "^5.5.0",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"typescript": "^4.4.3", "typescript": "^4.5.5",
"webpack": "^5.57.1", "webpack": "^5.69.1",
"webpack-cli": "^4.9.0", "webpack-cli": "^4.9.2",
"webpack-dev-server": "^3.11.2" "webpack-dev-server": "^3.11.3"
}, },
"dependencies": { "dependencies": {
"tsrpc-browser": "^3.0.7" "tsrpc-browser": "^3.2.0"
}, },
"browserslist": [ "browserslist": [
"defaults" "defaults"

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;
}