54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
import mongoose from 'mongoose';
|
|
|
|
let Schema = mongoose.Schema;
|
|
|
|
|
|
let SpiderDataSchema = new Schema({
|
|
id: {type: Number},
|
|
data: {type: Schema.Types.Mixed},
|
|
type: {type: String},
|
|
status: {type: Number, default: 0}
|
|
}, {
|
|
collection: 'spider_data',
|
|
timestamps: true
|
|
});
|
|
|
|
class SpiderDataClass {
|
|
static async updateData(data) {
|
|
try {
|
|
let record = await SpiderDataModel.findOne({'data.id': data.id});
|
|
record.data = data;
|
|
record.status = 1;
|
|
await record.save();
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
static async saveList(list, type) {
|
|
for(let record of list) {
|
|
if (record.id > 0) {
|
|
let sdata = new SpiderDataModel({
|
|
type: type,
|
|
data: record,
|
|
status: 0
|
|
});
|
|
try {
|
|
await sdata.save();
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
SpiderDataSchema.loadClass(SpiderDataClass);
|
|
|
|
SpiderDataSchema.query.byType = function(type) {
|
|
return this.where({ type: type, status: 0});
|
|
};
|
|
|
|
let SpiderDataModel = mongoose.model('SpiderData', SpiderDataSchema);
|
|
|
|
export default SpiderDataModel;
|