Skip to content

Commit

Permalink
refactor: extract db model factories package
Browse files Browse the repository at this point in the history
Related to #83
  • Loading branch information
JoosepAlviste committed Feb 3, 2024
1 parent 6124bec commit cc2b18c
Show file tree
Hide file tree
Showing 31 changed files with 147 additions and 62 deletions.
8 changes: 8 additions & 0 deletions apps/api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"extends": "@serieslist/eslint-config-base",
"root": true,
"rules": {
"no-restricted-imports": [
"error",
{
"patterns": ["#/features/*/*", "!#/features/*/jobs"]
}
]
},
"overrides": [
{
"files": ["src/test/mockTMDBServer.ts"],
Expand Down
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@graphql-codegen/client-preset": "^4.0.1",
"@graphql-tools/executor-http": "^1.0.0",
"@graphql-typed-document-node/core": "^3.2.0",
"@serieslist/db-factories": "workspace:^",
"@serieslist/esbuild": "workspace:*",
"@serieslist/eslint-config-base": "workspace:*",
"@serieslist/prettier-config": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/features/auth/__tests__/auth.schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { user, type User } from '@serieslist/db'
import { userFactory } from '@serieslist/db-factories'
import { eq } from 'drizzle-orm'
import { nanoid } from 'nanoid'

import { userFactory } from '#/features/users'
import type { LoginInput, RegisterInput } from '#/generated/gql/graphql'
import { graphql } from '#/generated/gql/index'
import { db } from '#/lib/db'
Expand Down
13 changes: 7 additions & 6 deletions apps/api/src/features/series/__tests__/series.schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import {
series,
userSeriesStatus,
} from '@serieslist/db'
import {
episodeFactory,
seasonFactory,
seriesFactory,
userSeriesStatusFactory,
userFactory,
} from '@serieslist/db-factories'
import {
tmdbSeasonFactory,
tmdbEpisodeFactory,
Expand All @@ -19,7 +26,6 @@ import { subDays } from 'date-fns'
import { and, eq } from 'drizzle-orm'
import { nanoid } from 'nanoid'

import { userFactory } from '#/features/users'
import { graphql } from '#/generated/gql'
import type { SeriesUpdateStatusInput } from '#/generated/gql/graphql'
import { db } from '#/lib/db'
Expand All @@ -31,11 +37,6 @@ import {
} from '#/test/testUtils'
import { parseDate } from '#/utils/date'

import { episodeFactory } from '../episode.factory'
import { seasonFactory } from '../season.factory'
import { seriesFactory } from '../series.factory'
import { userSeriesStatusFactory } from '../userSeriesStatus.factory'

describe('features/series/series.schema', () => {
describe('seriesSearch query', () => {
const executeSearch = (keyword = 'testing') =>
Expand Down
12 changes: 7 additions & 5 deletions apps/api/src/features/series/__tests__/series.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { episode, season, series, seriesProgress } from '@serieslist/db'
import {
episodeFactory,
seasonFactory,
seriesFactory,
seriesProgressFactory,
userFactory,
} from '@serieslist/db-factories'
import {
tmdbNotFoundResponseFactory,
tmdbSeasonFactory,
Expand All @@ -9,9 +16,7 @@ import type { LiterallyAnything } from '@serieslist/type-utils'
import { subDays } from 'date-fns'
import { and, eq } from 'drizzle-orm'

import { seriesProgressFactory } from '#/features/seriesProgress'
import { tmdbEpisodeFactory, tmdbSeriesDetailsFactory } from '#/features/tmdb'
import { userFactory } from '#/features/users'
import { db } from '#/lib/db'
import {
createContext,
Expand All @@ -20,9 +25,6 @@ import {
} from '#/test/testUtils'
import { formatDate } from '#/utils/date'

import { episodeFactory } from '../episode.factory'
import { seasonFactory } from '../season.factory'
import { seriesFactory } from '../series.factory'
import {
findStatusForSeries,
reSyncSeries,
Expand Down
4 changes: 0 additions & 4 deletions apps/api/src/features/series/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export * from './series.schema'
export * from './series.factory'
export * from './season.factory'
export * from './episode.factory'
export * from './userSeriesStatus.factory'
export { UserSeriesStatusStatus } from '@serieslist/db'
export * as episodesService from './episodes.service'
export * as seasonService from './season.service'
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/features/series/series.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { addDays, isFuture, subDays } from 'date-fns'
import index from 'just-index'
import unique from 'just-unique'

import { seriesProgressService } from '#/features/seriesProgress'
// TODO: This is a circular import
import * as seriesProgressService from '#/features/seriesProgress/seriesProgress.service'
import type {
SeriesUpdateStatusInput,
SeriesSearchInput,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { seenEpisode, seriesProgress, type User } from '@serieslist/db'
import { addDays } from 'date-fns'
import { and, eq, inArray } from 'drizzle-orm'

import { UserSeriesStatusStatus } from '@serieslist/db'
import {
seenEpisodeFactory,
seriesProgressFactory,
episodeFactory,
UserSeriesStatusStatus,
userSeriesStatusFactory,
} from '#/features/series'
import { userFactory } from '#/features/users'
userFactory,
} from '@serieslist/db-factories'
import { addDays } from 'date-fns'
import { and, eq, inArray } from 'drizzle-orm'

import { graphql } from '#/generated/gql'
import type {
MarkSeriesEpisodesAsSeenInput,
Expand All @@ -22,9 +24,6 @@ import {
executeOperation,
} from '#/test/testUtils'

import { seenEpisodeFactory } from '../seenEpisode.factory'
import { seriesProgressFactory } from '../seriesProgress.factory'

describe('features/seriesProgress/seriesProgress.schema', () => {
describe('toggleEpisodeSeen mutation', () => {
const executeMutation = async ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { seriesProgress } from '@serieslist/db'
import { seriesProgressFactory, seriesFactory } from '@serieslist/db-factories'
import { and, eq } from 'drizzle-orm'

import { seriesFactory } from '#/features/series'
import { db } from '#/lib/db'
import {
createContext,
createSeenEpisodesForUser,
createSeriesWithEpisodesAndSeasons,
} from '#/test/testUtils'

import { seriesProgressFactory } from '../seriesProgress.factory'
import {
recalculateSeriesProgress,
findLatestSeenEpisodesForSeries,
Expand Down
3 changes: 0 additions & 3 deletions apps/api/src/features/seriesProgress/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export * from './seenEpisode.factory'
export * from './seriesProgress.factory'

export * as seriesProgressService from './seriesProgress.service'
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import type {
import type { NonNullableFields } from '@serieslist/type-utils'
import index from 'just-index'

import { episodesService, seasonService } from '#/features/series'
// TODO: Importing these from #/features/series causes a circular import error
import * as episodesService from '#/features/series/episodes.service'
import * as seasonService from '#/features/series/season.service'
import { isTruthy } from '#/utils/isTruthy'

import * as seenEpisodeRepository from './seenEpisode.repository'
Expand Down
1 change: 0 additions & 1 deletion apps/api/src/features/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './users.schema'
export * from './user.factory'
export * as usersService from './users.service'
10 changes: 7 additions & 3 deletions apps/api/src/test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
import type { User, Episode, Season, Series } from '@serieslist/db'
import {
episodeFactory,
seasonFactory,
seriesFactory,
seenEpisodeFactory,
userFactory,
} from '@serieslist/db-factories'
import type { AuthenticatedContext, Context } from '@serieslist/graphql-server'
import type { NotWorthIt } from '@serieslist/type-utils'
import { type ExecutionResult, print } from 'graphql'
import { createYoga, type YogaInitialContext } from 'graphql-yoga'

import { episodeFactory, seasonFactory, seriesFactory } from '#/features/series'
import { seenEpisodeFactory } from '#/features/seriesProgress'
import { userFactory } from '#/features/users'
import { createArrayOfLength } from '#/lib/createArrayOfLength'
import { db } from '#/lib/db'
import { schema } from '#/schema'
Expand Down
4 changes: 4 additions & 0 deletions packages/db-factories/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@serieslist/eslint-config-base",
"root": true
}
1 change: 1 addition & 0 deletions packages/db-factories/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@serieslist/prettier-config"
28 changes: 28 additions & 0 deletions packages/db-factories/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@serieslist/db-factories",
"version": "1.0.0",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"import": "./src/index.ts",
"types": "./src/index.ts"
}
},
"scripts": {
"lint": "eslint src",
"tsc": "tsc --noEmit"
},
"devDependencies": {
"@serieslist/eslint-config-base": "workspace:*",
"@serieslist/prettier-config": "workspace:*",
"@serieslist/typescript-config-base": "workspace:*",
"typescript": "^5.3.3"
},
"dependencies": {
"@serieslist/db": "workspace:^",
"@serieslist/logger": "workspace:^",
"fishery": "^2.2.2",
"nanoid": "^4.0.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { episode, type Episode } from '@serieslist/db'
import { Factory } from 'fishery'
import { nanoid } from 'nanoid'

import { db } from '#/lib/db'
import { generateRandomInt } from '#/utils/generateRandomInt'

import { db } from './lib/db'
import { seasonFactory } from './season.factory'
import { generateRandomInt } from './utils/generateRandomInt'

export const episodeFactory = Factory.define<Episode>(
({ sequence, onCreate, params }) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/db-factories/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './episode.factory'
export * from './season.factory'
export * from './seenEpisode.factory'
export * from './series.factory'
export * from './seriesProgress.factory'
export * from './user.factory'
export * from './userSeriesStatus.factory'
5 changes: 5 additions & 0 deletions packages/db-factories/src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createDbConnection } from '@serieslist/db'

import { log } from './logger'

export const { db } = await createDbConnection({ logger: log })
3 changes: 3 additions & 0 deletions packages/db-factories/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createLogger } from '@serieslist/logger'

export const log = createLogger({ name: 'db-factories' })
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { season, type Season } from '@serieslist/db'
import { Factory } from 'fishery'

import { db } from '#/lib/db'
import { generateRandomInt } from '#/utils/generateRandomInt'

import { db } from './lib/db'
import { seriesFactory } from './series.factory'
import { generateRandomInt } from './utils/generateRandomInt'

export const seasonFactory = Factory.define<Season>(
({ sequence, onCreate, params }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { seenEpisode, type SeenEpisode } from '@serieslist/db'
import { Factory } from 'fishery'

import { episodeFactory } from '#/features/series'
import { userFactory } from '#/features/users'
import { db } from '#/lib/db'
import { episodeFactory } from './episode.factory'
import { db } from './lib/db'
import { userFactory } from './user.factory'

export const seenEpisodeFactory = Factory.define<SeenEpisode>(
({ onCreate, params }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { series, type Series } from '@serieslist/db'
import { Factory } from 'fishery'
import { nanoid } from 'nanoid'

import { db } from '#/lib/db'
import { generateRandomInt } from '#/utils/generateRandomInt'
import { db } from './lib/db'
import { generateRandomInt } from './utils/generateRandomInt'

export const seriesFactory = Factory.define<Series>(
({ sequence, onCreate }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { seriesProgress, type SeriesProgress } from '@serieslist/db'
import { Factory } from 'fishery'

import { episodeFactory, seriesFactory } from '#/features/series'
import { userFactory } from '#/features/users'
import { db } from '#/lib/db'
import { episodeFactory } from './episode.factory'
import { db } from './lib/db'
import { seriesFactory } from './series.factory'
import { userFactory } from './user.factory'

export const seriesProgressFactory = Factory.define<SeriesProgress>(
({ params, onCreate }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { user, type User } from '@serieslist/db'
import { Factory } from 'fishery'
import { nanoid } from 'nanoid'

import { db } from '#/lib/db'
import { db } from './lib/db'

export const userFactory = Factory.define<User>(({ sequence, onCreate }) => {
onCreate(async (userArgs) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import type { UserSeriesStatus } from '@serieslist/db'
import { UserSeriesStatusStatus, userSeriesStatus } from '@serieslist/db'
import { Factory } from 'fishery'

import { db } from '#/lib/db'

import { userFactory } from '../users'

import { db } from './lib/db'
import { seriesFactory } from './series.factory'
import { userFactory } from './user.factory'

export const userSeriesStatusFactory = Factory.define<UserSeriesStatus>(
({ params, onCreate }) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/db-factories/src/utils/generateRandomInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const generateRandomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min)
}
3 changes: 3 additions & 0 deletions packages/db-factories/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@serieslist/typescript-config-base"
}
6 changes: 0 additions & 6 deletions packages/eslint-config-base/eslint-config-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ module.exports = {
},
},
],
'no-restricted-imports': [
'error',
{
patterns: ['#/features/*/*', '!#/features/*/jobs'],
},
],
'@nx/enforce-module-boundaries': ['error'],
},
overrides: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@serieslist/<?= name %>",
"name": "@serieslist/<%= name %>",
"version": "1.0.0",
"type": "module",
"sideEffects": false,
Expand Down
Loading

0 comments on commit cc2b18c

Please sign in to comment.