@cloudbase/database 0.12.0 → 0.12.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.
@@ -4,6 +4,6 @@ import Aggregation from './aggregate';
4
4
  export declare class CollectionReference extends Query {
5
5
  get name(): string;
6
6
  doc(docID?: string | number): DocumentReference;
7
- add(data: Object, callback?: any): Promise<any>;
7
+ add(data: Object | Object[], callback?: any): Promise<any>;
8
8
  aggregate(): Aggregation;
9
9
  }
@@ -38,24 +38,34 @@ var DocumentReference = (function () {
38
38
  }
39
39
  DocumentReference.prototype.create = function (data, callback) {
40
40
  callback = callback || (0, util_1.createPromiseCallback)();
41
+ var isBatch = Array.isArray(data);
41
42
  var params = {
42
43
  collectionName: this._coll,
43
- data: (0, datatype_1.serialize)(data)
44
+ data: isBatch ? data.map(function (item) { return (0, datatype_1.serialize)(item); }) : (0, datatype_1.serialize)(data)
44
45
  };
45
- if (this.id) {
46
+ if (!isBatch && this.id) {
46
47
  params['_id'] = this.id;
47
48
  }
48
49
  this.request
49
50
  .send('database.addDocument', params)
50
51
  .then(function (res) {
52
+ var _a;
51
53
  if (res.code) {
52
54
  callback(0, res);
53
55
  }
54
56
  else {
55
- callback(0, {
56
- id: res.data._id,
57
- requestId: res.requestId
58
- });
57
+ if (isBatch) {
58
+ callback(0, {
59
+ ids: ((_a = res.data) === null || _a === void 0 ? void 0 : _a.insertedIds) || [],
60
+ requestId: res.requestId
61
+ });
62
+ }
63
+ else {
64
+ callback(0, {
65
+ id: res.data._id,
66
+ requestId: res.requestId
67
+ });
68
+ }
59
69
  }
60
70
  })
61
71
  .catch(function (err) {
@@ -4,6 +4,6 @@ import Aggregation from './aggregate';
4
4
  export declare class CollectionReference extends Query {
5
5
  get name(): string;
6
6
  doc(docID?: string | number): DocumentReference;
7
- add(data: Object, callback?: any): Promise<any>;
7
+ add(data: Object | Object[], callback?: any): Promise<any>;
8
8
  aggregate(): Aggregation;
9
9
  }
@@ -35,24 +35,34 @@ var DocumentReference = (function () {
35
35
  }
36
36
  DocumentReference.prototype.create = function (data, callback) {
37
37
  callback = callback || createPromiseCallback();
38
+ var isBatch = Array.isArray(data);
38
39
  var params = {
39
40
  collectionName: this._coll,
40
- data: serialize(data)
41
+ data: isBatch ? data.map(function (item) { return serialize(item); }) : serialize(data)
41
42
  };
42
- if (this.id) {
43
+ if (!isBatch && this.id) {
43
44
  params['_id'] = this.id;
44
45
  }
45
46
  this.request
46
47
  .send('database.addDocument', params)
47
48
  .then(function (res) {
49
+ var _a;
48
50
  if (res.code) {
49
51
  callback(0, res);
50
52
  }
51
53
  else {
52
- callback(0, {
53
- id: res.data._id,
54
- requestId: res.requestId
55
- });
54
+ if (isBatch) {
55
+ callback(0, {
56
+ ids: ((_a = res.data) === null || _a === void 0 ? void 0 : _a.insertedIds) || [],
57
+ requestId: res.requestId
58
+ });
59
+ }
60
+ else {
61
+ callback(0, {
62
+ id: res.data._id,
63
+ requestId: res.requestId
64
+ });
65
+ }
56
66
  }
57
67
  })
58
68
  .catch(function (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/database",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "description": "database for (node.)js sdk ",
5
5
  "main": "dist/commonjs/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/collection.ts CHANGED
@@ -42,11 +42,12 @@ export class CollectionReference extends Query {
42
42
  }
43
43
 
44
44
  /**
45
- * 添加一篇文档
45
+ * 添加一篇或多篇文档
46
46
  *
47
- * @param data - 数据
47
+ * @param data - 单个文档数据或文档数据数组
48
+ * @param callback - 回调函数
48
49
  */
49
- add(data: Object, callback?: any): Promise<any> {
50
+ add(data: Object | Object[], callback?: any): Promise<any> {
50
51
  let docRef = new DocumentReference(this._db, this._coll, undefined)
51
52
  return docRef.create(data, callback)
52
53
  }
package/src/document.ts CHANGED
@@ -64,21 +64,22 @@ export class DocumentReference {
64
64
  }
65
65
 
66
66
  /**
67
- * 创建一篇文档
67
+ * 创建一篇或多篇文档
68
68
  *
69
- * @param data - 文档数据
69
+ * @param data - 文档数据,支持单个对象或对象数组
70
70
  * @internal
71
71
  */
72
72
  create(data: any, callback?: any): Promise<any> {
73
73
  callback = callback || createPromiseCallback()
74
74
 
75
+ const isBatch = Array.isArray(data)
76
+
75
77
  let params = {
76
78
  collectionName: this._coll,
77
- // data: Util.encodeDocumentDataForReq(data, false, false)
78
- data: serialize(data)
79
+ data: isBatch ? data.map(item => serialize(item)) : serialize(data)
79
80
  }
80
81
 
81
- if (this.id) {
82
+ if (!isBatch && this.id) {
82
83
  params['_id'] = this.id
83
84
  }
84
85
 
@@ -88,10 +89,17 @@ export class DocumentReference {
88
89
  if (res.code) {
89
90
  callback(0, res)
90
91
  } else {
91
- callback(0, {
92
- id: res.data._id,
93
- requestId: res.requestId
94
- })
92
+ if (isBatch) {
93
+ callback(0, {
94
+ ids: res.data?.insertedIds || [],
95
+ requestId: res.requestId
96
+ })
97
+ } else {
98
+ callback(0, {
99
+ id: res.data._id,
100
+ requestId: res.requestId
101
+ })
102
+ }
95
103
  }
96
104
  })
97
105
  .catch(err => {