Skip to content
Snippets Groups Projects
Commit d29e91b8 authored by VILLAUME Lucas's avatar VILLAUME Lucas
Browse files

Interactions avec la db

parent 524d5ea1
No related branches found
No related tags found
No related merge requests found
...@@ -6,24 +6,50 @@ class Database{ ...@@ -6,24 +6,50 @@ class Database{
constructor(url){ constructor(url){
this.#client = new MongoClient(url); this.#client = new MongoClient(url);
this.connect();
} }
async connect(){ async connect(){
try { try {
await this.#client.connect(); await this.#client.connect();
await this.listDatabases()
} catch (e) { } catch (e) {
console.error(e); console.error(e);
await this.#client.close(); await this.#client.close();
} }
} }
async listDatabases(){ async close(){
let databasesList = await this.#client.db().admin().listDatabases(); await this.#client.close();
}
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` - ${db.name}`)); async listCollections(){
return await this.#db.collections();
}; };
async init(nomDB){
this.#db = this.#client.db(nomDB);
await this.#db.createCollection('users'); // table des utilisateurs
await this.#db.createCollection('documents'); // table des documents
await this.#db.createCollection('partage'); // table qui associe les utilisateurs aux documents
console.log("Collections créées !");
}
async insert(collection, data){
await this.#db.collection(collection).insertOne(data);
}
async insertMany(collection, data){
await this.#db.collection(collection).insertMany(data);
}
async find(collection, query){
return await this.#db.collection(collection).find(query).toArray();
}
} }
module.exports = Database; db = new Database("mongodb://127.0.0.1:27017/test?retryWrites=true&w=majority");
\ No newline at end of file db.init("collab");
module.exports = db;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment