@tylix/orm 0.1.2 → 0.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylix/orm",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -85,6 +85,10 @@ export class MongoAdapter extends DatabaseAdapter {
85
85
  }
86
86
  }
87
87
 
88
+ quoteIdentifier(name) {
89
+ return name
90
+ }
91
+
88
92
  ensureConnected() {
89
93
  if (!this.db) {
90
94
  throw new Error('MongoAdapter is not connected. Call connect() first.')
@@ -41,6 +41,10 @@ export class MysqlAdapter extends DatabaseAdapter {
41
41
  }
42
42
  }
43
43
 
44
+ quoteIdentifier(name) {
45
+ return `\`${name.replace(/`/g, '``')}\``
46
+ }
47
+
44
48
  ensureConnected() {
45
49
  if (!this.connection) {
46
50
  throw new Error('MysqlAdapter is not connected. Call connect() first.')
@@ -40,7 +40,6 @@ export class PostgresAdapter extends DatabaseAdapter {
40
40
  this.pool = new pg.Pool(this.config)
41
41
  return this
42
42
  }
43
-
44
43
  async close() {
45
44
  if (this.pool) {
46
45
  await this.pool.end()
@@ -48,6 +47,10 @@ export class PostgresAdapter extends DatabaseAdapter {
48
47
  }
49
48
  }
50
49
 
50
+ quoteIdentifier(name) {
51
+ return `"${name.replace(/"/g, '""')}"`
52
+ }
53
+
51
54
  ensureConnected() {
52
55
  if (!this.pool) {
53
56
  throw new Error('PostgresAdapter is not connected. Call connect() first.')
@@ -32,6 +32,10 @@ export class SqliteAdapter extends DatabaseAdapter {
32
32
  }
33
33
  }
34
34
 
35
+ quoteIdentifier(name) {
36
+ return `"${name.replace(/"/g, '""')}"`
37
+ }
38
+
35
39
  ensureConnected() {
36
40
  if (!this.db) {
37
41
  throw new Error('SqliteAdapter is not connected. Call connect() first.')
@@ -0,0 +1,64 @@
1
+ import pg from 'pg'
2
+ import mysql from 'mysql2/promise'
3
+
4
+ export async function ensureDatabaseExists(databaseConfig) {
5
+ const { driver } = databaseConfig
6
+ if (driver === 'postgres') {
7
+ await ensurePostgresDatabase(databaseConfig)
8
+ } else if (driver === 'mysql') {
9
+ await ensureMysqlDatabase(databaseConfig)
10
+ }
11
+ // sqlite: the file is created automatically on connect, nothing to do here.
12
+ // mongodb: databases/collections are created lazily on first write, nothing to do here.
13
+ }
14
+
15
+ async function ensurePostgresDatabase({
16
+ connectionString,
17
+ host,
18
+ port,
19
+ user,
20
+ password,
21
+ database,
22
+ }) {
23
+ // A connection string can already point at a specific target db and
24
+ // may not even include a "postgres" admin database to fall back to --
25
+ // safest to skip auto-create rather than guess at rewriting it.
26
+ if (connectionString) return
27
+
28
+ const adminPool = new pg.Pool({
29
+ host,
30
+ port,
31
+ user,
32
+ password,
33
+ database: 'postgres',
34
+ })
35
+ try {
36
+ const { rows } = await adminPool.query(
37
+ 'SELECT 1 FROM pg_database WHERE datname = $1',
38
+ [database],
39
+ )
40
+ if (rows.length === 0) {
41
+ // Postgres doesn't support "CREATE DATABASE IF NOT EXISTS", and
42
+ // database names can't be parameterized -- quote defensively.
43
+ await adminPool.query(`CREATE DATABASE "${database.replace(/"/g, '""')}"`)
44
+ }
45
+ } finally {
46
+ await adminPool.end()
47
+ }
48
+ }
49
+
50
+ async function ensureMysqlDatabase({ host, port, user, password, database }) {
51
+ const connection = await mysql.createConnection({
52
+ host,
53
+ port,
54
+ user,
55
+ password,
56
+ })
57
+ try {
58
+ await connection.query(
59
+ `CREATE DATABASE IF NOT EXISTS \`${database.replace(/`/g, '``')}\``,
60
+ )
61
+ } finally {
62
+ await connection.end()
63
+ }
64
+ }
package/src/index.js CHANGED
@@ -8,3 +8,4 @@ export * from './adapters/MysqlAdapter.js'
8
8
  export * from './adapters/MongoAdapter.js'
9
9
  export * from './query/QueryBuilder.js'
10
10
  export * from './query/buildSelectSql.js'
11
+ export * from './ensureDatabaseExists.js'