@vandenberghinc/volt 1.1.34 → 1.1.35

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.
@@ -502,10 +502,12 @@ export class Collection {
502
502
  */
503
503
  async init() {
504
504
  if (this.initialized === false) {
505
+ this.db.server.log(3, "Initializing collection: ", this.name);
505
506
  // Initialize NON transaction based.
506
507
  if (!this.is_transaction) {
507
508
  // Create collection.
508
509
  if (this._col == null) {
510
+ this.db.server.log(3, "Checking collection: ", this.name);
509
511
  // Start connection in dev mode.
510
512
  if (!this.db.server.production) {
511
513
  await this.db.ensure_connection();
@@ -519,10 +521,13 @@ export class Collection {
519
521
  }
520
522
  // Check if the collection exists
521
523
  if (this.db._listed_cols == null) {
524
+ this.db.server.log(3, "Listing collections...");
522
525
  this.db._listed_cols = await this.db._db.listCollections().toArray();
526
+ this.db.server.log(3, "Listed collections: " + this.db._listed_cols.map(x => x.name).join(", "));
523
527
  }
524
528
  // Create collection with retry logic for race conditions
525
529
  if (!this.db._listed_cols.find(x => x.name === this.name)) {
530
+ this.db.server.log(3, "Creating collection: " + this.name);
526
531
  let create_col_retries = 3;
527
532
  let last_error = null;
528
533
  let collection_created = false;
@@ -550,6 +555,7 @@ export class Collection {
550
555
  }
551
556
  }
552
557
  // Create collection.
558
+ this.db.server.log(3, "Initializing mongodb collection connection: " + this.name);
553
559
  this._col = this.db._db.collection(this.name);
554
560
  }
555
561
  // Assign as initialized when the column is created.
@@ -557,11 +563,13 @@ export class Collection {
557
563
  this.initialized = true;
558
564
  // Create ttl index.
559
565
  if (this.ttl_enabled) {
566
+ this.db.server.log(3, "Setting up TTL index for collection: " + this.name);
560
567
  await this._setup_ttl();
561
568
  }
562
569
  // Create indexes.
563
570
  if (this._init_indexes?.length) {
564
571
  for (const item of this._init_indexes) {
572
+ this.db.server.log(3, "Creating index " + JSON.stringify(item) + " on collection: " + this.name);
565
573
  await this.create_index(item);
566
574
  }
567
575
  }
@@ -11,6 +11,8 @@ export declare namespace Database {
11
11
  interface Opts {
12
12
  /** The database URI. */
13
13
  uri: string;
14
+ /** The database name, if not provided it will the database name from the connection URI will be used. */
15
+ database?: string;
14
16
  /** The additional cient options. */
15
17
  client?: mongodb.MongoClientOptions;
16
18
  }
@@ -26,6 +28,10 @@ export declare class Database {
26
28
  type: string;
27
29
  default: null;
28
30
  };
31
+ database_name: {
32
+ type: string;
33
+ default: undefined;
34
+ };
29
35
  client: {
30
36
  type: string;
31
37
  default: {};
@@ -35,13 +41,14 @@ export declare class Database {
35
41
  };
36
42
  };
37
43
  uri: string;
44
+ database_name: undefined | string;
38
45
  client_opts?: mongodb.MongoClientOptions;
39
46
  server: Server;
40
47
  client?: MongoClient;
41
48
  _db?: Db;
42
49
  collections: Map<string, Collection<any>>;
43
50
  _listed_cols: any;
44
- constructor({ uri, client, _server, }: Database.Opts & {
51
+ constructor({ uri, database, client, _server, }: Database.Opts & {
45
52
  _server: Server;
46
53
  });
47
54
  db(): Promise<Db>;
@@ -16,6 +16,7 @@ import { Collection } from "./collection.js";
16
16
  export class Database {
17
17
  static constructor_scheme = {
18
18
  uri: { type: "string", default: null },
19
+ database_name: { type: "string", default: undefined },
19
20
  client: { type: "object", default: {} },
20
21
  _server: { type: ["object", "undefined"] },
21
22
  // source: {type: "string", default: null},
@@ -27,6 +28,7 @@ export class Database {
27
28
  };
28
29
  // Attributes.
29
30
  uri;
31
+ database_name;
30
32
  client_opts;
31
33
  server;
32
34
  client;
@@ -34,8 +36,9 @@ export class Database {
34
36
  collections = new Map();
35
37
  // System.
36
38
  _listed_cols;
37
- constructor({ uri, client, _server, }) {
39
+ constructor({ uri, database, client, _server, }) {
38
40
  this.uri = uri;
41
+ this.database_name = database;
39
42
  this.client_opts = client;
40
43
  this.server = _server;
41
44
  // DEPRECATED
@@ -94,26 +97,30 @@ export class Database {
94
97
  connect_promise;
95
98
  async connect() {
96
99
  if (this.connect_promise) {
97
- return await this.connect_promise;
100
+ return this.connect_promise;
98
101
  }
99
102
  return this.connect_promise = new Promise(async (resolve, reject) => {
100
103
  try {
101
104
  if (this.client == null) {
102
105
  throw new Error('MongoDB client is not initialized.');
103
106
  }
107
+ this.server.log(3, "Connecting to the database client.");
104
108
  await this.client.connect();
105
- this._db = this.client.db();
109
+ this.server.log(3, "Connecting to the database.");
110
+ this._db = this.client.db(this.database_name);
106
111
  this.connected = true;
107
112
  this.server.log(1, "Connected to the database.");
113
+ resolve();
108
114
  }
109
115
  catch (error) {
110
116
  this.server.log.error(error);
111
- throw new Error('Error connecting to the database');
117
+ reject(new Error('Error connecting to the database'));
112
118
  }
113
119
  });
114
120
  }
115
121
  /** Initialize. */
116
122
  async initialize() {
123
+ this.server.log(3, "Initializing the database.");
117
124
  // Initialize client (same as before)
118
125
  const opts = this.client_opts ?? {};
119
126
  opts.serverApi ??= {
@@ -130,18 +137,15 @@ export class Database {
130
137
  opts.serverSelectionTimeoutMS = 1000;
131
138
  opts.connectTimeoutMS = 1000;
132
139
  this.client = new MongoClient(this.uri, opts);
133
- // In development we start the connection in the background so the server
134
- // can finish booting immediately. In production we still block.
135
- if (this.server.production === false) {
136
- this.connect(); // don’t await
137
- }
138
- else {
140
+ // In production we instanlty connect and initialize all columns.
141
+ if (this.server.production) {
139
142
  await this.connect(); // block in prod
140
143
  // In production we also wanna initialize all collections right away.
141
144
  for (const col of this.collections.values()) {
142
145
  await col.init();
143
146
  }
144
147
  }
148
+ this.server.log(3, "Database initialized.");
145
149
  }
146
150
  /** Ensure connection. */
147
151
  async ensure_connection() {
@@ -1545,24 +1545,17 @@ export class Server {
1545
1545
  // Create static endpoints.
1546
1546
  await this._initialize_statics();
1547
1547
  /* @performance */ this.performance.end("_initialize_statics()");
1548
+ /**
1549
+ * Initialize keys.
1550
+ * Its required to await this in case no keys exist.
1551
+ * If in this case a user would perform any key requiring operations inside an
1552
+ * "initialize" callback, then it would not yet be created.
1553
+ */
1554
+ await this._initialize_keys();
1555
+ /* @performance */ this.performance.end("load-keys");
1548
1556
  // Add promises using the database.
1549
1557
  const promises = [];
1550
1558
  /* @performance */ this.performance.start();
1551
- // Initialize keys,
1552
- // uses the database so dont await in non production,
1553
- // to speed up restarts.
1554
- if (this.production) {
1555
- promises.push(this._initialize_keys());
1556
- }
1557
- else {
1558
- this._initialize_keys().then(() => {
1559
- this.log(1, "Finished loading keys.");
1560
- }).catch((err) => {
1561
- this.log(0, `Error while loading keys.`);
1562
- this.log.error(err);
1563
- });
1564
- }
1565
- // /* @performance */ this.performance.end("load-keys");
1566
1559
  // Initialize users.
1567
1560
  promises.push(this.users._initialize());
1568
1561
  // /* @performance */ this.performance.end("users._initialize()");
@@ -1614,10 +1607,7 @@ export class Server {
1614
1607
  /* @performance */ this.performance.end("initialize-endpoints");
1615
1608
  // On initialize callbacks.
1616
1609
  for (const callback of this.events.get("initialize")) {
1617
- const res = callback();
1618
- if (res instanceof Promise) {
1619
- await res;
1620
- }
1610
+ await callback();
1621
1611
  }
1622
1612
  /* @performance */ this.performance.end("on-initialize-callbacks");
1623
1613
  /* @performance */ this.performance.end("initialize()", initialize_start);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Daan van den Bergh",
3
3
  "name": "@vandenberghinc/volt",
4
- "version": "1.1.34",
4
+ "version": "1.1.35",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "types": "./backend/dist/esm/volt.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@vandenberghinc/vhighlight": "^1.3.15",
38
- "@vandenberghinc/vlib": "^1.6.33",
38
+ "@vandenberghinc/vlib": "^1.6.34",
39
39
  "blob-stream": "^0.1.3",
40
40
  "clean-css": "^5.3.3",
41
41
  "esbuild": "^0.25.12",