webcake-storefront-mcp 1.31.0 → 1.31.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,11 @@
1
1
  [
2
+ {
3
+ "v": "1.31.1",
4
+ "d": "26/06/2026",
5
+ "type": "Fixed",
6
+ "en": "The webcake-data SDK reference embedded in get_http_function and get_site_custom_code now documents the correct Mongoose-document API: filters are…",
7
+ "vi": "Tài liệu tham chiếu SDK webcake-data được nhúng trong get_http_function và get_site_custom_code nay ghi lại đúng API kiểu Mongoose-document: bộ lọc…"
8
+ },
2
9
  {
3
10
  "v": "1.31.0",
4
11
  "d": "26/06/2026",
@@ -33,12 +40,5 @@
33
40
  "type": "Added",
34
41
  "en": "build_page now accepts an seo object (title, description, keyword, favicon, thumbnail) that is written to page.settings.seo with Open Graph…",
35
42
  "vi": "build_page nay nhận thêm đối tượng seo (title, description, keyword, favicon, thumbnail) và ghi vào page.settings.seo kèm mirroring Open Graph; hỗ…"
36
- },
37
- {
38
- "v": "1.26.1",
39
- "d": "26/06/2026",
40
- "type": "Fixed",
41
- "en": "list_products now correctly unwraps the { products, total_product } API response shape, returning a product array and accurate total instead of…",
42
- "vi": "list_products nay giải nén đúng kiểu phản hồi { products, total_product } từ API, trả về mảng sản phẩm và tổng chính xác thay vì trả thẳng đối tượng…"
43
43
  }
44
44
  ]
package/dist/guides.js CHANGED
@@ -22,38 +22,59 @@ ANY method → /api/v1/{site_id}/_functions/{FunctionName}
22
22
  The caller receives your return value as data.result. From the storefront, the
23
23
  webcake-fn client (api.method_FunctionName(params)) returns that result directly.
24
24
 
25
- ## webcake-data — Database SDK (built-in)
25
+ ## webcake-data — Database SDK (built-in). This is the MAIN way to read/write a site's collections.
26
26
  import { DBConnection } from 'webcake-data';
27
27
  const db = new DBConnection(); // auto-uses the sandbox global site/token
28
- const Model = db.model('collection_name');
29
-
30
- ### Model CRUD (all async unless noted)
31
- - Model.create(doc) created doc
32
- - Model.insertMany([doc, ...]) array
33
- - Model.find(filter) → QueryBuilder (NOT a promise — chain then .exec()/await)
34
- - Model.findOne(filter, { select, sort, populate })
35
- - Model.findById(id, { select, populate })
36
- - Model.updateOne(filter, update) → { acknowledged, matchedCount, modifiedCount }
37
- - Model.findByIdAndUpdate(id, update, { new: true })
38
- - Model.findOneAndUpdate(filter, update)
39
- - Model.updateMany(filter, update)
40
- - Model.deleteOne(filter) → { acknowledged, deletedCount }
41
- - Model.findByIdAndDelete(id) / Model.findOneAndDelete(filter)
42
- - Model.deleteMany(filter)
43
- - Model.countDocuments(filter) → number
44
- - Model.exists(filter) → boolean
45
-
46
- ### QueryBuilder (from Model.find())
47
- Chain then terminate with .exec() (or just await the chain):
48
- Model.find().where('age').gte(25).lte(40).in('role',['admin']).like('email','%@ex.com')
49
- .sort({ age:-1 }).limit(20).skip(10).select('name email').exec()
50
- Operators: where, eq, ne, gt, gte, lt, lte, in, nin, between, like, sort, limit, skip, select, populate.
51
-
52
- ### Populate (join another collection)
53
- Model.find().populate({
54
- field:'posts', table:'posts', referenceField:'user_id',
55
- select:'title', where:{}, sort:{ created_at:-1 }, limit:5, skip:0, justOne:false
56
- }).exec()
28
+ const Model = db.model('collection_name'); // 'collection_name' = the table_name of a collection
29
+
30
+ The API is Mongoose-DOCUMENT style: filters are plain MongoDB-style OBJECTS, chains are
31
+ DIRECTLY AWAITABLE (NO .exec()), and you select fields with an ARRAY. Every document has an
32
+ \`id\` (UUID string) plus \`inserted_at\` / \`updated_at\`.
33
+
34
+ ### Read
35
+ - await Model.findOne(filter) → one doc (or null)
36
+ - await Model.findOne(filter, { populate:{ field, select:[...] } })
37
+ - await Model.find(filter).select([...]).populate({...}).sort({...}).limit(n) → array (await the CHAIN; no .exec())
38
+ - await Model.countDocuments(filter) → number
39
+
40
+ filter is a MongoDB-style object. Operators go INSIDE the field value:
41
+ { thanh_vien: userId } // equals
42
+ { status: { $in: [0, 1, 2] } } // in a list
43
+ { trang_thai_tg: { $ne: 3 } } // not equal
44
+ // also $nin, $gt, $gte, $lt, $lte, $exists — the usual MongoDB query operators.
45
+
46
+ Chain methods on a Model.find(filter):
47
+ .select(["id", "name", "diem_so"]) // ARRAY of field names to return
48
+ .sort({ inserted_at: -1 }) // 1 ascending, -1 descending (multi-key ok)
49
+ .limit(20) .skip(0)
50
+ .populate({ field: "thanh_vien", select: ["id", "name", "avatar"] })
51
+
52
+ ### Populate (resolve a reference field)
53
+ A reference field stores the related row's \`id\` (a string). \`.populate({ field, select:[...] })\`
54
+ replaces it with the related OBJECT (only the selected fields). After populate, read it as an
55
+ object; before/without populate it's the raw id string. Handle both:
56
+ const id = typeof row.thanh_vien === "object" ? row.thanh_vien.id : row.thanh_vien;
57
+ populate also works as a 2nd-arg option on findOne: Model.findOne(filter, { populate:{ field, select:[...] } }).
58
+
59
+ ### Write
60
+ - await Model.create(doc) → created doc (use doc.id afterwards)
61
+ - await Model.findOneAndUpdate(filter, update, { new: true }) → the UPDATED doc ({ new:true } = return the new version)
62
+ - await Model.updateOne(filter, update) → write result
63
+ - await Model.updateMany(filter, update) → bulk update matching rows
64
+ - await Model.deleteMany(filter) → delete matching rows
65
+ (update is a plain object of the fields to set, e.g. { status: 1, ty_le_thang: 75 }.)
66
+
67
+ ### Real example pattern (from a production function)
68
+ const Members = db.model("thanh_vien_ps");
69
+ const rows = await Members
70
+ .find({ playspace: psId, status: { $in: [0, 1, 2] } })
71
+ .select(["id", "thanh_vien", "tien_con_lai", "status"])
72
+ .populate({ field: "thanh_vien", select: ["id", "name", "avatar"] })
73
+ .sort({ status: 1, inserted_at: 1 })
74
+ .limit(50);
75
+ const count = await Members.countDocuments({ playspace: psId, status: { $in: [0, 1, 2] } });
76
+ const created = await Members.create({ thanh_vien: userId, playspace: psId, status: 0 });
77
+ await Members.updateOne({ id: created.id }, { tien_con_lai: 100000 });
57
78
 
58
79
  ## Built-in @webcake/* modules (first arg is always request; they auth via global.token)
59
80
  Thin wrappers over the backend's /cms_function/{site_id}/... endpoints. Pass request so
@@ -136,18 +157,36 @@ Runs sandboxed: ~4 MB memory, ~30 s timeout. The return value MUST be JSON-seria
136
157
  ## Cron jobs (jobs_config JSON)
137
158
  { "jobs": [{ "functionLocation": "backend/http_function", "functionName": "myFunc", "executionConfig": { "cronExpression": "0 2 * * *" } }] }
138
159
 
139
- ## Example
160
+ ## Example (real-world shape)
161
+ Declare the models + db ONCE at module top, then one export per endpoint. Read the caller from
162
+ request.customer?.id (auth) and the inputs from request.params. Return a plain JSON object —
163
+ the convention is { mess: "OK", ...data } on success or { mess: "ERROR_CODE" } on failure.
140
164
  import { DBConnection } from 'webcake-data';
141
165
  import { findCustomerById } from '@webcake/customer';
142
- export const post_RecentOrders = async (request) => {
143
- const { params, customer } = request;
144
- const db = new DBConnection();
145
- const orders = await db.model('orders')
146
- .find().where('customer_id').eq(customer.id || params.customer_id)
147
- .sort({ created_at:-1 }).limit(10)
148
- .populate({ field:'items', table:'order_items', referenceField:'order_id', limit:50 })
149
- .exec();
150
- return { count: orders.length, orders };
166
+
167
+ const db = new DBConnection();
168
+ const Members = db.model('thanh_vien_ps');
169
+
170
+ export const post_MyMembers = async (request) => {
171
+ const userId = request.customer?.id ?? ""; // the logged-in storefront customer
172
+ if (!userId) return { mess: "NO_ACCOUNT_CALL" };
173
+ const { playspace = "" } = request.params || {}; // POST body params
174
+ try {
175
+ const rows = await Members
176
+ .find({ playspace, status: { $in: [0, 1, 2] } })
177
+ .select(["id", "thanh_vien", "tien_con_lai", "status"])
178
+ .populate({ field: "thanh_vien", select: ["id", "name", "avatar"] })
179
+ .sort({ status: 1, inserted_at: 1 })
180
+ .limit(50);
181
+ const data = rows.map((row) => {
182
+ const m = row.thanh_vien || {}; // populated object
183
+ return { thanh_vien_ps: row.id, ten: m.name || "", tien: Number(row.tien_con_lai) || 0 };
184
+ });
185
+ return { mess: "OK", data };
186
+ } catch (err) {
187
+ console.error(err?.message || err);
188
+ return { mess: "SYSTEM_ERROR" };
189
+ }
151
190
  };
152
191
  `;
153
192
  export const CUSTOM_CODE_GUIDE = `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webcake-storefront-mcp",
3
- "version": "1.31.0",
3
+ "version": "1.31.1",
4
4
  "description": "MCP server for the WebCake/StoreCake storefront builder — page CRUD, page authoring, products, orders, and more",
5
5
  "mcpName": "io.github.vuluu2k/webcake-storefront-mcp",
6
6
  "license": "MIT",