Skip to content

Commit

Permalink
feat: sequelize integration part 5 - finished schemas
Browse files Browse the repository at this point in the history
creation in the express and fastify files
  • Loading branch information
AnthonyLzq committed Aug 7, 2022
1 parent 82151c0 commit 31fe704
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 3 deletions.
4 changes: 1 addition & 3 deletions example/fastify-graphql/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { Static, Type } from '@sinclair/typebox'
import Ajv from 'ajv/dist/2019.js'
import addFormats from 'ajv-formats'

const id = Type.String({
pattern: '^[a-zA-Z0-9]{24,}$'
})
const id = Type.String({ pattern: '^[a-zA-Z0-9]{24,}$' })

type ID = Static<typeof id>

Expand Down
92 changes: 92 additions & 0 deletions lib/src/functions/api/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,97 @@ ${projectName}/src/graphQL/models/utils/messages`
await Promise.all(processes)
}

/**
* @param {Object} args
* @param {String} args.projectName
* @param {Boolean} args.dbIsSQL
*/
const schemas = async ({ projectName, dbIsSQL }) => {
const createFoldersCommand = `mkdir ${projectName}/src/schemas`

if (platform() === 'win32')
await exec(createFoldersCommand.replaceAll('/', '\\'))
else await exec(createFoldersCommand)

const schemas = {
index: {
content: `import { Static, Type } from '@sinclair/typebox'
import Ajv from 'ajv/dist/2019.js'
import addFormats from 'ajv-formats'
const id = ${
dbIsSQL
? `Type.String({ pattern: '^[a-zA-Z0-9]{24,}$' })`
: `Type.Number()`
}
type ID = Static<typeof id>
const idSchema = Type.Object({ id })
type IDSchema = Static<typeof idSchema>
const ajv = addFormats(new Ajv(), ['email'])
.addKeyword('kind')
.addKeyword('modifier')
export { id, ID, idSchema, IDSchema, ajv }
export * from './user'
`,
file: `${projectName}/src/schemas/index.ts`
},
user: {
content: `import { Static, Type } from '@sinclair/typebox'
import { id } from '.'
const user = Type.Object({
lastName: Type.String(),
name: Type.String()
})
type User = Static<typeof user>
const userWithId = Type.Intersect([user, Type.Object({ id })])
type UserWithId = Static<typeof userWithId>
const userDto = Type.Object({
id: Type.Optional(id),
lastName: Type.String(),
name: Type.String(),
createdAt: Type.Optional(Type.String()),
updatedAt: Type.Optional(Type.String())
})
type UserDTO = Static<typeof userDto>
const storeUserDto = Type.Object({
args: user
})
type StoreUserDTO = Static<typeof storeUserDto>
export {
userDto,
UserDTO,
userWithId,
UserWithId,
user,
User,
storeUserDto,
StoreUserDTO
}
`,
file: `${projectName}/src/schemas/user.ts`
}
}

await Promise.all([
writeFile(schemas.index.file, schemas.index.content),
writeFile(schemas.user.file, schemas.user.content)
])
}

/**
* @param {Object} args
* @param {String} args.projectName
Expand All @@ -1505,6 +1596,7 @@ const main = async ({ projectName, graphQL, database }) => {

await types({ projectName, graphQL, dbIsSQL })
await network({ projectName, graphQL })
await schemas({ projectName, dbIsSQL })

if (dbIsSQL) await sql({ projectName, db: database })
else await mongo({ projectName })
Expand Down
92 changes: 92 additions & 0 deletions lib/src/functions/api/fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,97 @@ ${projectName}/src/graphQL/models/utils ${projectName}/src/graphQL/models/utils/
await Promise.all(processes)
}

/**
* @param {Object} args
* @param {String} args.projectName
* @param {Boolean} args.dbIsSQL
*/
const schemas = async ({ projectName, dbIsSQL }) => {
const createFoldersCommand = `mkdir ${projectName}/src/schemas`

if (platform() === 'win32')
await exec(createFoldersCommand.replaceAll('/', '\\'))
else await exec(createFoldersCommand)

const schemas = {
index: {
content: `import { Static, Type } from '@sinclair/typebox'
import Ajv from 'ajv/dist/2019.js'
import addFormats from 'ajv-formats'
const id = ${
dbIsSQL
? `Type.String({ pattern: '^[a-zA-Z0-9]{24,}$' })`
: `Type.Number()`
}
type ID = Static<typeof id>
const idSchema = Type.Object({ id })
type IDSchema = Static<typeof idSchema>
const ajv = addFormats(new Ajv(), ['email'])
.addKeyword('kind')
.addKeyword('modifier')
export { id, ID, idSchema, IDSchema, ajv }
export * from './user'
`,
file: `${projectName}/src/schemas/index.ts`
},
user: {
content: `import { Static, Type } from '@sinclair/typebox'
import { id } from '.'
const user = Type.Object({
lastName: Type.String(),
name: Type.String()
})
type User = Static<typeof user>
const userWithId = Type.Intersect([user, Type.Object({ id })])
type UserWithId = Static<typeof userWithId>
const userDto = Type.Object({
id: Type.Optional(id),
lastName: Type.String(),
name: Type.String(),
createdAt: Type.Optional(Type.String()),
updatedAt: Type.Optional(Type.String())
})
type UserDTO = Static<typeof userDto>
const storeUserDto = Type.Object({
args: user
})
type StoreUserDTO = Static<typeof storeUserDto>
export {
userDto,
UserDTO,
userWithId,
UserWithId,
user,
User,
storeUserDto,
StoreUserDTO
}
`,
file: `${projectName}/src/schemas/user.ts`
}
}

await Promise.all([
writeFile(schemas.index.file, schemas.index.content),
writeFile(schemas.user.file, schemas.user.content)
])
}

/**
* @param {Object} args
* @param {String} args.projectName
Expand All @@ -1557,6 +1648,7 @@ const main = async ({ projectName, graphQL, database }) => {

await types({ projectName, graphQL, dbIsSQL })
await network({ projectName, graphQL })
await schemas({ projectName, graphQL, dbIsSQL })

if (dbIsSQL) await sql({ projectName, db: database })
else await mongo({ projectName })
Expand Down

0 comments on commit 31fe704

Please sign in to comment.