Skip to content

Instantly share code, notes, and snippets.

@Sven65
Created April 16, 2021 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sven65/fc4985b2c26a095d6255c1e9f2d3029d to your computer and use it in GitHub Desktop.
Save Sven65/fc4985b2c26a095d6255c1e9f2d3029d to your computer and use it in GitHub Desktop.
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
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
{
"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