|
| 1 | +#! -*- coding: utf-8 -*- |
| 2 | +from os import environ, path |
| 3 | + |
| 4 | +from weixin import WxAppCloudAPI |
| 5 | + |
| 6 | +appid = environ.get("WXAPP_APPID", "appid") |
| 7 | +secret = environ.get("WXAPP_SECRET", "secret") |
| 8 | +env = "test-id" |
| 9 | + |
| 10 | +example_db = path.abspath(path.join(path.dirname(__file__), "./example_db")) |
| 11 | + |
| 12 | + |
| 13 | +app_cloud = WxAppCloudAPI( |
| 14 | + appid=appid, app_secret=secret, grant_type="client_credential" |
| 15 | +) |
| 16 | +token = app_cloud.client_credential_for_access_token().get("access_token") |
| 17 | +print(token) |
| 18 | + |
| 19 | +cloud_api = WxAppCloudAPI(access_token=token) |
| 20 | + |
| 21 | +# 获取库的集合信息 |
| 22 | +db_info = cloud_api.db_collection_info(json_body={"env": env, "limit": 10}) |
| 23 | +print(db_info) |
| 24 | +# 新建集合 |
| 25 | +resp = cloud_api.db_collection_add(json_body={"env": env, "collection_name": "example"}) |
| 26 | +print("new collection: ", resp) |
| 27 | + |
| 28 | +# 获取库的集合信息 |
| 29 | +db_info = cloud_api.db_collection_info(json_body={"env": env, "limit": 10}) |
| 30 | +print("db info: ", db_info) |
| 31 | + |
| 32 | +# 导入数据 |
| 33 | +resp = cloud_api.db_migrate_import( |
| 34 | + json_body={ |
| 35 | + "env": env, |
| 36 | + "collection_name": "example", |
| 37 | + "file_path": example_db, |
| 38 | + "file_type": 2, |
| 39 | + "stop_on_error": False, |
| 40 | + "conflict_mode": 2, |
| 41 | + } |
| 42 | +) |
| 43 | +print("db migrate import: ", resp) |
| 44 | + |
| 45 | +# 数据库统计记录数量 |
| 46 | +resp = cloud_api.db_count( |
| 47 | + json_body={"env": env, "query": 'db.collection("example").count()'} |
| 48 | +) |
| 49 | +print("count", resp) |
| 50 | + |
| 51 | +# 插入数据 |
| 52 | +resp = cloud_api.db_add( |
| 53 | + json_body={"env": env, "query": 'db.collection("example").add({data: [{age: 12}]})'} |
| 54 | +) |
| 55 | +print("add: ", resp) |
| 56 | + |
| 57 | +id_list = resp.get("id_list") |
| 58 | +item_id = id_list[0] |
| 59 | + |
| 60 | +# 数据库统计记录数量 |
| 61 | +resp = cloud_api.db_count( |
| 62 | + json_body={"env": env, "query": 'db.collection("example").count()'} |
| 63 | +) |
| 64 | +print("count", resp) |
| 65 | +# 查询记录 |
| 66 | + |
| 67 | +resp = cloud_api.db_query( |
| 68 | + json_body={ |
| 69 | + "env": env, |
| 70 | + "query": 'db.collection("example").where({_id: "%s"}).limit(10).skip(0).get()' |
| 71 | + % item_id, |
| 72 | + } |
| 73 | +) |
| 74 | +print("query: ", resp) |
| 75 | + |
| 76 | +# 更新数据 |
| 77 | + |
| 78 | +resp = cloud_api.db_update( |
| 79 | + json_body={ |
| 80 | + "env": env, |
| 81 | + "query": 'db.collection("example").where({_id: "%s"}).update({data: {age: _.inc(1)}})' |
| 82 | + % item_id, |
| 83 | + } |
| 84 | +) |
| 85 | +print("update: ", resp) |
| 86 | + |
| 87 | +# 查询记录 |
| 88 | +resp = cloud_api.db_query( |
| 89 | + json_body={ |
| 90 | + "env": env, |
| 91 | + "query": 'db.collection("example").where({_id: "%s"}).limit(10).skip(0).get()' |
| 92 | + % item_id, |
| 93 | + } |
| 94 | +) |
| 95 | +print("query: ", resp) |
| 96 | +# 删除数据 |
| 97 | +resp = cloud_api.db_delete( |
| 98 | + json_body={ |
| 99 | + "env": env, |
| 100 | + "query": 'db.collection("example").where({_id: "%s"}).remove()' % item_id, |
| 101 | + } |
| 102 | +) |
| 103 | +print("remove: ", resp) |
| 104 | + |
| 105 | +# 查询记录 |
| 106 | +resp = cloud_api.db_query( |
| 107 | + json_body={ |
| 108 | + "env": env, |
| 109 | + "query": 'db.collection("example").where({_id: "%s"}).limit(10).skip(0).get()' |
| 110 | + % item_id, |
| 111 | + } |
| 112 | +) |
| 113 | +print("query: ", resp) |
| 114 | +# 删除集合 |
| 115 | +resp = cloud_api.db_collection_delete( |
| 116 | + json_body={"env": env, "collection_name": "example"} |
| 117 | +) |
| 118 | + |
| 119 | +# 获取库的集合信息 |
| 120 | + |
| 121 | +db_info = cloud_api.db_collection_info(json_body={"env": env, "limit": 10}) |
| 122 | +print("db info: ", db_info) |
0 commit comments