Skip to content

Typescript HTTP driver #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,13 @@ commands:
export DEPLOY_NPM_PASSWORD=$REPO_TYPEDB_PASSWORD
bazel run --jobs=8 --define version=$(git rev-parse HEAD) //nodejs:deploy-npm -- snapshot

deploy-http-ts-npm-snapshot-unix:
steps:
- run: |
export DEPLOY_NPM_USERNAME=$REPO_TYPEDB_USERNAME
export DEPLOY_NPM_PASSWORD=$REPO_TYPEDB_PASSWORD
bazel run --define version=$(git rev-parse HEAD) //http-ts:deploy-npm -- snapshot

test-npm-snapshot-unix:
steps:
- run: |
Expand All @@ -497,6 +504,14 @@ commands:
export DEPLOY_NPM_TOKEN=$REPO_NPM_TOKEN
bazel run --jobs=8 --define version=$(cat VERSION) //nodejs:deploy-npm --compilation_mode=opt -- release

deploy-http-ts-npm-release-unix:
steps:
- run: |
wget -q -O - https://cli-assets.heroku.com/apt/release.key | apt-key add -
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
export DEPLOY_NPM_TOKEN=$REPO_NPM_TOKEN
bazel run --define version=$(cat VERSION) //http-ts:deploy-npm --compilation_mode=opt -- release

jobs:

#################
Expand Down Expand Up @@ -621,6 +636,7 @@ jobs:
bazel-arch: amd64
- deploy-crate-snapshot-unix
- deploy-maven-snapshot-unix
- deploy-http-ts-npm-snapshot-unix
# - deploy-npm-snapshot-unix

deploy-snapshot-dotnet-any:
Expand Down Expand Up @@ -777,6 +793,7 @@ jobs:
bazel-arch: amd64
- deploy-crate-release-unix
- deploy-maven-release-unix
- deploy=http-ts-npm-release-unix
# - deploy-npm-release-unix

deploy-release-dotnet-any:
Expand Down
2 changes: 2 additions & 0 deletions .factory/automation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ build:
# tool/test/stop-cluster-servers.sh
# exit $TEST_SUCCESS

# TODO: introduce integration/behaviour tests for http-ts driver

# test-nodejs-integration:
# image: typedb-ubuntu-22.04
# dependencies:
Expand Down
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ npm_translate_lock(
load("@nodejs_npm//:repositories.bzl", "npm_repositories")
npm_repositories()

npm_translate_lock(
name = "http-ts_npm",
pnpm_lock = "//http-ts:pnpm-lock.yaml",
)

load("@http-ts_npm//:repositories.bzl", http_ts_npm_repositories = "npm_repositories")
http_ts_npm_repositories()

# Setup rules_ts
load("@aspect_rules_ts//ts:repositories.bzl", "rules_ts_dependencies")

Expand Down
94 changes: 94 additions & 0 deletions http-ts/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

exports_files([
"pnpm-lock.yaml",
"package.json",
"tsconfig.json",
])

load("@typedb_bazel_distribution//npm:rules.bzl", "assemble_npm", "deploy_npm")
load("@typedb_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
load("@typedb_dependencies//distribution:deployment.bzl", "deployment")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@aspect_rules_js//js:defs.bzl", "js_binary")
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package", "npm_package")
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
load("@http-ts_npm//:defs.bzl", "npm_link_all_packages")
load("//nodejs:tool/typedoc/rules.bzl", "typedoc_docs")
load("//nodejs:docs_structure.bzl", "dir_mapping")
load("//tool/docs:nodejs/rules.bzl", "typedoc_to_adoc")

npm_link_all_packages(
name = "node_modules",
)

ts_project(
name = "driver-http-ts",
srcs = glob(["*.ts"]),
tsconfig = ":tsconfig.json",
declaration = True,
deps = [":node_modules/typescript"],
transpiler = "tsc",
visibility = ["//visibility:public"],
out_dir = "dist",
)

jq(
name = "package",
srcs = ["package.json"],
filter = "|".join([
# Don't directly reference $STAMP as it's only set when stamping
# This 'as' syntax results in $stamp being null in unstamped builds.
"$ARGS.named.STAMP as $stamp",
# Provide a default using the "alternative operator" in case $stamp is null.
".version = ($stamp.STABLE_VERSION // \"0.0.0\")"
]),
)

npm_package(
name = "driver-http-ts-npm-package",
srcs = [":driver-http-ts", ":package", "README.md"],
include_runfiles = False,
replace_prefixes = { "dist/": "" },
visibility = ["//visibility:public"],
)

assemble_npm(
name= "assemble-npm",
target = ":driver-http-ts-npm-package",
)

deploy_npm(
name = "deploy-npm",
target = ":assemble-npm",
snapshot = deployment['npm']['snapshot'],
release = deployment['npm']['release'],
)

checkstyle_test(
name = "checkstyle",
include = glob([
"*",
]),
exclude = glob([
"*.json",
"*.md",
"pnpm-lock.yaml",
]),
license_type = "apache-header",
)
47 changes: 47 additions & 0 deletions http-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# TypeDB HTTP Typescript Driver

## Driver Architecture

To learn about how the TypeDB HTTP driver communicates with the TypeDB Server,
refer to the [HTTP API Reference](https://typedb.com/docs/reference/http-api).

## API Reference

To learn about the methods available for executing queries and retrieving their answers using Typescript, refer to
the [API Reference](https://typedb.com/docs/reference/http-drivers/typescript).

## Install TypeDB HTTP Typescript Driver through NPM

1. Install `typedb-driver-http` through npm:

```bash
npm install typedb-driver-http
```

2. Make sure the [TypeDB Server](https://docs.typedb.com/docs/running-typedb/install-and-run#start-the-typedb-server) is
running.
3. Use TypeDB Driver in your program:

```ts
import { TypeDBHttpDriver, isApiErrorResponse } from "typedb-driver-http";

const driver = new TypeDBHttpDriver({
username: "admin",
password: "password",
addresses: [ "localhost:1729" ],
});

const transactionResponse = await driver.openTransaction("database-name", "read");
if (isApiErrorResponse(transactionResponse)) throw transactionResponse.err;
const transactionId = transactionResponse.ok.transactionId;

const answerResponse = await driver.query(transactionId, "match entity $x;");
if (isApiErrorResponse(answerResponse)) throw answerResponse.err;
const answer = answerResponse.ok;

if (answer.answerType === "conceptRows") {
answer.answers.forEach((row) => {
console.log(row.data)
})
}
```
80 changes: 80 additions & 0 deletions http-ts/concept.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export type TypeKind = "entityType" | "relationType" | "attributeType" | "roleType";

export type ThingKind = "entity" | "relation" | "attribute";

export type ValueKind = "value";

export type ValueType = "boolean" | "integer" | "double" | "decimal" | "date" | "datetime" | "datetime-tz" | "duration" | "string" | "struct";

export type EdgeKind = "isa" | "has" | "links" | "sub" | "owns" | "relates" | "plays" | "isaExact" | "subExact" | "assigned" | "argument";

export interface EntityType {
kind: "entityType";
label: string;
}

export interface RelationType {
kind: "relationType";
label: string;
}

export interface RoleType {
kind: "roleType";
label: string;
}

export type AttributeType = {
label: string;
kind: "attributeType";
valueType: ValueType;
}

export type Type = InstantiableType | RoleType;
export type InstantiableType = EntityType | RelationType | AttributeType;

export interface Entity {
kind: "entity";
iid: string;
type: EntityType;
}

export interface Relation {
kind: "relation";
iid: string;
type: RelationType;
}

export interface Attribute {
kind: "attribute";
iid: string;
value: any;
valueType: ValueType;
type: AttributeType;
}

export interface Value {
kind: ValueKind;
value: any;
valueType: ValueType;
}

export type Concept = Type | Entity | Relation | Attribute | Value;
Loading