Add SQLite support for easy setup (#336)
* Modifies schema to allow SQLite as a DB driver. SQLite is preinstalled in most OSes allowing for minimal prerequisites in the installation process. * Removes Redis dependency and replaces "real" redis instance with ioredis-mock this is not feature complete and errors are expected. * Updates medusa new command to only ask for Postgres credentials if the starter template has database_type === "postgres" in medusa-config.js * Small improvements to bin resolution * Improvements to endpoint stability
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import path from "path"
|
||||
import { Column, ColumnOptions, ColumnType } from "typeorm"
|
||||
import { getConfigFile } from "medusa-core-utils"
|
||||
|
||||
const pgSqliteTypeMapping: { [key: string]: ColumnType } = {
|
||||
increment: "rowid",
|
||||
timestamptz: "datetime",
|
||||
jsonb: "simple-json",
|
||||
enum: "text",
|
||||
}
|
||||
|
||||
const pgSqliteGenerationMapping: {
|
||||
[key: string]: "increment" | "uuid" | "rowid"
|
||||
} = {
|
||||
increment: "rowid",
|
||||
}
|
||||
|
||||
let dbType: string
|
||||
export function resolveDbType(pgSqlType: ColumnType): ColumnType {
|
||||
if (!dbType) {
|
||||
const { configModule } = getConfigFile(path.resolve("."), `medusa-config`)
|
||||
dbType = configModule.projectConfig.database_type
|
||||
}
|
||||
|
||||
if (dbType === "sqlite" && pgSqlType in pgSqliteTypeMapping) {
|
||||
return pgSqliteTypeMapping[pgSqlType.toString()]
|
||||
}
|
||||
return pgSqlType
|
||||
}
|
||||
|
||||
export function resolveDbGenerationStrategy(
|
||||
pgSqlType: "increment" | "uuid" | "rowid"
|
||||
): "increment" | "uuid" | "rowid" {
|
||||
if (!dbType) {
|
||||
const { configModule } = getConfigFile(path.resolve("."), `medusa-config`)
|
||||
dbType = configModule.projectConfig.database_type
|
||||
}
|
||||
|
||||
if (dbType === "sqlite" && pgSqlType in pgSqliteTypeMapping) {
|
||||
return pgSqliteGenerationMapping[pgSqlType]
|
||||
}
|
||||
return pgSqlType
|
||||
}
|
||||
|
||||
export function DbAwareColumn(columnOptions: ColumnOptions) {
|
||||
const pre = columnOptions.type
|
||||
if (columnOptions.type) {
|
||||
columnOptions.type = resolveDbType(columnOptions.type)
|
||||
}
|
||||
|
||||
if (pre === "jsonb" && pre !== columnOptions.type) {
|
||||
if ("default" in columnOptions) {
|
||||
columnOptions.default = JSON.stringify(columnOptions.default)
|
||||
}
|
||||
}
|
||||
|
||||
return Column(columnOptions)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import path from "path"
|
||||
import { getConnection } from "typeorm"
|
||||
import { getConfigFile } from "medusa-core-utils"
|
||||
|
||||
export async function manualAutoIncrement(
|
||||
tableName: string
|
||||
): Promise<number | null> {
|
||||
const { configModule } = getConfigFile(path.resolve("."), `medusa-config`)
|
||||
const dbType = configModule.projectConfig.database_type
|
||||
if (dbType === "sqlite") {
|
||||
const connection = getConnection()
|
||||
const [rec] = await connection.query(
|
||||
`SELECT MAX(rowid) as mr FROM "${tableName}"`
|
||||
)
|
||||
|
||||
let mr = 0
|
||||
if (rec && rec.mr) {
|
||||
mr = rec.mr
|
||||
}
|
||||
return mr + 1
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user