@travetto/model-sqlite 3.0.0-rc.4 → 3.0.0-rc.7

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  <!-- This file was generated by @travetto/doc and should not be modified directly -->
2
- <!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-sqlite/doc.ts and execute "npx trv doc" to rebuild -->
2
+ <!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-sqlite/DOC.ts and execute "npx trv doc" to rebuild -->
3
3
  # SQLite Model Service
4
4
  ## SQLite backing for the travetto model module, with real-time modeling support for SQL schemas.
5
5
 
@@ -16,7 +16,7 @@ a [SQL](https://en.wikipedia.org/wiki/SQL) database. Every table generated will
16
16
  Supported features:
17
17
 
18
18
  * [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/service/crud.ts#L11)
19
- * [Bulk](https://github.com/travetto/travetto/tree/main/module/model/src/service/bulk.ts#L23)
19
+ * [Bulk](https://github.com/travetto/travetto/tree/main/module/model/src/service/bulk.ts#L19)
20
20
  * [Query Crud](https://github.com/travetto/travetto/tree/main/module/model-query/src/service/crud.ts#L11)
21
21
  * [Facet](https://github.com/travetto/travetto/tree/main/module/model-query/src/service/facet.ts#L12)
22
22
  * [Query](https://github.com/travetto/travetto/tree/main/module/model-query/src/service/query.ts#L10)
@@ -53,7 +53,7 @@ import { Config } from '@travetto/config';
53
53
  * SQL Model Config
54
54
  */
55
55
  @Config('model.sql')
56
- export class SQLModelConfig {
56
+ export class SQLModelConfig<T extends {} = {}> {
57
57
  /**
58
58
  * Host to connect to
59
59
  */
@@ -89,9 +89,10 @@ export class SQLModelConfig {
89
89
  /**
90
90
  * Raw client options
91
91
  */
92
- options = {};
92
+
93
+ options: T = {} as T;
93
94
  }
94
95
  ```
95
96
 
96
- Additionally, you can see that the class is registered with the [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L9) annotation, and so these values can be overridden using the
97
- standard [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Environment-aware config management using yaml files")resolution paths.
97
+ Additionally, you can see that the class is registered with the [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) annotation, and so these values can be overridden using the
98
+ standard [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support")resolution paths.
File without changes
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-sqlite",
3
- "displayName": "SQLite Model Service",
4
- "version": "3.0.0-rc.4",
3
+ "version": "3.0.0-rc.7",
5
4
  "description": "SQLite backing for the travetto model module, with real-time modeling support for SQL schemas.",
6
5
  "keywords": [
7
6
  "sql",
@@ -18,25 +17,25 @@
18
17
  "name": "Travetto Framework"
19
18
  },
20
19
  "files": [
21
- "index.ts",
20
+ "__index__.ts",
22
21
  "src"
23
22
  ],
24
- "main": "index.ts",
23
+ "main": "__index__.ts",
25
24
  "repository": {
26
25
  "url": "https://github.com/travetto/travetto.git",
27
26
  "directory": "module/model-sqlite"
28
27
  },
29
28
  "dependencies": {
30
- "@travetto/config": "^3.0.0-rc.4",
31
- "@travetto/context": "^3.0.0-rc.4",
32
- "@travetto/model": "^3.0.0-rc.4",
33
- "@travetto/model-query": "3.0.0-rc.4",
34
- "@travetto/model-sql": "3.0.0-rc.4",
35
- "better-sqlite3": "^7.6.2",
36
- "@types/better-sqlite3": "^7.6.0"
29
+ "@travetto/config": "^3.0.0-rc.7",
30
+ "@travetto/context": "^3.0.0-rc.7",
31
+ "@travetto/model": "^3.0.0-rc.7",
32
+ "@travetto/model-query": "^3.0.0-rc.7",
33
+ "@travetto/model-sql": "^3.0.0-rc.7",
34
+ "@types/better-sqlite3": "^7.6.3",
35
+ "better-sqlite3": "^7.6.2"
37
36
  },
38
- "devDependencies": {
39
- "@travetto/app": "^3.0.0-rc.4"
37
+ "travetto": {
38
+ "displayName": "SQLite Model Service"
40
39
  },
41
40
  "publishConfig": {
42
41
  "access": "public"
package/src/connection.ts CHANGED
@@ -1,11 +1,10 @@
1
- import type * as sqlite3 from 'better-sqlite3';
2
- import Db = require('better-sqlite3');
3
- import * as pool from 'generic-pool';
1
+ import Db, * as sqlite3 from 'better-sqlite3';
2
+ import pool from 'generic-pool';
4
3
 
5
- import { ShutdownManager, Util } from '@travetto/base';
4
+ import { path } from '@travetto/manifest';
5
+ import { ShutdownManager, TimeUtil } from '@travetto/base';
6
6
  import { AsyncContext, WithAsyncContext } from '@travetto/context';
7
7
  import { ExistsError } from '@travetto/model';
8
- import { AppCache } from '@travetto/boot';
9
8
  import { SQLModelConfig, Connection } from '@travetto/model-sql';
10
9
 
11
10
  /**
@@ -15,12 +14,12 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
15
14
 
16
15
  isolatedTransactions = false;
17
16
 
18
- #config: SQLModelConfig;
17
+ #config: SQLModelConfig<sqlite3.Options & { file?: string }>;
19
18
  #pool: pool.Pool<sqlite3.Database>;
20
19
 
21
20
  constructor(
22
21
  context: AsyncContext,
23
- config: SQLModelConfig
22
+ config: SQLModelConfig<sqlite3.Options & { file?: string }>
24
23
  ) {
25
24
  super(context);
26
25
  this.#config = config;
@@ -33,7 +32,7 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
33
32
  } catch (err) {
34
33
  if (err instanceof Error && retries > 1 && err.message.includes('database is locked')) {
35
34
  console.error('Failed, and waiting', retries);
36
- await Util.wait(delay);
35
+ await TimeUtil.wait(delay);
37
36
  retries -= 1;
38
37
  } else {
39
38
  throw err;
@@ -42,26 +41,29 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
42
41
  }
43
42
  }
44
43
 
44
+ async #create(): Promise<sqlite3.Database> {
45
+ const file = path.resolve(this.#config.options.file ?? '.trv-sqlite_db');
46
+ const db = new Db(file, this.#config.options);
47
+ await db.pragma('foreign_keys = ON');
48
+ await db.pragma('journal_mode = WAL');
49
+ db.function('regexp', (a, b) => new RegExp(a).test(b) ? 1 : 0);
50
+ return db;
51
+ }
52
+
45
53
  /**
46
54
  * Initializes connection and establishes crypto extension for use with hashing
47
55
  */
48
56
  @WithAsyncContext()
49
57
  override async init(): Promise<void> {
50
- this.#pool = pool.createPool({
51
- create: () => this.#withRetries(async () => {
52
- const db = Db(AppCache.toEntryName('sqlite_db'),
53
- this.#config.options
54
- );
55
- await db.pragma('foreign_keys = ON');
56
- await db.pragma('journal_mode = WAL');
57
- db.function('regexp', (a, b) => new RegExp(a).test(b) ? 1 : 0);
58
- return db;
59
- }),
58
+ await this.#create();
59
+
60
+ this.#pool = pool.createPool<sqlite3.Database>({
61
+ create: () => this.#withRetries(() => this.#create()),
60
62
  destroy: async db => { db.close(); }
61
63
  }, { max: 1 });
62
64
 
63
65
  // Close postgres
64
- ShutdownManager.onShutdown(this.constructor.ᚕid, () => this.#pool.clear());
66
+ ShutdownManager.onShutdown(this, () => this.#pool.clear());
65
67
  }
66
68
 
67
69
  async execute<T = unknown>(conn: sqlite3.Database, query: string): Promise<{ count: number, records: T[] }> {
@@ -86,7 +88,7 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
86
88
  }
87
89
 
88
90
  async acquire(): Promise<Db.Database> {
89
- return this.#pool.acquire();
91
+ return await this.#pool.acquire();
90
92
  }
91
93
 
92
94
  async release(db: sqlite3.Database): Promise<void> {