Programmatic API
In many cases, it's useful to start Platformatic Service using an API instead of the command line, e.g., in tests where we want to start and stop our server programmatically.
Using buildServer Function
The buildServer function allows starting the Platformatic Service programmatically.
Basic Example
server.js
import { buildServer } from '@platformatic/service'
const app = await buildServer('path/to/platformatic.service.json')
await app.start()
const res = await fetch(app.url)
console.log(await res.json())
// do something
await app.close()
Custom Configuration
It is also possible to customize the configuration:
import { buildServer } from '@platformatic/service'
const app = await buildServer({
server: {
hostname: '127.0.0.1',
port: 0
}
})
await app.start()
const res = await fetch(app.url)
console.log(await res.json())
// do something
await app.close()
Creating a Reusable Application on Top of Platformatic Service
Platformatic DB is built on top of Platformatic Serivce. If you want to build a similar kind of tool, follow this example:
Example Plugin
import { buildServer, schema } from '@platformatic/service'
import { readFileSync } from 'node:fs'
async function myPlugin (app, opts) {
// app.platformatic.configManager contains an instance of the ConfigManager
console.log(app.platformatic.configManager.current)
await app.register(platformaticService, opts)
}
// break Fastify encapsulation
myPlugin[Symbol.for('skip-override')] = true
myPlugin.configType = 'myPlugin'
// This is the schema for this reusable application configuration file,
// customize at will but retain the base properties of the schema from
// @platformatic/service
myPlugin.schema = schema
// The configuration of the ConfigManager
myPlugin.configManagerConfig = {
version: JSON.parse(readFileSync(new URL(import.meta.url, 'package.json'))).version
schema: foo.schema,
allowToWatch: ['.env'],
schemaOptions: {
useDefaults: true,
coerceTypes: true,
allErrors: true,
strict: false
},
async transformConfig () {
console.log(this.current) // this is the current config
// In this method you can alter the configuration before the application
// is started. It's useful to apply some defaults that cannot be derived
// inside the schema, such as resolving paths.
}
}
const server = await buildServer('path/to/config.json', myPlugin)
await server.start()
const res = await fetch(server.listeningOrigin)
console.log(await res.json())
// do something
await service.close()
Using beforePlugins Option
If you want to provide functionality before the plugins are loaded, but after metrics and telemetry are in place,
you can use the beforePlugins option:
Example Plugin
async function myPlugin (app, opts) {
await app.register(platformaticService, {
...opts,
beforePlugins: [async function (app) {
app.decorate('myvalue', 42)
}]
})
}
TypeScript Support
To ensure this module works in a TypeScript setup (outside of an application created with create-platformatic), you need to add the following to your types: