Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #81 from dbpunk-labs/feat/support_doc_id
Browse files Browse the repository at this point in the history
support docid
  • Loading branch information
imotai authored Jun 19, 2023
2 parents f866a9a + c269744 commit 26fd38c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/store/document_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ async function runQueryInternal<T>(col: Collection, query: Query) {
return {
doc: JSON.parse(doc.doc) as T,
id: doc.id,
owner: doc.owner,
} as DocumentEntry<T>
})
return {
Expand Down Expand Up @@ -180,9 +179,9 @@ export async function addDoc(col: Collection, doc: DocumentData) {
payload,
col.db.client.nonce.toString()
)
if (response.code == 0) {
if (response.code == 0 && response.items.length > 0) {
col.db.client.nonce += 1
return [response.id, response.block, response.order]
return [response.id, response.block, response.order, response.items[0].value]
} else {
throw new Error('fail to create collection')
}
Expand Down
100 changes: 98 additions & 2 deletions tests/client_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ describe('test db3.js client module', () => {
[index]
)
await new Promise((r) => setTimeout(r, 3000))
const [txId2, block2, order2] = await addDoc(collection, {
const [txId2, block2, order2, docId2] = await addDoc(collection, {
city: 'beijing',
author: 'imotai',
age: 10,
})

const [txId3, block3, order3] = await addDoc(collection, {
const [txId3, block3, order3, docId3] = await addDoc(collection, {
city: 'beijing2',
author: 'imotai1',
age: 1,
Expand Down Expand Up @@ -148,6 +148,102 @@ describe('test db3.js client module', () => {
expect(1).toBe(0)
}
})
test('test create/update/delete document', async () => {
const client = await createTestClient()
try {
const { db, result } = await createDocumentDatabase(client, 'db_for_update_delete')
const index: Index = {
path: '/city',
indexType: IndexType.StringKey,
}
{
const { collection, result } = await createCollection(
db,
'col',
[index]
)
await new Promise((r) => setTimeout(r, 3000))
const [txId2, block2, order2, docId2] = await addDoc(collection, {
city: 'beijing',
author: 'imotai',
age: 10,
})

const [txId3, block3, order3, docId3] = await addDoc(collection, {
city: 'beijing2',
author: 'imotai1',
age: 1,
})
await new Promise((r) => setTimeout(r, 3000))
{
const queryStr = '/[city = beijing]'
const resultSet = await queryDoc<Profile>(
collection,
queryStr
)
expect(1).toBe(resultSet.docs.length)
expect(resultSet.docs[0].doc.city).toBe('beijing')
expect(resultSet.docs[0].doc.author).toBe('imotai')
expect(resultSet.docs[0].doc.age).toBe(10)
expect(resultSet.docs[0].id).toBe(docId2)
}
{
const queryStr = '/[city = beijing2]'
const resultSet = await queryDoc<Profile>(
collection,
queryStr
)
expect(1).toBe(resultSet.docs.length)
expect(resultSet.docs[0].doc.city).toBe('beijing2')
expect(resultSet.docs[0].doc.author).toBe('imotai1')
expect(resultSet.docs[0].doc.age).toBe(1)
expect(resultSet.docs[0].id).toBe(docId3)
}
const [txId4, block4, order4] = await updateDoc(collection,
docId2,
{
city: 'beijing3',
author: 'imotai3',
age: 3,
}, [])
await new Promise((r) => setTimeout(r, 3000))
{
const queryStr = '/[city = beijing]'
const resultSet = await queryDoc<Profile>(
collection,
queryStr
)
expect(0).toBe(resultSet.docs.length)
}
{
const queryStr = '/[city = beijing3]'
const resultSet = await queryDoc<Profile>(
collection,
queryStr
)
expect(1).toBe(resultSet.docs.length)
expect(resultSet.docs[0].doc.city).toBe('beijing3')
expect(resultSet.docs[0].doc.author).toBe('imotai3')
expect(resultSet.docs[0].doc.age).toBe(3)
expect(resultSet.docs[0].id).toBe(docId2)
}
const [txId, block, order] = await deleteDoc(collection, [docId2])
await new Promise((r) => setTimeout(r, 3000))
{
const queryStr = '/[city = beijing3]'
const resultSet = await queryDoc<Profile>(
collection,
queryStr
)
expect(0).toBe(resultSet.docs.length)
}
}

} catch (e) {
console.log(e)
expect(1).toBe(0)
}
})
test('create database v2', async () => {
const client = await createTestClient()
try {
Expand Down

0 comments on commit 26fd38c

Please sign in to comment.