@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/tsconfig.json CHANGED
@@ -1,12 +1,22 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "module": "CommonJS",
4
- "moduleResolution": "node",
3
+ "module": "commonjs",
5
4
  "esModuleInterop": true,
6
- "allowSyntheticDefaultImports": true,
7
- "target": "es2019",
8
- "outDir": "build",
5
+ "lib": ["es2022"],
6
+ "target": "es2022",
9
7
  "sourceMap": true,
10
- "strictNullChecks": true
8
+ "pretty": true,
9
+ "noEmitOnError": true,
10
+ "incremental": true,
11
+ "strict": true,
12
+ "outDir": "dist",
13
+ "skipLibCheck": true
14
+ },
15
+ "watchOptions": {
16
+ "watchFile": "fixedPollingInterval",
17
+ "watchDirectory": "fixedPollingInterval",
18
+ "fallbackPolling": "dynamicPriority",
19
+ "synchronousWatchDirectory": true,
20
+ "excludeDirectories": ["**/node_modules", "dist"]
11
21
  }
12
22
  }
package/db.mjs DELETED
@@ -1,86 +0,0 @@
1
- #! /usr/bin/env node
2
-
3
- import commist from 'commist'
4
- import parseArgs from 'minimist'
5
- import isMain from 'es-main'
6
- import helpMe from 'help-me'
7
- import { readFile } from 'fs/promises'
8
- import { join } from 'desm'
9
- import { start, buildCompileCmd } from '@platformatic/service'
10
- import { printAndExitLoadConfigError } from '@platformatic/config'
11
- import { platformaticDB } from './index.js'
12
-
13
- import { applyMigrations } from './lib/migrate.mjs'
14
- import { seed } from './lib/seed.mjs'
15
- import { generateTypes } from './lib/gen-types.mjs'
16
- import { printGraphQLSchema, printOpenAPISchema, generateJsonSchemaConfig } from './lib/gen-schema.mjs'
17
- import { generateMigration } from './lib/gen-migration.mjs'
18
- import { createDB } from './lib/create.mjs'
19
-
20
- const compile = buildCompileCmd(platformaticDB)
21
-
22
- const help = helpMe({
23
- dir: join(import.meta.url, 'help'),
24
- // the default
25
- ext: '.txt',
26
- })
27
-
28
- function wrapCommand (fn) {
29
- return async function (...args) {
30
- try {
31
- return await fn(...args)
32
- } catch (err) {
33
- printAndExitLoadConfigError(err)
34
- }
35
- }
36
- }
37
-
38
- const program = commist({ maxDistance: 2 })
39
-
40
- program.register('help', help.toStdout)
41
- program.register('help start', help.toStdout.bind(null, ['start']))
42
- program.register('help compile', help.toStdout.bind(null, ['compile']))
43
- program.register('help migrations apply', help.toStdout.bind(null, ['migrations apply']))
44
- program.register({ command: 'help seed', strict: true }, help.toStdout.bind(null, ['seed']))
45
- program.register('help schema', help.toStdout.bind(null, ['schema']))
46
-
47
- program.register('start', (argv) => {
48
- start(platformaticDB, argv).catch(printAndExitLoadConfigError)
49
- })
50
-
51
- program.register('create', wrapCommand(createDB))
52
- program.register('compile', wrapCommand(compile))
53
- program.register('migrations create', wrapCommand(generateMigration))
54
- program.register('migrations apply', wrapCommand(applyMigrations))
55
- program.register('seed', wrapCommand(seed))
56
- program.register('types', wrapCommand(generateTypes))
57
- program.register('schema graphql', wrapCommand(printGraphQLSchema))
58
- program.register('schema openapi', wrapCommand(printOpenAPISchema))
59
- program.register('schema config', wrapCommand(generateJsonSchemaConfig))
60
- program.register('schema', help.toStdout.bind(null, ['schema']))
61
-
62
- // TODO add help command
63
-
64
- export async function runDB (argv) {
65
- const args = parseArgs(argv, {
66
- alias: {
67
- v: 'version',
68
- },
69
- })
70
-
71
- if (args.version) {
72
- console.log('v' + JSON.parse(await readFile(join(import.meta.url, 'package.json'), 'utf-8')).version)
73
- process.exit(0)
74
- }
75
-
76
- const output = await program.parseAsync(argv)
77
-
78
- return {
79
- output,
80
- help,
81
- }
82
- }
83
-
84
- if (isMain(import.meta)) {
85
- await runDB(process.argv.splice(2))
86
- }
package/help/compile.txt DELETED
@@ -1,17 +0,0 @@
1
- Compile typescript plugins.
2
-
3
- ``` bash
4
- $ platformatic db compile
5
- ```
6
-
7
- As a result of executing this command, the Platformatic DB will compile typescript
8
- plugins in the `outDir` directory.
9
-
10
- If not specified, the configuration will be loaded from any of the following, in the current directory.
11
-
12
- * `platformatic.json`, or
13
- * `platformatic.yml`, or
14
- * `platformatic.tml`
15
-
16
- You can find more details about the configuration format here:
17
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
package/help/create.txt DELETED
@@ -1,13 +0,0 @@
1
- Creates a new Platformatic DB application.
2
-
3
- Options are
4
-
5
- * `dir <string>` - the directory where to create the project (Default: `process.cwd() + 'platformatic-composer'`)
6
- * `port <string>` - the port where the application will listen (Default: `3042`)
7
- * `hostname <string>` - the hostname where the application will listen (Default: `0.0.0.0`)
8
- * `connectionString <string>` - the connection string for your database (Default: `sqlite://./db.sqlite`)
9
- * `migrations <boolean>` - Creates sample migrations (Default: `true`)
10
- * `git <boolean>` - Init the git repository (Default: `true`)
11
- * `typescript <boolean>` - Use Typescript (Default: `false`)
12
- * `install <boolean>` - Run or not `npm install` after creating the files (Default: `true`)
13
- * `plugin <boolean>` - Creates a sample plugin and tests (Default: `true`)
package/help/help.txt DELETED
@@ -1,11 +0,0 @@
1
- Available commands:
2
-
3
- * `help` - show this help message.
4
- * `help <command>` - shows more information about a command.
5
- * `start` - start the server.
6
- * `compile` - compile typescript plugins.
7
- * `seed` - run a seed file.
8
- * `types` - generate typescript types for entities.
9
- * `schema` - generate and print api schema.
10
- * `migrations create` - generate do and undo migration files.
11
- * `migrations apply` - apply migration files.
@@ -1,45 +0,0 @@
1
- Apply all configured migrations to the database:
2
-
3
- ``` bash
4
- $ platformatic db migrations apply
5
- ```
6
-
7
- The migrations will be applied in the order they are specified in the
8
- folder defined in the configuration file. If you want to apply a specific migration,
9
- you can use the `--to` option:
10
-
11
- ``` bash
12
- $ platformatic db migrations apply --to 001
13
- ```
14
-
15
- Here is an example migration:
16
-
17
- ``` sql
18
- CREATE TABLE graphs (
19
- id SERIAL PRIMARY KEY,
20
- name TEXT
21
- );
22
- ```
23
-
24
- You can always rollback to a specific migration with:
25
-
26
- ``` bash
27
- $ platformatic db migrations apply --to VERSION
28
- ```
29
-
30
- Use 000 to reset to the initial state.
31
-
32
- Options:
33
-
34
- * `-c, --config <path>` - Path to the configuration file.
35
- * `-t, --to <version>` - Migrate to a specific version.
36
-
37
- If not specified, the configuration will be loaded from any of the following, in the current directory.
38
-
39
- * `platformatic.json`, or
40
- * `platformatic.yml`, or
41
- * `platformatic.tml`
42
-
43
- You can find more details about the configuration format here:
44
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
45
-
@@ -1,27 +0,0 @@
1
- Create next migration files.
2
-
3
- ``` bash
4
- $ platformatic db migrations create
5
- ```
6
-
7
- It will generate do and undo sql files in the migrations folder. The name of the
8
- files will be the next migration number.
9
-
10
- ``` bash
11
- $ platformatic db migrations create
12
- ```
13
-
14
- The migration files are named `001.<do|undo>.sql`, `002.<do|undo>.sql` etc...
15
-
16
- Options:
17
-
18
- * `-c, --config <path>` - Path to the configuration file.
19
-
20
- If not specified, the configuration will be loaded from any of the following, in the current directory.
21
-
22
- * `platformatic.json`, or
23
- * `platformatic.yml`, or
24
- * `platformatic.tml`
25
-
26
- You can find more details about the configuration format here:
27
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
@@ -1,4 +0,0 @@
1
- Available commands:
2
-
3
- * `migrations create` - generate do and undo migration files.
4
- * `migrations apply` - apply migration files.
package/help/schema.txt DELETED
@@ -1,25 +0,0 @@
1
- Update the config schema file:
2
-
3
- * `schema config` - update the JSON schema config available on `platformatic.json`
4
-
5
- Your configuration on `platformatic.json` has a schema defined to improve the developer experience and avoid mistakes when updating the configuration of Platformatic DB.
6
- When you run `platformatic db init`, a new JSON `$schema` property is added in `platformatic.json`. This can allow your IDE to add suggestions (f.e. mandatory/missing fields, types, default values) by opening the config in `platformatic.json`.
7
- Running `platformatic schema config` you can update your schema so that it matches well the latest changes available on your config.
8
-
9
- Generate a schema from the database and prints it to standard output:
10
-
11
- * `schema graphql` - generate the GraphQL schema
12
- * `schema openapi` - generate the OpenAPI schema
13
-
14
- Options:
15
-
16
- * `-c, --config FILE` - Specify a configuration file to use.
17
-
18
- If not specified, the configuration will be loaded from any of the following, in the current directory.
19
-
20
- * `platformatic.json`, or
21
- * `platformatic.yml`, or
22
- * `platformatic.tml`
23
-
24
- You can find more details about the configuration format here:
25
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
package/help/seed.txt DELETED
@@ -1,36 +0,0 @@
1
- Load a seed into the database. This is a convenience method that loads
2
- a JavaScript file and configure @platformatic/sql-mapper to connect to
3
- the database specified in the configuration file.
4
-
5
- Here is an example of a seed file:
6
-
7
- ``` javascript
8
- 'use strict'
9
-
10
- module.exports = async function ({ entities, db, sql }) {
11
- await entities.graph.save({ input: { name: 'Hello' } })
12
- await db.query(sql`
13
- INSERT INTO graphs (name) VALUES ('Hello 2');
14
- `)
15
- }
16
- ```
17
-
18
- You can run this using the `seed` command:
19
-
20
- ``` bash
21
- $ platformatic db seed seed.js
22
- ```
23
-
24
- Options:
25
-
26
- * `--config` - Path to the configuration file.
27
-
28
- If not specified, the configuration will be loaded from any of the following, in the current directory.
29
-
30
- * `platformatic.json`, or
31
- * `platformatic.yml`, or
32
- * `platformatic.tml`
33
-
34
- You can find more details about the configuration format here:
35
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
36
-
package/help/start.txt DELETED
@@ -1,47 +0,0 @@
1
- Start the Platformatic DB server with the following command:
2
-
3
- ``` bash
4
- $ platformatic db start
5
- ```
6
-
7
- You will need a configuration file. Here is an example to get you started,
8
- save the following as `platformatic.json`:
9
-
10
- ``` json
11
- {
12
- "server": {
13
- "hostname": "127.0.0.1",
14
- "port": 0,
15
- "logger": {
16
- "level": "info"
17
- }
18
- },
19
- "db": {
20
- "connectionString": "sqlite://./db"
21
- },
22
- "migrations": {
23
- "dir": "./migrations"
24
- }
25
- }
26
- ```
27
-
28
- Remember to create a migration, run the `db help migrate` command to know more.
29
-
30
- All outstanding migrations will be applied to the database unless the
31
- `migrations.autoApply` configuration option is set to false.
32
-
33
- By sending the SIGUSR2 signal, the server can be reloaded.
34
-
35
- Options:
36
-
37
- * `-c, --config FILE` - Specify a configuration file to use.
38
-
39
- If not specified, the configuration will be loaded from any of the following, in the current directory.
40
-
41
- * `platformatic.json`, or
42
- * `platformatic.yml`, or
43
- * `platformatic.tml`
44
-
45
- You can find more details about the configuration format here:
46
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
47
-
package/help/types.txt DELETED
@@ -1,40 +0,0 @@
1
- Generate typescript types for your entities from the database.
2
-
3
- ``` bash
4
- $ platformatic db types
5
- ```
6
-
7
- As a result of executing this command, the Platformatic DB will generate a `types`
8
- folder with a typescript file for each database entity. It will also generate a
9
- `global.d.ts` file that injects the types into the Application instance.
10
-
11
- In order to add type support to your plugins, you need to install some additional
12
- dependencies. To do this, copy and run an `npm install` command with dependencies
13
- that "platformatic db types" will ask you.
14
-
15
- Here is an example of a platformatic plugin.js with jsdoc support.
16
- You can use it to add autocomplete to your code.
17
-
18
- ``` javascript
19
- /// <reference path="./global.d.ts" />
20
- 'use strict'
21
-
22
- /** @param {import('fastify').FastifyInstance} app */
23
- module.exports = async function (app) {
24
- app.get('/movie', async () => {
25
- const movies = await app.platformatic.entities.movie.find({
26
- where: { title: { eq: 'The Hitchhiker\'s Guide to the Galaxy' } }
27
- })
28
- return movies[0].id
29
- })
30
- }
31
- ```
32
-
33
- If not specified, the configuration will be loaded from any of the following, in the current directory.
34
-
35
- * `platformatic.json`, or
36
- * `platformatic.yml`, or
37
- * `platformatic.tml`
38
-
39
- You can find more details about the configuration format here:
40
- * [Platformatic DB Configuration](https://docs.platformatic.dev/docs/db/configuration)
package/index.test-d.ts DELETED
@@ -1,43 +0,0 @@
1
- import { buildServer, PlatformaticApp, PlatformaticDBMixin, PlatformaticDBConfig, Entities, errors } from '.'
2
- import ConfigManager from '@platformatic/config'
3
- import type { Database } from '@platformatic/sql-mapper'
4
- import { SQL } from '@databases/sql'
5
- import { expectType } from 'tsd'
6
- import { OpenAPI } from 'openapi-types'
7
- import type { MercuriusPlugin } from 'mercurius'
8
- import { FastifyError } from '@fastify/error'
9
-
10
- declare module 'fastify' {
11
- interface FastifyInstance {
12
- platformatic: PlatformaticApp<PlatformaticDBConfig> & PlatformaticDBMixin<Entities>
13
- }
14
- }
15
-
16
- async function main (): Promise<void> {
17
- // TODO this configuration is incomplete, type it fully
18
- const server = await buildServer({
19
- server: {
20
- port: 3042,
21
- host: '127.0.0.1',
22
- },
23
- })
24
-
25
- expectType<Database>(server.platformatic.db)
26
- expectType<SQL>(server.platformatic.sql)
27
- expectType<ConfigManager<PlatformaticDBConfig>>(server.platformatic.configManager)
28
- expectType<PlatformaticDBConfig>(server.platformatic.config)
29
- expectType<OpenAPI.Document>(server.swagger())
30
- expectType<MercuriusPlugin>(server.graphql)
31
- }
32
-
33
- main().catch(console.error)
34
-
35
- // Errors
36
- type ErrorWithNoParams = () => FastifyError
37
- type ErrorWithOneParam = (param: string) => FastifyError
38
-
39
- expectType<ErrorWithNoParams>(errors.MigrateMissingMigrationsError)
40
- expectType<ErrorWithNoParams>(errors.UnknownDatabaseError)
41
- expectType<ErrorWithOneParam>(errors.MigrateMissingMigrationsDirError)
42
- expectType<ErrorWithNoParams>(errors.MissingSeedFileError)
43
- expectType<ErrorWithNoParams>(errors.MigrationsToApplyError)
@@ -1,42 +0,0 @@
1
- 'use strict'
2
- const { resolve } = require('path')
3
- const { readFile } = require('fs/promises')
4
- const { platformaticService } = require('@platformatic/service')
5
-
6
- module.exports = async function adjustConfig (configManager) {
7
- await platformaticService.configManagerConfig.transformConfig.call(configManager)
8
-
9
- const dirOfConfig = configManager.dirname
10
- if (configManager.current.db && configManager.current.db.connectionString.indexOf('sqlite') === 0 && configManager.current.db.connectionString !== 'sqlite://:memory:') {
11
- const originalSqlitePath = configManager.current.db.connectionString.replace('sqlite://', '')
12
- const sqliteFullPath = resolve(dirOfConfig, originalSqlitePath)
13
- configManager.current.db.connectionString = 'sqlite://' + sqliteFullPath
14
- }
15
-
16
- /* c8 ignore next 3 */
17
- if (configManager.current.db.graphql?.schemaPath) {
18
- configManager.current.db.graphql.schema = await readFile(configManager.current.db.graphql.schemaPath, 'utf8')
19
- }
20
-
21
- /* c8 ignore next 2 */
22
- const arePostgresqlSchemaDefined = configManager.current.db?.connectionString.indexOf('postgres') === 0 && configManager.current.db?.schema?.length > 0
23
- const migrationsTableName = arePostgresqlSchemaDefined ? 'public.versions' : 'versions'
24
-
25
- // relative-to-absolute migrations path
26
- if (configManager.current.migrations) {
27
- configManager.current.migrations.table = configManager.current.migrations.table || migrationsTableName
28
- }
29
-
30
- if (configManager.current.migrations && configManager.current.db) {
31
- // TODO remove the ignores
32
- /* c8 ignore next 4 */
33
- configManager.current.db.ignore = configManager.current.db.ignore || {}
34
- configManager.current.db.ignore = Object.assign({}, {
35
- [configManager.current.migrations.table || migrationsTableName]: true,
36
- }, configManager.current.db.ignore)
37
- }
38
-
39
- if (configManager.current.types?.autogenerate === 'true') {
40
- configManager.current.types.autogenerate = true
41
- }
42
- }
package/lib/create.mjs DELETED
@@ -1,89 +0,0 @@
1
- 'use strict'
2
- import minimist from 'minimist'
3
- import { Generator } from '../lib/generator/db-generator.js'
4
- import { join } from 'node:path'
5
- import { getPkgManager } from '@platformatic/utils'
6
- import { execa } from 'execa'
7
- import ora from 'ora'
8
- import { Table } from 'console-table-printer'
9
- import pino from 'pino'
10
- import pinoPretty from 'pino-pretty'
11
-
12
- function printAppSummary (args, logger) {
13
- logger.info('Creating a Platformatic DB app with this config: ')
14
- const table = [
15
- { config: 'Directory', value: args.dir },
16
- { config: 'Connection String', value: args.connectionString },
17
- { config: 'Language', value: args.typescript ? 'Typescript' : 'Javascript' },
18
- { config: 'Init Git Repository', value: args.git },
19
- { config: 'Install Dependencies', value: args.install },
20
- { config: 'Sample Plugin and Tests', value: args.plugin },
21
- { config: 'Create Sample Migrations', value: args.migrations },
22
- ]
23
-
24
- const p = new Table({
25
- columns: [
26
- { name: 'config', alignment: 'right' },
27
- { name: 'value', alignment: 'left' },
28
- ],
29
- })
30
-
31
- p.addRows(table)
32
- p.printTable()
33
- }
34
- async function createDB (_args) {
35
- const stream = pinoPretty({
36
- translateTime: 'SYS:HH:MM:ss',
37
- ignore: 'hostname,pid',
38
- minimumLevel: 'debug',
39
- sync: true,
40
- })
41
-
42
- const logger = pino(stream)
43
-
44
- const args = minimist(process.argv.slice(2), {
45
- string: ['dir', 'port', 'hostname', 'connectionString'],
46
- boolean: ['typescript', 'install', 'migrations', 'plugin', 'git'],
47
- default: {
48
- dir: join(process.cwd(), 'platformatic-db'),
49
- port: 3042,
50
- hostname: '0.0.0.0',
51
- plugin: true,
52
- typescript: false,
53
- git: false,
54
- install: true,
55
- migrations: true,
56
- connectionString: 'sqlite://./db.sqlite',
57
- },
58
-
59
- })
60
-
61
- printAppSummary(args, logger)
62
-
63
- const gen = new Generator({})
64
- gen.setConfig({
65
- port: args.port,
66
- hostname: args.hostname,
67
- plugin: args.plugin,
68
- tests: args.plugin,
69
- typescript: args.typescript,
70
- initGitRepository: args.git,
71
- targetDirectory: args.dir,
72
- })
73
-
74
- try {
75
- await gen.run()
76
- if (args.install) {
77
- const pkgManager = getPkgManager()
78
- const spinner = ora('Installing dependencies...').start()
79
- await execa(pkgManager, ['install'], { cwd: args.dir })
80
- spinner.succeed()
81
- }
82
-
83
- logger.info('Done! 🎉')
84
- } catch (err) {
85
- logger.error(err.message)
86
- }
87
- }
88
-
89
- export { createDB }
@@ -1,53 +0,0 @@
1
- import { join } from 'path'
2
- import { writeFile } from 'fs/promises'
3
- import pino from 'pino'
4
- import pretty from 'pino-pretty'
5
- import { loadConfig } from '@platformatic/config'
6
- import { Migrator } from './migrator.mjs'
7
- import { platformaticDB } from '../index.js'
8
- import errors from './errors.js'
9
-
10
- async function generateMigration (_args) {
11
- const logger = pino(pretty({
12
- translateTime: 'SYS:HH:MM:ss',
13
- ignore: 'hostname,pid',
14
- }))
15
-
16
- const { configManager } = await loadConfig({}, _args, platformaticDB)
17
- await configManager.parseAndValidate()
18
- const config = configManager.current
19
-
20
- let migrator = null
21
- try {
22
- const migrationsConfig = config.migrations
23
- if (migrationsConfig === undefined) {
24
- throw new errors.MigrateMissingMigrationsError()
25
- }
26
-
27
- migrator = new Migrator(migrationsConfig, config.db, logger)
28
-
29
- const nextMigrationVersion = await migrator.getNextMigrationVersion()
30
- const nextMigrationVersionStr = migrator.convertVersionToStr(nextMigrationVersion)
31
-
32
- const nextDoMigrationName = `${nextMigrationVersionStr}.do.sql`
33
- const nextUndoMigrationName = `${nextMigrationVersionStr}.undo.sql`
34
-
35
- const doFile = join(migrator.migrationDir, nextDoMigrationName)
36
- const undoFile = join(migrator.migrationDir, nextUndoMigrationName)
37
-
38
- await Promise.all([
39
- writeFile(doFile, ''),
40
- writeFile(undoFile, ''),
41
- ])
42
-
43
- logger.info({ do: doFile, undo: undoFile }, 'Created migration files')
44
- } catch (error) {
45
- logger.error(error.message)
46
- } finally {
47
- if (migrator !== null) {
48
- await migrator.close()
49
- }
50
- }
51
- }
52
-
53
- export { generateMigration }