Sequelize models contain optional fields corresponding to table joins.
For example, VideoModel
has a VideoChannel?: VideoChannelModel
field. It can be filled if the SQL query joined with the videoChannel
table or empty if not.
It can be difficult in TypeScript to understand if a function argument expects associations to be filled or not.
To improve clarity and reduce bugs, PeerTube defines multiple versions of a database model depending on its associations in server/core/types/models/
.
These models start with M
and by default do not include any association. MVideo
for example corresponds to VideoModel
without any association, where VideoChannel
attribute doesn’t exist. On the other hand, MVideoWithChannel
is a MVideo
that has a VideoChannel
field. This way, a function that accepts video: MVideoWithChannel
argument expects a video with channel populated. Main PeerTube code should never use ...Model
(VideoModel
) database type, but always M...
instead (MVideo
, MVideoChannel
etc).
Here’s a list of all the parts of the server to update if you want to add a new feature (new API REST endpoints for example) to the PeerTube server.
Some of these may be optional (for example your new endpoint may not need to send notifications) but this guide tries to be exhaustive.
config/default.yaml
and config/production.yaml
config/dev.yaml
and config/test.yaml
server/core/initializers/config.ts
server/core/initializers/checker-before-init.ts
server/core/initializers/checker-after-init.ts
packages/models/src/server/core/server-config.model.ts
server/core/lib/server-config-manager.ts
to include your new configurationpackages/models/src/server/core/custom-config.model.ts
server/core/controllers/api/config.ts
controllerserver/core/middlewares/validators
that will be used by your controllersserver/core/middlewares/validators/index.ts
so it’s easier to importserver/core/types/express.d.ts
to attach the database model loaded by your middleware to the express responseserver/core/helpers/custom-validators
packages/models
index.ts
of current directory to facilitate the importsserver/core/models/
:
@Column
loadBy...
listBy...
toFormattedJSON
that creates the JSON to send to the REST API from the modelserver/core/initializers/database.ts
server/core/types
to define multiple versions of your Sequelize model depending on database associationsserver/core/types/*/index.ts
to facilitate the importsserver/core/initializers/migrations
using raw SQL (copy the same SQL query as at PeerTube startup)LAST_MIGRATION_VERSION
in server/core/initializers/constants.ts
server/core/models/
initializers/migrations
using Sequelize Query Interface (.addColumn
, .dropTable
, .changeColumn
)LAST_MIGRATION_VERSION
in server/core/initializers/constants.ts
packages/models/src/users/user-notification.model.ts
server/core/lib/notifier/shared
:
server/core/lib/notifier/notifier.ts
server/core/assets/email-templates
:
../common/grettings
that already says “Hi” and “Cheers”. You just have to write the title and the content blocks that will be inserted in the appropriate places in the HTML templateuserNotification
:
UserNotificationModel
(don’t forget the index)packages/models/src/users/user-notification.model.ts
)UserNotificationModel.toFormattedJSON
UserNotificationsComponent
)UserNotification
)packages/server-commands/
that will wrap HTTP requests to your new endpointindex.ts
of current directorypackages/server-commands/src/server/core.ts
server/core/tests/api/check-params
to test middleware validators/authentification/user rights (offensive tests)server/core/tests/api/check-params/index.ts
server/core/tests/api
to test your new endpointsindex.ts
of current directoryserver/core/tests/api/notifications
support/doc/api/openapi.yaml