Skip to content

Commit be16732

Browse files
committed
feat: add support for sqlite
1 parent f2acebc commit be16732

19 files changed

Lines changed: 187 additions & 89 deletions

File tree

.env

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
# Default settings, override in .env.local file that is auto-generated on install
2+
3+
4+
# Database
5+
### sqlite (default)
6+
DATABASE_TYPE=sqlite3
7+
# sqlite file path, absolute or relative path
8+
DATABASE_URL=./mydb.sqlite
9+
### postgres
10+
211
# NOTE: The default database is provided to get you up and running but is not secure!
12+
# DATABASE_TYPE=pg
313
# DATABASE_URL=postgres://postgres:ZTE*meq1mzh3abn!cmk@db.xbolptvmhqplniydcjcz.supabase.co:5432/postgres
4-
DATABASE_URL=postgres://postgres:P8Pwj96n38ilb5a2@db.ageqtrdagwgucljhwtab.supabase.co:5432/postgres
14+
# DATABASE_URL=postgres://postgres:P8Pwj96n38ilb5a2@db.ageqtrdagwgucljhwtab.supabase.co:5432/postgres
15+
16+
# Plugins
517
CLIENT_PLUGINS=discord,rest,loop,finetune,ethereum
618
SERVER_PLUGINS=discord,rest,loop,finetune,ethereum
719

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ dev.db
4646
package-lock.json
4747
yarn.lock
4848

49-
./files/
49+
./files/
50+
51+
*.sqlite

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,17 @@ There are a few things to keep in mind while this project is actively being work
4949

5050
## Database
5151

52+
### Local Sqlite (default)
53+
54+
Simply add a relative or absolute path to your sqlite file in the `.env`, run `npm run migrate` and you're all set.
55+
56+
### Supabase public database
57+
5258
_Please be aware Magick is under heavy development and changes can cause your DB to be wiped. Back up your spells via export regularly._
5359

54-
The default setup of Magick is connected to a test database on Supabase. It will get you going, but it will likely be wiped regularly and is not a good storage for your spells.
60+
Magick can connect to a test database on Supabase. It will get you going, but it will likely be wiped regularly and is not a good storage for your spells.
61+
62+
just enable the pg database in the `.env` file.
5563

5664
### Deploy your own database
5765

apps/server/knexfile.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
const config = {
2-
client: 'pg',
3-
connection: process.env.DATABASE_URL
1+
const dbType = process.env.DATABASE_TYPE
2+
const dbURL = process.env.DATABASE_URL
3+
const configs = {
4+
pg: {
5+
client: dbType,
6+
connection: dbURL
7+
},
8+
sqlite3: {
9+
client: dbType,
10+
connection: {
11+
// handling both absolute and relative paths,
12+
// for relative path resolve back to the main directory,
13+
// where .env file exists
14+
filename: dbURL.startsWith('/') ? dbURL : `../../${dbURL}`
15+
},
16+
useNullAsDefault: true
17+
}
418
}
519

6-
module.exports = config
20+
module.exports = configs[dbType]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
"socket.io": "^4.5.4",
140140
"socket.io-client": "^4.5.4",
141141
"solc": "^0.8.18",
142+
"sqlite3": "^5.1.4",
142143
"stream-browserify": "^3.0.0",
143144
"styled-components": "^5.3.6",
144145
"swagger-ui-dist": "^4.15.5",

packages/server-core/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import socketio from '@feathersjs/socketio'
55

66
import type { Application } from './declarations'
77
import { logError } from './hooks'
8-
import { postgresql } from './postgresql'
8+
import { dbClient } from './dbClient'
99
import { services } from './services'
1010
import channels from './channels'
1111
// import swagger from 'feathers-swagger'
@@ -68,7 +68,7 @@ app.configure(
6868
handleSockets(app)
6969
)
7070
)
71-
app.configure(postgresql)
71+
app.configure(dbClient)
7272
app.configure(services)
7373
app.configure(channels)
7474

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// For more information about this file see https://dove.feathersjs.com/guides/cli/databases.html
2+
import knex from 'knex'
3+
import type { Knex } from 'knex'
4+
import type { Application, SupportedDbs } from './declarations'
5+
6+
declare module './declarations' {
7+
interface Configuration {
8+
dbClient: Knex
9+
}
10+
11+
type SupportedDbs = 'pg' | 'sqlite3'
12+
}
13+
const getDatabaseConfig = () => {
14+
const dbType = process.env.DATABASE_TYPE || ''
15+
const dbURL = process.env.DATABASE_URL
16+
17+
if(!dbURL) throw new Error("Missing DATABASE_URL in your .env file.")
18+
19+
// postgres config
20+
if (dbType === 'pg')
21+
return {
22+
client: dbType,
23+
connection: dbURL
24+
}
25+
// sqlite config
26+
if (dbType === 'sqlite3')
27+
return {
28+
client: dbType,
29+
connection: {
30+
filename: dbURL
31+
},
32+
// sqlite does not support inserting default values
33+
useNullAsDefault: true,
34+
pool: {
35+
// afterCreate: function (conn, done) {
36+
// // in this example we use pg driver's connection API
37+
// conn.query('SET timezone="UTC";', function (err) {
38+
// if (err) {
39+
// // first query failed,
40+
// // return error and don't try to make next query
41+
// done(err, conn);
42+
// } else {
43+
// // do the second query...
44+
// conn.query(
45+
// 'SELECT set_limit(0.01);',
46+
// function (err) {
47+
// // if err is not falsy,
48+
// // connection is discarded from pool
49+
// // if connection aquire was triggered by a
50+
// // query the error is passed to query promise
51+
// done(err, conn);
52+
// });
53+
// }
54+
// });
55+
// }
56+
}
57+
}
58+
59+
throw new Error("Unsupported database type, use `pg` or `sqlite3`")
60+
}
61+
62+
export const dbClient = (app: Application) => {
63+
const config = getDatabaseConfig()
64+
const db = knex(config)
65+
app.set('dbClient', db)
66+
}
67+
68+
const dbSupportJson: Record<SupportedDbs, boolean> = {
69+
'pg': true,
70+
'sqlite3': false
71+
}
72+
73+
export const doesDbSupportJson = () :boolean => dbSupportJson[process.env.DATABASE_TYPE]

packages/server-core/src/postgresql.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/server-core/src/services/agents/agents.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class AgentService<ServiceParams extends Params = AgentParams> extends Kn
1919
export const getOptions = (app: Application): KnexAdapterOptions => {
2020
return {
2121
paginate: app.get('paginate'),
22-
Model: app.get('postgresqlClient'),
22+
Model: app.get('dbClient'),
2323
name: 'agents'
2424
}
2525
}

packages/server-core/src/services/agents/agents.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ export const agentQuerySchema = Type.Intersect(
6767
export type AgentQuery = Static<typeof agentQuerySchema>
6868
export const agentQueryValidator = getValidator(agentQuerySchema, queryValidator)
6969
export const agentQueryResolver = resolve<AgentQuery, HookContext>({})
70+
export const agentJsonFields = ['spells', 'data']

0 commit comments

Comments
 (0)