Created
September 15, 2023 23:47
-
-
Save mihairaulea/d4e8f3061aa7bc3538c73e5af7686ef3 to your computer and use it in GitHub Desktop.
seq.js
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
const { Sequelize, DataTypes } = require('sequelize'); | |
const sequelize = new Sequelize('postgres://postgres:HfQFIgMH-15H3-VfhI5TFA@postgresql-kauog-u4087.vm.elestio.app:34523/postgres'); | |
// get searches while there are still processed: false | |
const Search = sequelize.define('Search', { | |
searchURL: { | |
type:DataTypes.STRING, | |
processed: DataTypes.BOOLEAN, | |
allowNull:false | |
} | |
}); | |
// MODELS | |
const User = sequelize.define('User', { | |
username: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
unique: true | |
}, | |
name: { | |
type: DataTypes.STRING, | |
allowNull: false | |
}, | |
email: { | |
type: DataTypes.STRING | |
}, | |
starredRepos: { | |
type: DataTypes.ARRAY(DataTypes.STRING), | |
allowNull: true | |
}, | |
contributedRepos: { | |
type: DataTypes.ARRAY(DataTypes.STRING), | |
allowNull: true | |
}, | |
contributedReposMain: { | |
type: DataTypes.ARRAY(DataTypes.STRING), | |
allowNull: true | |
}, | |
userProcessed: { | |
type: DataTypes.BOOLEAN, | |
allowNull: false | |
} | |
}, { | |
// Other model options go here | |
}); | |
const Repo = sequelize.define('Repo', { | |
// example: mihairaulea/atlas | |
uniqueName: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
unique: true | |
}, | |
description: { | |
type: DataTypes.STRING, | |
allowNull: false | |
}, | |
tags: { | |
type: DataTypes.ARRAY(DataTypes.STRING) | |
}, | |
noOfStars: { | |
type: DataTypes.BIGINT | |
}, | |
usersStarredProcessed: { | |
type: DataTypes.BOOLEAN | |
}, | |
usersContributedProcessed: { | |
type: DataTypes.BOOLEAN | |
} | |
}); | |
// this will be initied once | |
async function insertSearches() { | |
} | |
async function insertUser(userData) { | |
const jane = User.build(userData); | |
await jane.save(); | |
} | |
async function insertRepo(repoData) { | |
const repo = Repo.build(repoData) | |
repo.save(); | |
} | |
// turn this into a parameter, to change it to another time interval(ex: one week, 3 months, etc) | |
// change = a user is unprocessed IF it was crawled one month ago | |
async function getOneUserUnprocessed() { | |
return await User.findOne({ where: { userProcessed: false } }); | |
} | |
async function getOneRepoStarsUnprocessed() { | |
return await Repo.findOne({ where: { usersStarredProcessed: false } }); | |
} | |
async function getOneRepoContributorsUnprocessed() { | |
return await Repo.findOne({ where: { usersContributedProcessed: false } }); | |
} | |
async function initDatabase() { | |
await sequelize.authenticate(); | |
console.log('Connection has been established successfully.'); | |
await User.sync(); | |
await Repo.sync(); | |
await Search.sync(); | |
} | |
// Option 1: Passing a connection URL | |
(async () => { | |
console.log('start'); | |
try { | |
await initDatabase(); | |
//await insertUser({ username: "janedow", name: "Jane", userProcessed: false }); | |
//await insertRepo({uniqueName: 'mihairaulea/atlas', description:'ia uite descriere', tags:['osm', 'atlas'], noOfStars: 38, usersStarredProcessed:false, usersContributedProcessed: false}); | |
let user = await getOneUserUnprocessed(); | |
let repo = await getOneRepoStarsUnprocessed(); | |
let repo2 = await getOneRepoContributorsUnprocessed(); | |
} catch (error) { | |
console.error('Unable to connect to the database:', error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment