Skip to content

Commit

Permalink
Add decodeType to Appendable api (#56)
Browse files Browse the repository at this point in the history
* decode

* cleaner check

* add enums + helper func
  • Loading branch information
friendlymatthew authored Jan 22, 2024
1 parent 79a8ff2 commit 800b5b3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
35 changes: 33 additions & 2 deletions examples/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,39 @@
return "Error: 'value' in 'where' clause is missing.";
}

// todo: find out how to decode bigint field types. i.e. what is a 34n?
// assert `where` type
const fieldType = fieldTypes[whereNode.key];

if (whereNode.value === null) {
if (
!Appendable.containsType(fieldType, Appendable.FieldType.Null)
) {
return `Error: 'key: ${whereNode.key} does not have type: null.`;
}
}
if (typeof whereNode.value === "boolean") {
if (
!Appendable.containsType(fieldType, Appendable.FieldType.Boolean)
) {
return `Error: 'key: ${whereNode.key} does not have type: boolean.`;
}
}
if (
typeof whereNode.value === "number" ||
typeof whereNode.value === "bigint"
) {
if (
!Appendable.containsType(fieldType, Appendable.FieldType.Number)
) {
return `Error: 'key: ${whereNode.key} does not have type: number.`;
}
}
if (typeof whereNode.value === "string") {
if (
!Appendable.containsType(fieldType, Appendable.FieldType.String)
) {
return `Error: 'key: ${whereNode.key} does not have type: string.`;
}
}
}

if (query.orderBy) {
Expand Down
14 changes: 13 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataFile } from "./data-file";
import { IndexFile, VersionedIndexFile } from "./index-file";
import { VersionedIndexFile } from "./index-file";

type Schema = {
[key: string]: {};
Expand All @@ -21,6 +21,18 @@ export type Query<T extends Schema> = {
orderBy?: OrderBy<T>[];
};

export enum FieldType {
String = 1 << 0,
Number = 1 << 1,
Boolean = 1 << 4,
Null = 1 << 5,
}

// given a fieldType and the desired type, this function performs a bitwise operation to test membership
export function containsType(fieldType: bigint, desiredType: FieldType) {
return (fieldType & BigInt(desiredType)) !== BigInt(0);
}

function parseIgnoringSuffix(x: string) {
// TODO: implement a proper parser.
try {
Expand Down
38 changes: 26 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { DataFile } from "./data-file";
import { Database } from "./database";
import { Database, FieldType, containsType } from "./database";
import { IndexFile } from "./index-file";
import { RangeResolver } from "./resolver";

export async function init(
dataUrl: string | RangeResolver,
indexUrl: string | RangeResolver
dataUrl: string | RangeResolver,
indexUrl: string | RangeResolver
) {
return Database.forDataFileAndIndexFile(
typeof dataUrl === "string"
? DataFile.forUrl(dataUrl)
: DataFile.forResolver(dataUrl),
typeof indexUrl === "string"
? await IndexFile.forUrl(indexUrl)
: await IndexFile.forResolver(indexUrl)
);
return Database.forDataFileAndIndexFile(
typeof dataUrl === "string"
? DataFile.forUrl(dataUrl)
: DataFile.forResolver(dataUrl),
typeof indexUrl === "string"
? await IndexFile.forUrl(indexUrl)
: await IndexFile.forResolver(indexUrl)
);
}

interface GlobalMap {
Appendable: {
init: Function;
FieldType: typeof FieldType;
containsType: typeof containsType;
};
}

declare global {
var Appendable: GlobalMap["Appendable"];
}

globalThis.Appendable = {
init,
init,
FieldType,
containsType,
};
19 changes: 18 additions & 1 deletion src/tests/database.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Database, Query } from "../database";
import { Database, FieldType, Query, containsType } from "../database";
import { DataFile } from "../data-file";
import { IndexFile, VersionedIndexFile } from "../index-file";

Expand Down Expand Up @@ -94,3 +94,20 @@ describe("test query relation", () => {
expect(results).toEqual([25]);
});
});

describe("test field type", () => {
it("check valid type", async () => {
const testCases = [
{ fieldType: BigInt(2), desiredType: FieldType.Number, expected: true },
{ fieldType: BigInt(34), desiredType: FieldType.Null, expected: true },
{ fieldType: BigInt(2), desiredType: FieldType.Null, expected: false },
{ fieldType: BigInt(1), desiredType: FieldType.String, expected: true },
];

testCases.forEach(({ fieldType, desiredType, expected }) => {
const result = containsType(fieldType, desiredType);

expect(result).toEqual(expected);
});
});
});

0 comments on commit 800b5b3

Please sign in to comment.