[wip] nodejs client

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-15 10:19:50 +02:00
parent ffb64f1d65
commit aa580b158e
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
13 changed files with 394 additions and 19 deletions

175
.gitignore vendored Normal file
View file

@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

21
README.md Normal file
View file

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

BIN
bun.lockb Executable file

Binary file not shown.

115
dal/Builder.ts Normal file
View file

@ -0,0 +1,115 @@
import type { Request } from "./Protocol";
import { METHODS } from "./Protocol";
type Primitive = string | number | boolean | null;
interface Filter extends Record<string, unknown> {
$eq?: Primitive;
$ne?: Primitive;
$gt?: Primitive;
$gte?: Primitive;
$lt?: Primitive;
$lte?: Primitive;
$in?: Primitive[];
$nin?: Primitive[];
$like?: string;
$nlike?: string;
$glob?: string;
$between?: [Primitive, Primitive];
$nbetween?: [Primitive, Primitive];
}
interface FindFilter {
[key: string]: Primitive | Filter | Filter[] | undefined;
}
type JoinCondition = "inner" | "left" | "cross" | "full outer";
type JoinFilter = {
$for: string;
$do: FindFilter;
$as?: JoinCondition;
};
export type SortOptions = Record<string, 1 | -1 | "asc" | "desc">;
export default class Builder {
private request: Request;
constructor(database: string) {
this.request = {
id: 0,
db: database,
commands: [],
};
}
private format(): void {
this.request.commands = METHODS.map((method) => {
const command = this.request.commands.find((command) => command.method === method);
return command;
}).filter(Boolean) as Request["commands"];
}
In(table: string): Builder {
this.request.commands.push({ method: "In", args: [table] });
return this;
}
Find(filter: FindFilter): Builder {
this.request.commands.push({ method: "Find", args: [filter] });
return this;
}
Select(fields: string[]): Builder {
this.request.commands.push({ method: "Select", args: fields });
return this;
}
Fields(fields: string[]): Builder {
this.Select(fields);
return this;
}
Join(...joins: JoinFilter[]): Builder {
this.request.commands.push({ method: "Join", args: joins });
return this;
}
Group(fields: string[]): Builder {
this.request.commands.push({ method: "Group", args: fields });
return this;
}
Sort(fields: SortOptions): Builder {
this.request.commands.push({ method: "Sort", args: fields });
return this;
}
Limit(limit: number): Builder {
this.request.commands.push({ method: "Limit", args: [limit] });
return this;
}
Offset(offset: number): Builder {
this.request.commands.push({ method: "Offset", args: [offset] });
return this;
}
Delete(): Builder {
this.request.commands.push({ method: "Delete", args: [] });
return this;
}
Insert(data: Record<string, unknown>): Builder {
this.request.commands.push({ method: "Insert", args: [data] });
return this;
}
Set(data: Record<string, unknown>): Builder {
this.request.commands.push({ method: "Set", args: [data] });
return this;
}
Update(data: Record<string, unknown>): Builder {
this.Set(data);
return this;
}
OnConflict(...fields: string[]): Builder {
this.request.commands.push({ method: "OnConflict", args: fields });
return this;
}
DoUpdate(...fields: string[]): Builder {
this.request.commands.push({ method: "DoUpdate", args: fields });
return this;
}
DoNothing(): Builder {
this.request.commands.push({ method: "DoNothing", args: [] });
return this;
}
}

18
dal/Protocol.ts Normal file
View file

@ -0,0 +1,18 @@
import { encode } from '@msgpack/msgpack';
export interface Method {
method: string;
args: any;
}
export interface Request {
id: number;
db: string;
commands: Method[];
}
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 {
return encode(request);
}

0
dal/index.ts Normal file
View file

3
dal/readme.md Normal file
View file

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

14
package.json Normal file
View file

@ -0,0 +1,14 @@
{
"name": "dal",
"module": "dal/index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@msgpack/msgpack": "^3.0.0-beta2"
}
}

View file

@ -1,3 +1,4 @@
//@ts-ignore
import { encode } from "https://deno.land/x/msgpack@v1.2/mod.ts";
const Query = {
@ -17,4 +18,5 @@ const Query = {
};
const encoded: Uint8Array = encode(Query);
//@ts-ignore
Deno.writeFileSync("proto_test.msgpack", encoded);

View file

@ -12,7 +12,7 @@ import (
//go:generate msgp
type BuildCmd struct {
type BuilderMethod struct {
Method string `msg:"method"`
Args []interface{} `msg:"args"`
}
@ -20,7 +20,7 @@ type BuildCmd struct {
type Request struct {
Id uint32 `msg:"id"`
Db string `msg:"db"`
Commands []BuildCmd `msg:"commands"`
Commands []BuilderMethod `msg:"commands"`
}
var allowedMethods = strings.Split(builder.BUILDER_CLIENT_METHODS, "|")

View file

@ -7,7 +7,7 @@ import (
)
// DecodeMsg implements msgp.Decodable
func (z *BuildCmd) DecodeMsg(dc *msgp.Reader) (err error) {
func (z *BuilderMethod) DecodeMsg(dc *msgp.Reader) (err error) {
var field []byte
_ = field
var zb0001 uint32
@ -61,7 +61,7 @@ func (z *BuildCmd) DecodeMsg(dc *msgp.Reader) (err error) {
}
// EncodeMsg implements msgp.Encodable
func (z *BuildCmd) EncodeMsg(en *msgp.Writer) (err error) {
func (z *BuilderMethod) EncodeMsg(en *msgp.Writer) (err error) {
// map header, size 2
// write "method"
err = en.Append(0x82, 0xa6, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64)
@ -94,7 +94,7 @@ func (z *BuildCmd) EncodeMsg(en *msgp.Writer) (err error) {
}
// MarshalMsg implements msgp.Marshaler
func (z *BuildCmd) MarshalMsg(b []byte) (o []byte, err error) {
func (z *BuilderMethod) MarshalMsg(b []byte) (o []byte, err error) {
o = msgp.Require(b, z.Msgsize())
// map header, size 2
// string "method"
@ -114,7 +114,7 @@ func (z *BuildCmd) MarshalMsg(b []byte) (o []byte, err error) {
}
// UnmarshalMsg implements msgp.Unmarshaler
func (z *BuildCmd) UnmarshalMsg(bts []byte) (o []byte, err error) {
func (z *BuilderMethod) UnmarshalMsg(bts []byte) (o []byte, err error) {
var field []byte
_ = field
var zb0001 uint32
@ -169,7 +169,7 @@ func (z *BuildCmd) UnmarshalMsg(bts []byte) (o []byte, err error) {
}
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (z *BuildCmd) Msgsize() (s int) {
func (z *BuilderMethod) Msgsize() (s int) {
s = 1 + 7 + msgp.StringPrefixSize + len(z.Method) + 5 + msgp.ArrayHeaderSize
for za0001 := range z.Args {
s += msgp.GuessSize(z.Args[za0001])
@ -217,7 +217,7 @@ func (z *Request) DecodeMsg(dc *msgp.Reader) (err error) {
if cap(z.Commands) >= int(zb0002) {
z.Commands = (z.Commands)[:zb0002]
} else {
z.Commands = make([]BuildCmd, zb0002)
z.Commands = make([]BuilderMethod, zb0002)
}
for za0001 := range z.Commands {
var zb0003 uint32
@ -417,7 +417,7 @@ func (z *Request) UnmarshalMsg(bts []byte) (o []byte, err error) {
if cap(z.Commands) >= int(zb0002) {
z.Commands = (z.Commands)[:zb0002]
} else {
z.Commands = make([]BuildCmd, zb0002)
z.Commands = make([]BuilderMethod, zb0002)
}
for za0001 := range z.Commands {
var zb0003 uint32

View file

@ -10,7 +10,7 @@ import (
)
func TestMarshalUnmarshalBuildCmd(t *testing.T) {
v := BuildCmd{}
v := BuilderMethod{}
bts, err := v.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
@ -33,7 +33,7 @@ func TestMarshalUnmarshalBuildCmd(t *testing.T) {
}
func BenchmarkMarshalMsgBuildCmd(b *testing.B) {
v := BuildCmd{}
v := BuilderMethod{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
@ -42,7 +42,7 @@ func BenchmarkMarshalMsgBuildCmd(b *testing.B) {
}
func BenchmarkAppendMsgBuildCmd(b *testing.B) {
v := BuildCmd{}
v := BuilderMethod{}
bts := make([]byte, 0, v.Msgsize())
bts, _ = v.MarshalMsg(bts[0:0])
b.SetBytes(int64(len(bts)))
@ -54,7 +54,7 @@ func BenchmarkAppendMsgBuildCmd(b *testing.B) {
}
func BenchmarkUnmarshalBuildCmd(b *testing.B) {
v := BuildCmd{}
v := BuilderMethod{}
bts, _ := v.MarshalMsg(nil)
b.ReportAllocs()
b.SetBytes(int64(len(bts)))
@ -68,7 +68,7 @@ func BenchmarkUnmarshalBuildCmd(b *testing.B) {
}
func TestEncodeDecodeBuildCmd(t *testing.T) {
v := BuildCmd{}
v := BuilderMethod{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
@ -77,7 +77,7 @@ func TestEncodeDecodeBuildCmd(t *testing.T) {
t.Log("WARNING: TestEncodeDecodeBuildCmd Msgsize() is inaccurate")
}
vn := BuildCmd{}
vn := BuilderMethod{}
err := msgp.Decode(&buf, &vn)
if err != nil {
t.Error(err)
@ -92,7 +92,7 @@ func TestEncodeDecodeBuildCmd(t *testing.T) {
}
func BenchmarkEncodeBuildCmd(b *testing.B) {
v := BuildCmd{}
v := BuilderMethod{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
b.SetBytes(int64(buf.Len()))
@ -106,7 +106,7 @@ func BenchmarkEncodeBuildCmd(b *testing.B) {
}
func BenchmarkDecodeBuildCmd(b *testing.B) {
v := BuildCmd{}
v := BuilderMethod{}
var buf bytes.Buffer
msgp.Encode(&buf, &v)
b.SetBytes(int64(buf.Len()))

27
tsconfig.json Normal file
View file

@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}