Files
speckle-server/packages/server/modules/core/dbSchema.ts
T
Gergő Jedlicska d1d5984e30 gergo/summaryEmails (#979)
* refactor(server emails): email transports module refactor to TypeScript

* refactor(docker-compose deps): move local email server to common dev compose file

* chore(server launch.json): add ts-node script running example

* chore(server deps): add nodemailer types package

* refactor(server activitystream): add strongly typed activity definitions

* feat(server activitystream): add activity repository

* feat(server info): add canonical url on the service level

* feat(server): add static file serving route to server core

* feat(server): add dependencies for periodical email digests

* feat(server activity stream): call the initialization step from the activity stream module

* feat(server activity digest): add WIP weekly email digest implementation

* feat(server digest email): smul upgrades and fixes to the email template and its contents

* just for Fabs to test

* chore(root package.json): remove deleted docker-compose references

* feat(frontend profile): add notification preferences panel

* feat(server digest emails): set prod ready cron tab and timespan

* refactor(server email digest): move templates into the email module

* refactor(server activity digests): refactor to use notifications infrastructure

* test(server activities): add tests and some refactor to activities and notification preferences

* refactor(notification preferences): fix minor issues

* test(server notification preferences test): fix describe nesting

* fix(server activities): add missing action types

* fix(server activities): fix errors after merging main

* test(server activity notifications): add test coverage for activity notifications service

* refactor(server activities): fixing tests and some cleanup

* feat(server cli): add summary notification command to cli

* chore(dev env db versions): upgrade local dev env versions

* chore(server deps): upgrade local dev db to pg 14

* fix(docker-compose): bind maildev to localhost

* process-scoped notifications test queues

* test(activity tests): add  sleep to fix flaky CI

* feat(activity digests): add demo date for digest trigger

* feat(activity digest): add UK timezone trigger date

Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com>
Co-authored-by: Fabians <fabis94@live.com>
2022-09-09 12:46:57 +02:00

244 lines
5.0 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import knex from '@/db/knex'
import { Knex } from 'knex'
import { reduce } from 'lodash'
/**
* TODO:
* ServerInvites:
* - Get rid of the 'used' field, it's not used anymore
*/
type SchemaConfig<T extends string, C extends string> = InnerSchemaConfig<T, C> & {
/**
* Return schema helper with custom configuration options
*/
with: (params?: SchemaConfigParams) => InnerSchemaConfig<T, C>
/**
* Helper with withoutTablePrefix set to true
*/
withoutTablePrefix: InnerSchemaConfig<T, C>
}
type InnerSchemaConfig<T extends string, C extends string> = {
/**
* Table name
*/
name: T
/**
* Get `knex(tableName)` QueryBuilder instance. Use the generic argument to type the results of the query.
*/
knex: <TResult = any>() => Knex.QueryBuilder<any, TResult>
/**
* Get names of table columns. The names can be prefixed with the table name or not, depending
* on whether `withoutTablePrefix` was set when accessing the helper.
*/
col: {
[colName in C]: string
}
/**
* All of the column names in an array
*/
cols: string[]
}
type SchemaConfigParams = {
/**
* Configure `col` properties to not have the table name prefixed. For the most part you want the prefix,
* cause this helps in queries with JOINS (when multiple tables have a col with the same name), but you don't
* want the prefix when triggering UPDATE queries, because the `SET <name> = <value>` syntax doesn't support
* column names with table prefixes.
*/
withoutTablePrefix?: boolean
}
function buildTableHelper<T extends string, C extends string>(
tableName: T,
columns: C[]
): SchemaConfig<T, C> {
function buildInnerSchemaConfig(
params: SchemaConfigParams = {}
): InnerSchemaConfig<T, C> {
const colName = (col: string) =>
params.withoutTablePrefix ? col : `${tableName}.${col}`
return {
name: tableName,
knex: () => knex(tableName),
col: reduce(
columns,
(prev, curr) => {
prev[curr] = colName(curr)
return prev
},
{} as Record<C, string>
),
cols: columns.map(colName)
}
}
return {
...buildInnerSchemaConfig(),
with: buildInnerSchemaConfig,
withoutTablePrefix: buildInnerSchemaConfig({ withoutTablePrefix: true })
}
}
/*
* TABLE HELPERS
* The generated helpers are used like this:
*
* Streams.name - TableName
* Streams.col.id - Get column names
* Streams.knex() - Get knex() instance for this specific table
*
* Streams.with({...}) - configure helper, e.g. disable table name being prefixed to col names:
* Streams.with({withoutTablePrefix: true}).col.id
*/
export const Streams = buildTableHelper('streams', [
'id',
'name',
'description',
'isPublic',
'clonedFrom',
'createdAt',
'updatedAt',
'allowPublicComments',
'isDiscoverable'
])
export const StreamAcl = buildTableHelper('stream_acl', [
'userId',
'resourceId',
'role'
])
export const StreamFavorites = buildTableHelper('stream_favorites', [
'streamId',
'userId',
'createdAt',
'cursor'
])
export const Users = buildTableHelper('users', [
'id',
'suuid',
'createdAt',
'name',
'bio',
'company',
'email',
'verified',
'avatar',
'profiles',
'passwordDigest',
'ip'
])
export const ServerAcl = buildTableHelper('server_acl', ['userId', 'role'])
export const Comments = buildTableHelper('comments', [
'id',
'streamId',
'authorId',
'createdAt',
'updatedAt',
'text',
'screenshot',
'data',
'archived',
'parentComment'
])
export const CommentLinks = buildTableHelper('comment_links', [
'commentId',
'resourceId',
'resourceType'
])
export const ServerInvites = buildTableHelper('server_invites', [
'id',
'target',
'inviterId',
'createdAt',
'used',
'message',
'resourceTarget',
'resourceId',
'role',
'token'
])
export const PasswordResetTokens = buildTableHelper('pwdreset_tokens', [
'id',
'email',
'createdAt'
])
export const RefreshTokens = buildTableHelper('refresh_tokens', [
'id',
'tokenDigest',
'appId',
'userId',
'createdAt',
'lifespan'
])
export const AuthorizationCodes = buildTableHelper('authorization_codes', [
'id',
'appId',
'userId',
'challenge',
'createdAt',
'lifespan'
])
export const ApiTokens = buildTableHelper('api_tokens', [
'id',
'tokenDigest',
'owner',
'name',
'lastChars',
'revoked',
'lifespan',
'createdAt',
'lastUsed'
])
export const EmailVerifications = buildTableHelper('email_verifications', [
'id',
'email',
'createdAt',
'used'
])
export const ServerAccessRequests = buildTableHelper('server_access_requests', [
'id',
'requesterId',
'resourceType',
'resourceId',
'createdAt',
'updatedAt'
])
export const StreamActivity = buildTableHelper('stream_activity', [
'streamId',
'time',
'resourceType',
'resourceId',
'actionType',
'userId',
'info',
'message'
])
export const UserNotificationPreferences = buildTableHelper(
'user_notification_preferences',
['userId', 'preferences']
)
export { knex }