@prisma/client#Generator TypeScript Examples
The following examples show how to use
@prisma/client#Generator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: editPlaylist.tsx From frames with Mozilla Public License 2.0 | 6 votes |
PlaylistAtom = atom<Omit<FramesPlaylist, 'location'>>({
key: 'playlist',
default: {
name: '',
identifier: '',
isPublic: false,
generator: Generator.USER,
overview: '',
backdrop: '',
logo: '',
videos: [],
timestamp: ''
}
})
Example #2
Source File: listEditors.ts From frames with Mozilla Public License 2.0 | 6 votes |
/**
* @desc creates a playlist in the order the videos where provided
* @param videos - a number[] of the videoIds to be added to the playlist
* @param name - user generic name provided for the playlist
* @param userId - user identifier
* @param overview - user provided overview for the playlist
* @param generator - generated automatically by frames or by the user
* @param isPublic - if the playlist is public or not
*/
public async createPlaylists(videos: number[], name: string, userId: string, overview: string, generator: Generator, isPublic = false): Promise<string> {
const identifier = this.generateKey(7, 5);
if (generator === Generator.FRAMES) await this.prisma.playlist.deleteMany({where: {name, userId}});
await this.prisma.playlist.create({
data: {
userId, name, identifier, generator, isPublic, overview,
created: new Date(),
}
});
const data = videos.map(e => {
return {videoId: e, playlistId: identifier}
})
await this.prisma.playlistVideo.createMany({data});
return identifier;
}
Example #3
Source File: listEditors.ts From frames with Mozilla Public License 2.0 | 6 votes |
/**
* @desc generates a shuffled playlist of all the episode in a tv show media
* @param mediaId - media identifier for which episodes would be shuffled
* @param userId - user identifier
*/
public async shuffleMedia(mediaId: number, userId: string): Promise<PlayListResponse | null> {
const media = await this.prisma.media.findFirst({where: {id: mediaId}, include: {videos: true}});
if (media && media.type === 'SHOW') {
const videos = this.randomise(media.videos, media.videos.length, 0);
const shuffle = videos.map(e => e.id);
const overview = `Frames generated a shuffled playlist for ${media.name}: ${media.overview}`;
const identifier = await this.createPlaylists(shuffle, 'shuffle', userId, overview, Generator.FRAMES);
return await this.findFirstVideo(identifier, userId);
}
return null;
}
Example #4
Source File: listEditors.ts From frames with Mozilla Public License 2.0 | 6 votes |
/**
* @desc gets the displayable information for all the playlists of a user
* @param userId - user identifier
*/
public async getAllPlaylists(userId: string): Promise<Array<Omit<FramesPlaylist, 'videos'> & {videos: number[]}>> {
const playlist = await this.prisma.playlist.findMany({
where: {AND: [{userId}, {NOT: {generator: Generator.FRAMES}}]},
include: {playlistVideos: {orderBy: {id: 'asc'}, include: {video: {include: {media: true}}}}},
orderBy: {created: 'desc'}
});
return playlist.map(e => {
const {name, identifier, isPublic, overview, generator, playlistVideos} = e;
const {media} = playlistVideos[0].video;
const videos = playlistVideos.map(e => e.video.id);
const location = `/watch?playlistId=${playlistVideos[0].id}`;
return {
location,
timestamp: this.compareDates(e.created),
identifier, isPublic, overview, generator, name,
backdrop: media.backdrop, logo: media.logo, videos
}
})
}
Example #5
Source File: listEditors.ts From frames with Mozilla Public License 2.0 | 5 votes |
/**
* @desc saves a new playlist to the database
* @param userId - user identifier
* @param videos - videos to be saved
* @param name - name of the playlist
* @param overview - description of the playlist
* @param isPublic - is the playlist public
* @param identifier - identifier of the playlist
*/
public async createPlaylist(userId: string, videos: number[], name: string, overview: string, isPublic: boolean, identifier: string) {
const generator = Generator.USER;
const playlist = await this.prisma.playlist.upsert({
where: {identifier},
create: {
identifier,
name,
overview,
isPublic,
generator,
created: new Date(),
userId,
},
update: {
isPublic, overview, name,
created: new Date(),
},
});
if (playlist) {
await this.prisma.playlistVideo.deleteMany({where: {playlistId: playlist.identifier}});
await this.prisma.playlistVideo.createMany({
data: videos.map(video => ({
playlistId: playlist.identifier,
videoId: video,
})),
});
return true;
}
return false;
}
Example #6
Source File: listEditors.ts From frames with Mozilla Public License 2.0 | 5 votes |
/**
* @desc gets the integral playlist information
* @param identifier - the playlist identifier
* @param userId - user identifier
*/
public async getPlaylist(identifier: string, userId: string): Promise<FramesPlaylist> {
const playlist = await this.prisma.playlist.findUnique({
where: {identifier},
include: {playlistVideos: {orderBy: {id: 'asc'} ,include: {video: {include: {media: true, episode: true}}}}}
});
if (playlist && (playlist.userId === userId || playlist.isPublic)) {
const name = playlist.name;
const playlistVideos = playlist.playlistVideos;
const videos: FramePlaylistVideo[] = playlistVideos.map(v => {
const {media, episode} = v.video;
return {
id: v.id,
overview: episode?.overview || media.overview,
name: episode ? `${media.name}, S${episode.seasonId}: E${episode.episode}` : media.name,
backdrop: media.backdrop, logo: media.logo, type: media.type,
}
})
return {
name, identifier, videos,
isPublic: playlist.isPublic,
generator: playlist.generator,
overview: playlist.overview,
backdrop: videos[0]?.backdrop,
logo: videos[0]?.logo,
timestamp: this.compareDates(playlist.created),
location: `/watch?playlistId=${playlistVideos[0].id}`,
}
}
return {
name: '', identifier: identifier, videos: [],
isPublic: true,
generator: Generator.USER,
overview: '',
backdrop: '',
logo: '',
timestamp: '',
location: '',
}
}
Example #7
Source File: listEditors.ts From frames with Mozilla Public License 2.0 | 5 votes |
/**
* @desc Generates the playlist for a given item for a user
* @param type - the type of playlist to generate
* @param itemId - the item identifier
* @param userId - the user identifier
*/
public async generatePlaylist(type: 'PERSON' | 'COLLECTION' | 'COMPANY', itemId: number | string, userId: string) {
const user = await this.prisma.user.findUnique({where: {userId}});
let videoId: PlayListResponse | null = null;
if (user) {
switch (type) {
case "COLLECTION":
const collection = await this.prisma.media.findMany({
where: {
collection: {
path: ['id'], equals: itemId
}
}, orderBy: {release: 'asc'}, include: {videos: true}
});
const videosIds = collection.map(e => e.videos.map(e => e.id)).flat();
if (videosIds.length) {
const collection1 = collection[0].collection as { name: string };
const overview = `Frames created a playlist for the ${collection1.name} Collection: with ${videosIds.length} items`;
const playlistId = await this.createPlaylists(videosIds, collection1.name, user.userId, overview, Generator.FRAMES, false);
videoId = await this.findFirstVideo(playlistId, user.userId);
}
break;
case "PERSON":
const personMedia = await this.prisma.media.findMany({
where: {
castCrews: {
some: {tmdbId: itemId as number}
}
}, include: {videos: true, castCrews: true}, orderBy: {release: 'asc'}
});
const videosIdsPerson = personMedia.map(e => e.videos.map(e => e.id)).flat();
if (videosIdsPerson.length) {
const person = personMedia[0].castCrews.find(e => e.tmdbId === itemId);
const overview = `Frames created a playlist for ${person!.name} with ${videosIdsPerson.length} items`;
const playlistId = await this.createPlaylists(videosIdsPerson, person!.name, user.userId, overview, Generator.FRAMES, false);
videoId = await this.findFirstVideo(playlistId, user.userId);
}
break;
case "COMPANY":
const media = await this.prisma.media.findMany({include: {videos: true}}) as any[];
const productionCompanies = media.filter(e => e.production.some((e: any) => e.id === itemId));
const videosIdsCompany = productionCompanies.map(e => e.videos.map((e: any) => e.id)).flat();
if (videosIdsCompany.length) {
const company = productionCompanies[0].production.find((e: any) => e.id === itemId);
const overview = `Frames created a playlist for ${company!.name} with ${videosIdsCompany.length} items`;
const playlistId = await this.createPlaylists(videosIdsCompany, company!.name, user.userId, overview, Generator.FRAMES, false);
videoId = await this.findFirstVideo(playlistId, user.userId);
}
break;
}
}
return videoId;
}
Example #8
Source File: editPlaylist.tsx From frames with Mozilla Public License 2.0 | 4 votes |
export default function usePlaylist() {
const base = useBase();
const dispatch = useInfoDispatch();
const [state, setState] = useRecoilState(PlaylistAtom);
const setSides = useSetRecoilState(PlaylistSides);
const setIdentifier = useSetRecoilState(identifierAtom);
const [vid, setVideo] = useRecoilState(PlaylistVideoAtom);
const setSideState = useSetRecoilState(PlaylistAtomState);
const [name, setName] = useRecoilState(PlaylistNameSAtom);
const close = () => {
setIdentifier(null);
setTimeout(() => {
setState({
name: '',
identifier: '',
isPublic: false,
generator: Generator.USER,
overview: '',
backdrop: '',
logo: '',
videos: [],
timestamp: ''
});
setName('');
setVideo([]);
}, 300);
}
const openPlaylist = async (id: number) => {
const data = await base.makeRequest<{data: FramePlaylistVideo[], name: string}>(`/api/settings/getVideosForMedia`, {mediaId: id}, 'GET');
setVideo(data?.data || []);
setName(data?.name || '');
pushPlaylist(['playlists', 'overview', 'videos'], null);
}
const addVideoToPlaylist = async (videoId: number) => {
const data = await base.makeRequest<{data: FramePlaylistVideo, name: string}>(`/api/settings/getVideoForPlaylist`, {videoId}, 'GET');
if (data) {
setVideo([...vid, data.data]);
setName(data.name);
pushPlaylist(['playlists', 'overview', 'videos'], null);
}
}
const pushPlaylist = (sides: string[], identifier: string | null) => {
setSides(sides);
setSideState(sides[0]);
identifier = identifier || base.generateKey(7, 5);
setIdentifier(identifier);
}
const getPlaylists = async (set: (a: FramesPlaylist[]) => void) => {
const data = await base.makeRequest<FramesPlaylist[]>('/api/settings/getPlaylists', null);
if (data) set(data);
else set([]);
}
const getVideoForMedia = async (id: number, set: (a: UpdateSearch[]) => void, set2: (a: string) => void) => {
const data = await base.makeRequest<{name: string, data: FramePlaylistVideo[]}>(`/api/settings/getVideosForMedia`, {mediaId: id}, 'GET');
if (data) {
set([]);
set2('');
setVideo([...vid, ...data.data]);
setName(data.name);
}
}
const removeVideo = (id: number) => {
setState(p => {
return {
...p,
videos: p.videos.filter(v => v.id !== id)
}
});
}
const loadPlaylist = async (identifier: string) => {
let data = await base.makeRequest<FramesPlaylist>(`/api/settings/getPlaylist`, { identifier }, 'GET');
if (data) {
data.name = data.name === '' && name !== '' ? `${name}: Playlist` : data.name;
setState(data);
vid.length === 0 && setSideState('overview');
}
}
const saveToPlaylist = async (identifier: string) => {
const params = {
identifier,
videos: vid.map(e => e.id)
}
const data = await base.makeRequest<boolean>('/api/modify/addToPlaylist', params, 'POST');
close();
dispatch({
type: data ? 'alert' : 'error',
heading: data ? 'Playlist saved' : 'Error saving playlist',
message: data ? 'Playlist saved successfully' : 'There was an error saving the playlist'
});
}
const submit = async () => {
if (state.name !== '' && state.identifier !== '' && (state.videos.length > 0 || vid.length > 0) && state.overview !== '') {
const params = {
name: state.name,
identifier: state.identifier,
isPublic: state.isPublic,
generator: state.generator,
overview: state.overview,
backdrop: state.backdrop,
logo: state.logo,
videos: state.videos.map(e => e.id).concat(vid.map(e => e.id)),
timestamp: state.timestamp
}
const data = await base.makeRequest<boolean>('/api/modify/createPlaylist', params, 'POST');
close();
dispatch({
type: data ? 'alert' : 'error',
heading: data ? 'Playlist saved' : 'Error saving playlist',
message: data ? 'Playlist saved successfully' : 'There was an error saving the playlist'
});
} else {
dispatch({
type: 'error',
heading: 'Error saving playlist',
message: 'There was an error saving the playlist, consider adding a name, description, and some videos'
});
}
}
const deletePlaylist = async () => {
const data = await base.makeRequest<boolean>('/api/modify/deletePlaylist', { identifier: state.identifier }, 'GET');
close();
dispatch({
type: data ? 'alert' : 'error',
heading: data ? 'Playlist deleted' : 'Error deleting playlist',
message: data ? 'Playlist deleted successfully' : 'There was an error deleting the playlist'
});
}
return {addVideoToPlaylist, close, deletePlaylist, saveToPlaylist, loadPlaylist, pushPlaylist, getVideoForMedia, removeVideo, getPlaylists, openPlaylist, submit, setSideState}
}