0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(schemas): improve regexp

This commit is contained in:
Gao Sun 2023-09-19 12:03:58 +08:00
parent d065cbc623
commit a856145f81
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 8 additions and 5 deletions

View file

@ -17,7 +17,7 @@ export const generateSchema = ({ name, fields }: TableWithType) => {
`export type ${databaseEntryType} = {`, `export type ${databaseEntryType} = {`,
...fields.map( ...fields.map(
({ name, comments, type, isArray, nullable, hasDefaultValue }) => ({ name, comments, type, isArray, nullable, hasDefaultValue }) =>
conditionalString(comments && ` /** ${comments} */\n`) + conditionalString(comments && ` /**${comments}*/\n`) +
` ${camelcase(name)}${conditionalString( ` ${camelcase(name)}${conditionalString(
(nullable || hasDefaultValue || name === tenantId) && '?' (nullable || hasDefaultValue || name === tenantId) && '?'
)}: ${type}${conditionalString(isArray && '[]')}${conditionalString( )}: ${type}${conditionalString(isArray && '[]')}${conditionalString(
@ -29,7 +29,7 @@ export const generateSchema = ({ name, fields }: TableWithType) => {
`export type ${modelName} = {`, `export type ${modelName} = {`,
...fields.map( ...fields.map(
({ name, comments, type, isArray, nullable, hasDefaultValue }) => ({ name, comments, type, isArray, nullable, hasDefaultValue }) =>
conditionalString(comments && ` /** ${comments} */\n`) + conditionalString(comments && ` /**${comments}*/\n`) +
` ${camelcase(name)}: ${type}${conditionalString(isArray && '[]')}${ ` ${camelcase(name)}: ${type}${conditionalString(isArray && '[]')}${
nullable && !hasDefaultValue ? ' | null' : '' nullable && !hasDefaultValue ? ' | null' : ''
};` };`

View file

@ -6,13 +6,16 @@ import type { Field } from './types.js';
export const normalizeWhitespaces = (string: string): string => export const normalizeWhitespaces = (string: string): string =>
string.replaceAll(/\s+/g, ' ').trim(); string.replaceAll(/\s+/g, ' ').trim();
// eslint-disable-next-line unicorn/prevent-abbreviations -- JSDoc is a term
const leadingJsDocRegex = /^\s*\/\*\*([^*]*?)\*\//;
// eslint-disable-next-line unicorn/prevent-abbreviations -- JSDoc is a term // eslint-disable-next-line unicorn/prevent-abbreviations -- JSDoc is a term
export const stripLeadingJsDocComments = (string: string): string => export const stripLeadingJsDocComments = (string: string): string =>
string.replace(/^\s*\/\*\*[^*]+\*\//, '').trim(); string.replace(leadingJsDocRegex, '').trim();
// eslint-disable-next-line unicorn/prevent-abbreviations -- JSDoc is a term // eslint-disable-next-line unicorn/prevent-abbreviations -- JSDoc is a term
const getLeadingJsDocComments = (string: string): Optional<string> => const getLeadingJsDocComments = (string: string): Optional<string> =>
/^\s*\/\*\*([^*]+)\*\//g.exec(string)?.[1]?.trim(); leadingJsDocRegex.exec(string)?.[1];
// Remove all comments not start with @ // Remove all comments not start with @
export const removeUnrecognizedComments = (string: string): string => export const removeUnrecognizedComments = (string: string): string =>

View file

@ -18,7 +18,7 @@ create table sentinel_activities (
payload jsonb /* @use SentinelActivityPayload */ not null, payload jsonb /* @use SentinelActivityPayload */ not null,
/** The sentinel decision for the action. */ /** The sentinel decision for the action. */
decision sentinel_decision not null, decision sentinel_decision not null,
/** The expiry date of the decision. */ /** The expiry date of the decision. For instant decisions, this is the date the activity was created. */
decision_expires_at timestamptz not null default(now()), decision_expires_at timestamptz not null default(now()),
/** The time the activity was created. */ /** The time the activity was created. */
created_at timestamptz not null default(now()), created_at timestamptz not null default(now()),