Skip to content

Commit

Permalink
fix: improve schema field validation
Browse files Browse the repository at this point in the history
fix: improve delete method + testing
fix: improve update method + testing
fix: improve general testing across all storages
  • Loading branch information
elribonazo committed Dec 31, 2024
1 parent 04c29d6 commit 07596fb
Show file tree
Hide file tree
Showing 58 changed files with 837 additions and 417 deletions.
4 changes: 3 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ By default RIDB is bundled with a default InMemory storage with support for writ
| **Data Encryption Plugin** | Secure data with encryption plugins |
| **Migration Plugin** | Support for data migrations |
| **Integrity Plugin** | Support for data has not been tampered with |
| **IndexDB Storage** | Robust type safe replacement for Dexie
| **IndexDB Storage** | Robust type safe replacement for Dexie
| **InMemory Storage** | Robust type safe implementation of an inMemory storage
| **LevelDB Storage** | Robust type safe implementation for LevelDB 'classic-level'

36 changes: 34 additions & 2 deletions packages/ridb-level/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<h3 align="center">A secure light-weight and dependency free database wrapper for the web.</h3>
</p>


<p align="center">
<a href="https://github.com/trust0-project/RIDB/releases"><img src="https://img.shields.io/github/v/release/trust0-project/ridb?color=%23ff00a0&include_prereleases&label=version&sort=semver&style=flat-square"></a>
&nbsp;
Expand All @@ -17,4 +16,37 @@
</p>

### Description
This package is a leveldb wrapper for the ridb database.
This package is a leveldb wrapper for the ridb database.

## Installation & usage (typescript)
```bash
npm install @trust0/ridb-level # or yarn add @trust0/ridb-level
```

```typescript
import { RIDB, SchemaFieldType, Doc } from '@trust0/ridb';
import { LevelDB } from '@trust0/ridb-level';
const db = new RIDB(
{
dbName: "test",
schemas: {
demo: {
version: 0,
primaryKey: 'id',
type: SchemaFieldType.object,
properties: {
id: {
type: SchemaFieldType.string,
maxLength: 60
}
}
}
} as const
}
)
await db.start({
storageType: LevelDB,
password: "test"
});
```

58 changes: 17 additions & 41 deletions packages/ridb-level/docs/classes/LevelDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

#### Defined in

[packages/ridb-level/src/index.ts:44](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L44)
[ridb-level/src/index.ts:49](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L49)

## Properties

Expand All @@ -62,7 +62,7 @@

#### Defined in

node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:403
ridb-wasm/pkg/ridb\_wasm.d.ts:199

***

Expand All @@ -72,7 +72,7 @@ node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:403
#### Defined in

[packages/ridb-level/src/index.ts:34](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L34)
[ridb-level/src/index.ts:39](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L39)

***

Expand All @@ -86,7 +86,7 @@ node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:403

#### Defined in

node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:400
ridb-wasm/pkg/ridb\_wasm.d.ts:196

***

Expand All @@ -100,7 +100,7 @@ node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:400

#### Defined in

node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:402
ridb-wasm/pkg/ridb\_wasm.d.ts:198

***

Expand All @@ -114,7 +114,7 @@ node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:402

#### Defined in

node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:401
ridb-wasm/pkg/ridb\_wasm.d.ts:197

## Methods

Expand All @@ -134,7 +134,7 @@ Close the database

#### Defined in

[packages/ridb-level/src/index.ts:53](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L53)
[ridb-level/src/index.ts:58](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L58)

***

Expand Down Expand Up @@ -164,7 +164,7 @@ keyof `T`

#### Defined in

[packages/ridb-level/src/index.ts:99](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L99)
[ridb-level/src/index.ts:126](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L126)

***

Expand Down Expand Up @@ -194,13 +194,13 @@ keyof `T`

#### Defined in

[packages/ridb-level/src/index.ts:119](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L119)
[ridb-level/src/index.ts:146](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L146)

***

### findDocumentById()

> **findDocumentById**(`collectionName`, `id`): `Promise`\<`null` \| `Doc`\<`T`\[keyof `T`\]\>\>
> **findDocumentById**(`collectionName`, `id`): `Promise`\<`undefined` \| `Doc`\<`T`\[keyof `T`\]\>\>
Find a document by its ID

Expand All @@ -216,15 +216,15 @@ keyof `T`

#### Returns

`Promise`\<`null` \| `Doc`\<`T`\[keyof `T`\]\>\>
`Promise`\<`undefined` \| `Doc`\<`T`\[keyof `T`\]\>\>

#### Overrides

`BaseStorage.findDocumentById`

#### Defined in

[packages/ridb-level/src/index.ts:57](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L57)
[ridb-level/src/index.ts:62](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L62)

***

Expand All @@ -248,7 +248,7 @@ keyof `T`

#### Defined in

node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:411
ridb-wasm/pkg/ridb\_wasm.d.ts:207

***

Expand All @@ -272,31 +272,7 @@ node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:411

#### Defined in

node\_modules/@trust0/ridb-wasm/ridb\_wasm.d.ts:412

***

### matchesQuery()

> **matchesQuery**(`doc`, `query`): `boolean`
#### Parameters

##### doc

`Doc`\<`T`\[keyof `T`\]\>

##### query

`Query`\<`any`\>

#### Returns

`boolean`

#### Defined in

[packages/ridb-level/src/index.ts:138](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L138)
ridb-wasm/pkg/ridb\_wasm.d.ts:208

***

Expand All @@ -316,7 +292,7 @@ Start the database

#### Defined in

[packages/ridb-level/src/index.ts:49](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L49)
[ridb-level/src/index.ts:54](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L54)

***

Expand All @@ -342,7 +318,7 @@ Write an operation (insert, update, delete)

#### Defined in

[packages/ridb-level/src/index.ts:75](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L75)
[ridb-level/src/index.ts:83](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L83)

***

Expand Down Expand Up @@ -378,4 +354,4 @@ Write an operation (insert, update, delete)

#### Defined in

[packages/ridb-level/src/index.ts:35](https://github.com/elribonazo/RIDB/blob/56953e7daf1db67092f8732dd6adee955f103a76/packages/ridb-level/src/index.ts#L35)
[ridb-level/src/index.ts:40](https://github.com/elribonazo/RIDB/blob/04c29d6492c780dacdbe8ba762df162b604656ba/packages/ridb-level/src/index.ts#L40)
40 changes: 31 additions & 9 deletions packages/ridb-level/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ export class LevelDB<T extends SchemaTypeRecord> extends BaseStorage<T> {
async findDocumentById(
collectionName: keyof T,
id: string
): Promise<Doc<T[keyof T]> | null> {
): Promise<Doc<T[keyof T]> | undefined> {
const key = `${String(collectionName)}:${id}`;
try {
const value = await this.db.get(key);
if (!value) {
return undefined
}
const doc = JSON.parse(value);
return doc;
} catch (err: any) {
if (err.notFound) {
return null;
return undefined;
} else {
throw err;
}
Expand All @@ -79,19 +82,38 @@ export class LevelDB<T extends SchemaTypeRecord> extends BaseStorage<T> {
/** Write an operation (insert, update, delete) */
async write(op: Operation<T[keyof T]>): Promise<Doc<T[keyof T]>> {
const collectionName = String(op.collection);
const id = "id" in op.data ? op.data.id : null;
if (!id) {
throw new Error("Document ID is required");
}
const key = `${collectionName}:${id}`;
const primaryKey = this.getSchema(collectionName).primaryKey;

switch (op.opType) {
case OpType.CREATE:
case OpType.UPDATE: {
case OpType.CREATE: {
const id = primaryKey in op.data ? (op.data as any)[primaryKey] : null;
if (!id) {
throw new Error("Document ID is required");
}
const key = `${collectionName}:${id}`;
const value = JSON.stringify(op.data);
await this.db.put(key, value);
break;
}
case OpType.UPDATE: {
const id = primaryKey in op.data ? (op.data as any)[primaryKey] : null;
if (!id) {
throw new Error("Document ID is required");
}
const existingRecord = await this.findDocumentById(collectionName, id);
if (!existingRecord) {
throw new Error("Document ID not found");
}
const key = `${collectionName}:${id}`;
const value = JSON.stringify({
...existingRecord,
...op.data
});
await this.db.put(key, value);
break;
}
case OpType.DELETE: {
const key = `${collectionName}:${op.data}`;
await this.db.del(key);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ridb-wasm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PACKAGEJSON=./pkg/package.json
IMPORTFILE=./pkg/ridb_wasm.js

echo "Building the rust library"
wasm-pack --log-level error build --target=web --scope trust0
wasm-pack --log-level error build --target=web --scope trust0 --debug


if is_mac; then
Expand Down
19 changes: 18 additions & 1 deletion packages/ridb-wasm/src/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use js_sys::{Object, Reflect};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use crate::schema::Schema;
Expand Down Expand Up @@ -207,10 +208,24 @@ impl Collection {
/// * `document` - A `JsValue` representing the partial document to update.
#[wasm_bindgen]
pub async fn update(&mut self, document: JsValue) -> Result<JsValue, JsValue> {
let primary_key = self.schema()?.primary_key;
let doc_primary_key = Reflect::get(
&document,
&JsValue::from(primary_key)
)?;

let existing_doc = self.find_by_id(doc_primary_key).await?;
let merge_docs = JsValue::from(
Object::assign(
&Object::from(existing_doc),
&Object::from(document)
)
);

let processed_document = self.storage.call(
&self.name,
HookType::Create,
document
merge_docs
).await?;

let res = match self.storage.write(&self.name, processed_document).await {
Expand All @@ -234,12 +249,14 @@ impl Collection {
/// * `document` - A `JsValue` representing the document to create.
#[wasm_bindgen]
pub async fn create(&mut self, document: JsValue) -> Result<JsValue, JsValue> {
let schema = self.schema()?;
let processed_document = self.storage.call(
&self.name,
HookType::Create,
document
).await?;

schema.validate_document(processed_document.clone())?;

let res = match self.storage.write(&self.name, processed_document).await {
Ok(result) => Ok(result),
Expand Down
Loading

0 comments on commit 07596fb

Please sign in to comment.