Skip to content

Commit

Permalink
fix: improve the add doc error
Browse files Browse the repository at this point in the history
  • Loading branch information
imotai committed Aug 12, 2023
1 parent 7ded300 commit 366bbb4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 7 additions & 1 deletion sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export type {
MutationResult,
QueryResult,
} from './store/types'
export { addDoc, updateDoc, deleteDoc, queryDoc, getDoc } from './store/document_v2'
export {
addDoc,
updateDoc,
deleteDoc,
queryDoc,
getDoc,
} from './store/document_v2'

export { SystemConfig, SystemStatus, Version } from './proto/db3_base'
export {
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/store/document_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ export async function updateDoc(
}
}

/**
* Add a document to the collection.
*
* @param col The collection to add the document to.
* @param doc The document to add.
* @returns The ID of the newly added document.
*/
export async function addDoc(col: Collection, doc: DocumentData) {
const documentMutation: DocumentMutation = {
collectionName: col.name,
Expand All @@ -250,6 +257,7 @@ export async function addDoc(col: Collection, doc: DocumentData) {
payload,
col.db.client.nonce.toString()
)

if (response.code == 0 && response.items.length > 0) {
col.db.client.nonce += 1
return {
Expand All @@ -259,6 +267,8 @@ export async function addDoc(col: Collection, doc: DocumentData) {
id: response.items[0].value,
}
} else {
throw new Error('fail to create collection')
throw new Error(
'fail to addDoc, maybe you can syncAccountNonce to resolve the problem'
)
}
}
4 changes: 2 additions & 2 deletions sdk/tests/client_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
deleteDoc,
updateDoc,
queryDoc,
getDoc
getDoc,
} from '../src/store/document_v2'
import {
createFromPrivateKey,
Expand Down Expand Up @@ -383,7 +383,7 @@ describe('test db3.js client module', () => {
try {
const doc = await getDoc(collection, 1000000000000)
except(1).toBe(0)
} catch(e) {}
} catch (e) {}
}

{
Expand Down

0 comments on commit 366bbb4

Please sign in to comment.