Skip to content

Support Java client #30

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ npm install @zeit/cosmosdb-server

It exposes the `cosmosdb-server` cli command as well.

### Setup for Java

Add the certificate to the Java CA certificates store like the following:

```sh
# Example for MacOS. Adjust paths and command according to your environment.
sudo keytool -keystore "$JAVA_HOME/jre/lib/security/cacerts" -importcert -alias vercel-cosmosdb-server -file node_modules/@zeit/cosmosdb-server/cert.pem
```

Also ensure to use Gateway mode for the connection policy since this server doesn't support Direct mode:

```java
import com.azure.cosmos.ConnectionMode;
import com.azure.cosmos.ConnectionPolicy

...

ConnectionPolicy policy = ConnectionPolicy.getDefaultPolicy();
policy.setConnectionMode(ConnectionMode.GATEWAY);
```

## API

#### cosmosServer(opts?: https.ServerOptions): https.Server
Expand Down
46 changes: 40 additions & 6 deletions src/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
/* eslint-disable class-methods-use-this, no-underscore-dangle */
import Databases from "./databases";
import Item from "./item";
import ItemObject from "./item-object";

export default class Account extends Item {
databases: Databases;

constructor() {
constructor(port: number) {
super({
id: "",
_etag: "",
_rid: "",
_rid: "localhost",
_self: "",
_ts: 0
});
_dbs: "//dbs/",
id: "localhost",
media: "//media/",
addresses: "//addresses/",
writableLocations: [
{
name: "South Central US",
databaseAccountEndpoint: `https://localhost:${port}/`
}
],
readableLocations: [
{
name: "South Central US",
databaseAccountEndpoint: `https://localhost:${port}/`
}
],
enableMultipleWriteLocations: false,
userReplicationPolicy: {
asyncReplication: false,
minReplicaSetSize: 1,
maxReplicasetSize: 4
},
userConsistencyPolicy: {
defaultConsistencyLevel: "Session"
},
systemReplicationPolicy: {
minReplicaSetSize: 1,
maxReplicasetSize: 4
},
readPolicy: {
primaryReadCoefficient: 1,
secondaryReadCoefficient: 1
},
queryEngineConfiguration:
'{"maxSqlQueryInputLength":262144,"maxJoinsPerSqlQuery":5,"maxLogicalAndPerSqlQuery":500,"maxLogicalOrPerSqlQuery":500,"maxUdfRefPerSqlQuery":10,"maxInExpressionItemsCount":16000,"queryMaxInMemorySortDocumentCount":500,"maxQueryRequestTimeoutFraction":0.9,"sqlAllowNonFiniteNumbers":false,"sqlAllowAggregateFunctions":true,"sqlAllowSubQuery":true,"sqlAllowScalarSubQuery":true,"allowNewKeywords":true,"sqlAllowLike":false,"maxSpatialQueryCells":12,"spatialMaxGeometryPointCount":256,"sqlAllowTop":true,"enableSpatialIndexing":true}'
} as ItemObject);

this.databases = new Databases(this);
}

Expand Down
4 changes: 2 additions & 2 deletions src/account/item-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ type ItemObject = {
partitionKey?: {
paths: string[];
};
_etag: string;
_etag?: string;
_rid: string;
_self: string;
_ts: number;
_ts?: number;
};

// eslint-disable-next-line no-undef
Expand Down
3 changes: 3 additions & 0 deletions src/handler/read-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Account from "../account";

export default (account: Account) => account.read();
11 changes: 11 additions & 0 deletions src/handler/read-addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as http from "http";
import Account from "../account";

export default (
account: Account,
req: http.IncomingMessage,
res: http.ServerResponse
) => {
res.statusCode = 501;
return { Message: "Not implemented" };
};
1 change: 0 additions & 1 deletion src/handler/read-meta.ts

This file was deleted.

13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const options: ServerOptions = {
};

export default (opts?: ServerOptions) => {
const account = new Account();
let account: Account | undefined;

return createServer({...options, ...opts}, (req, res) => {
const server = createServer({ ...options, ...opts }, (req, res) => {
const route = routes(req);

(async () => {
Expand Down Expand Up @@ -59,5 +59,14 @@ export default (opts?: ServerOptions) => {
res.end("");
}
});
}).on("listening", () => {
const address = server.address();
if (typeof address === "object" && address) {
account = new Account(address.port);
} else {
throw new Error(`Unexpected address type: ${address}`);
}
});

return server;
};
6 changes: 4 additions & 2 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import queryCollections from "./handler/query-collections";
import queryDatabases from "./handler/query-databases";
import queryDocuments from "./handler/query-documents";
import queryUserDefinedFunctions from "./handler/query-user-defined-functions";
import readAccount from "./handler/read-account";
import readAddresses from "./handler/read-addresses";
import readCollection from "./handler/read-collection";
import readCollections from "./handler/read-collections";
import readDatabase from "./handler/read-database";
import readDatabases from "./handler/read-databases";
import readDocument from "./handler/read-document";
import readDocuments from "./handler/read-documents";
import readMeta from "./handler/read-meta";
import readPartitionKeyRanges from "./handler/read-partition-key-ranges";
import readUserDefinedFunction from "./handler/read-user-defined-function";
import readUserDefinedFunctions from "./handler/read-user-defined-functions";
Expand All @@ -35,6 +36,7 @@ export default router({
"/dbs/:dbId": deleteDatabase
},
GET: {
"/addresses": readAddresses,
"/dbs/:dbId/colls/:collId/docs/:docId": readDocument,
"/dbs/:dbId/colls/:collId/docs": readDocuments,
"/dbs/:dbId/colls/:collId/pkranges": readPartitionKeyRanges,
Expand All @@ -44,7 +46,7 @@ export default router({
"/dbs/:dbId/colls": readCollections,
"/dbs/:dbId": readDatabase,
"/dbs": readDatabases,
"/": readMeta
"/": readAccount
},
POST: {
"/dbs/:dbId/colls/:collId/docs": createDocument,
Expand Down
2 changes: 1 addition & 1 deletion test/sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ npm run build:test -- --force
mv src/tsconfig.json{.bak,}

ACCOUNT_HOST="https://localhost:$port" npm run integration-test:node -- --i --exit \
-g 'Authorization|database account|http proxy|Change Feed|Cross Partition|indexing|Offer CRUD|Parallel Query As String|Permission|Query Metrics On Single Partition Collection|ResourceLink Trimming|Session Token|spatial|sproc|stored procedure|Trigger|trigger|TTL|User|Non Partitioned|Validate SSL verification|matching constant version & package version'
-g 'Authorization|http proxy|Change Feed|Cross Partition|indexing|Offer CRUD|Parallel Query As String|Permission|Query Metrics On Single Partition Collection|ResourceLink Trimming|Session Token|spatial|sproc|stored procedure|Trigger|trigger|TTL|User|Non Partitioned|Validate SSL verification|matching constant version & package version'