@platformatic/db 3.3.0 → 3.4.0

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/index.js CHANGED
@@ -1,60 +1,10 @@
1
1
  import { resolve, validationOptions } from '@platformatic/basic'
2
2
  import { kMetadata, loadConfiguration as utilsLoadConfiguration } from '@platformatic/foundation'
3
- import { transform as serviceTransform } from '@platformatic/service'
4
- import { readFile } from 'node:fs/promises'
5
- import { resolve as resolvePath } from 'node:path'
6
3
  import { DatabaseCapability } from './lib/capability.js'
4
+ import { transform } from './lib/config.js'
7
5
  import { schema } from './lib/schema.js'
8
6
  import { upgrade } from './lib/upgrade.js'
9
7
 
10
- export async function transform (config) {
11
- config = await serviceTransform(config)
12
-
13
- if (
14
- config.db &&
15
- config.db.connectionString.indexOf('sqlite') === 0 &&
16
- config.db.connectionString !== 'sqlite://:memory:'
17
- ) {
18
- const originalSqlitePath = config.db.connectionString.replace('sqlite://', '')
19
- const sqliteFullPath = resolvePath(config[kMetadata].root, originalSqlitePath)
20
- config.db.connectionString = 'sqlite://' + sqliteFullPath
21
- }
22
-
23
- /* c8 ignore next 3 */
24
- if (config.db.graphql?.schemaPath) {
25
- config.db.graphql.schema = await readFile(config.db.graphql.schemaPath, 'utf8')
26
- }
27
-
28
- /* c8 ignore next 2 */
29
- const arePostgresqlSchemaDefined =
30
- config.db?.connectionString.indexOf('postgres') === 0 && config.db?.schema?.length > 0
31
- const migrationsTableName = arePostgresqlSchemaDefined ? 'public.versions' : 'versions'
32
-
33
- // relative-to-absolute migrations path
34
- if (config.migrations) {
35
- config.migrations.table = config.migrations.table || migrationsTableName
36
- }
37
-
38
- if (config.migrations && config.db) {
39
- // TODO remove the ignores
40
- /* c8 ignore next 4 */
41
- config.db.ignore = config.db.ignore || {}
42
- config.db.ignore = Object.assign(
43
- {},
44
- {
45
- [config.migrations.table || migrationsTableName]: true
46
- },
47
- config.db.ignore
48
- )
49
- }
50
-
51
- if (config.types?.autogenerate === 'true') {
52
- config.types.autogenerate = true
53
- }
54
-
55
- return config
56
- }
57
-
58
8
  export async function loadConfiguration (configOrRoot, sourceOrConfig, context) {
59
9
  const { root, source } = await resolve(configOrRoot, sourceOrConfig, 'db')
60
10
 
@@ -79,6 +29,7 @@ export const skipTelemetryHooks = true
79
29
  export { platformaticDatabase } from './lib/application.js'
80
30
  export { DatabaseCapability } from './lib/capability.js'
81
31
  export * from './lib/commands/index.js'
32
+ export { transform } from './lib/config.js'
82
33
  export * from './lib/errors.js'
83
34
  export * as errors from './lib/errors.js'
84
35
  export { Generator } from './lib/generator.js'
@@ -1,5 +1,6 @@
1
1
  import { loadConfiguration } from '@platformatic/foundation'
2
2
  import { utimesSync } from 'node:fs'
3
+ import { transform } from '../config.js'
3
4
  import { execute } from '../migrator.js'
4
5
  import { schema } from '../schema.js'
5
6
  import { updateSchemaLock } from '../utils.js'
@@ -7,7 +8,7 @@ import { generateTypes } from './types.js'
7
8
 
8
9
  export async function applyMigrations (logger, configFile, args, context) {
9
10
  const { parseArgs, logFatalError } = context
10
- const config = await loadConfiguration(configFile, schema)
11
+ const config = await loadConfiguration(configFile, schema, { transform })
11
12
 
12
13
  const {
13
14
  values: { to, rollback }
@@ -1,12 +1,14 @@
1
- import { loadConfiguration } from '@platformatic/foundation'
1
+ import { kMetadata, loadConfiguration } from '@platformatic/foundation'
2
2
  import { writeFile } from 'node:fs/promises'
3
3
  import { join, relative } from 'node:path'
4
+ import { transform } from '../config.js'
4
5
  import * as errors from '../errors.js'
5
6
  import { Migrator } from '../migrator.js'
6
7
  import { schema } from '../schema.js'
7
8
 
8
9
  export async function createMigrations (logger, configFile, _, { colorette: { bold } }) {
9
- const config = await loadConfiguration(configFile, schema)
10
+ const config = await loadConfiguration(configFile, schema, { transform })
11
+ const root = config[kMetadata].root
10
12
 
11
13
  let migrator = null
12
14
  try {
@@ -28,9 +30,7 @@ export async function createMigrations (logger, configFile, _, { colorette: { bo
28
30
 
29
31
  await Promise.all([writeFile(doFile, ''), writeFile(undoFile, '')])
30
32
 
31
- logger.info(
32
- `Created migration files ${bold(relative(process.cwd(), doFile))} and ${bold(relative(process.cwd(), undoFile))}.`
33
- )
33
+ logger.info(`Created migration files ${bold(relative(root, doFile))} and ${bold(relative(root, undoFile))}.`)
34
34
  } catch (error) {
35
35
  logger.error(error.message)
36
36
  } finally {
@@ -1,10 +1,11 @@
1
1
  import { abstractLogger, kMetadata, loadConfiguration } from '@platformatic/foundation'
2
2
  import { printSchema as printGraphqlSchema } from 'graphql'
3
3
  import { create } from '../../index.js'
4
+ import { transform } from '../config.js'
4
5
  import { schema } from '../schema.js'
5
6
 
6
7
  export async function printSchema (logger, configFile, args, { colorette: { bold }, logFatalError }) {
7
- const config = await loadConfiguration(configFile, schema)
8
+ const config = await loadConfiguration(configFile, schema, { transform })
8
9
 
9
10
  const type = args[0]
10
11
 
@@ -1,14 +1,15 @@
1
- import { loadConfiguration, loadModule } from '@platformatic/foundation'
1
+ import { kMetadata, loadConfiguration, loadModule } from '@platformatic/foundation'
2
2
  import { access } from 'fs/promises'
3
3
  import { createRequire } from 'node:module'
4
4
  import { resolve } from 'node:path'
5
+ import { transform } from '../config.js'
5
6
  import { MissingSeedFileError } from '../errors.js'
6
7
  import { Migrator } from '../migrator.js'
7
8
  import { schema } from '../schema.js'
8
9
  import { setupDB } from '../utils.js'
9
10
 
10
11
  export async function seed (logger, configFile, args, { colorette: { bold }, logFatalError }) {
11
- const config = await loadConfiguration(configFile, schema)
12
+ const config = await loadConfiguration(configFile, schema, { transform })
12
13
 
13
14
  if (config.migrations !== undefined) {
14
15
  const migrator = new Migrator(config.migrations, config.db, logger)
@@ -28,14 +29,15 @@ export async function seed (logger, configFile, args, { colorette: { bold }, log
28
29
  throw new MissingSeedFileError()
29
30
  }
30
31
 
31
- const seedFile = resolve(process.cwd(), args[0])
32
+ const root = config[kMetadata].root
33
+ const seedFile = resolve(root, args[0])
32
34
  await access(seedFile)
33
35
 
34
36
  logger.info(`Seeding from ${bold(seedFile)}`)
35
37
 
36
- const importedModule = await loadModule(createRequire(resolve(process.cwd(), 'noop.js')), seedFile)
38
+ const importedModule = await loadModule(createRequire(resolve(root, 'noop.js')), seedFile)
37
39
 
38
- const seedFunction = typeof importedModule?.seed !== 'function' ? importedModule : importedModule
40
+ const seedFunction = typeof importedModule?.seed !== 'function' ? importedModule : importedModule.seed
39
41
 
40
42
  if (typeof seedFunction !== 'function') {
41
43
  logFatalError(logger, 'Cannot find seed function.')
@@ -1,9 +1,10 @@
1
1
  import { loadConfiguration } from '@platformatic/foundation'
2
+ import { transform } from '../config.js'
2
3
  import { schema } from '../schema.js'
3
4
  import { execute } from '../types.js'
4
5
 
5
6
  export async function generateTypes (logger, configFile, _args) {
6
- const config = await loadConfiguration(configFile, schema)
7
+ const config = await loadConfiguration(configFile, schema, { transform })
7
8
 
8
9
  const count = await execute({ logger, config })
9
10
 
package/lib/config.js ADDED
@@ -0,0 +1,52 @@
1
+ import { kMetadata } from '@platformatic/foundation'
2
+ import { transform as serviceTransform } from '@platformatic/service'
3
+ import { readFile } from 'node:fs/promises'
4
+ import { resolve as resolvePath } from 'node:path'
5
+
6
+ export async function transform (config) {
7
+ config = await serviceTransform(config)
8
+
9
+ if (
10
+ config.db &&
11
+ config.db.connectionString.indexOf('sqlite') === 0 &&
12
+ config.db.connectionString !== 'sqlite://:memory:'
13
+ ) {
14
+ const originalSqlitePath = config.db.connectionString.replace('sqlite://', '')
15
+ const sqliteFullPath = resolvePath(config[kMetadata].root, originalSqlitePath)
16
+ config.db.connectionString = 'sqlite://' + sqliteFullPath
17
+ }
18
+
19
+ /* c8 ignore next 3 */
20
+ if (config.db.graphql?.schemaPath) {
21
+ config.db.graphql.schema = await readFile(config.db.graphql.schemaPath, 'utf8')
22
+ }
23
+
24
+ /* c8 ignore next 2 */
25
+ const arePostgresqlSchemaDefined =
26
+ config.db?.connectionString.indexOf('postgres') === 0 && config.db?.schema?.length > 0
27
+ const migrationsTableName = arePostgresqlSchemaDefined ? 'public.versions' : 'versions'
28
+
29
+ // relative-to-absolute migrations path
30
+ if (config.migrations) {
31
+ config.migrations.table = config.migrations.table || migrationsTableName
32
+ }
33
+
34
+ if (config.migrations && config.db) {
35
+ // TODO remove the ignores
36
+ /* c8 ignore next 4 */
37
+ config.db.ignore = config.db.ignore || {}
38
+ config.db.ignore = Object.assign(
39
+ {},
40
+ {
41
+ [config.migrations.table || migrationsTableName]: true
42
+ },
43
+ config.db.ignore
44
+ )
45
+ }
46
+
47
+ if (config.types?.autogenerate === 'true') {
48
+ config.types.autogenerate = true
49
+ }
50
+
51
+ return config
52
+ }
package/lib/types.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createDirectory, isFileAccessible } from '@platformatic/foundation'
1
+ import { createDirectory, isFileAccessible, kMetadata } from '@platformatic/foundation'
2
2
  import { mapOpenAPItoTypes, mapSQLEntityToJSONSchema } from '@platformatic/sql-json-schema-mapper'
3
3
  import camelcase from 'camelcase'
4
4
  import { readFile, readdir, unlink, writeFile } from 'node:fs/promises'
@@ -122,7 +122,8 @@ export async function execute ({ logger, config }) {
122
122
  return 0
123
123
  }
124
124
 
125
- const typesFolderPath = resolve(process.cwd(), config.types?.dir ?? 'types')
125
+ const root = config[kMetadata].root
126
+ const typesFolderPath = resolve(root, config.types?.dir ?? 'types')
126
127
 
127
128
  // Prepare the types folder
128
129
  if (await isFileAccessible(typesFolderPath)) {
@@ -149,8 +150,8 @@ export async function execute ({ logger, config }) {
149
150
  }
150
151
 
151
152
  // Generate plt-env.d.ts
152
- const environmentPath = join(process.cwd(), 'plt-env.d.ts')
153
- const pltEnvironment = await generateEnvironmentTypes(relative(process.cwd(), typesFolderPath))
153
+ const environmentPath = join(root, 'plt-env.d.ts')
154
+ const pltEnvironment = await generateEnvironmentTypes(relative(root, typesFolderPath))
154
155
  if (await writeFileIfChanged(environmentPath, pltEnvironment)) {
155
156
  logger.info('Regenerated plt-env.d.ts.')
156
157
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/db",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -66,17 +66,17 @@
66
66
  "rfdc": "^1.3.1",
67
67
  "rimraf": "^4.4.1",
68
68
  "semgrator": "^0.3.0",
69
- "@platformatic/basic": "3.3.0",
70
- "@platformatic/db-authorization": "3.3.0",
71
- "@platformatic/db-core": "3.3.0",
72
- "@platformatic/foundation": "3.3.0",
73
- "@platformatic/service": "3.3.0",
74
- "@platformatic/sql-events": "3.3.0",
75
- "@platformatic/sql-graphql": "3.3.0",
76
- "@platformatic/sql-mapper": "3.3.0",
77
- "@platformatic/sql-json-schema-mapper": "3.3.0",
78
- "@platformatic/sql-openapi": "3.3.0",
79
- "@platformatic/telemetry": "3.3.0"
69
+ "@platformatic/basic": "3.4.0",
70
+ "@platformatic/db-authorization": "3.4.0",
71
+ "@platformatic/db-core": "3.4.0",
72
+ "@platformatic/foundation": "3.4.0",
73
+ "@platformatic/service": "3.4.0",
74
+ "@platformatic/sql-graphql": "3.4.0",
75
+ "@platformatic/sql-json-schema-mapper": "3.4.0",
76
+ "@platformatic/sql-mapper": "3.4.0",
77
+ "@platformatic/sql-events": "3.4.0",
78
+ "@platformatic/sql-openapi": "3.4.0",
79
+ "@platformatic/telemetry": "3.4.0"
80
80
  },
81
81
  "engines": {
82
82
  "node": ">=22.19.0"
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/db/3.3.0.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/db/3.4.0.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Database Config",
5
5
  "type": "object",