t1y-sdk-js 5.0.0
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.
- package/LICENSE +21 -0
- package/README.md +313 -0
- package/README.zh-CN.md +313 -0
- package/dist/index.d.mts +920 -0
- package/dist/index.d.ts +920 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -0
- package/dist/umd/t1y.min.js +2 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 华易云联(杭州)网络科技有限责任公司
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# t1yOS SDK for JavaScript/TypeScript
|
|
2
|
+
|
|
3
|
+
[中文文档](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
[t1yOS](https://www.t1y.net) Serverless Platform JavaScript/TypeScript SDK — cloud database, metadata, and cloud functions client.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### npm / pnpm / yarn
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install t1y-sdk-js
|
|
13
|
+
# or
|
|
14
|
+
pnpm add t1y-sdk-js
|
|
15
|
+
# or
|
|
16
|
+
yarn add t1y-sdk-js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Script Tag (Browser)
|
|
20
|
+
|
|
21
|
+
```html
|
|
22
|
+
<script src="https://unpkg.com/t1y-sdk-js/dist/umd/t1y.min.js"></script>
|
|
23
|
+
<script>
|
|
24
|
+
const client = new T1Y.T1YOS({ appId: 1001, apiKey: '...', secretKey: '...' })
|
|
25
|
+
</script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { T1YOS, timeNow } from 't1y-sdk-js'
|
|
32
|
+
|
|
33
|
+
// 1. Create client
|
|
34
|
+
const client = new T1YOS({
|
|
35
|
+
appId: 1001, // Required: your application ID (>= 1001)
|
|
36
|
+
apiKey: '4fd7448cdc684431a62d8a0111dc69', // Required: 32-character API Key
|
|
37
|
+
secretKey: '17b784e359c946ffa65eebbf9ce29', // Required: 32-character Secret Key
|
|
38
|
+
// Optional with defaults:
|
|
39
|
+
// baseUrl: 'https://myapp.t1y.net',
|
|
40
|
+
// version: 0,
|
|
41
|
+
// isSafeMode: false,
|
|
42
|
+
// timeFormat: 'YYYY-MM-DD HH:mm:ss',
|
|
43
|
+
// offset: 0,
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// 2. Initialize (syncs time offset and safe mode with server)
|
|
47
|
+
await client.init()
|
|
48
|
+
|
|
49
|
+
// 3. Use the database!
|
|
50
|
+
await client.db.collection('users').insertOne({
|
|
51
|
+
name: 'Alice',
|
|
52
|
+
age: 25,
|
|
53
|
+
active: true,
|
|
54
|
+
customTimeAt: timeNow.Now(),
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Database Operations
|
|
59
|
+
|
|
60
|
+
### Single Document
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const db = client.db.collection('users')
|
|
64
|
+
|
|
65
|
+
// Insert one
|
|
66
|
+
const { data } = await db.insertOne({ name: 'Alice', age: 25 })
|
|
67
|
+
console.log(data.objectId) // '507f1f77bcf86cd799439011'
|
|
68
|
+
|
|
69
|
+
// Find by ObjectID
|
|
70
|
+
const { data: findResult } = await db.findById('507f1f77bcf86cd799439011')
|
|
71
|
+
console.log(findResult.result) // { _id: '507f1f77...', name: 'Alice', ... }
|
|
72
|
+
|
|
73
|
+
// Update by ObjectID
|
|
74
|
+
await db.updateById('507f1f77bcf86cd799439011', { $set: { age: 26 } })
|
|
75
|
+
|
|
76
|
+
// Delete by ObjectID
|
|
77
|
+
await db.deleteById('507f1f77bcf86cd799439011')
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Filter-based Operations
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// Find one by filter
|
|
84
|
+
const { data } = await db.findOne({ name: 'Alice' })
|
|
85
|
+
|
|
86
|
+
// Update one by filter
|
|
87
|
+
await db.updateOne(
|
|
88
|
+
{ name: 'Alice' }, // filter
|
|
89
|
+
{ $set: { age: 27 } } // update body
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
// Delete one by filter
|
|
93
|
+
await db.deleteOne({ name: 'Alice' })
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Bulk Operations
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
// Insert many
|
|
100
|
+
const { data } = await db.insertMany([
|
|
101
|
+
{ name: 'Alice', age: 25 },
|
|
102
|
+
{ name: 'Bob', age: 30 },
|
|
103
|
+
])
|
|
104
|
+
console.log(data.insertedCount) // 2
|
|
105
|
+
|
|
106
|
+
// Delete many
|
|
107
|
+
await db.deleteMany({ age: { $lt: 18 } })
|
|
108
|
+
|
|
109
|
+
// Update many
|
|
110
|
+
await db.updateMany({ status: 'inactive' }, { $set: { status: 'archived' } })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Advanced Queries
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
// Paginated find
|
|
117
|
+
const { data } = await db.find(
|
|
118
|
+
1, // page (1-based)
|
|
119
|
+
20, // page size (max 100)
|
|
120
|
+
{ createdAt: -1 }, // sort (newest first)
|
|
121
|
+
{ age: { $gte: 18 } } // filter
|
|
122
|
+
)
|
|
123
|
+
console.log(data.results) // Array of documents
|
|
124
|
+
console.log(data.pagination) // { totalItems: 42, totalPages: 3 }
|
|
125
|
+
|
|
126
|
+
// Aggregation pipeline
|
|
127
|
+
const { data } = await db.aggregate([
|
|
128
|
+
{ $match: { status: 'completed' } },
|
|
129
|
+
{ $group: { _id: '$category', total: { $sum: '$amount' } } },
|
|
130
|
+
{ $sort: { total: -1 } },
|
|
131
|
+
])
|
|
132
|
+
|
|
133
|
+
// Count
|
|
134
|
+
const { data: countData } = await client.db.collection('users').count({ status: 'active' })
|
|
135
|
+
console.log(countData.count)
|
|
136
|
+
|
|
137
|
+
// Distinct values
|
|
138
|
+
const { data: distinctData } = await client.db.collection('users').distinct('city')
|
|
139
|
+
// With filter
|
|
140
|
+
const { data: filteredDistinct } = await client.db
|
|
141
|
+
.collection('users')
|
|
142
|
+
.distinct('city', { country: 'China' })
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Schema Management
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// Get all collections
|
|
149
|
+
const { data } = await client.db.getCollections()
|
|
150
|
+
console.log(data.results) // ['users', 'orders', 'products']
|
|
151
|
+
|
|
152
|
+
// Create a collection
|
|
153
|
+
await client.db.collection('posts').create()
|
|
154
|
+
|
|
155
|
+
// Clear a collection
|
|
156
|
+
const { data: clearResult } = await client.db.collection('posts').clear()
|
|
157
|
+
console.log(clearResult.deletedCount)
|
|
158
|
+
|
|
159
|
+
// Drop a collection
|
|
160
|
+
await client.db.collection('posts').drop()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Special Types
|
|
164
|
+
|
|
165
|
+
The SDK provides helper functions that produce server-recognized type markers:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import {
|
|
169
|
+
ObjectID,
|
|
170
|
+
Date,
|
|
171
|
+
DateTime,
|
|
172
|
+
Timestamp,
|
|
173
|
+
Boolean,
|
|
174
|
+
Integer,
|
|
175
|
+
Bigint,
|
|
176
|
+
Float,
|
|
177
|
+
Double,
|
|
178
|
+
Array,
|
|
179
|
+
Map,
|
|
180
|
+
MapArray,
|
|
181
|
+
Null,
|
|
182
|
+
None,
|
|
183
|
+
Nil,
|
|
184
|
+
timeNow,
|
|
185
|
+
} from 't1y-sdk-js'
|
|
186
|
+
|
|
187
|
+
await db.insertOne({
|
|
188
|
+
// ObjectID reference
|
|
189
|
+
userId: ObjectID('507f1f77bcf86cd799439011'),
|
|
190
|
+
|
|
191
|
+
// Date types
|
|
192
|
+
birthday: Date('2000-01-01T00:00:00Z'),
|
|
193
|
+
eventTime: DateTime('2024-06-15T14:30:00Z'),
|
|
194
|
+
loginAt: Timestamp(1705312200),
|
|
195
|
+
|
|
196
|
+
// Numeric types
|
|
197
|
+
active: Boolean(true),
|
|
198
|
+
quantity: Integer(42),
|
|
199
|
+
bigNumber: Bigint(9007199254740991),
|
|
200
|
+
rating: Float(4.5),
|
|
201
|
+
preciseValue: Double(3.141592653589793),
|
|
202
|
+
|
|
203
|
+
// Structured types
|
|
204
|
+
tags: Array(['javascript', 'typescript']),
|
|
205
|
+
metadata: Map({ theme: 'dark', lang: 'en' }),
|
|
206
|
+
history: MapArray([{ action: 'login' }, { action: 'logout' }]),
|
|
207
|
+
|
|
208
|
+
// Null values
|
|
209
|
+
deletedAt: Null, // server converts to nil
|
|
210
|
+
middleName: None, // server converts to nil
|
|
211
|
+
|
|
212
|
+
// Server-time helpers
|
|
213
|
+
customTimeAt: timeNow.Now(), // server's time.Now()
|
|
214
|
+
unixCreatedAt: timeNow.NowUnix(), // server's time.Now().Unix()
|
|
215
|
+
})
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Metadata
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
// Get all metadata
|
|
222
|
+
const { data } = await client.getMeta()
|
|
223
|
+
console.log(data.results) // { version: 1, collections: [...], ... }
|
|
224
|
+
|
|
225
|
+
// Get specific field
|
|
226
|
+
const { data: versionData } = await client.getMeta('version')
|
|
227
|
+
console.log(versionData.result) // 1
|
|
228
|
+
|
|
229
|
+
// Check for updates
|
|
230
|
+
const hasUpdate = await client.checkUpdate()
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Cloud Functions
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
// Call a .jsc cloud function
|
|
237
|
+
const { data } = await client.callFunc('hello', { name: 'World' })
|
|
238
|
+
|
|
239
|
+
// With safe mode enabled for this specific call
|
|
240
|
+
const { data: safeData } = await client.callFunc('secureFunc', params, true)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Security
|
|
244
|
+
|
|
245
|
+
### Authentication Headers
|
|
246
|
+
|
|
247
|
+
Every request includes:
|
|
248
|
+
|
|
249
|
+
- `X-T1Y-Application-ID` — Your application ID
|
|
250
|
+
- `X-T1Y-API-Key` — Your 32-character API key
|
|
251
|
+
- `X-T1Y-Safe-Timestamp` — Unix timestamp (UTC + time offset from init)
|
|
252
|
+
- `X-T1Y-Safe-Sign` — HMAC-SHA256 signature (64 hex chars)
|
|
253
|
+
|
|
254
|
+
### Signature Algorithm
|
|
255
|
+
|
|
256
|
+
```
|
|
257
|
+
message = METHOD + "\n" + URL_PATH + "\n" + SHA256(body) + "\n" + appId + "\n" + timestamp
|
|
258
|
+
signature = HMAC-SHA256(secretKey, message)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Safe Mode (AES-256-GCM)
|
|
262
|
+
|
|
263
|
+
When safe mode is enabled (via `isSafeMode: true` or auto-detected from init), request bodies are encrypted with AES-256-GCM using your SecretKey, and server responses are decrypted automatically.
|
|
264
|
+
|
|
265
|
+
## API Reference
|
|
266
|
+
|
|
267
|
+
### T1YOS
|
|
268
|
+
|
|
269
|
+
| Method | Description |
|
|
270
|
+
| --------------------------------------------- | -------------------------------------------------- |
|
|
271
|
+
| `new T1YOS(config)` | Create client (validates appId, apiKey, secretKey) |
|
|
272
|
+
| `init()` | Sync time offset and safe mode with server |
|
|
273
|
+
| `db.collection(name)` | Get a collection instance (chainable) |
|
|
274
|
+
| `db.toObjectID(id)` | Create ObjectID marker string |
|
|
275
|
+
| `db.getCollections()` | List all collections |
|
|
276
|
+
| `getMeta(field?)` | Get application metadata |
|
|
277
|
+
| `checkUpdate()` | Check if newer version exists |
|
|
278
|
+
| `callFunc(name, params?, safeMode?)` | Call a cloud function |
|
|
279
|
+
| `request(method, path, params?, encryption?)` | Raw authenticated request |
|
|
280
|
+
|
|
281
|
+
### T1Collection
|
|
282
|
+
|
|
283
|
+
| Method | HTTP | Endpoint |
|
|
284
|
+
| -------------------------------- | ------ | ----------------------------------- |
|
|
285
|
+
| `insertOne(data)` | POST | `/v5/classes/:name` |
|
|
286
|
+
| `deleteById(objectId)` | DELETE | `/v5/classes/:name/:objectId` |
|
|
287
|
+
| `updateById(objectId, data)` | PUT | `/v5/classes/:name/:objectId` |
|
|
288
|
+
| `findById(objectId)` | GET | `/v5/classes/:name/:objectId` |
|
|
289
|
+
| `deleteOne(filter)` | DELETE | `/v5/classes/:name/one` |
|
|
290
|
+
| `updateOne(filter, body)` | PUT | `/v5/classes/:name/one` |
|
|
291
|
+
| `findOne(filter)` | POST | `/v5/classes/:name/one` |
|
|
292
|
+
| `insertMany(dataList)` | POST | `/v5/classes/:name/many` |
|
|
293
|
+
| `deleteMany(filter)` | DELETE | `/v5/classes/:name/many` |
|
|
294
|
+
| `updateMany(filter, body)` | PUT | `/v5/classes/:name/many` |
|
|
295
|
+
| `find(page, size, sort, filter)` | POST | `/v5/classes/:name/find` |
|
|
296
|
+
| `aggregate(pipeline)` | POST | `/v5/classes/:name/aggregate` |
|
|
297
|
+
| `count(filter?)` | POST | `/v5/classes/:name/count` |
|
|
298
|
+
| `distinct(fieldName, filter?)` | POST | `/v5/classes/:name/distinct/:field` |
|
|
299
|
+
| `create()` | POST | `/v5/schemas/:name` |
|
|
300
|
+
| `clear()` | PUT | `/v5/schemas/:name` |
|
|
301
|
+
| `drop()` | DELETE | `/v5/schemas/:name` |
|
|
302
|
+
|
|
303
|
+
T1YOS `db` object also provides:
|
|
304
|
+
|
|
305
|
+
| Method | HTTP | Endpoint |
|
|
306
|
+
| --------------------- | ---- | ------------- |
|
|
307
|
+
| `db.getCollections()` | GET | `/v5/schemas` |
|
|
308
|
+
|
|
309
|
+
## License
|
|
310
|
+
|
|
311
|
+
MIT
|
|
312
|
+
|
|
313
|
+
Copyright (c) 2024 华易云联(杭州)网络科技有限责任公司
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# t1yOS SDK for JavaScript/TypeScript
|
|
2
|
+
|
|
3
|
+
[English](./README.md)
|
|
4
|
+
|
|
5
|
+
[t1yOS](https://www.t1y.net) Serverless 平台 JavaScript/TypeScript SDK — 云数据库、元数据和云函数客户端。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
### npm / pnpm / yarn
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install t1y-sdk-js
|
|
13
|
+
# 或
|
|
14
|
+
pnpm add t1y-sdk-js
|
|
15
|
+
# 或
|
|
16
|
+
yarn add t1y-sdk-js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Script 标签(浏览器)
|
|
20
|
+
|
|
21
|
+
```html
|
|
22
|
+
<script src="https://unpkg.com/t1y-sdk-js/dist/umd/t1y.min.js"></script>
|
|
23
|
+
<script>
|
|
24
|
+
const client = new T1Y.T1YOS({ appId: 1001, apiKey: '...', secretKey: '...' })
|
|
25
|
+
</script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 快速开始
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { T1YOS, timeNow } from 't1y-sdk-js'
|
|
32
|
+
|
|
33
|
+
// 1. 创建客户端
|
|
34
|
+
const client = new T1YOS({
|
|
35
|
+
appId: 1001, // 必填:应用 ID(>= 1001)
|
|
36
|
+
apiKey: '4fd7448cdc684431a62d8a0111dc69', // 必填:32 位 API Key
|
|
37
|
+
secretKey: '17b784e359c946ffa65eebbf9ce29', // 必填:32 位 Secret Key
|
|
38
|
+
// 以下参数可选(均有默认值):
|
|
39
|
+
// baseUrl: 'https://myapp.t1y.net',
|
|
40
|
+
// version: 0,
|
|
41
|
+
// isSafeMode: false,
|
|
42
|
+
// timeFormat: 'YYYY-MM-DD HH:mm:ss',
|
|
43
|
+
// offset: 0,
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// 2. 初始化(与服务器同步时间偏移和安全模式)
|
|
47
|
+
await client.init()
|
|
48
|
+
|
|
49
|
+
// 3. 开始使用数据库!
|
|
50
|
+
await client.db.collection('users').insertOne({
|
|
51
|
+
name: '张三',
|
|
52
|
+
age: 25,
|
|
53
|
+
active: true,
|
|
54
|
+
customTimeAt: timeNow.Now(),
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 数据库操作
|
|
59
|
+
|
|
60
|
+
### 单条操作
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const db = client.db.collection('users')
|
|
64
|
+
|
|
65
|
+
// 插入一条
|
|
66
|
+
const { data } = await db.insertOne({ name: '张三', age: 25 })
|
|
67
|
+
console.log(data.objectId) // '507f1f77bcf86cd799439011'
|
|
68
|
+
|
|
69
|
+
// 通过 ObjectID 查询
|
|
70
|
+
const { data: findResult } = await db.findById('507f1f77bcf86cd799439011')
|
|
71
|
+
console.log(findResult.result) // { _id: '507f1f77...', name: '张三', ... }
|
|
72
|
+
|
|
73
|
+
// 通过 ObjectID 更新
|
|
74
|
+
await db.updateById('507f1f77bcf86cd799439011', { $set: { age: 26 } })
|
|
75
|
+
|
|
76
|
+
// 通过 ObjectID 删除
|
|
77
|
+
await db.deleteById('507f1f77bcf86cd799439011')
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 条件操作
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// 条件查询一条
|
|
84
|
+
const { data } = await db.findOne({ name: '张三' })
|
|
85
|
+
|
|
86
|
+
// 条件更新一条
|
|
87
|
+
await db.updateOne(
|
|
88
|
+
{ name: '张三' }, // 查询条件
|
|
89
|
+
{ $set: { age: 27 } } // 更新内容
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
// 条件删除一条
|
|
93
|
+
await db.deleteOne({ name: '张三' })
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 批量操作
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
// 插入多条
|
|
100
|
+
const { data } = await db.insertMany([
|
|
101
|
+
{ name: '张三', age: 25 },
|
|
102
|
+
{ name: '李四', age: 30 },
|
|
103
|
+
])
|
|
104
|
+
console.log(data.insertedCount) // 2
|
|
105
|
+
|
|
106
|
+
// 删除多条
|
|
107
|
+
await db.deleteMany({ age: { $lt: 18 } })
|
|
108
|
+
|
|
109
|
+
// 更新多条
|
|
110
|
+
await db.updateMany({ status: 'inactive' }, { $set: { status: 'archived' } })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 高级查询
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
// 分页查询
|
|
117
|
+
const { data } = await db.find(
|
|
118
|
+
1, // 页码(从 1 开始)
|
|
119
|
+
20, // 每页条数(最大 100)
|
|
120
|
+
{ createdAt: -1 }, // 排序(按创建时间倒序)
|
|
121
|
+
{ age: { $gte: 18 } } // 查询条件
|
|
122
|
+
)
|
|
123
|
+
console.log(data.results) // 文档数组
|
|
124
|
+
console.log(data.pagination) // { totalItems: 42, totalPages: 3 }
|
|
125
|
+
|
|
126
|
+
// 聚合查询
|
|
127
|
+
const { data } = await db.aggregate([
|
|
128
|
+
{ $match: { status: 'completed' } },
|
|
129
|
+
{ $group: { _id: '$category', total: { $sum: '$amount' } } },
|
|
130
|
+
{ $sort: { total: -1 } },
|
|
131
|
+
])
|
|
132
|
+
|
|
133
|
+
// 计数
|
|
134
|
+
const { data: countData } = await client.db.collection('users').count({ status: 'active' })
|
|
135
|
+
console.log(countData.count)
|
|
136
|
+
|
|
137
|
+
// 去重查询
|
|
138
|
+
const { data: distinctData } = await client.db.collection('users').distinct('city')
|
|
139
|
+
// 带条件过滤
|
|
140
|
+
const { data: filteredDistinct } = await client.db
|
|
141
|
+
.collection('users')
|
|
142
|
+
.distinct('city', { country: 'China' })
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 表管理
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// 获取所有表
|
|
149
|
+
const { data } = await client.db.getCollections()
|
|
150
|
+
console.log(data.results) // ['users', 'orders', 'products']
|
|
151
|
+
|
|
152
|
+
// 创建表
|
|
153
|
+
await client.db.collection('posts').create()
|
|
154
|
+
|
|
155
|
+
// 清空表
|
|
156
|
+
const { data: clearResult } = await client.db.collection('posts').clear()
|
|
157
|
+
console.log(clearResult.deletedCount)
|
|
158
|
+
|
|
159
|
+
// 删除表
|
|
160
|
+
await client.db.collection('posts').drop()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## 特殊类型
|
|
164
|
+
|
|
165
|
+
SDK 提供了一系列辅助函数,用于生成服务端可识别的类型标记:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import {
|
|
169
|
+
ObjectID,
|
|
170
|
+
Date,
|
|
171
|
+
DateTime,
|
|
172
|
+
Timestamp,
|
|
173
|
+
Boolean,
|
|
174
|
+
Integer,
|
|
175
|
+
Bigint,
|
|
176
|
+
Float,
|
|
177
|
+
Double,
|
|
178
|
+
Array,
|
|
179
|
+
Map,
|
|
180
|
+
MapArray,
|
|
181
|
+
Null,
|
|
182
|
+
None,
|
|
183
|
+
Nil,
|
|
184
|
+
timeNow,
|
|
185
|
+
} from 't1y-sdk-js'
|
|
186
|
+
|
|
187
|
+
await db.insertOne({
|
|
188
|
+
// ObjectID 引用
|
|
189
|
+
userId: ObjectID('507f1f77bcf86cd799439011'),
|
|
190
|
+
|
|
191
|
+
// 日期类型
|
|
192
|
+
birthday: Date('2000-01-01T00:00:00Z'),
|
|
193
|
+
eventTime: DateTime('2024-06-15T14:30:00Z'),
|
|
194
|
+
loginAt: Timestamp(1705312200),
|
|
195
|
+
|
|
196
|
+
// 数值类型
|
|
197
|
+
active: Boolean(true),
|
|
198
|
+
quantity: Integer(42),
|
|
199
|
+
bigNumber: Bigint(9007199254740991),
|
|
200
|
+
rating: Float(4.5),
|
|
201
|
+
preciseValue: Double(3.141592653589793),
|
|
202
|
+
|
|
203
|
+
// 结构化类型
|
|
204
|
+
tags: Array(['javascript', 'typescript']),
|
|
205
|
+
metadata: Map({ theme: 'dark', lang: 'zh' }),
|
|
206
|
+
history: MapArray([{ action: 'login' }, { action: 'logout' }]),
|
|
207
|
+
|
|
208
|
+
// 空值
|
|
209
|
+
deletedAt: Null, // 服务端转为 nil
|
|
210
|
+
middleName: None, // 服务端转为 nil
|
|
211
|
+
|
|
212
|
+
// 服务端时间辅助
|
|
213
|
+
customTimeAt: timeNow.Now(), // 服务端的 time.Now()
|
|
214
|
+
unixCreatedAt: timeNow.NowUnix(), // 服务端的 time.Now().Unix()
|
|
215
|
+
})
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## 元数据
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
// 获取全部元数据
|
|
222
|
+
const { data } = await client.getMeta()
|
|
223
|
+
console.log(data.results) // { version: 1, collections: [...], ... }
|
|
224
|
+
|
|
225
|
+
// 获取指定字段
|
|
226
|
+
const { data: versionData } = await client.getMeta('version')
|
|
227
|
+
console.log(versionData.result) // 1
|
|
228
|
+
|
|
229
|
+
// 检查更新
|
|
230
|
+
const hasUpdate = await client.checkUpdate()
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## 云函数
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
// 调用 .jsc 云函数
|
|
237
|
+
const { data } = await client.callFunc('hello', { name: 'World' })
|
|
238
|
+
|
|
239
|
+
// 为此调用单独启用安全模式
|
|
240
|
+
const { data: safeData } = await client.callFunc('secureFunc', params, true)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## 安全机制
|
|
244
|
+
|
|
245
|
+
### 认证请求头
|
|
246
|
+
|
|
247
|
+
每个请求都会携带以下请求头:
|
|
248
|
+
|
|
249
|
+
- `X-T1Y-Application-ID` — 应用 ID
|
|
250
|
+
- `X-T1Y-API-Key` — 32 位 API Key
|
|
251
|
+
- `X-T1Y-Safe-Timestamp` — Unix 时间戳(UTC + 初始化时获取的时间偏移)
|
|
252
|
+
- `X-T1Y-Safe-Sign` — HMAC-SHA256 签名(64 位十六进制)
|
|
253
|
+
|
|
254
|
+
### 签名算法
|
|
255
|
+
|
|
256
|
+
```
|
|
257
|
+
message = METHOD + "\n" + URL_PATH + "\n" + SHA256(body) + "\n" + appId + "\n" + timestamp
|
|
258
|
+
signature = HMAC-SHA256(secretKey, message)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### 安全模式(AES-256-GCM)
|
|
262
|
+
|
|
263
|
+
当启用安全模式时(通过 `isSafeMode: true` 或初始化时自动检测),请求体将使用 AES-256-GCM 加密,密钥为应用的 SecretKey,服务端响应也会自动解密。
|
|
264
|
+
|
|
265
|
+
## API 参考
|
|
266
|
+
|
|
267
|
+
### T1YOS
|
|
268
|
+
|
|
269
|
+
| 方法 | 说明 |
|
|
270
|
+
| --------------------------------------------- | ------------------------------------------- |
|
|
271
|
+
| `new T1YOS(config)` | 创建客户端(校验 appId、apiKey、secretKey) |
|
|
272
|
+
| `init()` | 与服务端同步时间偏移和安全模式 |
|
|
273
|
+
| `db.collection(name)` | 获取集合操作实例(链式调用) |
|
|
274
|
+
| `db.toObjectID(id)` | 创建 ObjectID 标记字符串 |
|
|
275
|
+
| `db.getCollections()` | 获取所有表 |
|
|
276
|
+
| `getMeta(field?)` | 获取应用元数据 |
|
|
277
|
+
| `checkUpdate()` | 检查是否存在新版本 |
|
|
278
|
+
| `callFunc(name, params?, safeMode?)` | 调用云函数 |
|
|
279
|
+
| `request(method, path, params?, encryption?)` | 原始认证请求 |
|
|
280
|
+
|
|
281
|
+
### T1Collection
|
|
282
|
+
|
|
283
|
+
| 方法 | HTTP | 端点 |
|
|
284
|
+
| -------------------------------- | ------ | ----------------------------------- |
|
|
285
|
+
| `insertOne(data)` | POST | `/v5/classes/:name` |
|
|
286
|
+
| `deleteById(objectId)` | DELETE | `/v5/classes/:name/:objectId` |
|
|
287
|
+
| `updateById(objectId, data)` | PUT | `/v5/classes/:name/:objectId` |
|
|
288
|
+
| `findById(objectId)` | GET | `/v5/classes/:name/:objectId` |
|
|
289
|
+
| `deleteOne(filter)` | DELETE | `/v5/classes/:name/one` |
|
|
290
|
+
| `updateOne(filter, body)` | PUT | `/v5/classes/:name/one` |
|
|
291
|
+
| `findOne(filter)` | POST | `/v5/classes/:name/one` |
|
|
292
|
+
| `insertMany(dataList)` | POST | `/v5/classes/:name/many` |
|
|
293
|
+
| `deleteMany(filter)` | DELETE | `/v5/classes/:name/many` |
|
|
294
|
+
| `updateMany(filter, body)` | PUT | `/v5/classes/:name/many` |
|
|
295
|
+
| `find(page, size, sort, filter)` | POST | `/v5/classes/:name/find` |
|
|
296
|
+
| `aggregate(pipeline)` | POST | `/v5/classes/:name/aggregate` |
|
|
297
|
+
| `count(filter?)` | POST | `/v5/classes/:name/count` |
|
|
298
|
+
| `distinct(fieldName, filter?)` | POST | `/v5/classes/:name/distinct/:field` |
|
|
299
|
+
| `create()` | POST | `/v5/schemas/:name` |
|
|
300
|
+
| `clear()` | PUT | `/v5/schemas/:name` |
|
|
301
|
+
| `drop()` | DELETE | `/v5/schemas/:name` |
|
|
302
|
+
|
|
303
|
+
T1YOS `db` 对象还提供:
|
|
304
|
+
|
|
305
|
+
| 方法 | HTTP | 端点 |
|
|
306
|
+
| --------------------- | ---- | ------------- |
|
|
307
|
+
| `db.getCollections()` | GET | `/v5/schemas` |
|
|
308
|
+
|
|
309
|
+
## License
|
|
310
|
+
|
|
311
|
+
MIT
|
|
312
|
+
|
|
313
|
+
Copyright (c) 2024 华易云联(杭州)网络科技有限责任公司
|