@cfworker/cosmos 1.12.2 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -4
- package/dist/client.d.ts +5 -4
- package/dist/client.js +10 -4
- package/dist/signer.d.ts +1 -1
- package/dist/signer.js +4 -4
- package/dist/types.d.ts +4 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +21 -0
- package/package.json +1 -1
- package/src/client.ts +30 -18
- package/src/signer.ts +4 -4
- package/src/types.ts +14 -0
- package/src/util.ts +28 -0
package/README.md
CHANGED
|
@@ -44,10 +44,14 @@ import { CosmosClient } from '@cfworker/cosmos';
|
|
|
44
44
|
|
|
45
45
|
// 🚨️ Do not commit your Cosmos DB master key to source control!
|
|
46
46
|
// Use a bundler like Rollup, Parcel, or Webpack to interpolate your master key at build-time.
|
|
47
|
-
const
|
|
47
|
+
const accountKey = '...top secret master key copied from the azure portal...';
|
|
48
48
|
const endpoint = 'https://xxxxxxxxxxxx.documents.azure.com';
|
|
49
49
|
|
|
50
|
-
const client = new CosmosClient({ endpoint,
|
|
50
|
+
const client = new CosmosClient({ endpoint, accountKey });
|
|
51
|
+
|
|
52
|
+
// or
|
|
53
|
+
|
|
54
|
+
const client = new CosmosClient({ connectionString });
|
|
51
55
|
```
|
|
52
56
|
|
|
53
57
|
### Specify consistency level
|
|
@@ -58,7 +62,7 @@ You may override the default value when constructing the CosmosClient.
|
|
|
58
62
|
```ts
|
|
59
63
|
const consistencyLevel = 'Eventual';
|
|
60
64
|
|
|
61
|
-
const client = new CosmosClient({ endpoint,
|
|
65
|
+
const client = new CosmosClient({ endpoint, accountKey, consistencyLevel });
|
|
62
66
|
```
|
|
63
67
|
|
|
64
68
|
Most methods on CosmosClient accept a `consistencyLevel` argument to enable overriding the client's consistency level on a case-by-case basis.
|
|
@@ -76,7 +80,7 @@ You may specify default values for these arguments when constructing the CosmosC
|
|
|
76
80
|
const dbId = 'my-db';
|
|
77
81
|
const collId = 'my-coll';
|
|
78
82
|
|
|
79
|
-
const client = new CosmosClient({ endpoint,
|
|
83
|
+
const client = new CosmosClient({ endpoint, accountKey, dbId, collId });
|
|
80
84
|
```
|
|
81
85
|
|
|
82
86
|
### Authenticating with resource tokens
|
package/dist/client.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { FeedResponse, ItemResponse } from './response.js';
|
|
2
2
|
import { RetryPolicy } from './retry.js';
|
|
3
3
|
import { SessionContainer } from './session.js';
|
|
4
|
-
import { Collection, ConsistencyLevel, Database, Document, IndexingDirective, IndexingPolicy, OfferType, PartitionKeyDefinition, Resource } from './types.js';
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
import { Collection, ConnectionInfo, ConsistencyLevel, Database, Document, IndexingDirective, IndexingPolicy, OfferType, PartitionKeyDefinition, Resource } from './types.js';
|
|
5
|
+
export declare type CosmosClientConfig = (ConnectionInfo & CosmosClientConfigBase) | ({
|
|
6
|
+
connectionString: string;
|
|
7
|
+
} & CosmosClientConfigBase);
|
|
8
|
+
interface CosmosClientConfigBase {
|
|
8
9
|
retryPolicy?: RetryPolicy;
|
|
9
10
|
consistencyLevel?: ConsistencyLevel;
|
|
10
11
|
dbId?: string;
|
package/dist/client.js
CHANGED
|
@@ -2,13 +2,19 @@ import { FeedResponse, ItemResponse } from './response.js';
|
|
|
2
2
|
import { defaultRetryPolicy } from './retry.js';
|
|
3
3
|
import { DefaultSessionContainer } from './session.js';
|
|
4
4
|
import { getSigner } from './signer.js';
|
|
5
|
-
import { assertArg, escapeNonASCII, uri } from './util.js';
|
|
5
|
+
import { assertArg, escapeNonASCII, parseConnectionString, uri } from './util.js';
|
|
6
6
|
export class CosmosClient {
|
|
7
7
|
constructor(config) {
|
|
8
8
|
this.requestCharges = 0;
|
|
9
9
|
this.retries = { count: 0, delayMs: 0 };
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
let { endpoint, accountKey } = 'connectionString' in config
|
|
11
|
+
? parseConnectionString(config.connectionString)
|
|
12
|
+
: config;
|
|
13
|
+
if (endpoint.endsWith('/')) {
|
|
14
|
+
endpoint = endpoint.slice(0, -1);
|
|
15
|
+
}
|
|
16
|
+
this.endpoint = endpoint;
|
|
17
|
+
this.signer = getSigner(accountKey);
|
|
12
18
|
this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
|
|
13
19
|
this.consistencyLevel = config.consistencyLevel ?? 'Session';
|
|
14
20
|
this.dbId = config.dbId;
|
|
@@ -16,7 +22,7 @@ export class CosmosClient {
|
|
|
16
22
|
this.enableCrossPartitionQueries =
|
|
17
23
|
config.enableCrossPartitionQueries ?? true;
|
|
18
24
|
this.sessions = config.sessions ?? new DefaultSessionContainer();
|
|
19
|
-
this.systemFetch = config.fetch ?? fetch.bind(
|
|
25
|
+
this.systemFetch = config.fetch ?? fetch.bind(globalThis);
|
|
20
26
|
}
|
|
21
27
|
async getDatabases(args = {}) {
|
|
22
28
|
const url = this.endpoint + `/dbs`;
|
package/dist/signer.d.ts
CHANGED
package/dist/signer.js
CHANGED
|
@@ -35,9 +35,9 @@ export class Signer {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
const signers = {};
|
|
38
|
-
export function getSigner(
|
|
39
|
-
if (!signers[
|
|
40
|
-
signers[
|
|
38
|
+
export function getSigner(accountKey) {
|
|
39
|
+
if (!signers[accountKey]) {
|
|
40
|
+
signers[accountKey] = new Signer(accountKey);
|
|
41
41
|
}
|
|
42
|
-
return signers[
|
|
42
|
+
return signers[accountKey];
|
|
43
43
|
}
|
package/dist/types.d.ts
CHANGED
package/dist/util.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ConnectionInfo } from './types';
|
|
1
2
|
export declare function escapeNonASCII(s: string): string;
|
|
2
3
|
export declare function assertArg(name: string, value: any): asserts value;
|
|
3
4
|
export declare function uri(strings: TemplateStringsArray, ...values: any[]): string;
|
|
5
|
+
export declare function parseConnectionString(connString: string): ConnectionInfo;
|
package/dist/util.js
CHANGED
|
@@ -13,3 +13,24 @@ export function uri(strings, ...values) {
|
|
|
13
13
|
}
|
|
14
14
|
return s;
|
|
15
15
|
}
|
|
16
|
+
export function parseConnectionString(connString) {
|
|
17
|
+
const props = {};
|
|
18
|
+
connString.split(';').forEach(kv => {
|
|
19
|
+
const pos = kv.indexOf('=');
|
|
20
|
+
if (pos < 1) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const key = kv.slice(0, pos);
|
|
24
|
+
const val = kv.slice(pos + 1);
|
|
25
|
+
if (key && val) {
|
|
26
|
+
props[key] = val;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (!props.AccountEndpoint || !props.AccountKey) {
|
|
30
|
+
throw new Error('Invalid connection string');
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
endpoint: props.AccountEndpoint,
|
|
34
|
+
accountKey: props.AccountKey
|
|
35
|
+
};
|
|
36
|
+
}
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { DefaultSessionContainer, SessionContainer } from './session.js';
|
|
|
4
4
|
import { getSigner, Signer } from './signer.js';
|
|
5
5
|
import {
|
|
6
6
|
Collection,
|
|
7
|
+
ConnectionInfo,
|
|
7
8
|
ConsistencyLevel,
|
|
8
9
|
Database,
|
|
9
10
|
Document,
|
|
@@ -13,21 +14,25 @@ import {
|
|
|
13
14
|
PartitionKeyDefinition,
|
|
14
15
|
Resource
|
|
15
16
|
} from './types.js';
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
17
|
+
import {
|
|
18
|
+
assertArg,
|
|
19
|
+
escapeNonASCII,
|
|
20
|
+
parseConnectionString,
|
|
21
|
+
uri
|
|
22
|
+
} from './util.js';
|
|
23
|
+
|
|
24
|
+
export type CosmosClientConfig =
|
|
25
|
+
| (ConnectionInfo & CosmosClientConfigBase)
|
|
26
|
+
| ({
|
|
27
|
+
/**
|
|
28
|
+
* Cosmos DB connection string.
|
|
29
|
+
* @example
|
|
30
|
+
* "AccountEndpoint=https://xxxxxxxxxx.documents.azure.com:443/;accountKey=xxxxxxxxxx;"
|
|
31
|
+
*/
|
|
32
|
+
connectionString: string;
|
|
33
|
+
} & CosmosClientConfigBase);
|
|
34
|
+
|
|
35
|
+
interface CosmosClientConfigBase {
|
|
31
36
|
/**
|
|
32
37
|
* The retry policy to use when requests are throttled.
|
|
33
38
|
*/
|
|
@@ -79,8 +84,15 @@ export class CosmosClient {
|
|
|
79
84
|
public retries = { count: 0, delayMs: 0 };
|
|
80
85
|
|
|
81
86
|
constructor(config: CosmosClientConfig) {
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
let { endpoint, accountKey } =
|
|
88
|
+
'connectionString' in config
|
|
89
|
+
? parseConnectionString(config.connectionString)
|
|
90
|
+
: config;
|
|
91
|
+
if (endpoint.endsWith('/')) {
|
|
92
|
+
endpoint = endpoint.slice(0, -1);
|
|
93
|
+
}
|
|
94
|
+
this.endpoint = endpoint;
|
|
95
|
+
this.signer = getSigner(accountKey);
|
|
84
96
|
this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
|
|
85
97
|
this.consistencyLevel = config.consistencyLevel ?? 'Session';
|
|
86
98
|
this.dbId = config.dbId;
|
|
@@ -88,7 +100,7 @@ export class CosmosClient {
|
|
|
88
100
|
this.enableCrossPartitionQueries =
|
|
89
101
|
config.enableCrossPartitionQueries ?? true;
|
|
90
102
|
this.sessions = config.sessions ?? new DefaultSessionContainer();
|
|
91
|
-
this.systemFetch = config.fetch ?? fetch.bind(
|
|
103
|
+
this.systemFetch = config.fetch ?? fetch.bind(globalThis);
|
|
92
104
|
}
|
|
93
105
|
|
|
94
106
|
public async getDatabases(
|
package/src/signer.ts
CHANGED
|
@@ -55,9 +55,9 @@ export class Signer {
|
|
|
55
55
|
|
|
56
56
|
const signers: Record<string, Signer> = {};
|
|
57
57
|
|
|
58
|
-
export function getSigner(
|
|
59
|
-
if (!signers[
|
|
60
|
-
signers[
|
|
58
|
+
export function getSigner(accountKey: string) {
|
|
59
|
+
if (!signers[accountKey]) {
|
|
60
|
+
signers[accountKey] = new Signer(accountKey);
|
|
61
61
|
}
|
|
62
|
-
return signers[
|
|
62
|
+
return signers[accountKey];
|
|
63
63
|
}
|
package/src/types.ts
CHANGED
|
@@ -66,3 +66,17 @@ export interface Collection extends PersistedResource {
|
|
|
66
66
|
_udfs: 'udfs/';
|
|
67
67
|
_conflicts: 'conflicts/';
|
|
68
68
|
}
|
|
69
|
+
|
|
70
|
+
export interface ConnectionInfo {
|
|
71
|
+
/**
|
|
72
|
+
* Cosmos DB endpoint.
|
|
73
|
+
* @example
|
|
74
|
+
* "https://xxxxxxxxxx.documents.azure.com"
|
|
75
|
+
*/
|
|
76
|
+
endpoint: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Cosmos DB key.
|
|
80
|
+
*/
|
|
81
|
+
accountKey: string;
|
|
82
|
+
}
|
package/src/util.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ConnectionInfo } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Escape non-ASCII characters as \uXXXX
|
|
3
5
|
*/
|
|
@@ -29,3 +31,29 @@ export function uri(strings: TemplateStringsArray, ...values: any[]) {
|
|
|
29
31
|
}
|
|
30
32
|
return s;
|
|
31
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parses a connection string for Cosmos DB.
|
|
37
|
+
* @param connString Cosmos DB connection string.
|
|
38
|
+
*/
|
|
39
|
+
export function parseConnectionString(connString: string): ConnectionInfo {
|
|
40
|
+
const props: Record<string, string> = {};
|
|
41
|
+
connString.split(';').forEach(kv => {
|
|
42
|
+
const pos = kv.indexOf('=');
|
|
43
|
+
if (pos < 1) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const key = kv.slice(0, pos);
|
|
47
|
+
const val = kv.slice(pos + 1);
|
|
48
|
+
if (key && val) {
|
|
49
|
+
props[key] = val;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
if (!props.AccountEndpoint || !props.AccountKey) {
|
|
53
|
+
throw new Error('Invalid connection string');
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
endpoint: props.AccountEndpoint,
|
|
57
|
+
accountKey: props.AccountKey
|
|
58
|
+
};
|
|
59
|
+
}
|