Skip to content

Commit

Permalink
chore(utils): allow immutable in utils types (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Apr 3, 2024
1 parent 16bd643 commit 9b4ec81
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const TYPE_ADAPTERS = Object.freeze({
bool: 'boolean',
});

const defaultTypeShorthands: ColumnDefinitions = {
const DEFAULT_TYPE_SHORTHANDS: Readonly<ColumnDefinitions> = Object.freeze({
id: { type: 'serial', primaryKey: true }, // convenience type for serial primary keys
};
});

// some convenience adapters -- see above
export function applyTypeAdapters(type: string): string {
Expand All @@ -25,11 +25,11 @@ export function applyTypeAdapters(type: string): string {
: type;
}

function toType(type: string | ColumnDefinition): ColumnDefinition {
function toType(type: string | Readonly<ColumnDefinition>): ColumnDefinition {
return typeof type === 'string' ? { type } : type;
}

function removeType<TColumnDefinition extends ColumnDefinition>({
function removeType<TColumnDefinition extends Readonly<ColumnDefinition>>({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type,
...rest
Expand All @@ -38,11 +38,11 @@ function removeType<TColumnDefinition extends ColumnDefinition>({
}

export function applyType(
type: Type,
extendingTypeShorthands: ColumnDefinitions = {}
type: Readonly<Type>,
extendingTypeShorthands: Readonly<ColumnDefinitions> = {}
): ColumnDefinition & FunctionParamType {
const typeShorthands: ColumnDefinitions = {
...defaultTypeShorthands,
...DEFAULT_TYPE_SHORTHANDS,
...extendingTypeShorthands,
};

Expand Down
53 changes: 45 additions & 8 deletions test/utils/types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
import { describe, expect, it } from 'vitest';
import type { ColumnDefinitions } from '../../src/operations/tablesTypes';
import { applyType } from '../../src/utils';
import { applyType, applyTypeAdapters } from '../../src/utils';

describe('utils', () => {
describe('applyTypeAdapters', () => {
it.each([
['int', 'integer'],
['string', 'text'],
['float', 'real'],
['double', 'double precision'],
['datetime', 'timestamp'],
['bool', 'boolean'],
])('should map %s to %s', (type, mappedType) => {
const actual = applyTypeAdapters(type);

expect(actual).toBeTypeOf('string');
expect(actual).toBe(mappedType);
});

it('should map custom value', () => {
const actual = applyTypeAdapters('custom');

expect(actual).toBeTypeOf('string');
expect(actual).toBe('custom');
});
});

describe('applyType', () => {
it('should convert string', () => {
const type = 'type';

expect(applyType(type)).toEqual({ type });
const actual = applyType(type);

expect(actual).toBeTypeOf('object');
expect(actual).toStrictEqual({ type });
});

it('should apply id shorthand', () => {
expect(applyType('id')).toEqual({ type: 'serial', primaryKey: true });
const actual = applyType('id');

expect(actual).toBeTypeOf('object');
expect(actual).toStrictEqual({ type: 'serial', primaryKey: true });
});

it('should apply shorthand', () => {
const shorthandName = 'type';
const shorthandDefinition = { type: 'integer', defaultValue: 1 };

expect(
applyType(shorthandName, { [shorthandName]: shorthandDefinition })
).toEqual(shorthandDefinition);
const actual = applyType(shorthandName, {
[shorthandName]: shorthandDefinition,
});

expect(actual).toBeTypeOf('object');
expect(actual).toStrictEqual(shorthandDefinition);
});

it('should apply recursive shorthand', () => {
Expand All @@ -29,7 +61,10 @@ describe('utils', () => {
user: { type: 'ref', references: 'users' },
};

expect(applyType('user', shorthands)).toEqual({
const actual = applyType('user', shorthands);

expect(actual).toBeTypeOf('object');
expect(actual).toStrictEqual({
type: 'integer',
onDelete: 'CASCADE',
references: 'users',
Expand All @@ -42,7 +77,9 @@ describe('utils', () => {
user: { type: 'ref', references: 'users' },
};

expect(() => applyType('user', shorthands)).toThrow();
expect(() => applyType('user', shorthands)).toThrow(
new Error('Shorthands contain cyclic dependency: user, ref, user')
);
});
});
});

0 comments on commit 9b4ec81

Please sign in to comment.