创建内容

post.service.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import {connection} from '../app/database/mysql';
import { PostModel } from './post.model';

/**
* 创建内容
*/
export const createPost = async (post:PostModel) =>{
// 准备查询
const statement = `
INSERT INTO post
SET ?
`
//执行查询
const [data] = await connection.promise().query(statement,post)

//提供数据
return data;
}

post.controller.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Request, Response, NextFunction } from 'express'; //引入接口、处理器、参数需要的类型
import { getPosts,createPost } from './post.service';

/**
* 创建内容
*/
export const store = async (
request:Request,
response:Response,
next:NextFunction
)=>{
//准备数据
const {title,content} = request.body;

// 创建内容
try {
const data = await createPost({title,content});
response.status(201).send(data);
} catch (error) {
next(error)
}
}

post.router.ts:

1
2
3
4
5
6
7
8
9
10
11
import express from 'express';
import * as postController from './post.controller';
/**
* 创建内容
*/
router.post('/posts',postController.store);

/**
* 导出路由
*/
export default router;

更新内容

post.service.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import {connection} from '../app/database/mysql';
import { PostModel } from './post.model';

/**
* 更新内容
*/
export const updatePost = async (postId:number,post:PostModel) =>{
//准备查询
const statement = `
UPDATE post
SET ?
WHERE id = ?
`;
//执行查询
const [data] = await connection.promise().query(statement,[post,postId]);

//提供数据
return data;
}

post.controller.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { Request, Response, NextFunction } from 'express'; //引入接口、处理器、参数需要的类型
import { getPosts,createPost, updatePost } from './post.service';
/**
* 更新内容
*/
export const update = async (
request:Request,
response:Response,
next:NextFunction
) =>{
//获取内容ID
const {postId} = request.params;

//准备数据
const {title,content} = request.body;

//更新
try {
const data = await updatePost(parseInt(postId,10),{title,content});
response.send(data)
} catch (error) {
next(error);
}
}

post.router.ts:

1
2
3
4
/**
* 更新内容
*/
router.patch('/posts/:postId',postController.update)

使用帮手 lodash

安装:

1
npm install lodash

如果使用了 typescript 还需安装类型定义:

1
npm install @types/lodash --save-dev

使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import _ from 'lodash';   //在controller中引入

/**
* 更新内容
*/
export const update = async (
request:Request,
response:Response,
next:NextFunction
) =>{
//获取内容ID
const {postId} = request.params;

//准备数据
// const {title,content} = request.body;
const post = _.pick(request.body,['title','content']) //此处使用lodash写法去创建post

//更新
try {
const data = await updatePost(parseInt(postId,10),post);
response.send(data)
} catch (error) {
next(error);
}
}

总结

准备好一个数据仓库服务,在里面可以给应用创建一个数据仓库,设计一下里面的数据表格,再给服务端安装一个数据驱动,用它去连接使用数据仓库服务,这样就可以在应用里用代码的形式处理数据仓库里的数据了,比如使用 node+mysql,使用的驱动为 mysql2,它可以用在 node 项目中去驱动 mysql 类型的数据仓库服务。如果在应用里定义的服务接口需要处理数据仓库里的数据可以先准备好一段要执行的 sql,再用数据驱动提供的方法去把这段准备好的 sql 交给数据服务去执行。