Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
peeeuzin committed Oct 25, 2022
0 parents commit f4d8573
Show file tree
Hide file tree
Showing 14 changed files with 4,927 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
/node_modules
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"es2021": true,
"node": true,
"jest/globals": true
},
"extends": ["standard", "prettier"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "prettier", "jest"],
"rules": {
"prettier/prettier": "error"
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dist
/src/models/*
/node_modules
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"tabWidth": 4,
"endOfLine": "lf"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2022 Rustbase

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Rustbase JS
A driver to use Rustbase with NodeJS

# Installation
```bash
npm install rustbase-js
```

# Usage
```js
const { Client } = require('rustbase-js');

const rustbase = new Client("rustbase://localhost:23561");

```

# Development
```bash
git clone https://github.com/rustbase/rustbase-js
cd rustbase-js
yarn install # Install dependencies
yarn run build:proto # Build the protobuf files
```

# Roadmap
- [x] Send queries
- [ ] Build a query builder


# License
[MIT License](/LICENSE)
29 changes: 29 additions & 0 deletions bin/proto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://github.com/CatsMiaow/node-grpc-typescript/blob/master/bin/proto.js (8/30/22)

import path from 'path';
import fs from 'fs';
import { execSync } from 'child_process';

const PROTO_DIR = path.join(__dirname, '../proto');
const MODEL_DIR = path.join(__dirname, '../src/models');
const PROTOC_PATH = path.join(
__dirname,
'../node_modules/grpc-tools/bin/protoc'
);
const PLUGIN_PATH = path.join(
__dirname,
'../node_modules/.bin/protoc-gen-ts_proto'
);

fs.mkdirSync(MODEL_DIR, { recursive: true });

const protoConfig = [
`--plugin=${PLUGIN_PATH}`,

'--ts_proto_opt=outputServices=grpc-js,env=node,useOptionals=messages,exportCommonSymbols=false,esModuleInterop=true',

`--ts_proto_out=${MODEL_DIR}`,
`--proto_path=${PROTO_DIR} ${PROTO_DIR}/*.proto`,
];

execSync(`${PROTOC_PATH} ${protoConfig.join(' ')}`);
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */

const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/src/tests/**/*.test.ts'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
}),
};
53 changes: 53 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "rustbase-js",
"version": "0.1.0",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"description": "A Javascript/Typescript Rustbase driver",
"license": "MIT",
"files": [
"dist"
],
"author": {
"name": "Rustbase"
},
"repository": {
"type": "git",
"url": "https://github.com/rustbase/rustbase-js"
},
"keywords": [
"Rustbase",
"Driver"
],
"scripts": {
"build": "tsc --build && tscpaths -p ./tsconfig.json -s . -o ./dist",
"build:proto": "ts-node ./bin/proto.ts",
"release": "yarn build:proto && yarn build"
},
"devDependencies": {
"@types/jest": "^29.0.0",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.3",
"eslint-plugin-n": "^15.2.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.1",
"grpc-tools": "^1.11.3",
"jest": "^29.0.2",
"prettier": "^2.7.1",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"ts-proto": "^1.123.1",
"tscpaths": "^0.0.9",
"typescript": "^4.8.2"
},
"dependencies": {
"@grpc/grpc-js": "^1.6.11",
"@grpc/proto-loader": "^0.7.2",
"bson": "^4.7.0"
}
}
28 changes: 28 additions & 0 deletions proto/rustbase.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";

package rustbase;

option optimize_for = SPEED;

service Rustbase {
rpc Query(QueryMessage) returns (QueryResult) {}
}

message QueryMessage {
string query = 1;
string database = 2;
}

enum QueryResultType {
OK = 0;
NOT_FOUND = 1;
ERROR = 2;
SYNTAX_ERROR = 3;
}

message QueryResult {
QueryResultType result_type = 1;
optional string message = 2;
optional bytes bson = 3;
optional string error_message = 4;
}
62 changes: 62 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { credentials } from '@grpc/grpc-js';
import { deserialize, Document } from 'bson';
import { RustbaseClient, QueryResultType } from '@models/rustbase';

export class Client {
private _database?: string;
private readonly client: RustbaseClient;

constructor(address: string) {
const client = new RustbaseClient(
address,
credentials.createInsecure()
);

this.client = client;
}

database(name: string) {
this._database = name;
}

async query(query: string) {
const database = this._database;

if (!database) {
throw new Error('No database selected');
}

return await new Promise<Document>((resolve, reject) => {
this.client.query(
{
database,
query,
},
(error, response) => {
if (error) {
reject(error);
}

if (
response.resultType === QueryResultType.OK &&
response.bson
) {
resolve(deserialize(response.bson));
} else if (response.resultType !== QueryResultType.OK) {
reject(new Error(response.errorMessage));
}
}
);
});
}
}

export function connect(uri: string): Client {
const url = new URL(uri);

if (url.protocol !== 'rustbase:') {
throw new Error('Invalid protocol');
}

return new Client(url.host);
}
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist",
"declaration": true,
"declarationMap": true,
"removeComments": true,
"baseUrl": ".",
"paths": {
"@models/*": ["src/models/*"]
}
}
}
Loading

0 comments on commit f4d8573

Please sign in to comment.