

Also from Kynth Studios


Also from Kynth Studios


Also from Kynth Studios
12345# 服务(Service)67我们一般将业务逻辑写在`Service`层,`Controller`层只做参数校验、数据转换等操作,`Service`层做具体的业务逻辑处理。89`cool-admin`对基本的`Service`进行封装;1011## 重写 CRUD1213`Controller`的六个快速方法,`add`、`update`、`delete`、`info`、`list`、`page`,是通过调用一个通用的`BaseService`的方法实现,所以我们可以重写`Service`的方法来实现自己的业务逻辑。1415**示例**1617重写 add 方法1819```ts20import { DemoGoodsEntity } from "./../entity/goods";21import { Provide } from "@midwayjs/core";22import { BaseService } from "@cool-midway/core";23import { InjectEntityModel } from "@midwayjs/typeorm";24import { Repository } from "typeorm";2526/**27 * 商品示例28 */29@Provide()30export class DemoGoodsService extends BaseService {31 @InjectEntityModel(DemoGoodsEntity)32 demoGoodsEntity: Repository<DemoGoodsEntity>;3334 /**35 * 新增36 * @param param37 * @returns38 */39 async add(param: any) {40 // 调用原本的add,如果不需要可以不用这样写,完全按照自己的新增逻辑写41 const result = await super.add(param);42 // 你自己的业务逻辑43 return result;44 }45}46```4748记得在`Controller`上配置对应的`Service`才会使其生效4950```ts51import { DemoGoodsService } from "../../service/goods";52import { DemoGoodsEntity } from "../../entity/goods";53import { Body, Inject, Post, Provide } from "@midwayjs/core";54import { CoolController, BaseController } from "@cool-midway/core";55import { InjectEntityModel } from "@midwayjs/typeorm";56import { Repository } from "typeorm";5758/**59 * 测试60 */61@Provide()62@CoolController({63 api: ["add", "delete", "update", "info", "list", "page"],64 entity: DemoGoodsEntity,65 service: DemoGoodsService66})67export class AppDemoGoodsController extends BaseController {}68```6970## 普通查询(TypeOrm)7172普通查询基于[TypeOrm](mdc:https:/typeorm.io),点击查看官方详细文档7374**示例**7576```ts77import { DemoGoodsEntity } from "./../entity/goods";78import { Provide } from "@midwayjs/core";79import { BaseService } from "@cool-midway/core";80import { InjectEntityModel } from "@midwayjs/typeorm";81import { In, Repository } from "typeorm";8283/**84 * 商品示例85 */86@Provide()87export class DemoGoodsService extends BaseService {88 @InjectEntityModel(DemoGoodsEntity)89 demoGoodsEntity: Repository<DemoGoodsEntity>;9091 async typeorm() {92 // 新增单个,传入的参数字段在数据库中一定要存在93 await this.demoGoodsEntity.insert({ title: "xxx" });94 // 新增单个,传入的参数字段在数据库中可以不存在95 await this.demoGoodsEntity.save({ title: "xxx" });96 // 新增多个97 await this.demoGoodsEntity.save([{ title: "xxx" }]);98 // 查找单个99 await this.demoGoodsEntity.findOneBy({ id: 1 });100 // 查找多个101 await this.demoGoodsEntity.findBy({ id: In([1, 2]) });102 // 删除单个103 await this.demoGoodsEntity.delete(1);104 // 删除多个105 await this.demoGoodsEntity.delete([1]);106 // 根据ID更新107 await this.demoGoodsEntity.update(1, { title: "xxx" });108 // 根据条件更新109 await this.demoGoodsEntity.update({ price: 20 }, { title: "xxx" });110 // 多条件操作111 await this.demoGoodsEntity112 .createQueryBuilder()113 .where("id = :id", { id: 1 })114 .andWhere("price = :price", { price: 20 })115 .getOne();116 }117}118```119120## 高级查询(SQL)121122**1、普通 SQL 查询**123124```ts125import { DemoGoodsEntity } from "./../entity/goods";126import { Provide } from "@midwayjs/core";127import { BaseService } from "@cool-midway/core";128import { InjectEntityModel } from "@midwayjs/typeorm";129import { Repository } from "typeorm";130131/**132 * 商品示例133 */134@Provide()135export class DemoGoodsService extends BaseService {136 @InjectEntityModel(DemoGoodsEntity)137 demoGoodsEntity: Repository<DemoGoodsEntity>;138139 /**140 * 执行sql141 */142 async sql(query) {143 return this.nativeQuery("select * from demo_goods a where a.id = ?", [query.id]);144 }145}146```147148**2、分页 SQL 查询**149150```ts151import { DemoGoodsEntity } from "./../entity/goods";152import { Provide } from "@midwayjs/core";153import { BaseService } from "@cool-midway/core";154import { InjectEntityModel } from "@midwayjs/typeorm";155import { Repository } from "typeorm";156157/**158 * 商品示例159 */160@Provide()161export class DemoGoodsService extends BaseService {162 @InjectEntityModel(DemoGoodsEntity)163 demoGoodsEntity: Repository<DemoGoodsEntity>;164165 /**166 * 执行分页sql167 */168 async sqlPage(query) {169 return this.sqlRenderPage("select * from demo_goods ORDER BY id ASC", query, false);170 }171}172```173174**3、非 SQL 的分页查询**175176```ts177import { DemoGoodsEntity } from "./../entity/goods";178import { Provide } from "@midwayjs/core";179import { BaseService } from "@cool-midway/core";180import { InjectEntityModel } from "@midwayjs/typeorm";181import { In, Repository } from "typeorm";182183/**184 * 商品示例185 */186@Provide()187export class DemoGoodsService extends BaseService {188 @InjectEntityModel(DemoGoodsEntity)189 demoGoodsEntity: Repository<DemoGoodsEntity>;190191 /**192 * 执行entity分页193 */194 async entityPage(query) {195 const find = this.demoGoodsEntity.createQueryBuilder();196 find.where("id = :id", { id: 1 });197 return this.entityRenderPage(find, query);198 }199}200```201202**4、SQL 动态条件**203204分页查询和普通的 SQL 查询都支持动态条件,通过`this.setSql(条件,sql语句,参数)`来配置205206```ts207import { DemoGoodsEntity } from "./../entity/goods";208import { Provide } from "@midwayjs/core";209import { BaseService } from "@cool-midway/core";210import { InjectEntityModel } from "@midwayjs/typeorm";211import { Repository } from "typeorm";212213/**214 * 商品示例215 */216@Provide()217export class DemoGoodsService extends BaseService {218 @InjectEntityModel(DemoGoodsEntity)219 demoGoodsEntity: Repository<DemoGoodsEntity>;220221 /**222 * 执行sql223 */224 async sql(query) {225 return this.nativeQuery(`226 select * from demo_goods a227 WHERE 1=1228 ${this.setSql(query.id, "and a.id = ?", [query.id])}229 ORDER BY id ASC230 `);231 }232}233```234235## 修改之前(modifyBefore)236237有时候我们需要在数据进行修改动作之前,对它进行一些处理,比如:修改密码时,需要对密码进行加密,这时候我们可以使用`modifyBefore`方法来实现238239```ts240import { DemoGoodsEntity } from "./../entity/goods";241import { Provide } from "@midwayjs/core";242import { BaseService } from "@cool-midway/core";243import { InjectEntityModel } from "@midwayjs/typeorm";244import { Repository } from "typeorm";245import * as md5 from "md5";246247/**248 * 商品示例249 */250@Provide()251export class DemoGoodsService extends BaseService {252 @InjectEntityModel(DemoGoodsEntity)253 demoGoodsEntity: Repository<DemoGoodsEntity>;254255 /**256 * 修改之前257 * @param data258 * @param type259 */260 async modifyBefore(data: any, type: "delete" | "update" | "add") {261 if (type == "update") {262 data.password = md5(data.password);263 }264 }265}266```267268## 修改之后(modifyAfter)269270有时候我们需要在数据进行修改动作之后,对它进行一些处理,比如:修改完数据之后将它放入队列或者 ElasticSearch271272```ts273import { DemoGoodsEntity } from "./../entity/goods";274import { Provide } from "@midwayjs/core";275import { BaseService } from "@cool-midway/core";276import { InjectEntityModel } from "@midwayjs/typeorm";277import { Repository } from "typeorm";278import * as md5 from "md5";279280/**281 * 商品示例282 */283@Provide()284export class DemoGoodsService extends BaseService {285 @InjectEntityModel(DemoGoodsEntity)286 demoGoodsEntity: Repository<DemoGoodsEntity>;287288 /**289 * 修改之后290 * @param data291 * @param type292 */293 async modifyAfter(data: any, type: "delete" | "update" | "add") {294 // 你想做的其他事情295 }296}297```298299## 设置实体300301`Service`与`Service`之间相互调用`BaseService`里的方法,有可能出现“未设置操作实体”的问题可以通过以下方式设置实体302303::: warning 建议304但是一般不建议这样做,因为这样会导致`Service`与`Service`耦合,不利于代码的维护,如果要操作对应的表直接在当前的`Service`注入对应的表操作即可305:::306307```ts308@Provide()309export class XxxService extends BaseService {310 @InjectEntityModel(XxxEntity)311 xxxEntity: Repository<XxxEntity>;312313 @Init()314 async init() {315 await super.init();316 // 设置实体317 this.setEntity(this.xxxEntity);318 }319}320```321
One repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| cool-team-official/cool-admin-midway.cursor/rules/authority.mdc · 3.3k | Cursor rules | security | 54/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/cache.mdc · 3.3k | Cursor rules | no sections | 58/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/controller.mdc · 3.3k | Cursor rules | api | 46/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/db.mdc · 3.3k | Cursor rules | setupdatabase | 62/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/event.mdc · 3.3k | Cursor rules | no sections | 45/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/module.mdc · 3.3k | Cursor rules | no sections | 50/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/socket.mdc · 3.3k | Cursor rules | no sections | 58/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/task.mdc · 3.3k | Cursor rules | style | 58/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/tenant.mdc · 3.3k | Cursor rules | no sections | 50/100 | today | |
| cool-team-official/cool-admin-midway.cursorrules · 3.3k | .cursorrules | no sections | 39/100 | today |
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 14 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 46 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 14 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 14 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 14 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 14 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 14 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 14 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 46 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 14 days ago |
A badge carrying the measured quality of the strongest agent config file in this repository, out of 100. It reads from this index every time somebody loads your page, so it changes when the measurement changes and there is nothing to keep up to date. Free, no account, and the value is not something you or we can set by hand.
[](https://rulestack.kynth.studio/configs/cool-team-official-cool-admin-midway-cursor-rules-service)Would rather not hotlink us? Every badge is also served in shields.io’s endpoint schema, so shields renders the image and your readers never talk to our domain:
Published by Toolproof, the masthead over this index and eight others. The method behind the number is at toolproof.kynth.studio/methodology, and the whole thing is readable as JSON with no key at /api.