@cloudbase/database 1.4.2 → 1.4.3

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.
Files changed (102) hide show
  1. package/dist/commonjs/index.d.ts +2 -0
  2. package/dist/commonjs/utils/utils.d.ts +2 -3
  3. package/dist/esm/ObjectId/index.d.ts +11 -0
  4. package/dist/esm/ObjectId/index.js +17 -0
  5. package/dist/esm/aggregate.d.ts +29 -0
  6. package/dist/esm/aggregate.js +121 -0
  7. package/dist/esm/collection.d.ts +17 -0
  8. package/dist/esm/collection.js +69 -0
  9. package/dist/esm/command.d.ts +175 -0
  10. package/dist/esm/command.js +281 -0
  11. package/dist/esm/commands/logic.d.ts +24 -0
  12. package/dist/esm/commands/logic.js +74 -0
  13. package/dist/esm/commands/query.d.ts +73 -0
  14. package/dist/esm/commands/query.js +141 -0
  15. package/dist/esm/commands/update.d.ts +29 -0
  16. package/dist/esm/commands/update.js +44 -0
  17. package/dist/esm/config/app.config.d.ts +6 -0
  18. package/dist/esm/config/app.config.js +5 -0
  19. package/dist/esm/config/error.config.d.ts +3 -0
  20. package/dist/esm/config/error.config.js +13 -0
  21. package/dist/esm/const/code.d.ts +34 -0
  22. package/dist/esm/const/code.js +34 -0
  23. package/dist/esm/constant.d.ts +54 -0
  24. package/dist/esm/constant.js +67 -0
  25. package/dist/esm/document.d.ts +14 -0
  26. package/dist/esm/document.js +214 -0
  27. package/dist/esm/geo/index.d.ts +6 -0
  28. package/dist/esm/geo/index.js +6 -0
  29. package/dist/esm/geo/interface.d.ts +24 -0
  30. package/dist/esm/geo/interface.js +0 -0
  31. package/dist/esm/geo/lineString.d.ts +19 -0
  32. package/dist/esm/geo/lineString.js +54 -0
  33. package/dist/esm/geo/multiLineString.d.ts +18 -0
  34. package/dist/esm/geo/multiLineString.js +53 -0
  35. package/dist/esm/geo/multiPoint.d.ts +18 -0
  36. package/dist/esm/geo/multiPoint.js +47 -0
  37. package/dist/esm/geo/multiPolygon.d.ts +18 -0
  38. package/dist/esm/geo/multiPolygon.js +59 -0
  39. package/dist/esm/geo/point.d.ts +19 -0
  40. package/dist/esm/geo/point.js +40 -0
  41. package/dist/esm/geo/polygon.d.ts +19 -0
  42. package/dist/esm/geo/polygon.js +67 -0
  43. package/dist/esm/helper/symbol.d.ts +15 -0
  44. package/dist/esm/helper/symbol.js +15 -0
  45. package/dist/esm/index.d.ts +52 -0
  46. package/dist/esm/index.js +35 -0
  47. package/dist/esm/operator-map.d.ts +4 -0
  48. package/dist/esm/operator-map.js +20 -0
  49. package/dist/esm/query.d.ts +38 -0
  50. package/dist/esm/query.js +246 -0
  51. package/dist/esm/realtime/error.d.ts +7 -0
  52. package/dist/esm/realtime/error.js +8 -0
  53. package/dist/esm/realtime/listener.d.ts +14 -0
  54. package/dist/esm/realtime/listener.js +14 -0
  55. package/dist/esm/realtime/message.d.ts +3 -0
  56. package/dist/esm/realtime/message.js +6 -0
  57. package/dist/esm/realtime/snapshot.d.ts +16 -0
  58. package/dist/esm/realtime/snapshot.js +39 -0
  59. package/dist/esm/realtime/virtual-websocket-client.d.ts +63 -0
  60. package/dist/esm/realtime/virtual-websocket-client.js +733 -0
  61. package/dist/esm/realtime/websocket-client.d.ts +78 -0
  62. package/dist/esm/realtime/websocket-client.js +564 -0
  63. package/dist/esm/realtime/ws-event.d.ts +126 -0
  64. package/dist/esm/realtime/ws-event.js +137 -0
  65. package/dist/esm/regexp/index.d.ts +18 -0
  66. package/dist/esm/regexp/index.js +26 -0
  67. package/dist/esm/serializer/common.d.ts +11 -0
  68. package/dist/esm/serializer/common.js +82 -0
  69. package/dist/esm/serializer/datatype.d.ts +7 -0
  70. package/dist/esm/serializer/datatype.js +95 -0
  71. package/dist/esm/serializer/query.d.ts +8 -0
  72. package/dist/esm/serializer/query.js +251 -0
  73. package/dist/esm/serializer/update.d.ts +16 -0
  74. package/dist/esm/serializer/update.js +139 -0
  75. package/dist/esm/serverDate/index.d.ts +13 -0
  76. package/dist/esm/serverDate/index.js +19 -0
  77. package/dist/esm/transaction/index.d.ts +30 -0
  78. package/dist/esm/transaction/index.js +100 -0
  79. package/dist/esm/transaction.d.ts +42 -0
  80. package/dist/esm/transaction.js +121 -0
  81. package/dist/esm/typings/index.d.ts +45 -0
  82. package/dist/esm/typings/index.js +0 -0
  83. package/dist/esm/typings/realtime.d.ts +219 -0
  84. package/dist/esm/typings/realtime.js +0 -0
  85. package/dist/esm/util.d.ts +9 -0
  86. package/dist/esm/util.js +98 -0
  87. package/dist/esm/utils/error.d.ts +37 -0
  88. package/dist/esm/utils/error.js +49 -0
  89. package/dist/esm/utils/msg.d.ts +0 -0
  90. package/dist/esm/utils/msg.js +0 -0
  91. package/dist/esm/utils/symbol.d.ts +8 -0
  92. package/dist/esm/utils/symbol.js +36 -0
  93. package/dist/esm/utils/type.d.ts +13 -0
  94. package/dist/esm/utils/type.js +20 -0
  95. package/dist/esm/utils/utils.d.ts +19 -0
  96. package/dist/esm/utils/utils.js +55 -0
  97. package/dist/esm/validate.d.ts +1 -0
  98. package/dist/esm/validate.js +152 -0
  99. package/package.json +3 -2
  100. package/src/index.ts +2 -0
  101. package/.trunk/configs/.markdownlint.yaml +0 -10
  102. package/.trunk/trunk.yaml +0 -26
@@ -0,0 +1,5 @@
1
+ export default {
2
+ db_doc_size_limit: 512 * 1024,
3
+ db_realtime_ping_interval: 15 * 1000,
4
+ db_realtime_pong_wait_timeout: 15 * 1000,
5
+ };
@@ -0,0 +1,3 @@
1
+ export declare const ERR_CODE: {
2
+ [key: string]: string | number;
3
+ };
@@ -0,0 +1,13 @@
1
+ export const ERR_CODE = {
2
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
3
+ SDK_DATABASE_REALTIME_LISTENER_INIT_WATCH_FAIL: 'SDK_DATABASE_REALTIME_LISTENER_INIT_WATCH_FAIL',
4
+ SDK_DATABASE_REALTIME_LISTENER_RECONNECT_WATCH_FAIL: 'SDK_DATABASE_REALTIME_LISTENER_RECONNECT_WATCH_FAIL',
5
+ SDK_DATABASE_REALTIME_LISTENER_REBUILD_WATCH_FAIL: 'SDK_DATABASE_REALTIME_LISTENER_REBUILD_WATCH_FAIL',
6
+ SDK_DATABASE_REALTIME_LISTENER_CLOSE_WATCH_FAIL: 'SDK_DATABASE_REALTIME_LISTENER_CLOSE_WATCH_FAIL',
7
+ SDK_DATABASE_REALTIME_LISTENER_SERVER_ERROR_MSG: 'SDK_DATABASE_REALTIME_LISTENER_SERVER_ERROR_MSG',
8
+ SDK_DATABASE_REALTIME_LISTENER_RECEIVE_INVALID_SERVER_DATA: 'SDK_DATABASE_REALTIME_LISTENER_RECEIVE_INVALID_SERVER_DATA',
9
+ SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_ERROR: 'SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_ERROR',
10
+ SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_CLOSED: 'SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_CLOSED',
11
+ SDK_DATABASE_REALTIME_LISTENER_CHECK_LAST_FAIL: 'SDK_DATABASE_REALTIME_LISTENER_CHECK_LAST_FAIL',
12
+ SDK_DATABASE_REALTIME_LISTENER_UNEXPECTED_FATAL_ERROR: 'SDK_DATABASE_REALTIME_LISTENER_UNEXPECTED_FATAL_ERROR'
13
+ };
@@ -0,0 +1,34 @@
1
+ export declare const ERRORS: {
2
+ CREATE_WATCH_NET_ERROR: {
3
+ code: string;
4
+ message: string;
5
+ };
6
+ CREATE_WACTH_EXCEED_ERROR: {
7
+ code: string;
8
+ message: string;
9
+ };
10
+ CREATE_WATCH_SERVER_ERROR: {
11
+ code: string;
12
+ message: string;
13
+ };
14
+ CONN_ERROR: {
15
+ code: string;
16
+ message: string;
17
+ };
18
+ INVALID_PARAM: {
19
+ code: string;
20
+ message: string;
21
+ };
22
+ INSERT_DOC_FAIL: {
23
+ code: string;
24
+ message: string;
25
+ };
26
+ DATABASE_TRANSACTION_CONFLICT: {
27
+ code: string;
28
+ message: string;
29
+ };
30
+ DATABASE_REQUEST_FAILED: {
31
+ code: string;
32
+ message: string;
33
+ };
34
+ };
@@ -0,0 +1,34 @@
1
+ export const ERRORS = {
2
+ CREATE_WATCH_NET_ERROR: {
3
+ code: 'CREATE_WATCH_NET_ERROR',
4
+ message: 'create watch network error'
5
+ },
6
+ CREATE_WACTH_EXCEED_ERROR: {
7
+ code: 'CREATE_WACTH_EXCEED_ERROR',
8
+ message: 'maximum connections exceed'
9
+ },
10
+ CREATE_WATCH_SERVER_ERROR: {
11
+ code: 'CREATE_WATCH_SERVER_ERROR',
12
+ message: 'create watch server error'
13
+ },
14
+ CONN_ERROR: {
15
+ code: 'CONN_ERROR',
16
+ message: 'connection error'
17
+ },
18
+ INVALID_PARAM: {
19
+ code: 'INVALID_PARAM',
20
+ message: 'Invalid request param'
21
+ },
22
+ INSERT_DOC_FAIL: {
23
+ code: 'INSERT_DOC_FAIL',
24
+ message: 'insert document failed'
25
+ },
26
+ DATABASE_TRANSACTION_CONFLICT: {
27
+ code: 'DATABASE_TRANSACTION_CONFLICT',
28
+ message: 'database transaction conflict'
29
+ },
30
+ DATABASE_REQUEST_FAILED: {
31
+ code: 'DATABASE_REQUEST_FAILED',
32
+ message: 'database request failed'
33
+ }
34
+ };
@@ -0,0 +1,54 @@
1
+ declare enum ErrorCode {
2
+ DocIDError = "\u6587\u6863ID\u4E0D\u5408\u6CD5",
3
+ CollNameError = "\u96C6\u5408\u540D\u79F0\u4E0D\u5408\u6CD5",
4
+ OpStrError = "\u64CD\u4F5C\u7B26\u4E0D\u5408\u6CD5",
5
+ DirectionError = "\u6392\u5E8F\u5B57\u7B26\u4E0D\u5408\u6CD5",
6
+ IntergerError = "must be integer",
7
+ BooleanError = "must be boolean",
8
+ ArrayError = "must be array",
9
+ QueryParamTypeError = "\u67E5\u8BE2\u53C2\u6570\u5FC5\u987B\u4E3A\u5BF9\u8C61",
10
+ QueryParamValueError = "\u67E5\u8BE2\u53C2\u6570\u5BF9\u8C61\u503C\u4E0D\u80FD\u5747\u4E3Aundefined",
11
+ CentersPhereError = "centersPhere\u7ED3\u6784\u4E0D\u5408\u6CD5"
12
+ }
13
+ declare const FieldType: {
14
+ String: string;
15
+ Number: string;
16
+ Object: string;
17
+ Array: string;
18
+ Boolean: string;
19
+ Null: string;
20
+ GeoPoint: string;
21
+ GeoLineString: string;
22
+ GeoPolygon: string;
23
+ GeoMultiPoint: string;
24
+ GeoMultiLineString: string;
25
+ GeoMultiPolygon: string;
26
+ Date: string;
27
+ Command: string;
28
+ ServerDate: string;
29
+ BsonDate: string;
30
+ };
31
+ declare type OrderByDirection = 'desc' | 'asc';
32
+ declare const OrderDirectionList: string[];
33
+ declare type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>';
34
+ declare const WhereFilterOpList: string[];
35
+ declare enum Opeartor {
36
+ lt = "<",
37
+ gt = ">",
38
+ lte = "<=",
39
+ gte = ">=",
40
+ eq = "=="
41
+ }
42
+ declare const OperatorMap: {
43
+ [Opeartor.eq]: string;
44
+ [Opeartor.lt]: string;
45
+ [Opeartor.lte]: string;
46
+ [Opeartor.gt]: string;
47
+ [Opeartor.gte]: string;
48
+ };
49
+ declare const UpdateOperatorList: string[];
50
+ declare enum QueryType {
51
+ WHERE = "WHERE",
52
+ DOC = "DOC"
53
+ }
54
+ export { ErrorCode, FieldType, WhereFilterOp, WhereFilterOpList, Opeartor, OperatorMap, OrderByDirection, OrderDirectionList, UpdateOperatorList, QueryType };
@@ -0,0 +1,67 @@
1
+ var ErrorCode;
2
+ (function (ErrorCode) {
3
+ ErrorCode["DocIDError"] = "\u6587\u6863ID\u4E0D\u5408\u6CD5";
4
+ ErrorCode["CollNameError"] = "\u96C6\u5408\u540D\u79F0\u4E0D\u5408\u6CD5";
5
+ ErrorCode["OpStrError"] = "\u64CD\u4F5C\u7B26\u4E0D\u5408\u6CD5";
6
+ ErrorCode["DirectionError"] = "\u6392\u5E8F\u5B57\u7B26\u4E0D\u5408\u6CD5";
7
+ ErrorCode["IntergerError"] = "must be integer";
8
+ ErrorCode["BooleanError"] = "must be boolean";
9
+ ErrorCode["ArrayError"] = "must be array";
10
+ ErrorCode["QueryParamTypeError"] = "\u67E5\u8BE2\u53C2\u6570\u5FC5\u987B\u4E3A\u5BF9\u8C61";
11
+ ErrorCode["QueryParamValueError"] = "\u67E5\u8BE2\u53C2\u6570\u5BF9\u8C61\u503C\u4E0D\u80FD\u5747\u4E3Aundefined";
12
+ ErrorCode["CentersPhereError"] = "centersPhere\u7ED3\u6784\u4E0D\u5408\u6CD5";
13
+ })(ErrorCode || (ErrorCode = {}));
14
+ const FieldType = {
15
+ String: 'String',
16
+ Number: 'Number',
17
+ Object: 'Object',
18
+ Array: 'Array',
19
+ Boolean: 'Boolean',
20
+ Null: 'Null',
21
+ GeoPoint: 'GeoPoint',
22
+ GeoLineString: 'GeoLineString',
23
+ GeoPolygon: 'GeoPolygon',
24
+ GeoMultiPoint: 'GeoMultiPoint',
25
+ GeoMultiLineString: 'GeoMultiLineString',
26
+ GeoMultiPolygon: 'GeoMultiPolygon',
27
+ Date: 'Date',
28
+ Command: 'Command',
29
+ ServerDate: 'ServerDate',
30
+ BsonDate: 'BsonDate'
31
+ };
32
+ const OrderDirectionList = ['desc', 'asc'];
33
+ const WhereFilterOpList = ['<', '<=', '==', '>=', '>'];
34
+ var Opeartor;
35
+ (function (Opeartor) {
36
+ Opeartor["lt"] = "<";
37
+ Opeartor["gt"] = ">";
38
+ Opeartor["lte"] = "<=";
39
+ Opeartor["gte"] = ">=";
40
+ Opeartor["eq"] = "==";
41
+ })(Opeartor || (Opeartor = {}));
42
+ const OperatorMap = {
43
+ [Opeartor.eq]: '$eq',
44
+ [Opeartor.lt]: '$lt',
45
+ [Opeartor.lte]: '$lte',
46
+ [Opeartor.gt]: '$gt',
47
+ [Opeartor.gte]: '$gte'
48
+ };
49
+ const UpdateOperatorList = [
50
+ '$set',
51
+ '$inc',
52
+ '$mul',
53
+ '$unset',
54
+ '$push',
55
+ '$pop',
56
+ '$unshift',
57
+ '$shift',
58
+ '$currentDate',
59
+ '$each',
60
+ '$position'
61
+ ];
62
+ var QueryType;
63
+ (function (QueryType) {
64
+ QueryType["WHERE"] = "WHERE";
65
+ QueryType["DOC"] = "DOC";
66
+ })(QueryType || (QueryType = {}));
67
+ export { ErrorCode, FieldType, WhereFilterOpList, Opeartor, OperatorMap, OrderDirectionList, UpdateOperatorList, QueryType };
@@ -0,0 +1,14 @@
1
+ import { IWatchOptions, DBRealtimeListener } from './typings/index';
2
+ export declare class DocumentReference {
3
+ readonly id: string | number;
4
+ readonly _transactionId: string;
5
+ readonly projection: Object;
6
+ private _apiOptions;
7
+ set(data: Object): Promise<any>;
8
+ update(data: Object): Promise<any>;
9
+ delete(): Promise<any>;
10
+ remove(): Promise<any>;
11
+ get(): Promise<any>;
12
+ field(projection: Object): DocumentReference;
13
+ watch: (options: IWatchOptions) => DBRealtimeListener;
14
+ }
@@ -0,0 +1,214 @@
1
+ import { Db } from './index';
2
+ import { Util } from './util';
3
+ import { UpdateSerializer } from './serializer/update';
4
+ import { serialize } from './serializer/datatype';
5
+ import { UpdateCommand } from './commands/update';
6
+ import { RealtimeWebSocketClient } from './realtime/websocket-client';
7
+ import { QueryType } from './constant';
8
+ import { getReqOpts, stringifyByEJSON, processReturn } from './utils/utils';
9
+ import { ERRORS } from './const/code';
10
+ import { EJSON } from 'bson';
11
+ export class DocumentReference {
12
+ constructor(db, coll, apiOptions, docID, transactionId) {
13
+ this.watch = (options) => {
14
+ if (!Db.ws) {
15
+ Db.ws = new RealtimeWebSocketClient({
16
+ context: {
17
+ appConfig: {
18
+ docSizeLimit: 1000,
19
+ realtimePingInterval: 10000,
20
+ realtimePongWaitTimeout: 5000,
21
+ request: this.request
22
+ }
23
+ }
24
+ });
25
+ }
26
+ return Db.ws.watch(Object.assign(Object.assign({}, options), { envId: this._db.config.env, collectionName: this._coll, query: JSON.stringify({
27
+ _id: this.id
28
+ }) }));
29
+ };
30
+ this._db = db;
31
+ this._coll = coll;
32
+ this.id = docID;
33
+ this._transactionId = transactionId;
34
+ this.request = new Db.reqClass(this._db.config);
35
+ this._apiOptions = apiOptions;
36
+ }
37
+ async create(data) {
38
+ if (this.id) {
39
+ data['_id'] = this.id;
40
+ }
41
+ let params = {
42
+ collectionName: this._coll,
43
+ data: [stringifyByEJSON(serialize(data))],
44
+ transactionId: this._transactionId
45
+ };
46
+ const res = await this.request.send('database.insertDocument', params, getReqOpts(this._apiOptions));
47
+ if (res.code) {
48
+ return res;
49
+ }
50
+ if (this._transactionId) {
51
+ return {
52
+ inserted: 1,
53
+ ok: 1,
54
+ id: res.data.insertedIds[0],
55
+ requestId: res.requestId
56
+ };
57
+ }
58
+ return {
59
+ id: res.data.insertedIds[0],
60
+ requestId: res.requestId
61
+ };
62
+ }
63
+ async set(data) {
64
+ if (!this.id) {
65
+ return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: 'docId不能为空' }));
66
+ }
67
+ if (!data || typeof data !== 'object') {
68
+ return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
69
+ }
70
+ if (data.hasOwnProperty('_id')) {
71
+ return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
72
+ }
73
+ let hasOperator = false;
74
+ const checkMixed = objs => {
75
+ if (typeof objs === 'object') {
76
+ for (let key in objs) {
77
+ if (objs[key] instanceof UpdateCommand) {
78
+ hasOperator = true;
79
+ }
80
+ else if (typeof objs[key] === 'object') {
81
+ checkMixed(objs[key]);
82
+ }
83
+ }
84
+ }
85
+ };
86
+ checkMixed(data);
87
+ if (hasOperator) {
88
+ return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.DATABASE_REQUEST_FAILED), { message: 'update operator complicit' }));
89
+ }
90
+ let param = {
91
+ collectionName: this._coll,
92
+ queryType: QueryType.DOC,
93
+ data: stringifyByEJSON(serialize(data)),
94
+ transactionId: this._transactionId,
95
+ multi: false,
96
+ merge: false,
97
+ upsert: true
98
+ };
99
+ if (this.id) {
100
+ param['query'] = stringifyByEJSON({ _id: this.id });
101
+ }
102
+ const res = await this.request.send('database.modifyDocument', param, getReqOpts(this._apiOptions));
103
+ if (res.code) {
104
+ return res;
105
+ }
106
+ if (this._transactionId) {
107
+ return {
108
+ updated: res.data.updated,
109
+ upserted: [{ _id: res.data.upsert_id }],
110
+ requestId: res.requestId
111
+ };
112
+ }
113
+ return {
114
+ updated: res.data.updated,
115
+ upsertedId: res.data.upsert_id,
116
+ requestId: res.requestId
117
+ };
118
+ }
119
+ async update(data) {
120
+ if (!data || typeof data !== 'object') {
121
+ return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
122
+ }
123
+ if (data.hasOwnProperty('_id')) {
124
+ return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
125
+ }
126
+ const query = stringifyByEJSON({ _id: this.id });
127
+ const param = {
128
+ collectionName: this._coll,
129
+ transactionId: this._transactionId,
130
+ data: UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false),
131
+ query,
132
+ queryType: QueryType.DOC,
133
+ multi: false,
134
+ merge: true,
135
+ upsert: false
136
+ };
137
+ const res = await this.request.send('database.modifyDocument', param, getReqOpts(this._apiOptions));
138
+ if (res.code) {
139
+ return res;
140
+ }
141
+ return {
142
+ updated: res.data.updated,
143
+ requestId: res.requestId
144
+ };
145
+ }
146
+ async delete() {
147
+ return this.remove();
148
+ }
149
+ async remove() {
150
+ const query = stringifyByEJSON({ _id: this.id });
151
+ const param = {
152
+ collectionName: this._coll,
153
+ transactionId: this._transactionId,
154
+ query: query,
155
+ queryType: QueryType.DOC,
156
+ multi: false
157
+ };
158
+ const res = await this.request.send('database.removeDocument', param, getReqOpts(this._apiOptions));
159
+ if (res.code) {
160
+ return res;
161
+ }
162
+ return {
163
+ deleted: res.data.deleted,
164
+ requestId: res.requestId
165
+ };
166
+ }
167
+ async get() {
168
+ const query = stringifyByEJSON({ _id: this.id });
169
+ const { projection } = this._apiOptions;
170
+ const param = {
171
+ collectionName: this._coll,
172
+ query,
173
+ transactionId: this._transactionId,
174
+ queryType: QueryType.DOC,
175
+ multi: false
176
+ };
177
+ if (projection) {
178
+ param.projection = stringifyByEJSON(projection);
179
+ }
180
+ const res = await this.request.send('database.getDocument', param, getReqOpts(this._apiOptions));
181
+ if (res.code) {
182
+ return res;
183
+ }
184
+ const list = res.data.list.map(item => EJSON.parse(item));
185
+ const documents = Util.formatResDocumentData(list);
186
+ if (this._transactionId) {
187
+ return {
188
+ data: documents[0] || null,
189
+ requestId: res.requestId
190
+ };
191
+ }
192
+ return {
193
+ data: documents,
194
+ requestId: res.requestId
195
+ };
196
+ }
197
+ field(projection) {
198
+ let transformProjection = {};
199
+ for (let k in projection) {
200
+ if (typeof projection[k] === 'boolean') {
201
+ transformProjection[k] = projection[k] === true ? 1 : 0;
202
+ }
203
+ if (typeof projection[k] === 'number') {
204
+ transformProjection[k] = projection[k] > 0 ? 1 : 0;
205
+ }
206
+ if (typeof projection[k] === 'object') {
207
+ transformProjection[k] = projection[k];
208
+ }
209
+ }
210
+ let newApiOption = Object.assign({}, this._apiOptions);
211
+ newApiOption.projection = transformProjection;
212
+ return new DocumentReference(this._db, this._coll, newApiOption, this.id, this._transactionId);
213
+ }
214
+ }
@@ -0,0 +1,6 @@
1
+ export * from './point';
2
+ export * from './lineString';
3
+ export * from './polygon';
4
+ export * from './multiPoint';
5
+ export * from './multiLineString';
6
+ export * from './multiPolygon';
@@ -0,0 +1,6 @@
1
+ export * from './point';
2
+ export * from './lineString';
3
+ export * from './polygon';
4
+ export * from './multiPoint';
5
+ export * from './multiLineString';
6
+ export * from './multiPolygon';
@@ -0,0 +1,24 @@
1
+ export interface ISerializedPoint {
2
+ type: string;
3
+ coordinates: [number, number];
4
+ }
5
+ export interface ISerializedLineString {
6
+ type: string;
7
+ coordinates: [number, number][];
8
+ }
9
+ export interface ISerializedPolygon {
10
+ type: string;
11
+ coordinates: [number, number][][];
12
+ }
13
+ export interface ISerializedMultiPoint {
14
+ type: string;
15
+ coordinates: [number, number][];
16
+ }
17
+ export interface ISerializedMultiLineString {
18
+ type: string;
19
+ coordinates: [number, number][][];
20
+ }
21
+ export interface ISerializedMultiPolygon {
22
+ type: string;
23
+ coordinates: [number, number][][][];
24
+ }
File without changes
@@ -0,0 +1,19 @@
1
+ import { Point } from './point';
2
+ import { ISerializedLineString } from './interface';
3
+ export declare class LineString {
4
+ readonly points: Point[];
5
+ constructor(points: Point[]);
6
+ parse(key: any): {
7
+ [x: number]: {
8
+ type: string;
9
+ coordinates: number[][];
10
+ };
11
+ };
12
+ toJSON(): {
13
+ type: string;
14
+ coordinates: number[][];
15
+ };
16
+ static validate(lineString: ISerializedLineString): boolean;
17
+ static isClosed(lineString: LineString): boolean;
18
+ readonly _internalType: import("../utils/symbol").InternalSymbol;
19
+ }
@@ -0,0 +1,54 @@
1
+ import { SYMBOL_GEO_LINE_STRING } from '../helper/symbol';
2
+ import { Point } from './point';
3
+ import { isArray, isNumber } from '../utils/type';
4
+ export class LineString {
5
+ constructor(points) {
6
+ if (!isArray(points)) {
7
+ throw new TypeError(`"points" must be of type Point[]. Received type ${typeof points}`);
8
+ }
9
+ if (points.length < 2) {
10
+ throw new Error('"points" must contain 2 points at least');
11
+ }
12
+ points.forEach(point => {
13
+ if (!(point instanceof Point)) {
14
+ throw new TypeError(`"points" must be of type Point[]. Received type ${typeof point}[]`);
15
+ }
16
+ });
17
+ this.points = points;
18
+ }
19
+ parse(key) {
20
+ return {
21
+ [key]: {
22
+ type: 'LineString',
23
+ coordinates: this.points.map(point => point.toJSON().coordinates)
24
+ }
25
+ };
26
+ }
27
+ toJSON() {
28
+ return {
29
+ type: 'LineString',
30
+ coordinates: this.points.map(point => point.toJSON().coordinates)
31
+ };
32
+ }
33
+ static validate(lineString) {
34
+ if (lineString.type !== 'LineString' || !isArray(lineString.coordinates)) {
35
+ return false;
36
+ }
37
+ for (let point of lineString.coordinates) {
38
+ if (!isNumber(point[0]) || !isNumber(point[1])) {
39
+ return false;
40
+ }
41
+ }
42
+ return true;
43
+ }
44
+ static isClosed(lineString) {
45
+ const firstPoint = lineString.points[0];
46
+ const lastPoint = lineString.points[lineString.points.length - 1];
47
+ if (firstPoint.latitude === lastPoint.latitude && firstPoint.longitude === lastPoint.longitude) {
48
+ return true;
49
+ }
50
+ }
51
+ get _internalType() {
52
+ return SYMBOL_GEO_LINE_STRING;
53
+ }
54
+ }
@@ -0,0 +1,18 @@
1
+ import { LineString } from './lineString';
2
+ import { ISerializedMultiLineString } from './interface';
3
+ export declare class MultiLineString {
4
+ readonly lines: LineString[];
5
+ constructor(lines: LineString[]);
6
+ parse(key: any): {
7
+ [x: number]: {
8
+ type: string;
9
+ coordinates: number[][][];
10
+ };
11
+ };
12
+ toJSON(): {
13
+ type: string;
14
+ coordinates: number[][][];
15
+ };
16
+ static validate(multiLineString: ISerializedMultiLineString): boolean;
17
+ readonly _internalType: import("../utils/symbol").InternalSymbol;
18
+ }
@@ -0,0 +1,53 @@
1
+ import { SYMBOL_GEO_MULTI_LINE_STRING } from '../helper/symbol';
2
+ import { isArray, isNumber } from '../utils/type';
3
+ import { LineString } from './lineString';
4
+ export class MultiLineString {
5
+ constructor(lines) {
6
+ if (!isArray(lines)) {
7
+ throw new TypeError(`"lines" must be of type LineString[]. Received type ${typeof lines}`);
8
+ }
9
+ if (lines.length === 0) {
10
+ throw new Error('Polygon must contain 1 linestring at least');
11
+ }
12
+ lines.forEach(line => {
13
+ if (!(line instanceof LineString)) {
14
+ throw new TypeError(`"lines" must be of type LineString[]. Received type ${typeof line}[]`);
15
+ }
16
+ });
17
+ this.lines = lines;
18
+ }
19
+ parse(key) {
20
+ return {
21
+ [key]: {
22
+ type: 'MultiLineString',
23
+ coordinates: this.lines.map(line => {
24
+ return line.points.map(point => [point.longitude, point.latitude]);
25
+ })
26
+ }
27
+ };
28
+ }
29
+ toJSON() {
30
+ return {
31
+ type: 'MultiLineString',
32
+ coordinates: this.lines.map(line => {
33
+ return line.points.map(point => [point.longitude, point.latitude]);
34
+ })
35
+ };
36
+ }
37
+ static validate(multiLineString) {
38
+ if (multiLineString.type !== 'MultiLineString' || !isArray(multiLineString.coordinates)) {
39
+ return false;
40
+ }
41
+ for (let line of multiLineString.coordinates) {
42
+ for (let point of line) {
43
+ if (!isNumber(point[0]) || !isNumber(point[1])) {
44
+ return false;
45
+ }
46
+ }
47
+ }
48
+ return true;
49
+ }
50
+ get _internalType() {
51
+ return SYMBOL_GEO_MULTI_LINE_STRING;
52
+ }
53
+ }
@@ -0,0 +1,18 @@
1
+ import { Point } from './point';
2
+ import { ISerializedMultiPoint } from './interface';
3
+ export declare class MultiPoint {
4
+ readonly points: Point[];
5
+ constructor(points: Point[]);
6
+ parse(key: any): {
7
+ [x: number]: {
8
+ type: string;
9
+ coordinates: number[][];
10
+ };
11
+ };
12
+ toJSON(): {
13
+ type: string;
14
+ coordinates: number[][];
15
+ };
16
+ static validate(multiPoint: ISerializedMultiPoint): boolean;
17
+ readonly _internalType: import("../utils/symbol").InternalSymbol;
18
+ }