前言
眾所周知目前使用Node.js + mongodb已經(jīng)成為很多公司的技術(shù)棧。ThinkJS其實也提供了對mongo的支持,雖然官方文檔較少,但是保證了ORM的API的一致性,所以用起來需要查看基本的>Model api
基本的模型文件放在common/model下
獲取列表
getList(q, page) {  return this.select(); }分頁加條件搜索
search(q, page) {  if(q) {  q = new RegExp(q,'i'); } return this.where({'name':{ $regex: q}}).page(page, 20).countSelect(); }獲取詳情
getDetail(id) {   return this.where({'_id':id}).select();  }創(chuàng)建數(shù)據(jù)
addTag(tag) {  return this.add(tag); }更新數(shù)據(jù)
updateTag(id,data) {  return this.where({'_id':id}).update(data); }刪除數(shù)據(jù)
 removeOne(id) { return this.where({'_id':id}).delete(); }這樣你需要什么只需要調(diào)用相應(yīng)模型的方法就好了。
一個簡單的接口如下:
async createAction() {  let name = this.post('name'); let contents = this.post('contents'); // 獲取模型實例 let m = this.model('tag'); let res = await m.addTag({  'name': name,  'contents': contents,  'count': 0, }); if(res) {  this.success(''); }else{  this.apiErrorHandle(4000); } } ....總結(jié)
以上就是這篇文章的全部內(nèi)容改了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
新聞熱點
疑難解答