[chore] format ts

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-15 15:04:44 +02:00
parent 0cecd1243f
commit 60d213f21c
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
10 changed files with 282 additions and 280 deletions

View file

@ -5,17 +5,14 @@ Data Accees Layer for SQL databases written in Go.
Mongodb inspired query interface: Mongodb inspired query interface:
```typescript ```typescript
const query = Db const query = Db.In("users")
.In("users")
.Find({ .Find({
fullname: { $glob: "*son" } fullname: { $glob: "*son" },
}) })
.Query() .Query();
// Result: // Result:
console.log(users) console.log(users)[
[ ({ id: 25, fullname: "John Menson" }, { id: 76, fullname: "John Johnson" })
{ id: 25, fullname: "John Menson" }, ];
{ id: 76, fullname: "John Johnson" }
]
``` ```

BIN
bun.lockb

Binary file not shown.

View file

@ -36,10 +36,7 @@ type Options = {
url: string; url: string;
}; };
export default class Builder<I extends abstract new (...args: any) => any> {
export default class Builder <
I extends abstract new (...args: any) => any,
>{
private request: Request; private request: Request;
private url: string; private url: string;
private dtoTemplate: new (...args: any) => any = Object; private dtoTemplate: new (...args: any) => any = Object;
@ -54,14 +51,14 @@ I extends abstract new (...args: any) => any,
this.url = opts.url; this.url = opts.url;
} }
private formatRequest(): void { private formatRequest(): void {
this.request.commands = [] this.request.commands = [];
METHODS.forEach((method) => { METHODS.forEach((method) => {
const args = this.methodCalls.get(method); const args = this.methodCalls.get(method);
if (!args) { if (!args) {
return; return;
} }
this.request.commands.push({ method, args }); this.request.commands.push({ method, args });
}) });
} }
private formatRow(data: unknown[]) { private formatRow(data: unknown[]) {
if (!this.dtoTemplate) { if (!this.dtoTemplate) {
@ -175,7 +172,6 @@ I extends abstract new (...args: any) => any,
for await (const row of rows) { for await (const row of rows) {
result.push(row); result.push(row);
} }
return result return result;
} }
} }

View file

@ -1,4 +1,4 @@
import { encode, decode } from '@msgpack/msgpack'; import { encode, decode } from "@msgpack/msgpack";
export interface Method { export interface Method {
method: string; method: string;
@ -11,7 +11,10 @@ export interface Request {
commands: Method[]; commands: Method[];
} }
export const METHODS = "In|Find|Select|Fields|Join|Group|Sort|Limit|Offset|Delete|Insert|Set|Update|OnConflict|DoUpdate|DoNothing".split("|"); export const METHODS =
"In|Find|Select|Fields|Join|Group|Sort|Limit|Offset|Delete|Insert|Set|Update|OnConflict|DoUpdate|DoNothing".split(
"|",
);
export function encodeRequest(request: Request): Uint8Array { export function encodeRequest(request: Request): Uint8Array {
return encode(request); return encode(request);
@ -31,7 +34,7 @@ export function decodeRows(input: Uint8Array): Row[] {
if (input.at(count) != 0x81) { if (input.at(count) != 0x81) {
buf.push(input.at(count)); buf.push(input.at(count));
count++; count++;
continue continue;
} }
const [a, b, c] = ROW_TAG; const [a, b, c] = ROW_TAG;
const [aa, bb, cc] = input.slice(count, count + 4); const [aa, bb, cc] = input.slice(count, count + 4);
@ -49,7 +52,9 @@ export function decodeRows(input: Uint8Array): Row[] {
return rows.map((row) => decode(new Uint8Array(row as number[]))) as Row[]; return rows.map((row) => decode(new Uint8Array(row as number[]))) as Row[];
} }
export async function *decodeRowsIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<Row> { export async function* decodeRowsIterator(
stream: ReadableStream<Uint8Array>,
): AsyncGenerator<Row> {
const reader = stream.getReader(); const reader = stream.getReader();
let buf = new Uint8Array(); let buf = new Uint8Array();
for (;;) { for (;;) {

View file

@ -1,10 +1,10 @@
import { test, expect } from "bun:test"; import { test, expect } from "bun:test";
import { DAL } from ".." import { DAL } from "..";
const options = { const options = {
database: "test.sqlite", database: "test.sqlite",
url: "http://localhost:8111", url: "http://localhost:8111",
} };
class DTO { class DTO {
id: number = 0; id: number = 0;

Binary file not shown.

View file

@ -1 +1 @@
export { default as DAL } from './Builder'; export { default as DAL } from "./Builder";

View file

@ -1,3 +1,3 @@
# [wip] DAL # [wip] DAL
NodeJS Client for the [DAL]() Server.
NodeJS Client for the [DAL]() Server.

View file

@ -3,7 +3,8 @@
"module": "dal/index.ts", "module": "dal/index.ts",
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"@types/bun": "latest" "@types/bun": "latest",
"prettier": "^3.3.3"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.0.0" "typescript": "^5.0.0"
@ -14,6 +15,7 @@
"scripts": { "scripts": {
"test:client": "bun test:*", "test:client": "bun test:*",
"test:dal": "bun test dal/__test__", "test:dal": "bun test dal/__test__",
"test:serve": "cd dal/__test__/srv && go run main.go" "test:serve": "cd dal/__test__/srv && go run main.go",
"fmt": "prettier --write ."
} }
} }

View file

@ -2,17 +2,19 @@
import { encode } from "https://deno.land/x/msgpack@v1.2/mod.ts"; import { encode } from "https://deno.land/x/msgpack@v1.2/mod.ts";
const Query = { const Query = {
"db": "database.sqlite", db: "database.sqlite",
"commands": [ commands: [
{"method": "In", "args": ["data"]}, { method: "In", args: ["data"] },
{ {
"method": "Find", method: "Find",
"args": [{ args: [
"a": 1, {
"b": { a: 1,
"$gt": 2, b: {
$gt: 2,
}, },
}] },
],
}, },
], ],
}; };