generated from codinsonn/universal-app-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add mock db driver & collect:drivers scripts
- Loading branch information
Showing
21 changed files
with
494 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z, schema } from '@green-stack/schemas' | ||
import { createSchemaModel } from '@db/driver' | ||
|
||
/* --- Schema ---------------------------------------------------------------------------------- */ | ||
|
||
export const Setting = schema('Setting', { | ||
key: z.string().addMeta({ isID: true }), | ||
value: z.any() | ||
}) | ||
|
||
/* --- Types ----------------------------------------------------------------------------------- */ | ||
|
||
export type Setting = z.infer<typeof Setting> | ||
|
||
/* --- Model ----------------------------------------------------------------------------------- */ | ||
|
||
export const SettingsModel = createSchemaModel(Setting) | ||
|
||
/* --- Drivers --------------------------------------------------------------------------------- */ | ||
|
||
export const driverModel = SettingsModel.driver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { validateDriver } from '../utils/validateDriver.db' | ||
|
||
/* --- Import Driver Methods ------------------------------------------------------------------- */ | ||
|
||
import { createSchemaModel } from '../utils/createSchemaModel.mock' | ||
|
||
/* --- Driver Validation ----------------------------------------------------------------------- */ | ||
|
||
export const driver = validateDriver({ | ||
createSchemaModel, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { dbDrivers } from '@app/registries/drivers.generated' | ||
import { appConfig } from '@app/config' | ||
|
||
/* --- Determine Main DB driver ---------------------------------------------------------------- */ | ||
|
||
const dbDriver = dbDrivers[appConfig.drivers.db] | ||
|
||
/* --- Re-export Driver Methods ---------------------------------------------------------------- */ | ||
|
||
export const createSchemaModel = dbDriver['createSchemaModel'] | ||
|
||
/* --- Export Driver Helpers ------------------------------------------------------------------- */ | ||
|
||
export { validateDriver } from './utils/validateDriver.db' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* --- Re-export Driver Models ----------------------------------------------------------------- */ | ||
|
||
export * from '@app/registries/models.generated' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import glob from 'glob' | ||
import fs from 'fs' | ||
import { addSetItem } from '@green-stack/utils/arrayUtils' | ||
import { parseWorkspaces, replaceMany } from '@green-stack/scripts/helpers/scriptUtils' | ||
|
||
/* --- models.generated.ts --------------------------------------------------------------------- */ | ||
|
||
const modelsFileTemplate = `// -i- Auto generated with "npx turbo run @db/driver#collect-models" | ||
{{modelImportLines}} | ||
/* --- Reexports ------------------------------------------------------------------------------- */ | ||
export { | ||
{{modelModuleAliasLines}} | ||
} | ||
/* --- Models ---------------------------------------------------------------------------------- */ | ||
const dbModels = { | ||
{{modelModuleAliasLines}} | ||
} | ||
/* --- Exports --------------------------------------------------------------------------------- */ | ||
export type DB_MODEL = keyof typeof dbModels | ||
export default dbModels | ||
` | ||
|
||
/* --- Types ----------------------------------------------------------------------------------- */ | ||
|
||
type ModelRegistry = { | ||
modelImportLines: string[], | ||
modelAliasEntryLines: string[], | ||
} | ||
|
||
/* --- collect-models -------------------------------------------------------------------------- */ | ||
|
||
const collectModels = () => { | ||
try { | ||
// Get all model file paths in /features/ & /packages/ workspaces | ||
const featureModelPaths = glob.sync('../../features/**/models/*.model.ts') | ||
const packageModelPaths = glob.sync('../../packages/**/models/*.model.ts') | ||
const allModelPaths = [...featureModelPaths, ...packageModelPaths] | ||
|
||
// Figure out import paths from each workspace | ||
const { workspaceImports } = parseWorkspaces() | ||
|
||
// Build overview of lines to build model registry files with | ||
const modelRegistry = allModelPaths.reduce((acc, modelFilePath) => { | ||
|
||
// Skip if not a valid model | ||
const modelFileContents = fs.readFileSync(modelFilePath, 'utf-8') | ||
const isValidModel = modelFileContents.includes('export const driverModel = ') | ||
if (!isValidModel) return acc | ||
|
||
// Figure out model workspace from filename | ||
const workspaceEntry = Object.entries(workspaceImports).find(([pathKey]) => { | ||
return modelFilePath.includes(pathKey) | ||
}) | ||
|
||
// Figure out model name from filename | ||
const [workspacePath, modelWorkspace] = workspaceEntry! | ||
const innerModelFilePath = modelFilePath.split(workspacePath)[1] | ||
const modelFilename = innerModelFilePath.replace('/models/', '') | ||
const modelFileModuleName = modelFilename.replace('.tsx', '').replace('.ts', '') | ||
const [modelName] = modelFileModuleName.split('.') | ||
const modelImportPath = `${modelWorkspace}/models/${modelFileModuleName}` | ||
const modelImportLine = `import { driverModel as ${modelName} } from '${modelImportPath}'` // prettier-ignore | ||
const modelModuleAliasLine = ` ${modelName}` | ||
|
||
// Add to the accumulator | ||
return { | ||
modelImportLines: addSetItem(acc.modelImportLines, modelImportLine), | ||
modelAliasEntryLines: addSetItem(acc.modelAliasEntryLines, modelModuleAliasLine), | ||
} | ||
|
||
}, {} as ModelRegistry) | ||
|
||
// Build models.generated.ts file | ||
const modelModuleAliasLines = modelRegistry.modelAliasEntryLines.join('\n,') | ||
const modelImportLines = modelRegistry.modelImportLines.join('\n') | ||
let modelsFileContent = modelsFileTemplate.replace('{{modelImportLines}}', modelImportLines) // prettier-ignore | ||
modelsFileContent = replaceMany(modelsFileContent, [' {{modelModuleAliasLines}}'], modelModuleAliasLines) // prettier-ignore | ||
fs.writeFileSync('../../packages/@registries/models.generated.ts', modelsFileContent) | ||
|
||
} catch (err) { | ||
console.log(err) | ||
console.error(err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
/* --- init ------------------------------------------------------------------------------------ */ | ||
|
||
collectModels() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from '@green-stack/schemas' | ||
|
||
/* --- Schema ---------------------------------------------------------------------------------- */ | ||
|
||
export const DbDriverSchema = z.object({ | ||
createSchemaModel: z.function().args( | ||
z.instanceof(z.ZodObject<z.ZodRawShape>), | ||
z.string().optional(), | ||
), | ||
}) | ||
|
||
/* --- Types ----------------------------------------------------------------------------------- */ | ||
|
||
export type DbDriverShape = z.infer<typeof DbDriverSchema> | ||
|
||
/** --- validateDriver() ----------------------------------------------------------------------- */ | ||
/** -i- Validates whether a DB driver matches the expected methods */ | ||
export const validateDriver = <DB_DRIVER extends DbDriverShape>(driver: DB_DRIVER) => { | ||
DbDriverSchema.parse(driver) | ||
return driver | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.