mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
93 lines
2.0 KiB
JavaScript
93 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
let MongoSocket = think.adapter('socket', 'mongo');
|
|
/**
|
|
* mongo db class
|
|
*/
|
|
export default class {
|
|
/**
|
|
* constructor
|
|
* @param {Array} args []
|
|
* @return {} []
|
|
*/
|
|
constructor(...args){
|
|
this.init(...args);
|
|
}
|
|
/**
|
|
* init
|
|
* @param {Object} config []
|
|
* @return {} []
|
|
*/
|
|
init(config){
|
|
this.config = config;
|
|
this.lastInsertId = 0;
|
|
this._socket = null; //Mongo socket instance
|
|
}
|
|
/**
|
|
* connect mongo socket
|
|
* @return {Promise} []
|
|
*/
|
|
socket(){
|
|
if(this._socket){
|
|
return this._socket;
|
|
}
|
|
this._socket = new MongoSocket(this.config);
|
|
return this._socket;
|
|
}
|
|
/**
|
|
* get connection
|
|
* @return {Promise} []
|
|
*/
|
|
collection(table){
|
|
let instance = this.socket();
|
|
return instance.getConnection().then(db => db.collection(table));
|
|
}
|
|
/**
|
|
* get last insert id
|
|
* @return {String} []
|
|
*/
|
|
getLastInsertId(){
|
|
return this.lastInsertId;
|
|
}
|
|
/**
|
|
* add data
|
|
* @param {Objec} data []
|
|
* @param {Object} options []
|
|
*/
|
|
async add(data, options){
|
|
let collection = await this.collection(options.table);
|
|
let result = await collection.insertOne(data, options);
|
|
this.lastInsertId = result.insertedId;
|
|
return result;
|
|
}
|
|
/**
|
|
* add multi data
|
|
* @param {Array} dataList []
|
|
* @param {Object} options []
|
|
*/
|
|
async addMany(dataList, options){
|
|
let collection = await this.collection(options.table);
|
|
let result = await collection.insertMany(data, options);
|
|
this.lastInsertId = result.insertedIds;
|
|
return result;
|
|
}
|
|
/**
|
|
* select data
|
|
* @param {Object} options []
|
|
* @return {Promise} []
|
|
*/
|
|
async select(options){
|
|
let collection = await this.collection(options.table);
|
|
return collection.find({name: "welefen"}, {name: false}).limit(2).toArray();
|
|
}
|
|
/**
|
|
* close socket
|
|
* @return {} []
|
|
*/
|
|
close(){
|
|
if(this._socket){
|
|
this._socket.close();
|
|
this._socket = null;
|
|
}
|
|
}
|
|
} |