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:
@@ -7,6 +7,7 @@ const { createConnection } = require("typeorm");
|
||||
const DB_USERNAME = process.env.DB_USERNAME || "postgres";
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD || "";
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration`;
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
@@ -21,6 +22,8 @@ const keepTables = [
|
||||
"currency",
|
||||
];
|
||||
|
||||
let connectionType = "postgresql";
|
||||
|
||||
const DbTestUtil = {
|
||||
db_: null,
|
||||
|
||||
@@ -36,7 +39,12 @@ const DbTestUtil = {
|
||||
const entities = this.db_.entityMetadatas;
|
||||
const manager = this.db_.manager;
|
||||
|
||||
await manager.query(`SET session_replication_role = 'replica';`);
|
||||
if (connectionType === "sqlite") {
|
||||
await manager.query(`PRAGMA foreign_keys = OFF`);
|
||||
} else {
|
||||
await manager.query(`SET session_replication_role = 'replica';`);
|
||||
}
|
||||
|
||||
for (const entity of entities) {
|
||||
if (keepTables.includes(entity.tableName)) {
|
||||
continue;
|
||||
@@ -44,7 +52,11 @@ const DbTestUtil = {
|
||||
|
||||
await manager.query(`DELETE FROM "${entity.tableName}";`);
|
||||
}
|
||||
await manager.query(`SET session_replication_role = 'origin';`);
|
||||
if (connectionType === "sqlite") {
|
||||
await manager.query(`PRAGMA foreign_keys = ON`);
|
||||
} else {
|
||||
await manager.query(`SET session_replication_role = 'origin';`);
|
||||
}
|
||||
},
|
||||
|
||||
shutdown: async function () {
|
||||
@@ -58,28 +70,7 @@ const instance = DbTestUtil;
|
||||
|
||||
module.exports = {
|
||||
initDb: async function ({ cwd }) {
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
cwd,
|
||||
`node_modules`,
|
||||
`@medusajs`,
|
||||
`medusa`,
|
||||
`dist`,
|
||||
`migrations`
|
||||
)
|
||||
);
|
||||
|
||||
const databaseName = "medusa-integration";
|
||||
await createDatabase({ databaseName }, pgGodCredentials);
|
||||
|
||||
const connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
migrations: [`${migrationDir}/*.js`],
|
||||
});
|
||||
|
||||
await connection.runMigrations();
|
||||
await connection.close();
|
||||
const configPath = path.resolve(path.join(cwd, `medusa-config.js`));
|
||||
|
||||
const modelsLoader = require(path.join(
|
||||
cwd,
|
||||
@@ -90,16 +81,53 @@ module.exports = {
|
||||
`loaders`,
|
||||
`models`
|
||||
)).default;
|
||||
|
||||
const entities = modelsLoader({}, { register: false });
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities,
|
||||
});
|
||||
|
||||
instance.setDb(dbConnection);
|
||||
return dbConnection;
|
||||
const { projectConfig } = require(configPath);
|
||||
if (projectConfig.database_type === "sqlite") {
|
||||
connectionType = "sqlite";
|
||||
const dbConnection = await createConnection({
|
||||
type: "sqlite",
|
||||
database: projectConfig.database_database,
|
||||
synchronize: true,
|
||||
entities,
|
||||
});
|
||||
|
||||
instance.setDb(dbConnection);
|
||||
return dbConnection;
|
||||
} else {
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
cwd,
|
||||
`node_modules`,
|
||||
`@medusajs`,
|
||||
`medusa`,
|
||||
`dist`,
|
||||
`migrations`
|
||||
)
|
||||
);
|
||||
|
||||
const databaseName = "medusa-integration";
|
||||
await createDatabase({ databaseName }, pgGodCredentials);
|
||||
|
||||
const connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
migrations: [`${migrationDir}/*.js`],
|
||||
});
|
||||
|
||||
await connection.runMigrations();
|
||||
await connection.close();
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities,
|
||||
});
|
||||
|
||||
instance.setDb(dbConnection);
|
||||
return dbConnection;
|
||||
}
|
||||
},
|
||||
useDb: function () {
|
||||
return instance;
|
||||
|
||||
Reference in New Issue
Block a user