Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite fallback #159

Merged
merged 5 commits into from
Jun 9, 2024
Merged
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
15 changes: 15 additions & 0 deletions .changeset/mean-cows-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@oberoncms/plugin-vercel-postgres": patch
"create-oberon-app": patch
"@oberoncms/plugin-flydrive": patch
"@oberoncms/core": patch
"@oberoncms/plugin-sqlite": patch
"@oberoncms/plugin-pgsql": patch
"@oberoncms/plugin-turso": patch
"@oberon/docs": patch
"@oberon/playground": patch
"@oberon/nextjs-recipe": patch
---

Move development fallback to wrappers
Move local sqlite to separate plugin
10 changes: 0 additions & 10 deletions apps/documentation/src/pages/docs/plugins/database/turso.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,3 @@ To use Turso, you need to create a [Turso](https://https://turso.tech/) account
TURSO_URL=<libsql://your-database-name.turso.io>
TURSO_TOKEN=<your-api-key>
```

### Local

If you are self hosting, you can use a local sqlite database:

```sh
TURSO_USE_REMOTE=false
TURSO_FILE=<"file:filename.db"> # optional, defaults to "file:.oberon/oberon.db"
```

12 changes: 7 additions & 5 deletions apps/playground/oberon/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import "server-cli-only"

import { plugin as databasePlugin } from "@oberoncms/plugin-turso"
import { authPlugin } from "@oberoncms/core/auth"

import { initAdapter } from "@oberoncms/core/adapter"
import { authPlugin, withDevelopmentSend } from "@oberoncms/core/auth"
import { withDevelopmentDatabase } from "@oberoncms/plugin-sqlite"

import { plugin as tursoPlugin } from "@oberoncms/plugin-turso"
import { plugin as uploadthingPlugin } from "@oberoncms/plugin-uploadthing/plugin"

import { sendAdapterPlugin } from "./send"

export const adapter = initAdapter([
databasePlugin,
withDevelopmentDatabase(tursoPlugin),
withDevelopmentSend(sendAdapterPlugin),
authPlugin,
sendAdapterPlugin,
uploadthingPlugin,
])
3 changes: 0 additions & 3 deletions apps/playground/oberon/prebuild.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/usr/bin/env node
/* eslint-env node */
import dotenv from "dotenv"
dotenv.config({ path: ".env.local" })

import { exportTailwindClasses } from "@oberoncms/core/adapter"
import { adapter } from "./adapter"
Expand Down
1 change: 1 addition & 0 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@datacom-digital/ui-sample-components": "^0.3.2",
"@oberoncms/core": "workspace:*",
"@oberoncms/plugin-turso": "workspace:*",
"@oberoncms/plugin-sqlite": "workspace:*",
"@oberoncms/plugin-uploadthing": "workspace:*",
"next": "^14.2.3",
"react": "^18.2.0",
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@
"lint-staged": {
"**/*.{ts,tsx,js,jsx,css,md,json}": "prettier -l --write"
},
"dependencies": {
"dotenv": "^16.4.5"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@tohuhono/dev": "workspace:*",
"concurrently": "^8.2.2",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"eslint": "8.57.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
Expand Down
6 changes: 6 additions & 0 deletions packages/create-oberon-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@oberoncms/plugin-flydrive": "workspace:*",
"@oberoncms/plugin-pgsql": "workspace:*",
"@oberoncms/plugin-sqlite": "workspace:*",
"@oberoncms/plugin-turso": "workspace:*",
"@oberoncms/plugin-uploadthing": "workspace:*",
"@oberoncms/plugin-vercel-postgres": "workspace:*",
"@oberoncms/core": "workspace:*",
"@sendgrid/mail": "8.1.3",
"@tohuhono/utils": "workspace:*",
Expand Down
10 changes: 5 additions & 5 deletions packages/create-oberon-app/plugins/database/custom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
OberonAuthAdapter,
OberonDatabaseAdapter,
OberonPlugin,
import {
type OberonAuthAdapter,
type OberonDatabaseAdapter,
type OberonPlugin,
} from "@oberoncms/core"
import { notImplemented } from "@tohuhono/utils"

export const plugin: OberonPlugin = () => ({
export const databasePlugin: OberonPlugin = () => ({
name: "Custom Database Plugin",
adapter: {
// OberonDatabaseAdapter
Expand Down
50 changes: 15 additions & 35 deletions packages/create-oberon-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ import {
Option,
InvalidArgumentError,
} from "@commander-js/extra-typings"
import { initGit } from "./program/init-git"
import { installPackages } from "./program/install-packages"
import { initGit } from "./installer/init-git"
import { installPackages, packageManagers } from "./installer/install-packages"

import { promptOptions, recipes } from "./installer/prompt-options"
import { installTemplate } from "./installer/install-template"
import { installEnv } from "./installer/install-env"
import {
databaseIds,
databasePlugins,
packageManagers,
Plugin,
recipes,
installAdapter,
sendIds,
sendPlugins,
} from "./program/config"
import { promptOptions } from "./program/prompt-options"
import { copyTemplate } from "./program/copy-template"
import { writeEnv } from "./program/write-env"
import { updatePackageJson } from "./program/update-package"
import { writePlugins } from "./program/write-plugins"
Plugin,
} from "./installer/install-adapter"

program
.command("create")
Expand Down Expand Up @@ -78,35 +76,17 @@ program
},
]

/*
* Copy and apply template files into new directory
*/
await copyTemplate(appName, appPath, templatePath)

await writePlugins(oberonPath, pluginPath, plugins)

/*
* Adjust package.json
*/
const { workspaceDeps, workspaceDevDeps } = await updatePackageJson(
appName,
appPath,
)
await installTemplate(appName, appPath, templatePath)

await writeEnv(appPath, email)
await installAdapter(oberonPath, pluginPath, plugins)

const pluginDependencies = plugins.flatMap(
({ packageName, dependencies = [] }) => [
...dependencies,
...(packageName ? [packageName] : []),
],
)
await installEnv(appPath, email)

await installPackages({
packageManager,
appName,
appPath,
dependencies: [...workspaceDeps, ...pluginDependencies],
devDependencies: workspaceDevDeps,
packageManager,
plugins,
})

execSync(`${packageManager} run prettier:fix`, {
Expand Down
100 changes: 100 additions & 0 deletions packages/create-oberon-app/src/installer/install-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { copyFile, writeFile } from "fs/promises"
import path from "path"

export type Plugin = {
id: string
type: string
label: string
packageName?: string
dependencies?: string[]
}

export const databasePlugins = {
turso: {
label: "Turso (Recommended)",
packageName: "@oberoncms/plugin-turso",
},
pgsql: {
label: "PostgreSQL",
packageName: "@oberoncms/plugin-pgsql",
},
"vercel-postgres": {
label: "Vercel Postgres",
packageName: "@oberoncms/plugin-vercel-postgres",
},
custom: {
label: "Custom",
dependencies: ["@tohuhono/utils"],
},
}
export type DatabasePlugin = keyof typeof databasePlugins
export const databaseIds = Object.keys(databasePlugins) as DatabasePlugin[]

export const sendPlugins = {
resend: { label: "Resend (Recommended)", dependencies: ["resend"] },
sendgrid: { label: "Sendgrid", dependencies: ["@sendgrid/mail"] },
custom: { label: "Custom", dependencies: ["@tohuhono/utils"] },
}
export type SendPlugin = keyof typeof sendPlugins
export const sendIds = Object.keys(sendPlugins) as SendPlugin[]

const createAdapter = (plugins: Plugin[]) => {
const aliasedPlugins = plugins.map(({ packageName, type }) => ({
packageName,
type,
alias: `${type}Plugin`,
}))

const pluginImports = aliasedPlugins
.map(({ packageName, type, alias }) => {
if (packageName) {
return `import { plugin as ${alias} } from "${packageName}"`
}

return `import { plugin as ${alias} } from "./${type}"`
})
.join("\n")

const adapterPlugins = [
"databasePlugin",
...aliasedPlugins.map(({ alias }) => alias),
// Auth plugin should be last
"authPlugin",
]

return `
import "server-cli-only"

import { USE_DEVELOPMENT_DATABASE } from "@oberoncms/core"
import { initAdapter } from "@oberoncms/core/adapter"
import { authPlugin } from "@oberoncms/core/auth"

import { plugin as sqlitePlugin } from "@oberoncms/plugin-sqlite"

${pluginImports}

const databasePlugin = USE_DEVELOPMENT_DATABASE ? sqlitePlugin : databasePlugin

export const adapter = initAdapter([${adapterPlugins.join(", ")}])
`
}

export async function installAdapter(
oberonPath: string,
pluginPath: string,
plugins: Plugin[],
) {
for (const { packageName, type, id } of plugins) {
if (packageName) {
continue
}
await copyFile(
path.join(pluginPath, type, `${id}.ts`),
path.join(oberonPath, `${type}.ts`),
)
}

const adapter = createAdapter(plugins)

await writeFile(path.join(oberonPath, "adapter.ts"), adapter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { writeFile } from "fs/promises"
import { randomBytes } from "crypto"
import path from "path"

export async function writeEnv(appPath: string, email: string) {
export async function installEnv(appPath: string, email: string) {
await writeFile(
path.join(appPath, "./.env.local"),
path.join(appPath, "./.env"),
`
MASTER_EMAIL=${email}
EMAIL_FROM=${email}

AUTH_SECRET=${randomBytes(64).toString("hex")}

# Development builds
RESEND_USE_REMOTE=false

AUTH_TRUST_HOST=true
ANALYZE=false
`,
Expand Down
Loading