Skip to content

Commit

Permalink
✨ TableConfig を具象クラスのジェネリクスに追加, factories に fromUserId とか追加
Browse files Browse the repository at this point in the history
  • Loading branch information
wappon28dev committed Oct 26, 2024
1 parent 5be56b9 commit 5bc8c10
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/lib/classes/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const config = {

type Schema = Table2schema<typeof config>;

export class Category extends Table<Schema> {
export class Category extends Table<typeof config, Schema> {
constructor(data: Schema) {
super(data, config);
}
Expand Down
65 changes: 62 additions & 3 deletions src/lib/classes/project.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ResultAsync } from "neverthrow";
import { Category } from "./category";
import { Fruit } from "./project/fruit";
import { Pledge } from "./project/pledge";
import { Report } from "./project/report";
import { SponsorData } from "./project/sponsor-data";
import { Territory } from "./territory";
import { supabase } from "@/lib/services/supabase";
import { Table } from "@/lib/utils/table";
import {
type TableSchemaOf,
type TableConfig,
type Table2schema,
type TableError,
type TableResult,
} from "@/types/table";
import { type Override } from "@/types/utils";

Expand All @@ -31,14 +35,28 @@ type SchemaResolved = Override<
}
>;

export class Project extends Table<Schema, SchemaResolved> {
type ProjectRelated = {
sponsorData: SponsorData;
reports: Report[];
fruits: Fruit[];
pledges: Pledge[];
comments: Comment[];
};

type ProjectStatus = "seed" | "tsubomi" | "wakaba" | "tree" | "hana";

export class Project extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Project, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public getStatus(): ProjectStatus {
throw new Error(`Not implemented: ${this.config.className}.getStatus`);
}

public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from("projects")
Expand All @@ -55,4 +73,45 @@ export class Project extends Table<Schema, SchemaResolved> {
}))
.mapErr(this.transformError("resolveRelations"));
}

public resolveReferenced(): TableResult<ProjectRelated> {
const sponsorData = ResultAsync.fromSafePromise(
supabase
.from("sponsor_data")
.select("*")
.eq("project_id", this.data.project_id)
.single(),
)
.andThen(this.transform)
.map((data) => new SponsorData(data as TableSchemaOf<SponsorData>))
.mapErr(this.transformError("resolveReferenced"));

const others = ResultAsync.fromSafePromise(
supabase
.from("projects")
.select(
`
reports(*),
fruits(*),
pledges(*),
comments(*)
`,
)
.eq("project_id", this.data.project_id)
.single(),
)
.andThen(this.transform)
.map(({ reports, fruits, pledges, comments }) => ({
reports: reports.map((r) => new Report(r as TableSchemaOf<Report>)),
fruits: fruits.map((f) => new Fruit(f as TableSchemaOf<Fruit>)),
pledges: pledges.map((p) => new Pledge(p as TableSchemaOf<Pledge>)),
comments: comments.map((c) => new Comment(c as TableSchemaOf<Comment>)),
}))
.mapErr(this.transformError("resolveReferenced"));

return ResultAsync.combine([sponsorData, others]).map(([s, o]) => ({
sponsorData: s,
...o,
}));
}
}
6 changes: 3 additions & 3 deletions src/lib/classes/project/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type TableSchemaOf,
type TableConfig,
type Table2schema,
type TableError,
type TableResult,
} from "@/types/table";

const config = {
Expand All @@ -24,14 +24,14 @@ type SchemaResolved = Schema & {
project: Project;
};

export class Comment extends Table<Schema, SchemaResolved> {
export class Comment extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Comment, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from("comments")
Expand Down
6 changes: 3 additions & 3 deletions src/lib/classes/project/fruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Table2schema,
type TableConfig,
type TableSchemaOf,
type TableError,
type TableResult,
} from "@/types/table";

const config = {
Expand All @@ -27,14 +27,14 @@ type SchemaResolved = Schema & {
sponsor: Sponsor;
};

export class Fruit extends Table<Schema, SchemaResolved> {
export class Fruit extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Fruit, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/classes/project/pledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Table2schema,
type TableConfig,
type TableSchemaOf,
type TableError,
type TableResult,
} from "@/types/table";

const config = {
Expand All @@ -27,14 +27,14 @@ type SchemaResolved = Schema & {
sower: Sower;
};

export class Pledge extends Table<Schema, SchemaResolved> {
export class Pledge extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = this.getFactories(Pledge, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/classes/project/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Table2schema,
type TableConfig,
type TableSchemaOf,
type TableError,
type TableResult,
} from "@/types/table";

const config = {
Expand All @@ -27,14 +27,14 @@ type SchemaResolved = Schema & {
sponsor: Sponsor;
};

export class Report extends Table<Schema, SchemaResolved> {
export class Report extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Report, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
Expand Down
8 changes: 4 additions & 4 deletions src/lib/classes/project/sponsor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Table2schema,
type TableConfig,
type TableSchemaOf,
type TableError,
type TableResult,
} from "@/types/table";

const config = {
Expand All @@ -27,14 +27,14 @@ type SchemaResolved = Schema & {
sponsor: Sponsor;
};

export class SponsorData extends Table<Schema, SchemaResolved> {
export class SponsorData extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = this.getFactories(Sponsor, config);
static factories = this.getFactories(SponsorData, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/classes/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Table2schema,
type TableConfig,
type TableSchemaOf,
type TableError,
type TableResult,
} from "@/types/table";
import { type Override } from "@/types/utils";

Expand Down Expand Up @@ -45,14 +45,14 @@ type SchemaResolved = Override<
}
>;

export class Seed extends Table<Schema, SchemaResolved> {
export class Seed extends Table<typeof config, Schema, SchemaResolved> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Seed, config);

public override resolveRelations(): ResultAsync<SchemaResolved, TableError> {
public override resolveRelations(): TableResult<SchemaResolved> {
return ResultAsync.fromSafePromise(
supabase
.from("seeds")
Expand Down
36 changes: 27 additions & 9 deletions src/lib/classes/sower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
type UserId,
type TableSchemaOf,
type Table2schema,
type TableError,
type TableResult,
type TableInsertOf,
type TableBrandedId,
} from "@/types/table";
import { type Override, type OmitStrict } from "@/types/utils";
import { type Override } from "@/types/utils";

const config = {
className: "Sower",
Expand All @@ -21,17 +23,35 @@ type Schema = Override<
Table2schema<typeof config>,
{
user_id: UserId;
sower_id: TableBrandedId<Sower>;
}
>;

export class Sower extends Table<Schema> {
export class Sower extends Table<typeof config, Schema> {
constructor(data: Schema) {
super(data, config);
}

static factories = this.getFactories(Sower, config);
static factories = {
...this.getFactories(Sower, config),
fromUser(userId: UserId): TableResult<Sower> {
return ResultAsync.fromSafePromise(
supabase
.from("sowers")
.select()
.eq("user_id", userId)
.returns<TableSchemaOf<Sower>>()
.single(),
)
.andThen(Table.transform)
.map((data) => new Sower(data))
.mapErr((error) =>
Table.transformError(config, "factories.fromUser", error),
);
},
};

public fetchOwnSeeds(): ResultAsync<Seed[], TableError> {
public fetchOwnSeeds(): TableResult<Seed[]> {
return ResultAsync.fromSafePromise(
supabase
.from("seeds")
Expand All @@ -48,15 +68,13 @@ export class Sower extends Table<Schema> {
.mapErr(this.transformError("fetchOwnSeeds"));
}

public sowSeed(
sowData: OmitStrict<Seed["data"], "sower_id">,
): ResultAsync<Seed, TableError> {
public sowSeed(sowData: TableInsertOf<Seed>): TableResult<Seed> {
return ResultAsync.fromSafePromise(
supabase
.from("seeds")
.insert({
sower_id: this.data.sower_id,
...sowData,
sower_id: this.data.sower_id,
})
.select()
.returns<TableSchemaOf<Seed>>()
Expand Down
25 changes: 23 additions & 2 deletions src/lib/classes/sponsor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { ResultAsync } from "neverthrow";
import { supabase } from "@/lib/services/supabase";
import { Table } from "@/lib/utils/table";
import {
type UserId,
type TableConfig,
type Table2schema,
type TableResult,
type TableSchemaOf,
} from "@/types/table";
import { type Override } from "@/types/utils";

Expand All @@ -19,10 +23,27 @@ type Schema = Override<
user_id: UserId;
}
>;
export class Sponsor extends Table<Schema> {
export class Sponsor extends Table<typeof config, Schema> {
constructor(data: Schema) {
super(data, config);
}

static factories = this.getFactories(Sponsor, config);
static factories = {
...this.getFactories(Sponsor, config),
fromUser(userId: UserId): TableResult<Sponsor> {
return ResultAsync.fromSafePromise(
supabase
.from("sponsors")
.select()
.eq("user_id", userId)
.returns<TableSchemaOf<Sponsor>>()
.single(),
)
.andThen(Table.transform)
.map((data) => new Sponsor(data))
.mapErr((error) =>
Table.transformError(config, "factories.fromUser", error),
);
},
};
}
2 changes: 1 addition & 1 deletion src/lib/classes/territory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Schema = Override<
}
>;

export class Territory extends Table<Schema> {
export class Territory extends Table<typeof config, Schema> {
constructor(data: Schema) {
super(data, config);
}
Expand Down
Loading

0 comments on commit 5bc8c10

Please sign in to comment.