@oino-ts/db-bunsqlite 0.19.0 → 0.20.1

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.
@@ -154,6 +154,9 @@ class OINODbBunSqlite extends db_1.OINODb {
154
154
  async connect() {
155
155
  common_1.OINOBenchmark.startMetric("OINODb", "connect");
156
156
  let result = new common_1.OINOResult();
157
+ if (this.isConnected) {
158
+ return result;
159
+ }
157
160
  const filepath = this._params.url.substring(7);
158
161
  try {
159
162
  this._db = bun_sqlite_1.Database.open(filepath, { create: true, readonly: false, readwrite: true });
@@ -171,11 +174,15 @@ class OINODbBunSqlite extends db_1.OINODb {
171
174
  *
172
175
  */
173
176
  async validate() {
177
+ if (!this.isConnected) {
178
+ return new common_1.OINOResult().setError(400, "Database not connected!", "OINODbBunSqlite.validate");
179
+ }
174
180
  common_1.OINOBenchmark.startMetric("OINODb", "validate");
175
181
  let result = new common_1.OINOResult();
176
182
  try {
183
+ this.isValidated = false;
177
184
  const sql = this._getValidateSql(this._params.database);
178
- const sql_res = await this.sqlSelect(sql);
185
+ const sql_res = await this._query(sql);
179
186
  if (sql_res.isEmpty()) {
180
187
  result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
181
188
  }
@@ -190,28 +197,67 @@ class OINODbBunSqlite extends db_1.OINODb {
190
197
  }
191
198
  }
192
199
  catch (e) {
193
- result.setError(500, common_1.OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
200
+ result.setError(500, common_1.OINO_ERROR_PREFIX + " (OINODbBunSqlite.validate): Exception in db query: " + e.message, "OINODbBunSqlite.validate");
194
201
  }
195
202
  common_1.OINOBenchmark.endMetric("OINODb", "validate");
196
203
  return result;
197
204
  }
198
205
  /**
199
- * Execute a select operation.
200
- *
201
- * @param sql SQL statement.
206
+ * Connect to database.
202
207
  *
203
208
  */
204
- async sqlSelect(sql) {
205
- common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
209
+ async disconnect() {
210
+ this.isConnected = false;
211
+ this.isValidated = false;
212
+ }
213
+ async _query(sql) {
206
214
  let result;
207
215
  try {
208
- result = new OINOBunSqliteDataset(this._db?.query(sql).values(), []);
216
+ const sql_res = this._db?.query(sql).values();
217
+ if (sql_res) {
218
+ // console.log("OINODbBunSqlite._query: res", sql_res)
219
+ result = new OINOBunSqliteDataset(sql_res, []);
220
+ }
221
+ else {
222
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []);
223
+ }
209
224
  }
210
225
  catch (e) {
211
- result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message]);
226
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + " (OINODbBunSqlite._query): Exception in db query: " + e.message, "OINODbBunSqlite._query");
227
+ }
228
+ return result;
229
+ }
230
+ async _exec(sql) {
231
+ let result;
232
+ try {
233
+ const sql_res = this._db?.query(sql).values();
234
+ if (sql_res) {
235
+ // console.log("OINODbBunSqlite._exec: res", sql_res)
236
+ result = new OINOBunSqliteDataset(sql_res, []);
237
+ }
238
+ else {
239
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []);
240
+ }
212
241
  }
242
+ catch (e) {
243
+ result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []).setError(500, common_1.OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbBunSqlite._exec");
244
+ }
245
+ return result;
246
+ }
247
+ /**
248
+ * Execute a select operation.
249
+ *
250
+ * @param sql SQL statement.
251
+ *
252
+ */
253
+ async sqlSelect(sql) {
254
+ if (!this.isValidated) {
255
+ throw new Error(common_1.OINO_ERROR_PREFIX + ": Database connection not validated!");
256
+ }
257
+ common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
258
+ let result = await this._query(sql);
213
259
  common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
214
- return Promise.resolve(result);
260
+ return result;
215
261
  }
216
262
  /**
217
263
  * Execute other sql operations.
@@ -220,17 +266,13 @@ class OINODbBunSqlite extends db_1.OINODb {
220
266
  *
221
267
  */
222
268
  async sqlExec(sql) {
223
- common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
224
- let result;
225
- try {
226
- this._db?.exec(sql);
227
- result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, []);
228
- }
229
- catch (e) {
230
- result = new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"]);
269
+ if (!this.isValidated) {
270
+ return new OINOBunSqliteDataset(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (OINODbBunSqlite.sqlExec): Database connection not validated!"]);
231
271
  }
272
+ common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
273
+ let result = await this._exec(sql);
232
274
  common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
233
- return Promise.resolve(result);
275
+ return result;
234
276
  }
235
277
  _getSchemaSql(dbName, tableName) {
236
278
  const sql = "SELECT sql from sqlite_schema WHERE name='" + tableName + "'";
@@ -249,7 +291,7 @@ class OINODbBunSqlite extends db_1.OINODb {
249
291
  */
250
292
  async initializeApiDatamodel(api) {
251
293
  const schema_sql = this._getSchemaSql(this._params.database, api.params.tableName);
252
- const res = await this.sqlSelect(schema_sql);
294
+ const res = await this._query(schema_sql);
253
295
  const sql_desc = (res?.getRow()[0]);
254
296
  const excluded_fields = [];
255
297
  let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc);
@@ -151,6 +151,9 @@ export class OINODbBunSqlite extends OINODb {
151
151
  async connect() {
152
152
  OINOBenchmark.startMetric("OINODb", "connect");
153
153
  let result = new OINOResult();
154
+ if (this.isConnected) {
155
+ return result;
156
+ }
154
157
  const filepath = this._params.url.substring(7);
155
158
  try {
156
159
  this._db = BunSqliteDb.open(filepath, { create: true, readonly: false, readwrite: true });
@@ -168,11 +171,15 @@ export class OINODbBunSqlite extends OINODb {
168
171
  *
169
172
  */
170
173
  async validate() {
174
+ if (!this.isConnected) {
175
+ return new OINOResult().setError(400, "Database not connected!", "OINODbBunSqlite.validate");
176
+ }
171
177
  OINOBenchmark.startMetric("OINODb", "validate");
172
178
  let result = new OINOResult();
173
179
  try {
180
+ this.isValidated = false;
174
181
  const sql = this._getValidateSql(this._params.database);
175
- const sql_res = await this.sqlSelect(sql);
182
+ const sql_res = await this._query(sql);
176
183
  if (sql_res.isEmpty()) {
177
184
  result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
178
185
  }
@@ -187,28 +194,67 @@ export class OINODbBunSqlite extends OINODb {
187
194
  }
188
195
  }
189
196
  catch (e) {
190
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
197
+ result.setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite.validate): Exception in db query: " + e.message, "OINODbBunSqlite.validate");
191
198
  }
192
199
  OINOBenchmark.endMetric("OINODb", "validate");
193
200
  return result;
194
201
  }
195
202
  /**
196
- * Execute a select operation.
197
- *
198
- * @param sql SQL statement.
203
+ * Connect to database.
199
204
  *
200
205
  */
201
- async sqlSelect(sql) {
202
- OINOBenchmark.startMetric("OINODb", "sqlSelect");
206
+ async disconnect() {
207
+ this.isConnected = false;
208
+ this.isValidated = false;
209
+ }
210
+ async _query(sql) {
203
211
  let result;
204
212
  try {
205
- result = new OINOBunSqliteDataset(this._db?.query(sql).values(), []);
213
+ const sql_res = this._db?.query(sql).values();
214
+ if (sql_res) {
215
+ // console.log("OINODbBunSqlite._query: res", sql_res)
216
+ result = new OINOBunSqliteDataset(sql_res, []);
217
+ }
218
+ else {
219
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []);
220
+ }
206
221
  }
207
222
  catch (e) {
208
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message]);
223
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite._query): Exception in db query: " + e.message, "OINODbBunSqlite._query");
224
+ }
225
+ return result;
226
+ }
227
+ async _exec(sql) {
228
+ let result;
229
+ try {
230
+ const sql_res = this._db?.query(sql).values();
231
+ if (sql_res) {
232
+ // console.log("OINODbBunSqlite._exec: res", sql_res)
233
+ result = new OINOBunSqliteDataset(sql_res, []);
234
+ }
235
+ else {
236
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []);
237
+ }
209
238
  }
239
+ catch (e) {
240
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbBunSqlite._exec");
241
+ }
242
+ return result;
243
+ }
244
+ /**
245
+ * Execute a select operation.
246
+ *
247
+ * @param sql SQL statement.
248
+ *
249
+ */
250
+ async sqlSelect(sql) {
251
+ if (!this.isValidated) {
252
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!");
253
+ }
254
+ OINOBenchmark.startMetric("OINODb", "sqlSelect");
255
+ let result = await this._query(sql);
210
256
  OINOBenchmark.endMetric("OINODb", "sqlSelect");
211
- return Promise.resolve(result);
257
+ return result;
212
258
  }
213
259
  /**
214
260
  * Execute other sql operations.
@@ -217,17 +263,13 @@ export class OINODbBunSqlite extends OINODb {
217
263
  *
218
264
  */
219
265
  async sqlExec(sql) {
220
- OINOBenchmark.startMetric("OINODb", "sqlExec");
221
- let result;
222
- try {
223
- this._db?.exec(sql);
224
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []);
225
- }
226
- catch (e) {
227
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"]);
266
+ if (!this.isValidated) {
267
+ return new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (OINODbBunSqlite.sqlExec): Database connection not validated!"]);
228
268
  }
269
+ OINOBenchmark.startMetric("OINODb", "sqlExec");
270
+ let result = await this._exec(sql);
229
271
  OINOBenchmark.endMetric("OINODb", "sqlExec");
230
- return Promise.resolve(result);
272
+ return result;
231
273
  }
232
274
  _getSchemaSql(dbName, tableName) {
233
275
  const sql = "SELECT sql from sqlite_schema WHERE name='" + tableName + "'";
@@ -246,7 +288,7 @@ export class OINODbBunSqlite extends OINODb {
246
288
  */
247
289
  async initializeApiDatamodel(api) {
248
290
  const schema_sql = this._getSchemaSql(this._params.database, api.params.tableName);
249
- const res = await this.sqlSelect(schema_sql);
291
+ const res = await this._query(schema_sql);
250
292
  const sql_desc = (res?.getRow()[0]);
251
293
  const excluded_fields = [];
252
294
  let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc);
@@ -65,6 +65,13 @@ export declare class OINODbBunSqlite extends OINODb {
65
65
  *
66
66
  */
67
67
  validate(): Promise<OINOResult>;
68
+ /**
69
+ * Connect to database.
70
+ *
71
+ */
72
+ disconnect(): Promise<void>;
73
+ private _query;
74
+ private _exec;
68
75
  /**
69
76
  * Execute a select operation.
70
77
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-bunsqlite",
3
- "version": "0.19.0",
3
+ "version": "0.20.1",
4
4
  "description": "OINO TS package for using Bun Sqlite databases.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -20,8 +20,8 @@
20
20
  "module": "./dist/esm/index.js",
21
21
  "types": "./dist/types/index.d.ts",
22
22
  "dependencies": {
23
- "@oino-ts/common": "^0.19.0",
24
- "@oino-ts/db": "^0.19.0"
23
+ "@oino-ts/common": "^0.20.1",
24
+ "@oino-ts/db": "^0.20.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/bun": "latest",
@@ -163,6 +163,9 @@ export class OINODbBunSqlite extends OINODb {
163
163
  async connect(): Promise<OINOResult> {
164
164
  OINOBenchmark.startMetric("OINODb", "connect")
165
165
  let result:OINOResult = new OINOResult()
166
+ if (this.isConnected) {
167
+ return result
168
+ }
166
169
  const filepath:string = this._params.url.substring(7)
167
170
  try {
168
171
  this._db = BunSqliteDb.open(filepath, { create: true, readonly: false, readwrite: true })
@@ -180,11 +183,15 @@ export class OINODbBunSqlite extends OINODb {
180
183
  *
181
184
  */
182
185
  async validate(): Promise<OINOResult> {
186
+ if (!this.isConnected) {
187
+ return new OINOResult().setError(400, "Database not connected!", "OINODbBunSqlite.validate")
188
+ }
183
189
  OINOBenchmark.startMetric("OINODb", "validate")
184
190
  let result:OINOResult = new OINOResult()
185
191
  try {
192
+ this.isValidated = false
186
193
  const sql = this._getValidateSql(this._params.database)
187
- const sql_res:OINODbDataSet = await this.sqlSelect(sql)
194
+ const sql_res:OINODbDataSet = await this._query(sql)
188
195
  if (sql_res.isEmpty()) {
189
196
  result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate")
190
197
 
@@ -198,29 +205,69 @@ export class OINODbBunSqlite extends OINODb {
198
205
  this.isValidated = true
199
206
  }
200
207
  } catch (e:any) {
201
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate")
208
+ result.setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite.validate): Exception in db query: " + e.message, "OINODbBunSqlite.validate")
202
209
  }
203
210
  OINOBenchmark.endMetric("OINODb", "validate")
204
211
  return result
205
212
  }
206
213
 
207
214
  /**
208
- * Execute a select operation.
209
- *
210
- * @param sql SQL statement.
215
+ * Connect to database.
211
216
  *
212
217
  */
213
- async sqlSelect(sql:string): Promise<OINODbDataSet> {
214
- OINOBenchmark.startMetric("OINODb", "sqlSelect")
218
+ async disconnect(): Promise<void> {
219
+ this.isConnected = false
220
+ this.isValidated = false
221
+ }
222
+
223
+
224
+ private async _query(sql:string): Promise<OINODbDataSet> {
225
+ let result:OINODbDataSet
226
+ try {
227
+ const sql_res = this._db?.query(sql).values()
228
+ if (sql_res) {
229
+ // console.log("OINODbBunSqlite._query: res", sql_res)
230
+ result = new OINOBunSqliteDataset(sql_res, [])
231
+ } else {
232
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [])
233
+ }
234
+
235
+ } catch (e:any) {
236
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + " (OINODbBunSqlite._query): Exception in db query: " + e.message, "OINODbBunSqlite._query") as OINOBunSqliteDataset
237
+ }
238
+ return result
239
+ }
240
+ private async _exec(sql:string): Promise<OINODbDataSet> {
215
241
  let result:OINODbDataSet
216
242
  try {
217
- result = new OINOBunSqliteDataset(this._db?.query(sql).values(), [])
243
+ const sql_res = this._db?.query(sql).values()
244
+ if (sql_res) {
245
+ // console.log("OINODbBunSqlite._exec: res", sql_res)
246
+ result = new OINOBunSqliteDataset(sql_res, [])
247
+ } else {
248
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [])
249
+ }
218
250
 
219
251
  } catch (e:any) {
220
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message])
252
+ result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, []).setError(500, OINO_ERROR_PREFIX + ": Exception in db exec: " + e.message, "OINODbBunSqlite._exec") as OINOBunSqliteDataset
221
253
  }
254
+ return result
255
+ }
256
+
257
+ /**
258
+ * Execute a select operation.
259
+ *
260
+ * @param sql SQL statement.
261
+ *
262
+ */
263
+ async sqlSelect(sql:string): Promise<OINODbDataSet> {
264
+ if (!this.isValidated) {
265
+ throw new Error(OINO_ERROR_PREFIX + ": Database connection not validated!")
266
+ }
267
+ OINOBenchmark.startMetric("OINODb", "sqlSelect")
268
+ let result:OINODbDataSet = await this._query(sql)
222
269
  OINOBenchmark.endMetric("OINODb", "sqlSelect")
223
- return Promise.resolve(result)
270
+ return result
224
271
  }
225
272
 
226
273
  /**
@@ -230,17 +277,13 @@ export class OINODbBunSqlite extends OINODb {
230
277
  *
231
278
  */
232
279
  async sqlExec(sql:string): Promise<OINODbDataSet> {
233
- OINOBenchmark.startMetric("OINODb", "sqlExec")
234
- let result:OINODbDataSet
235
- try {
236
- this._db?.exec(sql)
237
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [])
238
-
239
- } catch (e:any) {
240
- result = new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"])
280
+ if (!this.isValidated) {
281
+ return new OINOBunSqliteDataset(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (OINODbBunSqlite.sqlExec): Database connection not validated!"])
241
282
  }
283
+ OINOBenchmark.startMetric("OINODb", "sqlExec")
284
+ let result:OINODbDataSet = await this._exec(sql)
242
285
  OINOBenchmark.endMetric("OINODb", "sqlExec")
243
- return Promise.resolve(result)
286
+ return result
244
287
  }
245
288
 
246
289
  private _getSchemaSql(dbName:string, tableName:string):string {
@@ -262,7 +305,7 @@ export class OINODbBunSqlite extends OINODb {
262
305
  */
263
306
  async initializeApiDatamodel(api:OINODbApi): Promise<void> {
264
307
  const schema_sql:string = this._getSchemaSql(this._params.database, api.params.tableName)
265
- const res:OINODbDataSet|null = await this.sqlSelect(schema_sql)
308
+ const res:OINODbDataSet|null = await this._query(schema_sql)
266
309
  const sql_desc:string = (res?.getRow()[0]) as string
267
310
  const excluded_fields:string[] = []
268
311
  let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc)