Skip to content

Commit 258ab61

Browse files
Claudebitnimble
andcommitted
refactor: reorganize schema file and use supabase schemas pattern
- Move all Zod type inferences to end of file - Delete manually created migration - Add collections.sql to supabase/schemas directory Co-authored-by: bitnimble <4076797+bitnimble@users.noreply.github.com>
1 parent c28ca18 commit 258ab61

3 files changed

Lines changed: 54 additions & 227 deletions

File tree

src/schema/collections.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import { ApiError, ApiSuccess } from './api';
55
export const CollectionUserProjection = z.object({
66
isFavorited: z.boolean(),
77
});
8-
export type CollectionUserProjection = z.infer<typeof CollectionUserProjection>;
98

109
export const CollectionMap = z.object({
1110
mapId: z.string(),
1211
position: z.number(),
1312
});
14-
export type CollectionMap = z.infer<typeof CollectionMap>;
1513

1614
export const Collection = z.object({
1715
id: z.string(),
@@ -24,31 +22,26 @@ export const Collection = z.object({
2422
favorites: z.number(),
2523
userProjection: CollectionUserProjection.optional(),
2624
});
27-
export type Collection = z.infer<typeof Collection>;
2825

2926
/* GET getCollection */
3027
export const GetCollectionSuccess = ApiSuccess.extend({
3128
collection: Collection,
3229
});
33-
export type GetCollectionSuccess = z.infer<typeof GetCollectionSuccess>;
3430

3531
export const GetCollectionResponse = z.discriminatedUnion('success', [
3632
GetCollectionSuccess,
3733
ApiError,
3834
]);
39-
export type GetCollectionResponse = z.infer<typeof GetCollectionResponse>;
4035

4136
/* GET findCollections */
4237
export const FindCollectionsSuccess = ApiSuccess.extend({
4338
collections: z.array(Collection),
4439
});
45-
export type FindCollectionsSuccess = z.infer<typeof FindCollectionsSuccess>;
4640

4741
export const FindCollectionsResponse = z.discriminatedUnion('success', [
4842
FindCollectionsSuccess,
4943
ApiError,
5044
]);
51-
export type FindCollectionsResponse = z.infer<typeof FindCollectionsResponse>;
5245

5346
/* POST createCollection */
5447
export const CreateCollectionRequest = z.object({
@@ -57,18 +50,15 @@ export const CreateCollectionRequest = z.object({
5750
albumArt: z.string().optional(),
5851
mapIds: z.array(z.string()).optional(),
5952
});
60-
export type CreateCollectionRequest = z.infer<typeof CreateCollectionRequest>;
6153

6254
export const CreateCollectionSuccess = ApiSuccess.extend({
6355
id: z.string(),
6456
});
65-
export type CreateCollectionSuccess = z.infer<typeof CreateCollectionSuccess>;
6657

6758
export const CreateCollectionResponse = z.discriminatedUnion('success', [
6859
CreateCollectionSuccess,
6960
ApiError,
7061
]);
71-
export type CreateCollectionResponse = z.infer<typeof CreateCollectionResponse>;
7262

7363
/* PUT updateCollection */
7464
export const UpdateCollectionRequest = z.object({
@@ -78,35 +68,47 @@ export const UpdateCollectionRequest = z.object({
7868
albumArt: z.string().optional(),
7969
mapIds: z.array(z.string()).optional(),
8070
});
81-
export type UpdateCollectionRequest = z.infer<typeof UpdateCollectionRequest>;
8271

8372
export const UpdateCollectionResponse = z.discriminatedUnion('success', [ApiSuccess, ApiError]);
84-
export type UpdateCollectionResponse = z.infer<typeof UpdateCollectionResponse>;
8573

8674
/* DELETE deleteCollection */
8775
export const DeleteCollectionSuccess = ApiSuccess.extend({});
88-
export type DeleteCollectionSuccess = z.infer<typeof DeleteCollectionSuccess>;
8976

9077
export const DeleteCollectionResponse = z.discriminatedUnion('success', [
9178
DeleteCollectionSuccess,
9279
ApiError,
9380
]);
94-
export type DeleteCollectionResponse = z.infer<typeof DeleteCollectionResponse>;
9581

9682
/* Collection Favorites */
9783
export const SetCollectionFavoritesRequest = z.object({
9884
collectionIds: z.array(z.string()),
9985
isFavorite: z.boolean(),
10086
});
101-
export type SetCollectionFavoritesRequest = z.infer<typeof SetCollectionFavoritesRequest>;
10287

10388
export const GetFavoriteCollectionsSuccess = ApiSuccess.extend({
10489
collections: z.array(Collection),
10590
});
106-
export type GetFavoriteCollectionsSuccess = z.infer<typeof GetFavoriteCollectionsSuccess>;
10791

10892
export const GetFavoriteCollectionsResponse = z.discriminatedUnion('success', [
10993
GetFavoriteCollectionsSuccess,
11094
ApiError,
11195
]);
96+
97+
/* Inferred types */
98+
export type CollectionUserProjection = z.infer<typeof CollectionUserProjection>;
99+
export type CollectionMap = z.infer<typeof CollectionMap>;
100+
export type Collection = z.infer<typeof Collection>;
101+
export type GetCollectionSuccess = z.infer<typeof GetCollectionSuccess>;
102+
export type GetCollectionResponse = z.infer<typeof GetCollectionResponse>;
103+
export type FindCollectionsSuccess = z.infer<typeof FindCollectionsSuccess>;
104+
export type FindCollectionsResponse = z.infer<typeof FindCollectionsResponse>;
105+
export type CreateCollectionRequest = z.infer<typeof CreateCollectionRequest>;
106+
export type CreateCollectionSuccess = z.infer<typeof CreateCollectionSuccess>;
107+
export type CreateCollectionResponse = z.infer<typeof CreateCollectionResponse>;
108+
export type UpdateCollectionRequest = z.infer<typeof UpdateCollectionRequest>;
109+
export type UpdateCollectionResponse = z.infer<typeof UpdateCollectionResponse>;
110+
export type DeleteCollectionSuccess = z.infer<typeof DeleteCollectionSuccess>;
111+
export type DeleteCollectionResponse = z.infer<typeof DeleteCollectionResponse>;
112+
export type SetCollectionFavoritesRequest = z.infer<typeof SetCollectionFavoritesRequest>;
113+
export type GetFavoriteCollectionsSuccess = z.infer<typeof GetFavoriteCollectionsSuccess>;
112114
export type GetFavoriteCollectionsResponse = z.infer<typeof GetFavoriteCollectionsResponse>;

supabase/migrations/20260209063547_add_collections.sql

Lines changed: 0 additions & 211 deletions
This file was deleted.

supabase/schemas/collections.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE TABLE collections (
2+
_id serial,
3+
id varchar(16) primary key,
4+
creator_id varchar(16) references users (id) not null,
5+
creation_date timestamp not null,
6+
name varchar(256) not null,
7+
description text,
8+
album_art text
9+
);
10+
11+
CREATE TABLE collection_maps (
12+
_id serial,
13+
collection_id varchar(16) references collections (id) not null,
14+
map_id varchar(16) references maps (id) not null,
15+
position int not null,
16+
constraint collection_map_position unique (collection_id, position),
17+
constraint collection_map_unique unique (collection_id, map_id)
18+
);
19+
20+
CREATE TABLE collection_favorites (
21+
_id serial,
22+
collection_id varchar(16) references collections (id) not null,
23+
user_id varchar(16) references users (id) not null,
24+
favorited_date timestamp not null,
25+
constraint collection_favorite unique (collection_id, user_id)
26+
);
27+
28+
CREATE INDEX idx_collections_creator_id ON collections (creator_id);
29+
CREATE INDEX idx_collection_maps_collection_id ON collection_maps (collection_id);
30+
CREATE INDEX idx_collection_maps_map_id ON collection_maps (map_id);
31+
CREATE INDEX idx_collection_favorites_collection_id ON collection_favorites (collection_id);
32+
CREATE INDEX idx_collection_favorites_user_id ON collection_favorites (user_id);
33+
34+
alter table collections enable row level security;
35+
alter table collection_maps enable row level security;
36+
alter table collection_favorites enable row level security;

0 commit comments

Comments
 (0)