This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
document_v2.ts
189 lines (177 loc) · 5.02 KB
/
document_v2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// document_v2.ts
// Copyright (C) 2023 db3.network Author imotai <codego.me@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import {
Mutation,
DocumentMutation,
DocumentMask,
Mutation_BodyWrapper,
MutationAction,
} from '../proto/db3_mutation_v2'
import { BSON } from 'db3-bson'
import { fromHEX } from '../crypto/crypto_utils'
import type { Collection, QueryResult } from './types'
import type { Query, QueryParameter } from '../proto/db3_database_v2'
import type { DocumentData, DocumentEntry } from '../client/base'
async function runQueryInternal<T>(col: Collection, query: Query) {
const response = await col.db.client.indexer.runQuery(
col.db.addr,
col.name,
query
)
const entries = response.documents.map((doc) => {
return {
doc: JSON.parse(doc.doc) as T,
id: doc.id,
owner: doc.owner,
} as DocumentEntry<T>
})
return {
docs: entries,
collection: col,
} as QueryResult<T>
}
/**
*
* Query document with a query language
*
* ```ts
* const queryStr = '/* | limit 1'
* const resultSet = await queryDoc<Profile>(collection, queryStr)
* ```
* @param col - the instance of collection
* @param queryStr - a document query string
* @param parameters - an optional query parameters
* @returns the {@link Queryresult}
*
**/
export async function queryDoc<T = DocumentData>(
col: Collection,
queryStr: string,
parameters?: QueryParameter[]
) {
if (!parameters) {
const query: Query = {
queryStr,
parameters: [],
}
return runQueryInternal(col, query)
} else {
const query: Query = {
queryStr,
parameters,
}
return runQueryInternal<T>(col, query)
}
}
export async function deleteDoc(col: Collection, ids: string[]) {
const documentMutation: DocumentMutation = {
collectionName: col.name,
documents: [],
ids,
masks: [],
}
const body: Mutation_BodyWrapper = {
body: {
oneofKind: 'documentMutation',
documentMutation,
},
dbAddress: fromHEX(col.db.addr),
}
const dm: Mutation = {
action: MutationAction.DeleteDocument,
bodies: [body],
}
const payload = Mutation.toBinary(dm)
const response = await col.db.client.provider.sendMutation(
payload,
col.db.client.nonce.toString()
)
if (response.code == 0) {
col.db.client.nonce += 1
return [response.id, response.block, response.order]
} else {
throw new Error('fail to create collection')
}
}
export async function updateDoc(
col: Collection,
id: string,
doc: DocumentData,
masks: string[]
) {
const documentMask: DocumentMask = {
fields: masks,
}
const documentMutation: DocumentMutation = {
collectionName: col.name,
documents: [BSON.serialize(doc)],
ids: [id],
masks: [documentMask],
}
const body: Mutation_BodyWrapper = {
body: {
oneofKind: 'documentMutation',
documentMutation,
},
dbAddress: fromHEX(col.db.addr),
}
const dm: Mutation = {
action: MutationAction.UpdateDocument,
bodies: [body],
}
const payload = Mutation.toBinary(dm)
const response = await col.db.client.provider.sendMutation(
payload,
col.db.client.nonce.toString()
)
if (response.code == 0) {
col.db.client.nonce += 1
return [response.id, response.block, response.order]
} else {
throw new Error('fail to create collection')
}
}
export async function addDoc(col: Collection, doc: DocumentData) {
const documentMutation: DocumentMutation = {
collectionName: col.name,
documents: [BSON.serialize(doc)],
ids: [],
masks: [],
}
const body: Mutation_BodyWrapper = {
body: {
oneofKind: 'documentMutation',
documentMutation,
},
dbAddress: fromHEX(col.db.addr),
}
const dm: Mutation = {
action: MutationAction.AddDocument,
bodies: [body],
}
const payload = Mutation.toBinary(dm)
const response = await col.db.client.provider.sendMutation(
payload,
col.db.client.nonce.toString()
)
if (response.code == 0) {
col.db.client.nonce += 1
return [response.id, response.block, response.order]
} else {
throw new Error('fail to create collection')
}
}