rads-db 3.0.0 → 3.0.2

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/dist/index.d.ts CHANGED
@@ -139,6 +139,8 @@ interface UiDecoratorArgs<T = any> {
139
139
  iconField?: keyof T;
140
140
  /** Search operator that's used in simple search by name. Defaults to 'istartsWith' */
141
141
  searchOperator?: 'startsWith' | 'istartsWith' | 'contains' | 'icontains';
142
+ /** Defines section name in rads-ui where this entity will be grouped under */
143
+ group?: string;
142
144
  }
143
145
  interface EntityDecoratorArgs {
144
146
  driver?: string;
@@ -12,7 +12,17 @@ const databaseClientCache = {};
12
12
  const containerClientCache = {};
13
13
  const simpleSubclauseRegex = /^[a-z0-9_@]+.[a-z0-9_@]+ = [a-z0-9_@.]+$/i;
14
14
  var _default = options => (schema, entity) => {
15
- const client = getCosmosContainerClient(options);
15
+ const normalizedOptions = {
16
+ databaseName: "main",
17
+ containerName: "main",
18
+ createDatabaseIfNotExists: false,
19
+ options: {},
20
+ ...options
21
+ };
22
+ const client = getCosmosContainerClient(normalizedOptions);
23
+ if (options.createDatabaseIfNotExists) {
24
+ createDatabaseIfNotExists(normalizedOptions, client.database.client);
25
+ }
16
26
  async function getItemByIds(ids, ctx) {
17
27
  const {
18
28
  query,
@@ -442,11 +452,6 @@ function getCosmosQueryWhereInner(ctx, parameters, whereArgs) {
442
452
  return fn(ctx, parameters, whereArgs);
443
453
  }
444
454
  function getCosmosContainerClient(options) {
445
- options = {
446
- databaseName: "main",
447
- containerName: "main",
448
- ...options
449
- };
450
455
  const {
451
456
  databaseName,
452
457
  containerName,
@@ -459,11 +464,6 @@ function getCosmosContainerClient(options) {
459
464
  return containerClientCache[key];
460
465
  }
461
466
  function getCosmosDatabaseClient(options) {
462
- options = {
463
- databaseName: "main",
464
- containerName: "main",
465
- ...options
466
- };
467
467
  const {
468
468
  databaseName,
469
469
  endpoint
@@ -475,11 +475,6 @@ function getCosmosDatabaseClient(options) {
475
475
  return databaseClientCache[key];
476
476
  }
477
477
  function getCosmosAccountClient(options) {
478
- options = {
479
- databaseName: "main",
480
- containerName: "main",
481
- ...options
482
- };
483
478
  const {
484
479
  endpoint,
485
480
  accountKey
@@ -500,4 +495,17 @@ function getWhereNameOperatorPair(whereKey) {
500
495
  return ["_type", operator];
501
496
  }
502
497
  return whereKey.split("_");
498
+ }
499
+ async function createDatabaseIfNotExists(options, client) {
500
+ try {
501
+ await client.databases.createIfNotExists({
502
+ id: options.databaseName
503
+ });
504
+ await client.database(options.databaseName).containers.create({
505
+ id: options.containerName,
506
+ partitionKey: "/_partition"
507
+ });
508
+ } catch (e) {
509
+ console.error("Error initializing CosmosDb: ", e);
510
+ }
503
511
  }
@@ -1,9 +1,15 @@
1
1
  import type { CosmosClientOptions } from '@azure/cosmos';
2
2
  interface AzureCosmosDriverOptions {
3
+ /** HTTPS endpoint of your cosmosdb instance. For example, https://mydb.documents.azure.com:443/ */
3
4
  endpoint: string;
4
- accountKey?: string;
5
+ /** Account key for your cosmosdb instance. Available in Azure Portal => CosmosDB => Keys => Primary Key */
6
+ accountKey: string;
7
+ /** Defaults to "main" */
5
8
  databaseName?: string;
9
+ /** Defaults to "main" */
6
10
  containerName?: string;
11
+ /** Creates database and/or container if they don't exist */
12
+ createDatabaseIfNotExists?: boolean;
7
13
  options?: Omit<CosmosClientOptions, 'key' | 'endpoint'>;
8
14
  }
9
15
  declare const _default: (options: AzureCosmosDriverOptions) => (schema: Schema, entity: string) => MinimalDriver;
@@ -5,7 +5,17 @@ const databaseClientCache = {};
5
5
  const containerClientCache = {};
6
6
  const simpleSubclauseRegex = /^[a-z0-9_@]+.[a-z0-9_@]+ = [a-z0-9_@.]+$/i;
7
7
  export default (options) => (schema, entity) => {
8
- const client = getCosmosContainerClient(options);
8
+ const normalizedOptions = {
9
+ databaseName: "main",
10
+ containerName: "main",
11
+ createDatabaseIfNotExists: false,
12
+ options: {},
13
+ ...options
14
+ };
15
+ const client = getCosmosContainerClient(normalizedOptions);
16
+ if (options.createDatabaseIfNotExists) {
17
+ createDatabaseIfNotExists(normalizedOptions, client.database.client);
18
+ }
9
19
  async function getItemByIds(ids, ctx) {
10
20
  const { query, parameters } = getCosmosQuery(schema, entity, { where: { id_in: ids } });
11
21
  const response = client.items.query({
@@ -279,7 +289,6 @@ function getCosmosQueryWhereInner(ctx, parameters, whereArgs) {
279
289
  return fn(ctx, parameters, whereArgs);
280
290
  }
281
291
  function getCosmosContainerClient(options) {
282
- options = { databaseName: "main", containerName: "main", ...options };
283
292
  const { databaseName, containerName, endpoint } = options;
284
293
  const key = `${endpoint}|${databaseName}|${containerName}`;
285
294
  if (!containerClientCache[key]) {
@@ -288,7 +297,6 @@ function getCosmosContainerClient(options) {
288
297
  return containerClientCache[key];
289
298
  }
290
299
  function getCosmosDatabaseClient(options) {
291
- options = { databaseName: "main", containerName: "main", ...options };
292
300
  const { databaseName, endpoint } = options;
293
301
  const key = `${endpoint}|${databaseName}`;
294
302
  if (!databaseClientCache[key]) {
@@ -297,7 +305,6 @@ function getCosmosDatabaseClient(options) {
297
305
  return databaseClientCache[key];
298
306
  }
299
307
  function getCosmosAccountClient(options) {
300
- options = { databaseName: "main", containerName: "main", ...options };
301
308
  const { endpoint, accountKey } = options;
302
309
  const key = endpoint;
303
310
  if (!cosmosClientCache[key]) {
@@ -312,3 +319,11 @@ function getWhereNameOperatorPair(whereKey) {
312
319
  }
313
320
  return whereKey.split("_");
314
321
  }
322
+ async function createDatabaseIfNotExists(options, client) {
323
+ try {
324
+ await client.databases.createIfNotExists({ id: options.databaseName });
325
+ await client.database(options.databaseName).containers.create({ id: options.containerName, partitionKey: "/_partition" });
326
+ } catch (e) {
327
+ console.error("Error initializing CosmosDb: ", e);
328
+ }
329
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "files": [
5
5
  "dist",
6
6
  "drivers",