-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests pass if you comment out sequelize init
- Loading branch information
1 parent
fd78273
commit 2655a80
Showing
20 changed files
with
1,324 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Sequelize } from "@sequelize/core"; | ||
import { PostgresDialect } from "@sequelize/postgres"; | ||
import { InternalServerError } from "../errors/index.js"; | ||
import { ShortLinkModel } from "../models/linkry.model.js"; | ||
import { FastifyInstance } from "fastify"; | ||
|
||
let logDebug: CallableFunction = console.log; | ||
let logFatal: CallableFunction = console.log; | ||
|
||
// Function to set the current logger for each invocation | ||
export function setSequelizeLogger( | ||
debugLogger: CallableFunction, | ||
fatalLogger: CallableFunction, | ||
) { | ||
logDebug = (msg: string) => debugLogger(msg); | ||
logFatal = (msg: string) => fatalLogger(msg); | ||
} | ||
|
||
export async function getSequelizeInstance( | ||
fastify: FastifyInstance, | ||
): Promise<Sequelize> { | ||
const postgresUrl = | ||
process.env.DATABASE_URL || fastify.secretValue?.postgres_url || ""; | ||
|
||
const sequelize = new Sequelize({ | ||
dialect: PostgresDialect, | ||
url: postgresUrl as string, | ||
ssl: { | ||
rejectUnauthorized: false, | ||
}, | ||
models: [ShortLinkModel], | ||
logging: logDebug as (sql: string, timing?: number) => void, | ||
pool: { | ||
max: 2, | ||
min: 0, | ||
idle: 0, | ||
acquire: 3000, | ||
evict: 30, // lambda function timeout in seconds | ||
}, | ||
}); | ||
try { | ||
await sequelize.sync(); | ||
} catch (e: unknown) { | ||
logFatal(`Could not authenticate to DB! ${e}`); | ||
throw new InternalServerError({ | ||
message: "Could not establish database connection.", | ||
}); | ||
} | ||
return sequelize; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
InferCreationAttributes, | ||
InferAttributes, | ||
Model, | ||
CreationOptional, | ||
DataTypes, | ||
} from "@sequelize/core"; | ||
import { | ||
AllowNull, | ||
Attribute, | ||
CreatedAt, | ||
NotNull, | ||
PrimaryKey, | ||
Table, | ||
UpdatedAt, | ||
} from "@sequelize/core/decorators-legacy"; | ||
|
||
@Table({ timestamps: true, tableName: "short_links" }) | ||
export class ShortLinkModel extends Model< | ||
InferAttributes<ShortLinkModel>, | ||
InferCreationAttributes<ShortLinkModel> | ||
> { | ||
@Attribute(DataTypes.STRING) | ||
@PrimaryKey | ||
declare slug: string; | ||
|
||
@Attribute(DataTypes.STRING) | ||
@NotNull | ||
declare full: string; | ||
|
||
@Attribute(DataTypes.ARRAY(DataTypes.STRING)) | ||
@AllowNull | ||
declare groups?: CreationOptional<string[]>; | ||
|
||
@Attribute(DataTypes.STRING) | ||
declare author: string; | ||
|
||
@Attribute(DataTypes.DATE) | ||
@CreatedAt | ||
declare createdAt: CreationOptional<Date>; | ||
|
||
@Attribute(DataTypes.DATE) | ||
@UpdatedAt | ||
declare updatedAt: CreationOptional<Date>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.