mongodb-crud
This commit is contained in:
3
examples/mongodb-crud/frontend/.gitignore
vendored
Normal file
3
examples/mongodb-crud/frontend/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.DS_STORE
|
||||
node_modules
|
||||
dist
|
24
examples/mongodb-crud/frontend/package.json
Normal file
24
examples/mongodb-crud/frontend/package.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "code-frontend",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "webpack serve --mode=development --open",
|
||||
"build": "webpack --mode=production"
|
||||
},
|
||||
"devDependencies": {
|
||||
"copy-webpack-plugin": "^9.0.1",
|
||||
"html-webpack-plugin": "^5.3.2",
|
||||
"ts-loader": "^9.2.3",
|
||||
"typescript": "^4.3.4",
|
||||
"webpack": "^5.40.0",
|
||||
"webpack-cli": "^4.7.2",
|
||||
"webpack-dev-server": "^3.11.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"tsrpc-browser": "^3.0.3"
|
||||
},
|
||||
"browserslist": [
|
||||
"defaults"
|
||||
]
|
||||
}
|
BIN
examples/mongodb-crud/frontend/public/favicon.ico
Normal file
BIN
examples/mongodb-crud/frontend/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
17
examples/mongodb-crud/frontend/public/index.html
Normal file
17
examples/mongodb-crud/frontend/public/index.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>TSRPC Browser</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>TSRPC CRUD</h1>
|
||||
|
||||
<p>See console.</p>
|
||||
</body>
|
||||
|
||||
</html>
|
33
examples/mongodb-crud/frontend/src/index.ts
Normal file
33
examples/mongodb-crud/frontend/src/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { HttpClient } from 'tsrpc-browser';
|
||||
import { serviceProto } from './shared/protocols/serviceProto';
|
||||
|
||||
let client = new HttpClient(serviceProto, {
|
||||
server: 'http://127.0.0.1:3000',
|
||||
logger: console
|
||||
});
|
||||
|
||||
window.onload = async () => {
|
||||
let retAdd = await client.callApi('AddPost', {
|
||||
newPost: {
|
||||
author: 'k8w',
|
||||
title: 'TSPRC',
|
||||
content: '太好用了'
|
||||
}
|
||||
});
|
||||
|
||||
let retGet = await client.callApi('GetPost', {
|
||||
_id: retAdd.res!.insertedId
|
||||
});
|
||||
|
||||
let post = retGet.res!.post;
|
||||
post.title = 'TSRPC 123';
|
||||
post.content = 'xxxxxxxx';
|
||||
|
||||
let retUpdate = await client.callApi('UpdatePost', {
|
||||
update: post
|
||||
});
|
||||
|
||||
let retDel = await client.callApi('DelPost', {
|
||||
_id: post._id
|
||||
})
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { Post } from "./models/Post";
|
||||
|
||||
export interface ReqAddPost {
|
||||
newPost: Omit<Post, '_id' | 'create' | 'update' | 'visitedNum'>;
|
||||
}
|
||||
|
||||
export interface ResAddPost {
|
||||
insertedId: string;
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
export interface ReqDelPost {
|
||||
_id: string;
|
||||
}
|
||||
|
||||
export interface ResDelPost {
|
||||
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { Post } from "./models/Post";
|
||||
|
||||
export interface ReqGetPost {
|
||||
_id: string;
|
||||
}
|
||||
|
||||
export interface ResGetPost {
|
||||
post: Post;
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { Post } from "./models/Post";
|
||||
|
||||
export interface ReqUpdatePost {
|
||||
update: { _id: string } & Partial<Pick<Post, 'title' | 'content'>>;
|
||||
}
|
||||
|
||||
export interface ResUpdatePost {
|
||||
matchedCount: number;
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
export interface Post {
|
||||
_id: string;
|
||||
author: string;
|
||||
title: string;
|
||||
content: string;
|
||||
visitedNum: number;
|
||||
|
||||
create: {
|
||||
uid: string;
|
||||
time: Date;
|
||||
}
|
||||
|
||||
update?: {
|
||||
uid: string,
|
||||
time: Date
|
||||
}
|
||||
}
|
@@ -0,0 +1,276 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { ReqAddPost, ResAddPost } from './PtlAddPost';
|
||||
import { ReqDelPost, ResDelPost } from './PtlDelPost';
|
||||
import { ReqGetPost, ResGetPost } from './PtlGetPost';
|
||||
import { ReqUpdatePost, ResUpdatePost } from './PtlUpdatePost';
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"AddPost": {
|
||||
req: ReqAddPost,
|
||||
res: ResAddPost
|
||||
},
|
||||
"DelPost": {
|
||||
req: ReqDelPost,
|
||||
res: ResDelPost
|
||||
},
|
||||
"GetPost": {
|
||||
req: ReqGetPost,
|
||||
res: ResGetPost
|
||||
},
|
||||
"UpdatePost": {
|
||||
req: ReqUpdatePost,
|
||||
res: ResUpdatePost
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"version": 16,
|
||||
"services": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "AddPost",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "DelPost",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "GetPost",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "UpdatePost",
|
||||
"type": "api"
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"PtlAddPost/ReqAddPost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "newPost",
|
||||
"type": {
|
||||
"target": {
|
||||
"type": "Reference",
|
||||
"target": "models/Post/Post"
|
||||
},
|
||||
"keys": [
|
||||
"_id",
|
||||
"create",
|
||||
"update",
|
||||
"visitedNum"
|
||||
],
|
||||
"type": "Omit"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"models/Post/Post": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "_id",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "author",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "title",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"name": "visitedNum",
|
||||
"type": {
|
||||
"type": "Number"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"name": "create",
|
||||
"type": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "uid",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"name": "update",
|
||||
"type": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "uid",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlAddPost/ResAddPost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "insertedId",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlDelPost/ReqDelPost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "_id",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlDelPost/ResDelPost": {
|
||||
"type": "Interface"
|
||||
},
|
||||
"PtlGetPost/ReqGetPost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "_id",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetPost/ResGetPost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "post",
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "models/Post/Post"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlUpdatePost/ReqUpdatePost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "update",
|
||||
"type": {
|
||||
"type": "Intersection",
|
||||
"members": [
|
||||
{
|
||||
"id": 1,
|
||||
"type": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "_id",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"type": {
|
||||
"type": "Partial",
|
||||
"target": {
|
||||
"target": {
|
||||
"type": "Reference",
|
||||
"target": "models/Post/Post"
|
||||
},
|
||||
"keys": [
|
||||
"title",
|
||||
"content"
|
||||
],
|
||||
"type": "Pick"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlUpdatePost/ResUpdatePost": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "matchedCount",
|
||||
"type": {
|
||||
"type": "Number"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
23
examples/mongodb-crud/frontend/tsconfig.json
Normal file
23
examples/mongodb-crud/frontend/tsconfig.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"dom",
|
||||
"es2015"
|
||||
],
|
||||
"module": "esnext",
|
||||
"target": "es5",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"moduleResolution": "node",
|
||||
"outDir": "dist",
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"jsx": "react-jsx",
|
||||
"sourceMap": true,
|
||||
"isolatedModules": true
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
56
examples/mongodb-crud/frontend/webpack.config.js
Normal file
56
examples/mongodb-crud/frontend/webpack.config.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const webpack = require('webpack');
|
||||
const path = require('path');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
||||
|
||||
const isProduction = process.argv.indexOf('--mode=production') > -1;
|
||||
|
||||
module.exports = {
|
||||
entry: './src/index.ts',
|
||||
output: {
|
||||
filename: 'bundle.[contenthash].js',
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
clean: true
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts', '.tsx', '.js', '.mjs', '.cjs']
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
use: [{
|
||||
loader: 'ts-loader',
|
||||
options: {
|
||||
// Compile to ES5 in production mode for better compatibility
|
||||
// Compile to ES2018 in development for better debugging (like async/await)
|
||||
compilerOptions: !isProduction ? {
|
||||
"target": "es2018",
|
||||
} : undefined
|
||||
}
|
||||
}],
|
||||
exclude: /node_modules/
|
||||
},
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
// Copy "public" to "dist"
|
||||
new CopyWebpackPlugin({
|
||||
patterns: [{
|
||||
from: 'public',
|
||||
to: '.',
|
||||
toType: 'dir',
|
||||
globOptions: {
|
||||
gitignore: true,
|
||||
ignore: [path.resolve(__dirname, 'public/index.html').replace(/\\/g, '/')]
|
||||
},
|
||||
noErrorOnMissing: true
|
||||
}]
|
||||
}),
|
||||
// Auto add <script> to "index.html"
|
||||
new HtmlWebpackPlugin({
|
||||
template: 'public/index.html'
|
||||
}),
|
||||
],
|
||||
devtool: isProduction ? false : 'inline-source-map'
|
||||
}
|
Reference in New Issue
Block a user