Updated usage examples with record management and updated assertions

This commit is contained in:
Tristan B. Velloza Kildaire 2022-12-29 19:47:27 +02:00
parent 0cd17f976e
commit b894b6e1c6
1 changed files with 75 additions and 38 deletions

View File

@ -29,6 +29,7 @@ This is just to show off the serialization method `serializeRecord(RecordType)`
public int age;
public string[] list;
public JSONValue extraJSON;
public EnumType eType;
}
Person p1;
@ -37,6 +38,7 @@ This is just to show off the serialization method `serializeRecord(RecordType)`
p1.age = 23;
p1.list = ["1", "2", "3"];
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
p1.eType = EnumType.CAT;
JSONValue serialized = PocketBase.serializeRecord(p1);
@ -44,6 +46,11 @@ This is just to show off the serialization method `serializeRecord(RecordType)`
assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0);
assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0);
assert(canFind(keys, "age") && serialized["age"].integer() == 23);
debug(dbg)
{
writeln(serialized.toPrettyString());
}
```
### Deserialization
@ -74,13 +81,43 @@ This is to show off deserialization method `fromJSON(RecordType)(JSONValue jsonI
Person person = PocketBase.fromJSON!(Person)(json);
debug(dbg)
{
writeln(person);
}
assert(cmp(person.firstname, "Tristan") == 0);
assert(cmp(person.lastname, "Kildaire") == 0);
assert(person.age == 23);
assert(person.isMale == true);
//TODO: object test case, list test case
assert(person.obj["bruh"].integer() == 1);
//TODO: list test case
```
### Record management
Below we have a few calls like create and delete:
```d
PocketBase pb = new PocketBase();
struct Person
{
string id;
string name;
int age;
}
Person p1 = Person();
p1.name = "Tristan Gonzales";
p1.age = 23;
Person recordStored = pb.createRecord("dummy", p1);
pb.deleteRecord("dummy", recordStored.id);
recordStored = pb.createRecord("dummy", p1);
pb.deleteRecord("dummy", recordStored);
```
## Development