Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-roses-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

feat(add): `drizzle` add-on can now create multiple configs _(a new folder `db-x` if `db` already exists)_
54 changes: 33 additions & 21 deletions packages/addons/drizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,22 @@ const options = defineAddonOptions()
})
.build();

let forNextSteps = '';

export default defineAddon({
id: 'drizzle',
shortDescription: 'database orm',
homepage: 'https://orm.drizzle.team',
options,
setup: ({ kit, unsupported, runsAfter, cwd, typescript }) => {
setup: ({ kit, unsupported, runsAfter }) => {
runsAfter('prettier');

const ext = typescript ? 'ts' : 'js';
if (!kit) {
return unsupported('Requires SvelteKit');
}

const baseDBPath = path.resolve(kit.libDirectory, 'server', 'db');
const paths = {
'drizzle config': path.relative(cwd, path.resolve(cwd, `drizzle.config.${ext}`)),
'database schema': path.relative(cwd, path.resolve(baseDBPath, `schema.${ext}`)),
database: path.relative(cwd, path.resolve(baseDBPath, `index.${ext}`))
};

for (const [fileType, filePath] of Object.entries(paths)) {
if (fs.existsSync(filePath)) {
unsupported(`Preexisting ${fileType} file at '${filePath}'`);
}
}
},
run: ({ sv, typescript, options, kit, dependencyVersion }) => {
run: ({ sv, typescript, options, kit, dependencyVersion, cwd }) => {
if (!kit) throw new Error('SvelteKit is required');
const ext = typescript ? 'ts' : 'js';

sv.devDependency('drizzle-orm', '^0.40.0');
Expand Down Expand Up @@ -206,7 +195,24 @@ export default defineAddon({
});
}

sv.file(`drizzle.config.${ext}`, (content) => {
let baseDBPath = path.resolve(cwd, kit.libDirectory, 'server', 'db');
const paths = {
drizzleConfig: path.resolve(cwd, `drizzle.config.${ext}`),
databaseSchema: path.resolve(baseDBPath, `schema.${ext}`),
database: path.resolve(baseDBPath, `index.${ext}`)
};

let index = 2; // let's start with 2.
while (Object.values(paths).some(fs.existsSync)) {
baseDBPath = path.resolve(cwd, kit.libDirectory, 'server', `db-${index}`);
forNextSteps = `You already have some database files. We'll create new files in___${path.relative(cwd, baseDBPath)}`;
paths.drizzleConfig = path.resolve(cwd, `drizzle-${index}.config.${ext}`);
paths.databaseSchema = path.resolve(baseDBPath, `schema.${ext}`);
paths.database = path.resolve(baseDBPath, `index.${ext}`);
index++;
}

sv.file(paths.drizzleConfig, (content) => {
const { ast, generateCode } = parseScript(content);

imports.addNamed(ast, { from: 'drizzle-kit', imports: { defineConfig: 'defineConfig' } });
Expand Down Expand Up @@ -234,7 +240,7 @@ export default defineAddon({
return generateCode();
});

sv.file(`${kit?.libDirectory}/server/db/schema.${ext}`, (content) => {
sv.file(paths.databaseSchema, (content) => {
const { ast, generateCode } = parseScript(content);

let userSchemaExpression;
Expand Down Expand Up @@ -286,7 +292,7 @@ export default defineAddon({
return generateCode();
});

sv.file(`${kit?.libDirectory}/server/db/index.${ext}`, (content) => {
sv.file(paths.database, (content) => {
const { ast, generateCode } = parseScript(content);

imports.addNamed(ast, {
Expand Down Expand Up @@ -417,9 +423,15 @@ export default defineAddon({
});
},
nextSteps: ({ options, highlighter, packageManager }) => {
const steps = [
const steps: string[] = [];
if (forNextSteps) {
const [p1, p2] = forNextSteps.split('___');
steps.push(`${p1} ${highlighter.path(p2)}`);
}
highlighter.route;
steps.push(
`You will need to set ${highlighter.env('DATABASE_URL')} in your production environment`
];
);
if (options.docker) {
const { command, args } = resolveCommand(packageManager, 'run', ['db:start'])!;
steps.push(
Expand Down
Loading