Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-knives-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"farstorm": minor
---

Add explicit way to set nullability by providing 'NULLABLE' and 'NOT NULL' instead of the hard to read true/false on the nullable column
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default defineConfig([ {
"no-unsafe-optional-chaining": "error",
"no-unused-private-class-members": "warn",
"no-useless-backreference": "warn",
"require-atomic-updates": "warn",
// "require-atomic-updates": "warn",
"use-isnan": "error",
"valid-typeof": "error",
"eqeqeq": "off",
Expand Down
28 changes: 16 additions & 12 deletions src/entities/BaseEntity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Nullable } from "./Nullable.js";


export type BaseEntity = {
fields: Record<string, BaseFieldCustomType<any, any>> & { 'id': BaseIdField },
oneToOneOwned: Record<string, OneToOneRelationOwned>,
Expand All @@ -15,22 +18,22 @@ export type NonStrictBaseEntity = {
};

export type BaseIdField = {
nullableOnInput: true,
nullableOnOutput: false,
nullableOnInput: 'NULLABLE',
nullableOnOutput: 'NOT NULL',
toType: (sqlInput: number) => string,
fromType: (tsInput: string) => number,
};

export type BaseFieldCustomType<TS, SQL> = {
toType: (sqlInput: SQL) => TS,
fromType: (tsInput: TS) => SQL,
nullableOnInput: boolean,
nullableOnOutput: boolean,
nullableOnInput: Nullable,
nullableOnOutput: Nullable,
};

type OneToOneRelationOwned = { entity: string, nullable: boolean, inverse?: never };
type ManyToOneRelation = { entity: string, nullable: boolean, inverse?: never };
type OneToOneRelationInverse = { entity: string, inverse: string, nullable: boolean };
type OneToOneRelationOwned = { entity: string, nullable: Nullable, inverse?: never };
type ManyToOneRelation = { entity: string, nullable: Nullable, inverse?: never };
type OneToOneRelationInverse = { entity: string, inverse: string, nullable: Nullable };
type OneToManyRelation = { entity: string, inverse: string, nullable?: never };

export function defineEntity<const T extends NonStrictBaseEntity>(entity: T): {
Expand All @@ -39,17 +42,18 @@ export function defineEntity<const T extends NonStrictBaseEntity>(entity: T): {
oneToOneInverse: NonNullable<T['oneToOneInverse']>,
manyToOne: NonNullable<T['manyToOne']>,
oneToMany: NonNullable<T['oneToMany']>,
relations?: never,
} {
// It would be really nice to encode this in the type system, but that is a major PITA
if ('relations' in entity && entity.relations != null) throw new Error('Entity should not have a `relations` property, use the appropriate relation type keys instead');

return {
...entity,
fields: entity.fields,
oneToOneOwned: entity.oneToOneOwned ?? {},
oneToOneInverse: entity.oneToOneInverse ?? {},
manyToOne: entity.manyToOne ?? {},
oneToMany: entity.oneToMany ?? {},
// manyToMany: entity.relations, // TODO
} as const;
}

Expand All @@ -61,18 +65,18 @@ const defaultFieldTypes = {
Json: defineCustomField(false, (x: any) => x, (x: any) => x),
} as const;

export function defineField<Null extends boolean, T extends keyof typeof defaultFieldTypes>(type: T, nullable: Null): Omit<typeof defaultFieldTypes[T], 'nullableOnInput' | 'nullableOnOutput'> & { nullableOnInput: Null, nullableOnOutput: Null } {
export function defineField<Null extends Nullable, T extends keyof typeof defaultFieldTypes>(type: T, nullable: Null): Omit<typeof defaultFieldTypes[T], 'nullableOnInput' | 'nullableOnOutput'> & { nullableOnInput: Null, nullableOnOutput: Null } {
return { ...defaultFieldTypes[type], nullableOnInput: nullable, nullableOnOutput: nullable } as const;
}

export function defineAutogeneratedField<T extends keyof typeof defaultFieldTypes>(type: T): Omit<typeof defaultFieldTypes[T], 'nullableOnInput' | 'nullableOnOutput'> & { nullableOnInput: true, nullableOnOutput: false } {
return { ...defaultFieldTypes[type], nullableOnInput: true, nullableOnOutput: false } as const;
return { ...defaultFieldTypes[type], nullableOnInput: 'NULLABLE', nullableOnOutput: 'NOT NULL' } as const;
}

export function defineCustomField<Null extends boolean, ToType extends (x: any) => any, FromType extends (x: any) => any>(nullable: Null, toType: ToType, fromType: FromType) {
export function defineCustomField<Null extends Nullable, ToType extends (x: any) => any, FromType extends (x: any) => any>(nullable: Null, toType: ToType, fromType: FromType) {
return { nullableOnInput: nullable, nullableOnOutput: nullable, toType, fromType } as const;
}

export function defineIdField() {
return { nullableOnInput: true, nullableOnOutput: false, toType: (x: number) => x.toString(), fromType: (x: string) => Number(x) } as const;
return { nullableOnInput: 'NULLABLE', nullableOnOutput: 'NOT NULL', toType: (x: number) => x.toString(), fromType: (x: string) => Number(x) } as const;
}
8 changes: 8 additions & 0 deletions src/entities/Nullable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type LegacyNullable = boolean;
export type Nullable = 'NULLABLE' | 'NOT NULL' | LegacyNullable;

export function isNullable(nullable: Nullable) {
if (nullable == 'NULLABLE') return true;
if (nullable == 'NOT NULL') return false;
return !!nullable;
}
14 changes: 14 additions & 0 deletions src/helpers/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ export function sql(strings: TemplateStringsArray, ...keys: any[]) {
if (i < (strings.length - 1)) sqlOutput += '$' + (i + 1);
}
return { sql: sqlOutput, params: keys };
}

/**
* Merges multiple SqlStatement objects into a single one
*/
export function mergeSql(...sqlStatements: SqlStatement[]): SqlStatement {
let sqlOutput = '';
const params = [];
for (const sqls of sqlStatements) {
sqlOutput += sqls.sql + ' ';
params.push(...sqls.params);
}
sqlOutput = sqlOutput.split(/\$\d+/).map((part, i) => i > 0 ? ('$' + i + part) : part).join('');
return { sql: sqlOutput.trim(), params };
}
Loading
Loading