Created
April 16, 2021 08:05
-
-
Save Sven65/fc4985b2c26a095d6255c1e9f2d3029d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Sequelize } from 'sequelize-typescript' | |
import DataTable from './models/DataTable.model' | |
const db = new Sequelize({ | |
database: process.env["POSTGRES_DB_NAME"] as string, | |
username: process.env["POSTGRES_USERNAME"] as string, | |
password: process.env["POSTGRES_PASSWORD"] as string, | |
host: process.env["POSTGRES_HOST"] as string, | |
port: parseInt(process.env['POSTGRES_PORT'] as string, 10), | |
dialect: 'postgres', | |
logging: false, //If every single query should be logged to the console | |
models: [ DataTable ], | |
}) | |
export default db |
This file contains hidden or 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
import {Table, Column, Model, PrimaryKey, Sequelize } from 'sequelize-typescript' | |
import { Op } from 'sequelize' | |
@Table({ | |
tableName: 'DataTable', | |
}) | |
export class DataTable extends Model { | |
@PrimaryKey | |
@Column | |
timestamp!: Date | |
@PrimaryKey | |
@Column | |
id!: string | |
static async getLatestData(): Promise<Data[] | null> { | |
const attribs = await this.findAll({ | |
attributes: [ | |
[ Sequelize.fn("max", Sequelize.col('timestamp')), 'timestamp' ], | |
'id', | |
], | |
group: ['id'], | |
raw: true, | |
}) | |
return await this.findAll({ | |
where: { | |
id: { | |
[Op.in]: attribs.map(attrib => attrib.id), | |
}, | |
timestamp: { // This fails | |
[Op.in]: attribs.map(attrib => attrib.timestamp), | |
}, | |
}, | |
}) | |
} | |
} | |
export default DataTable |
This file contains hidden or 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
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "ES2016", | |
"module": "CommonJS", | |
"allowJs": true, | |
"checkJs": true, | |
"strict": true, | |
"esModuleInterop": true, | |
"skipLibCheck": true, | |
"forceConsistentCasingInFileNames": true, | |
"outDir": "./dist", | |
"declaration": true, | |
"moduleResolution": "node", | |
"experimentalDecorators": true, | |
"emitDecoratorMetadata": true | |
}, | |
"include": [ | |
"src" | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment