mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
MASSIVE commit
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
"use strict";
|
||||
import { readdirSync } from "fs";
|
||||
import { basename as _basename } from "path";
|
||||
const basename = _basename(import.meta.filename);
|
||||
|
||||
import Sequelize from "sequelize";
|
||||
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
import seqConfig from "../config/sequelize.js"
|
||||
const env = process.env.NODE_ENV || "development";
|
||||
const config = seqConfig[env];
|
||||
const db = {};
|
||||
|
||||
let sequelize;
|
||||
if (config.use_env_variable) {
|
||||
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
||||
} else {
|
||||
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
logger.debug("Sequelize auth success");
|
||||
} catch (error) {
|
||||
logger.error("Sequelize auth error", { error });
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
|
||||
// Read model definitions from folder
|
||||
const modelFiles = readdirSync(import.meta.dirname)
|
||||
.filter(
|
||||
(file) => file.indexOf('.') !== 0
|
||||
&& file !== basename
|
||||
&& file.slice(-3) === '.js',
|
||||
);
|
||||
|
||||
await Promise.all(modelFiles.map(async file => {
|
||||
const model = await import(`./${file}`);
|
||||
if (!model.default) {
|
||||
return;
|
||||
}
|
||||
|
||||
const namedModel = model.default(sequelize, Sequelize.DataTypes);
|
||||
db[namedModel.name] = namedModel;
|
||||
}))
|
||||
|
||||
// Setup defined associations
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
// clean ts up
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
export { sequelize as sequelize };
|
||||
export { Sequelize as Sequelize };
|
||||
export default db;
|
||||
35
models/index.ts
Normal file
35
models/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
import { Sequelize } from "sequelize-typescript";
|
||||
|
||||
import seqConfig from "../config/sequelize.ts";
|
||||
|
||||
import links from "./links.ts";
|
||||
import playlists from "./playlists.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
if (!process.env["NODE_ENV"])
|
||||
throw new TypeError("Node environment not defined");
|
||||
if (!process.env["DB_URI"])
|
||||
throw new TypeError("Database connection URI not defined");
|
||||
|
||||
// Initialize
|
||||
const config = seqConfig[process.env["NODE_ENV"]];
|
||||
const seqConn: Sequelize = new Sequelize(process.env["DB_URI"], config);
|
||||
|
||||
// Check connection
|
||||
(async () => {
|
||||
try {
|
||||
await seqConn.authenticate();
|
||||
logger.info("Sequelize auth success");
|
||||
} catch (error) {
|
||||
logger.error("Sequelize auth error", { error });
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
|
||||
// Load models
|
||||
seqConn.addModels([links, playlists]);
|
||||
|
||||
export default seqConn;
|
||||
@@ -1,23 +0,0 @@
|
||||
"use strict";
|
||||
import { Model } from "sequelize";
|
||||
export default (sequelize, DataTypes) => {
|
||||
class links extends Model {
|
||||
/**
|
||||
* Helper method for defining associations.
|
||||
* This method is not a part of Sequelize lifecycle.
|
||||
* The `models/index` file will call this method automatically.
|
||||
*/
|
||||
static associate(models) {
|
||||
// define association here
|
||||
}
|
||||
}
|
||||
links.init({
|
||||
userID: DataTypes.STRING,
|
||||
from: DataTypes.STRING,
|
||||
to: DataTypes.STRING
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: "links",
|
||||
});
|
||||
return links;
|
||||
};
|
||||
23
models/links.ts
Normal file
23
models/links.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
import {
|
||||
AllowNull,
|
||||
Column,
|
||||
DataType,
|
||||
Model,
|
||||
Table,
|
||||
} from "sequelize-typescript";
|
||||
@Table
|
||||
class links extends Model<Partial<links>> {
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING)
|
||||
declare userID: string;
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING)
|
||||
declare from: string;
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING)
|
||||
declare to: string;
|
||||
}
|
||||
export default links;
|
||||
@@ -1,23 +0,0 @@
|
||||
"use strict";
|
||||
import { Model } from "sequelize";
|
||||
export default (sequelize, DataTypes) => {
|
||||
class playlists extends Model {
|
||||
/**
|
||||
* Helper method for defining associations.
|
||||
* This method is not a part of Sequelize lifecycle.
|
||||
* The `models/index` file will call this method automatically.
|
||||
*/
|
||||
static associate(models) {
|
||||
// define association here
|
||||
}
|
||||
}
|
||||
playlists.init({
|
||||
playlistID: DataTypes.STRING,
|
||||
playlistName: DataTypes.STRING,
|
||||
userID: DataTypes.STRING
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: "playlists",
|
||||
});
|
||||
return playlists;
|
||||
};
|
||||
24
models/playlists.ts
Normal file
24
models/playlists.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
import {
|
||||
AllowNull,
|
||||
Column,
|
||||
DataType,
|
||||
Model,
|
||||
Table,
|
||||
} from "sequelize-typescript";
|
||||
@Table
|
||||
class playlists extends Model<Partial<playlists>> {
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING)
|
||||
declare playlistID: string;
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING)
|
||||
declare playlistName: string;
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING)
|
||||
declare userID: string;
|
||||
}
|
||||
|
||||
export default playlists;
|
||||
Reference in New Issue
Block a user