Skip to content
Open
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
35 changes: 31 additions & 4 deletions packages/adapter-sequelize/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,37 @@ export interface SequelizeAdapterOptions {
Account: ModelCtor<AccountInstance>
Session: ModelCtor<SessionInstance>
VerificationToken: ModelCtor<VerificationTokenInstance>
}>
}>;
/**
* Override the default model {@link https://sequelize.org/docs/v6/core-concepts/assocs/ associations}
*
* Default Associations:
* ```ts
* Account.belongsTo(User, { onDelete: "cascade" })
* Session.belongsTo(User, { onDelete: "cascade" })
* ```
*
* Example:
* ```ts
* (User, Account, Session) => {
* Account.belongsTo(User, { onDelete: "cascade", foreignKey: "userId", as: 'user' });
* Session.belongsTo(User, { onDelete: "cascade", foreignKey: "userId", as: 'user' });
* }
* ```
*/
associations?: (
User: ModelCtor<UserInstance>,
Account: ModelCtor<AccountInstance>,
Session: ModelCtor<SessionInstance>,
VerificationToken: ModelCtor<VerificationTokenInstance>
) => void;
}

export default function SequelizeAdapter(
client: Sequelize,
options?: SequelizeAdapterOptions
): Adapter {
const { models, synchronize = true } = options ?? {}
const { models, synchronize = true, associations } = options ?? {}
const defaultModelOptions = { underscored: true, timestamps: false }
const { User, Account, Session, VerificationToken } = {
User:
Expand Down Expand Up @@ -111,8 +134,12 @@ export default function SequelizeAdapter(
}
}

Account.belongsTo(User, { onDelete: "cascade" })
Session.belongsTo(User, { onDelete: "cascade" })
if (associations) {
associations(User, Account, Session, VerificationToken);
} else {
Account.belongsTo(User, { onDelete: 'cascade' });
Session.belongsTo(User, { onDelete: 'cascade' });
}

return {
async createUser(user) {
Expand Down