Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Mario564 committed Nov 27, 2024
1 parent cf7e8fc commit 22c5562
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 147 deletions.
1 change: 0 additions & 1 deletion drizzle-orm/src/pg-core/columns/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
ColumnBuilderExtraConfig,
ColumnBuilderRuntimeConfig,
ColumnDataType,
GeneratedColumnConfig,
HasGenerated,
MakeColumnConfig,
} from '~/column-builder.ts';
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-core/columns/varchar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { type AnyPgTable, pgTable } from '~/pg-core/table.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

Expand Down
14 changes: 7 additions & 7 deletions drizzle-typebox/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ import type {
PgVector,
} from 'drizzle-orm/pg-core';
import type { SQLiteInteger, SQLiteReal, SQLiteText } from 'drizzle-orm/sqlite-core';
import { CONSTANTS } from './constants';
import { isColumnType, isWithEnum } from './utils';
import type { BufferSchema, JsonSchema } from './utils';
import { CONSTANTS } from './constants.ts';
import { isColumnType, isWithEnum } from './utils.ts';
import type { BufferSchema, JsonSchema } from './utils.ts';

export const literalSchema = t.Union([t.String(), t.Number(), t.Boolean(), t.Null()]);
export const jsonSchema: JsonSchema = t.Recursive((self) =>
t.Union([literalSchema, t.Array(self), t.Record(t.String(), self)])
) as any;
TypeRegistry.Set('Buffer', (_, value) => value instanceof Buffer);
TypeRegistry.Set('Buffer', (_, value) => value instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof
export const bufferSchema: BufferSchema = { [Kind]: 'Buffer', type: 'buffer' } as any;

export function mapEnumValues(values: string[]) {
return values.reduce((acc, value) => ({ ...acc, [value]: value }), {});
return Object.fromEntries(values.map((value) => [value, value]));
}

/** @internal */
Expand Down Expand Up @@ -202,8 +202,8 @@ function numberColumnToSchema(column: Column, t: typeof typebox): TSchema {

function bigintColumnToSchema(column: Column, t: typeof typebox): TSchema {
const unsigned = column.getSQLType().includes('unsigned');
let min = unsigned ? 0n : CONSTANTS.INT64_MIN;
let max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX;
const min = unsigned ? 0n : CONSTANTS.INT64_MIN;
const max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX;

return t.BigInt({
minimum: min,
Expand Down
9 changes: 1 addition & 8 deletions drizzle-typebox/src/column.types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import type * as t from '@sinclair/typebox';
import type { Assume, Column } from 'drizzle-orm';
import type {
ArrayHasAtLeastOneValue,
BufferSchema,
ColumnIsGeneratedAlwaysAs,
IsNever,
Json,
JsonSchema,
} from './utils';
import type { ArrayHasAtLeastOneValue, BufferSchema, ColumnIsGeneratedAlwaysAs, IsNever, JsonSchema } from './utils.ts';

export type GetEnumValuesFromColumn<TColumn extends Column> = TColumn['_'] extends { enumValues: [string, ...string[]] }
? TColumn['_']['enumValues']
Expand Down
4 changes: 2 additions & 2 deletions drizzle-typebox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './schema';
export * from './schema.types';
export * from './schema.ts';
export * from './schema.types.ts';
10 changes: 5 additions & 5 deletions drizzle-typebox/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import type { TSchema } from '@sinclair/typebox';
import { Column, getTableColumns, getViewSelectedFields, is, isTable, isView, SQL } from 'drizzle-orm';
import type { Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import { columnToSchema, mapEnumValues } from './column';
import { columnToSchema, mapEnumValues } from './column.ts';
import type { Conditions } from './schema.types.internal.ts';
import type {
CreateInsertSchema,
CreateSchemaFactoryOptions,
CreateSelectSchema,
CreateUpdateSchema,
} from './schema.types';
import type { Conditions } from './schema.types.internal';
import { isPgEnum } from './utils';
} from './schema.types.ts';
import { isPgEnum } from './utils.ts';

function getColumns(tableLike: Table | View) {
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
Expand Down Expand Up @@ -39,7 +39,7 @@ function handleColumns(
}

const column = is(selected, Column) ? selected : undefined;
const schema = !!column ? columnToSchema(column, factory?.typeboxInstance ?? t) : t.Any();
const schema = column ? columnToSchema(column, factory?.typeboxInstance ?? t) : t.Any();
const refined = typeof refinement === 'function' ? refinement(schema) : schema;

if (conditions.never(column)) {
Expand Down
4 changes: 2 additions & 2 deletions drizzle-typebox/src/schema.types.internal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as t from '@sinclair/typebox';
import type { Assume, Column, DrizzleTypeError, SelectedFieldsFlat, Simplify, Table, View } from 'drizzle-orm';
import type { GetBaseColumn, GetEnumValuesFromColumn, GetTypeboxType, HandleColumn } from './column.types';
import type { GetSelection, RemoveNever } from './utils';
import type { GetBaseColumn, GetEnumValuesFromColumn, GetTypeboxType, HandleColumn } from './column.types.ts';
import type { GetSelection, RemoveNever } from './utils.ts';

export interface Conditions {
never: (column?: Column) => boolean;
Expand Down
4 changes: 2 additions & 2 deletions drizzle-typebox/src/schema.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type * as t from '@sinclair/typebox';
import type { Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import type { EnumValuesToEnum } from './column.types';
import type { BuildRefine, BuildSchema, NoUnknownKeys } from './schema.types.internal';
import type { EnumValuesToEnum } from './column.types.ts';
import type { BuildRefine, BuildSchema, NoUnknownKeys } from './schema.types.internal.ts';

export interface CreateSelectSchema {
<TTable extends Table>(table: TTable): BuildSchema<'select', TTable['_']['columns'], undefined>;
Expand Down
2 changes: 1 addition & 1 deletion drizzle-typebox/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Kind, Static, TSchema } from '@sinclair/typebox';
import type { Column, SelectedFieldsFlat, Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import type { literalSchema } from './column';
import type { literalSchema } from './column.ts';

export function isColumnType<T extends Column>(column: Column, columnTypes: string[]): column is T {
return columnTypes.includes(column.columnType);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-typebox/tests/mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type as t } from '@sinclair/typebox';
import { Equal, sql } from 'drizzle-orm';
import { type Equal, sql } from 'drizzle-orm';
import { int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import { test } from 'vitest';
import { jsonSchema } from '~/column.ts';
Expand Down
2 changes: 1 addition & 1 deletion drizzle-typebox/tests/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type as t } from '@sinclair/typebox';
import { Equal, sql } from 'drizzle-orm';
import { type Equal, sql } from 'drizzle-orm';
import { integer, pgEnum, pgMaterializedView, pgSchema, pgTable, pgView, serial, text } from 'drizzle-orm/pg-core';
import { test } from 'vitest';
import { jsonSchema } from '~/column.ts';
Expand Down
2 changes: 1 addition & 1 deletion drizzle-typebox/tests/sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type as t } from '@sinclair/typebox';
import { Equal, sql } from 'drizzle-orm';
import { type Equal, sql } from 'drizzle-orm';
import { int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import { test } from 'vitest';
import { bufferSchema, jsonSchema } from '~/column.ts';
Expand Down
14 changes: 7 additions & 7 deletions drizzle-valibot/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ import type {
} from 'drizzle-orm/pg-core';
import type { SQLiteInteger, SQLiteReal, SQLiteText } from 'drizzle-orm/sqlite-core';
import * as v from 'valibot';
import { CONSTANTS } from './constants';
import { isColumnType, isWithEnum } from './utils';
import type { Json } from './utils';
import { CONSTANTS } from './constants.ts';
import { isColumnType, isWithEnum } from './utils.ts';
import type { Json } from './utils.ts';

export const literalSchema = v.union([v.string(), v.number(), v.boolean(), v.null()]);
export const jsonSchema: v.GenericSchema<Json> = v.union([
literalSchema,
v.array(v.lazy(() => jsonSchema)),
v.record(v.string(), v.lazy(() => jsonSchema)),
]);
export const bufferSchema: v.GenericSchema<Buffer> = v.custom<Buffer>((v) => v instanceof Buffer);
export const bufferSchema: v.GenericSchema<Buffer> = v.custom<Buffer>((v) => v instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof

export function mapEnumValues(values: string[]) {
return values.reduce((acc, value) => ({ ...acc, [value]: value }), {});
return Object.fromEntries(values.map((value) => [value, value]));
}

/** @internal */
Expand Down Expand Up @@ -185,8 +185,8 @@ function numberColumnToSchema(column: Column): v.GenericSchema {

function bigintColumnToSchema(column: Column): v.GenericSchema {
const unsigned = column.getSQLType().includes('unsigned');
let min = unsigned ? 0n : CONSTANTS.INT64_MIN;
let max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX;
const min = unsigned ? 0n : CONSTANTS.INT64_MIN;
const max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX;

return v.pipe(v.bigint(), v.minValue(min), v.maxValue(max));
}
Expand Down
9 changes: 7 additions & 2 deletions drizzle-valibot/src/column.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Assume, Column } from 'drizzle-orm';
import { PgArray, PgBinaryVector, PgVarchar } from 'drizzle-orm/pg-core';
import type * as v from 'valibot';
import type { ArrayHasAtLeastOneValue, ColumnIsGeneratedAlwaysAs, IsNever, Json, RemoveNeverElements } from './utils';
import type {
ArrayHasAtLeastOneValue,
ColumnIsGeneratedAlwaysAs,
IsNever,
Json,
RemoveNeverElements,
} from './utils.ts';

export type GetEnumValuesFromColumn<TColumn extends Column> = TColumn['_'] extends { enumValues: [string, ...string[]] }
? TColumn['_']['enumValues']
Expand Down
4 changes: 2 additions & 2 deletions drizzle-valibot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './schema';
export * from './schema.types';
export * from './schema.ts';
export * from './schema.types.ts';
10 changes: 5 additions & 5 deletions drizzle-valibot/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Column, getTableColumns, getViewSelectedFields, is, isTable, isView, SQ
import type { Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import * as v from 'valibot';
import { columnToSchema, mapEnumValues } from './column';
import type { CreateInsertSchema, CreateSelectSchema, CreateUpdateSchema } from './schema.types';
import type { Conditions } from './schema.types.internal';
import { isPgEnum } from './utils';
import { columnToSchema, mapEnumValues } from './column.ts';
import type { Conditions } from './schema.types.internal.ts';
import type { CreateInsertSchema, CreateSelectSchema, CreateUpdateSchema } from './schema.types.ts';
import { isPgEnum } from './utils.ts';

function getColumns(tableLike: Table | View) {
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
Expand All @@ -32,7 +32,7 @@ function handleColumns(
}

const column = is(selected, Column) ? selected : undefined;
const schema = !!column ? columnToSchema(column) : v.any();
const schema = column ? columnToSchema(column) : v.any();
const refined = typeof refinement === 'function' ? refinement(schema) : schema;

if (conditions.never(column)) {
Expand Down
4 changes: 2 additions & 2 deletions drizzle-valibot/src/schema.types.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
GetEnumValuesFromColumn,
GetValibotType,
HandleColumn,
} from './column.types';
import type { GetSelection, RemoveNever } from './utils';
} from './column.types.ts';
import type { GetSelection, RemoveNever } from './utils.ts';

export interface Conditions {
never: (column?: Column) => boolean;
Expand Down
4 changes: 2 additions & 2 deletions drizzle-valibot/src/schema.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import type * as v from 'valibot';
import type { EnumValuesToEnum } from './column.types';
import type { BuildRefine, BuildSchema, NoUnknownKeys } from './schema.types.internal';
import type { EnumValuesToEnum } from './column.types.ts';
import type { BuildRefine, BuildSchema, NoUnknownKeys } from './schema.types.internal.ts';

export interface CreateSelectSchema {
<TTable extends Table>(table: TTable): BuildSchema<'select', TTable['_']['columns'], undefined>;
Expand Down
2 changes: 1 addition & 1 deletion drizzle-valibot/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Column, SelectedFieldsFlat, Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import type * as v from 'valibot';
import type { literalSchema } from './column';
import type { literalSchema } from './column.ts';

export function isColumnType<T extends Column>(column: Column, columnTypes: string[]): column is T {
return columnTypes.includes(column.columnType);
Expand Down
26 changes: 13 additions & 13 deletions drizzle-valibot/tests/mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Equal, sql } from 'drizzle-orm';
import { type Equal, sql } from 'drizzle-orm';
import { int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import * as v from 'valibot';
import { test } from 'vitest';
Expand Down Expand Up @@ -199,12 +199,12 @@ test('refine table - select', (t) => {

const result = createSelectSchema(table, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
});
const expected = v.object({
c1: v.nullable(intSchema),
c2: v.pipe(intSchema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
});
expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
Expand All @@ -220,12 +220,12 @@ test('refine table - insert', (t) => {

const result = createInsertSchema(table, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
});
const expected = v.object({
c1: v.optional(v.nullable(intSchema)),
c2: v.pipe(intSchema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
});
expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
Expand All @@ -241,12 +241,12 @@ test('refine table - update', (t) => {

const result = createUpdateSchema(table, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
});
const expected = v.object({
c1: v.optional(v.nullable(intSchema)),
c2: v.optional(v.pipe(intSchema, v.maxValue(1000))),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
});
expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
Expand Down Expand Up @@ -277,29 +277,29 @@ test('refine view - select', (t) => {

const result = createSelectSchema(view, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
nested: {
c5: (schema) => v.pipe(schema, v.maxValue(1000)),
c6: v.pipe(v.string(), v.transform((v) => Number(v))),
c6: v.pipe(v.string(), v.transform(Number)),
},
table: {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
},
});
const expected = v.object({
c1: v.nullable(intSchema),
c2: v.nullable(v.pipe(intSchema, v.maxValue(1000))),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
nested: v.object({
c4: v.nullable(intSchema),
c5: v.nullable(v.pipe(intSchema, v.maxValue(1000))),
c6: v.pipe(v.string(), v.transform((v) => Number(v))),
c6: v.pipe(v.string(), v.transform(Number)),
}),
table: v.object({
c1: v.nullable(intSchema),
c2: v.nullable(v.pipe(intSchema, v.maxValue(1000))),
c3: v.pipe(v.string(), v.transform((v) => Number(v))),
c3: v.pipe(v.string(), v.transform(Number)),
c4: v.nullable(intSchema),
c5: v.nullable(intSchema),
c6: v.nullable(intSchema),
Expand Down
Loading

0 comments on commit 22c5562

Please sign in to comment.