## Introduction

Data plays a critical role in any computational process, including the emerging Web3 era. In order to successfully transition to Web3, it is imperative to enhance accessibility and accuracy of data. The zkDatabase use a distributed storage engine that improves the availability of data. It utilizes Zero-Knowledge Proof to ensuring the correctness of data in verifiable manner. With zkDatabase, it's allow developers to focus on developing their ideas, rather than managing the complexities of data storage and management.

**It's time for verifiable data.**

## Installation

```bash
npm install zkdb
```

### Connect to zkDatabase

```typescript
import { ZkDatabase } from 'zkdb';

const zkdb = new ZkDatabase({
  apiKey: 'your-api-key',
  url: 'https://serverless.zkdatabase.org/graphql',
});
```

### Create a Database

```typescript
const dbTest = zkdb.db('my_database');

if (!(await dbTest.exist()).unwrapOr(false)) {
  (await dbTest.create()).unwrap();
}
```

### Define Schema and Create Collection

```typescript
import { Schema, SchemaToObject } from '@zkdb/common';
import { Permission } from 'zkdb';

const BookSchema = new Schema([
  { name: 'name', kind: 'String' },
  { name: 'author', kind: 'String' },
  { name: 'release', kind: 'UInt32' },
]);

type TBook = SchemaToObject<ReturnType<typeof BookSchema.schemaDefinition>>;

const permissions = Permission.from({
  owner: { read: true, write: true, delete: true, system: true },
  group: { read: true, write: false, delete: false, system: false },
  other: { read: true, write: false, delete: false, system: false },
});

const collectionBook = dbTest.collection<TBook>('book');
await collectionBook.create(BookSchema, permissions, 'default');
```

### Insert Documents

```typescript
const docId = (
  await collectionBook.insert({
    name: '1984',
    author: 'George Orwell',
    release: 1949n,
  })
).unwrap();
```

### Query Documents

```typescript
// Find many
const books = (
  await collectionBook.findMany({ author: 'George Orwell' })
).unwrap();
books.data.forEach((b) => console.log(b.document.name));

// Find one
const book = (await collectionBook.findOne({ name: '1984' })).unwrap();
```

### Update Documents

```typescript
const docs = (await collectionBook.findMany({ name: '1984' })).unwrap();
if (docs.data.length > 0) {
  await docs.data[0].update({ release: 1950n });
}
```

### Delete Documents

```typescript
const docs = (await collectionBook.findMany({ name: '1984' })).unwrap();
if (docs.data.length > 0) {
  await docs.data[0].drop();
}
```

### Verify ZK Proofs

```typescript
const verifyResult = await dbTest.zkProofVerify();
console.log('Proof valid:', verifyResult.unwrap());

// Check zk proof state
const zkProofState = await dbTest.zkProofState();
console.log('Zk proof state:', zkProofState.unwrap());
```

## License

This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details

**built with ❤️**
