hedgedoc/backend/src/database/seeds/02_api_token.ts
Philip Molares 4790705f0d
feat: add seeding
This allows us to get a pre-populated database to develop new functionality

Signed-off-by: Philip Molares <philip.molares@udo.edu>
2025-04-23 22:49:11 +02:00

30 lines
947 B
TypeScript

/*
* SPDX-FileCopyrightText: 2025 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { createHash } from 'crypto';
import { Knex } from 'knex';
import { FieldNameApiToken, TableApiToken } from '../types';
export async function seed(knex: Knex): Promise<void> {
// Clear table beforehand
await knex(TableApiToken).del();
// Insert an api token
const apiToken =
'LaD52wgw7pi5zVitv4gR5lxoUa6ncTQGASPmXDSdppB9xcd9kCtqjlrdQ8OOfmG9DNXGvfkIwaOCAv8nRp8IoQ';
await knex(TableApiToken).insert({
[FieldNameApiToken.id]: 'pA4mOf51bpY',
[FieldNameApiToken.userId]: 2,
[FieldNameApiToken.label]: 'Local Test User API Token',
[FieldNameApiToken.secretHash]: createHash('sha512')
.update(apiToken)
.digest('hex'),
// Token is valid for 2 years
[FieldNameApiToken.validUntil]: new Date(
new Date().getTime() + 2 * 365 * 24 * 60 * 60 * 1000,
),
});
}