Skip to content

Instantly share code, notes, and snippets.

@dac09
Last active January 8, 2024 11:58
Show Gist options
  • Save dac09/19649e97790cefd498eb798c3d516225 to your computer and use it in GitHub Desktop.
Save dac09/19649e97790cefd498eb798c3d516225 to your computer and use it in GitHub Desktop.
Fake generate files test to see api server dev restart time
// To access your database
// Append api/* to import from api and web/* to import from web
import http from 'http'
import { performance } from 'perf_hooks'
import { db } from 'api/src/lib/db'
const fs = require('fs')
const path = require('path')
export default async ({ args }) => {
// Your script here...
const { level } = args
for (let i = 0; i < level; i++) {
const name = generateRandomName()
generateSdl(name)
generateService(name)
}
let endTime, serverBroughtDownOnce
const startTime = performance.now()
const pingHealth = () => {
http
.get('http://localhost:8911/health', (res) => {
if (res.statusCode === 200 && serverBroughtDownOnce) {
endTime = performance.now()
}
})
.on('error', (e) => {
// Measuring the time from the server being down to the server being up
if (e.code === 'ECONNREFUSED') {
serverBroughtDownOnce = true
}
})
}
const c = setInterval(() => {
if (startTime && endTime) {
clearInterval(c)
console.log(
`:: Total execution time: ${endTime - startTime} milliseconds ::`
)
process.exit(0)
} else {
pingHealth()
}
}, 10)
}
const getSDLContents = (ENTNAME) => `export const schema = gql\`
type ${ENTNAME} {
id: Int!
name: String!
email: String!
message: String!
createdAt: DateTime!
}
type Query {
${ENTNAME}s: [${ENTNAME}!]! @requireAuth
${ENTNAME}(id: Int!): ${ENTNAME} @requireAuth
}
input Create${ENTNAME}Input {
name: String!
email: String!
message: String!
}
input Update${ENTNAME}Input {
name: String
email: String
message: String
}
type Mutation {
create${ENTNAME}(input: Create${ENTNAME}Input!): ${ENTNAME}! @requireAuth
update${ENTNAME}(id: Int!, input: Update${ENTNAME}Input!): ${ENTNAME}! @requireAuth
delete${ENTNAME}(id: Int!): ${ENTNAME}! @requireAuth
}
\``
const getServiceContents = (ENTNAME) => `
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import { requireAuth } from 'src/lib/auth'
export const ${ENTNAME}s = () => {
logger.info('> in ${ENTNAME}s()')
return db.${ENTNAME}.findMany()
}
export const ${ENTNAME} = ({ id }) => {
logger.info('> in ${ENTNAME}()')
logger.debug({ ${ENTNAME}Id: id }, 'Fetching ${ENTNAME} by id')
return db.${ENTNAME}.findUnique({
where: { id },
})
}
export const create${ENTNAME} = ({ input }) => {
logger.info('> in create${ENTNAME}()')
logger.debug({ ${ENTNAME}: input }, 'Creating ${ENTNAME} with the input')
return db.${ENTNAME}.create({
data: input,
})
}
export const update${ENTNAME} = ({ id, input }) => {
logger.info('> in update${ENTNAME}()')
logger.debug({ ${ENTNAME}: input }, 'Updating ${ENTNAME} with the input')
return db.${ENTNAME}.update({
data: input,
where: { id },
})
}
export const delete${ENTNAME} = ({ id }) => {
logger.info('> in delete${ENTNAME}()')
logger.debug({ ${ENTNAME}Id: id }, 'Delete ${ENTNAME} by id')
return db.${ENTNAME}.delete({
where: { id },
})
}
`
const generateSdl = (name) => {
const directory = 'api/src/graphql'
const fileExtension = '.sdl.js'
const filePath = path.join(directory, `${name}${fileExtension}`)
const fileContents = getSDLContents(name)
fs.writeFileSync(filePath, fileContents)
}
const generateService = (name) => {
const directory = 'api/src/services'
const fileExtension = '.js'
const directoryPath = path.join(directory, name)
const filePath = path.join(directoryPath, `${name}${fileExtension}`)
const fileContents = getServiceContents(name)
fs.mkdirSync(directoryPath, { recursive: true })
fs.writeFileSync(filePath, fileContents)
}
const generateRandomName = () => {
const adjectives = [
'happy',
'sad',
'brave',
'kind',
'clever',
'gentle',
'funny',
'wise',
'calm',
'honest',
]
const nouns = [
'cat',
'dog',
'bird',
'tree',
'river',
'mountain',
'sun',
'moon',
'star',
'cloud',
]
const randomAdjectiveIndex = Math.floor(Math.random() * adjectives.length)
const randomNounIndex = Math.floor(Math.random() * nouns.length)
const randomAdjective = adjectives[randomAdjectiveIndex]
const randomNoun = nouns[randomNounIndex]
const capitalizedAdjective =
randomAdjective.charAt(0).toUpperCase() + randomAdjective.slice(1)
const capitalizedNoun =
randomNoun.charAt(0).toUpperCase() + randomNoun.slice(1)
const randomName = `${capitalizedAdjective}${capitalizedNoun}`
return randomName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment