diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..d262403 --- /dev/null +++ b/README.md @@ -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" } +] +``` \ No newline at end of file diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000..35721ef Binary files /dev/null and b/bun.lockb differ diff --git a/dal/Builder.ts b/dal/Builder.ts new file mode 100644 index 0000000..c005800 --- /dev/null +++ b/dal/Builder.ts @@ -0,0 +1,115 @@ +import type { Request } from "./Protocol"; +import { METHODS } from "./Protocol"; + +type Primitive = string | number | boolean | null; + +interface Filter extends Record { + $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; + + +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): Builder { + this.request.commands.push({ method: "Insert", args: [data] }); + return this; + } + Set(data: Record): Builder { + this.request.commands.push({ method: "Set", args: [data] }); + return this; + } + Update(data: Record): 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; + } + +} \ No newline at end of file diff --git a/dal/Protocol.ts b/dal/Protocol.ts new file mode 100644 index 0000000..b16aa8d --- /dev/null +++ b/dal/Protocol.ts @@ -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); +} \ No newline at end of file diff --git a/dal/index.ts b/dal/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/dal/readme.md b/dal/readme.md new file mode 100644 index 0000000..5dc6253 --- /dev/null +++ b/dal/readme.md @@ -0,0 +1,3 @@ +# [wip] DAL +NodeJS Client for the [DAL]() Server. + diff --git a/package.json b/package.json new file mode 100644 index 0000000..206dca6 --- /dev/null +++ b/package.json @@ -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" + } +} \ No newline at end of file diff --git a/pkg/__test__/proto_test.ts b/pkg/__test__/proto_test.ts index 737ce10..a7dd59c 100644 --- a/pkg/__test__/proto_test.ts +++ b/pkg/__test__/proto_test.ts @@ -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); \ No newline at end of file diff --git a/pkg/proto/request.go b/pkg/proto/request.go index 214750c..2c497ad 100644 --- a/pkg/proto/request.go +++ b/pkg/proto/request.go @@ -12,15 +12,15 @@ import ( //go:generate msgp -type BuildCmd struct { +type BuilderMethod struct { Method string `msg:"method"` Args []interface{} `msg:"args"` } type Request struct { - Id uint32 `msg:"id"` - Db string `msg:"db"` - Commands []BuildCmd `msg:"commands"` + Id uint32 `msg:"id"` + Db string `msg:"db"` + Commands []BuilderMethod `msg:"commands"` } var allowedMethods = strings.Split(builder.BUILDER_CLIENT_METHODS, "|") diff --git a/pkg/proto/request_gen.go b/pkg/proto/request_gen.go index 668ee67..ec08acd 100644 --- a/pkg/proto/request_gen.go +++ b/pkg/proto/request_gen.go @@ -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 diff --git a/pkg/proto/request_gen_test.go b/pkg/proto/request_gen_test.go index f3074c0..18037cf 100644 --- a/pkg/proto/request_gen_test.go +++ b/pkg/proto/request_gen_test.go @@ -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())) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/tsconfig.json @@ -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 + } +}