@platformatic/db 3.4.1 → 3.5.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.
Files changed (61) hide show
  1. package/README.md +1 -1
  2. package/config.d.ts +442 -107
  3. package/eslint.config.js +9 -5
  4. package/index.d.ts +53 -31
  5. package/index.js +30 -139
  6. package/lib/application.js +102 -0
  7. package/lib/capability.js +35 -0
  8. package/lib/commands/index.js +59 -0
  9. package/lib/commands/migrations-apply.js +63 -0
  10. package/lib/commands/migrations-create.js +48 -0
  11. package/lib/commands/print-schema.js +31 -0
  12. package/lib/commands/seed.js +74 -0
  13. package/lib/commands/types.js +22 -0
  14. package/lib/config.js +52 -0
  15. package/lib/errors.js +16 -12
  16. package/lib/generator.js +229 -0
  17. package/lib/{migrator.mjs → migrator.js} +46 -38
  18. package/lib/{root-endpoint/index.js → root.js} +6 -7
  19. package/lib/schema.js +41 -20
  20. package/lib/{generator/code-templates.js → templates.js} +57 -16
  21. package/lib/types.js +161 -0
  22. package/lib/upgrade.js +8 -12
  23. package/lib/utils.js +12 -23
  24. package/lib/versions/0.18.0.js +3 -5
  25. package/lib/versions/{from-zero-twenty-height-to-will-see.js → 0.28.0.js} +3 -5
  26. package/lib/versions/2.0.0.js +3 -5
  27. package/lib/versions/3.0.0.js +14 -0
  28. package/package.json +32 -40
  29. package/schema.json +1385 -164
  30. package/tsconfig.json +16 -6
  31. package/db.mjs +0 -86
  32. package/help/compile.txt +0 -17
  33. package/help/create.txt +0 -13
  34. package/help/help.txt +0 -11
  35. package/help/migrations apply.txt +0 -45
  36. package/help/migrations create.txt +0 -27
  37. package/help/migrations.txt +0 -4
  38. package/help/schema.txt +0 -25
  39. package/help/seed.txt +0 -36
  40. package/help/start.txt +0 -47
  41. package/help/types.txt +0 -40
  42. package/index.test-d.ts +0 -43
  43. package/lib/adjust-config.js +0 -42
  44. package/lib/create.mjs +0 -89
  45. package/lib/gen-migration.mjs +0 -53
  46. package/lib/gen-schema.mjs +0 -68
  47. package/lib/gen-types.mjs +0 -202
  48. package/lib/generator/README.md +0 -38
  49. package/lib/generator/db-generator.js +0 -260
  50. package/lib/generator.d.ts +0 -7
  51. package/lib/migrate.mjs +0 -81
  52. package/lib/seed.mjs +0 -90
  53. package/lib/stackable.js +0 -49
  54. /package/{lib/root-endpoint/public → public}/images/dark_mode.svg +0 -0
  55. /package/{lib/root-endpoint/public → public}/images/favicon.ico +0 -0
  56. /package/{lib/root-endpoint/public → public}/images/light_mode.svg +0 -0
  57. /package/{lib/root-endpoint/public → public}/images/platformatic-logo-dark.svg +0 -0
  58. /package/{lib/root-endpoint/public → public}/images/platformatic-logo-light.svg +0 -0
  59. /package/{lib/root-endpoint/public → public}/images/triangle_dark.svg +0 -0
  60. /package/{lib/root-endpoint/public → public}/images/triangle_light.svg +0 -0
  61. /package/{lib/root-endpoint/public → public}/index.html +0 -0
package/lib/seed.mjs DELETED
@@ -1,90 +0,0 @@
1
- import pino from 'pino'
2
- import pretty from 'pino-pretty'
3
- import { access, readFile } from 'fs/promises'
4
- import { setupDB } from './utils.js'
5
- import { Migrator } from './migrator.mjs'
6
- import { pathToFileURL } from 'url'
7
- import { loadConfig } from '@platformatic/config'
8
- import { platformaticDB } from '../index.js'
9
- import errors from './errors.js'
10
- import tsCompiler from '@platformatic/ts-compiler'
11
- import { join, resolve } from 'node:path'
12
-
13
- async function execute (logger, seedFile, config) {
14
- const { db, sql, entities } = await setupDB(logger, config.db)
15
-
16
- await access(seedFile)
17
-
18
- logger.info(`seeding from ${seedFile}`)
19
- let seedFunction
20
- const importedFunction = await import(pathToFileURL(seedFile))
21
-
22
- if (typeof importedFunction === 'function') {
23
- seedFunction = importedFunction
24
- } else if (typeof importedFunction.seed === 'function') {
25
- seedFunction = importedFunction.seed
26
- } else if (typeof importedFunction.default === 'function') {
27
- seedFunction = importedFunction.default
28
- }
29
-
30
- if (!seedFunction) {
31
- logger.error('Cannot find seed function.')
32
- logger.error('If you use an ESM module use the signature \'export async function seed (opts)\'.')
33
- logger.error('If you use a CJS module use the signature \'module.exports = async function seed (opts)\'.')
34
- logger.error('If you use Typescript use the signature \'export async function seed(opts)\'')
35
- return
36
- }
37
- await seedFunction({ db, sql, entities, logger })
38
- logger.info('seeding complete')
39
-
40
- // Once done seeding, close your connection.
41
- await db.dispose()
42
- }
43
-
44
- async function seed (_args) {
45
- const logger = pino(pretty({
46
- translateTime: 'SYS:HH:MM:ss',
47
- ignore: 'hostname,pid',
48
- }))
49
-
50
- const { configManager, args } = await loadConfig({
51
- alias: {
52
- c: 'config',
53
- },
54
- }, _args, platformaticDB)
55
- await configManager.parseAndValidate()
56
- const config = configManager.current
57
-
58
- if (config.migrations !== undefined) {
59
- const migrator = new Migrator(config.migrations, config.db, logger)
60
-
61
- try {
62
- const hasMigrationsToApply = await migrator.hasMigrationsToApply()
63
- if (hasMigrationsToApply) {
64
- throw new errors.MigrationsToApplyError()
65
- }
66
- } finally {
67
- await migrator.close()
68
- }
69
- }
70
- let seedFile = args._[0]
71
- if (!seedFile) {
72
- throw new errors.MissingSeedFileError()
73
- }
74
- // check if we are in Typescript and, in case, compile it
75
- if (seedFile.endsWith('.ts')) {
76
- await tsCompiler.compile({
77
- cwd: process.cwd(),
78
- logger,
79
- tsConfig: configManager.current.plugins?.typescript?.tsConfig,
80
- flags: configManager.current.plugins?.typescript?.flags,
81
- })
82
- const tsConfigPath = config?.plugins?.typescript?.tsConfig || resolve(process.cwd(), 'tsconfig.json')
83
- const tsConfig = JSON.parse(await readFile(tsConfigPath, 'utf8'))
84
- const outDir = tsConfig.compilerOptions.outDir
85
- seedFile = join(outDir, seedFile.replace('.ts', '.js'))
86
- }
87
- await execute(logger, seedFile, config)
88
- }
89
-
90
- export { seed, execute }
package/lib/stackable.js DELETED
@@ -1,49 +0,0 @@
1
- 'use strict'
2
-
3
- const { ServiceStackable } = require('@platformatic/service')
4
-
5
- class DbStackable extends ServiceStackable {
6
- constructor (options) {
7
- super(options)
8
- this.#updateConfig()
9
- }
10
-
11
- #updateConfig () {
12
- if (!this.context) return
13
-
14
- const { isProduction } = this.context
15
- const config = this.configManager.current
16
-
17
- if (isProduction) {
18
- if (config.autogenerate) {
19
- config.autogenerate = false
20
- }
21
- }
22
-
23
- this.configManager.update(config)
24
- }
25
-
26
- async getMeta () {
27
- await this.init()
28
- await this.app.ready()
29
-
30
- const serviceMeta = await super.getMeta()
31
-
32
- const config = this.configManager.current
33
-
34
- const dbConfig = config.db
35
- const connectionString = dbConfig?.connectionString
36
-
37
- if (connectionString) {
38
- return {
39
- ...serviceMeta,
40
- db: {
41
- connectionStrings: [connectionString]
42
- }
43
- }
44
- }
45
-
46
- return serviceMeta
47
- }
48
- }
49
- module.exports = { DbStackable }
File without changes