Skip to content

Commit

Permalink
added fs model example
Browse files Browse the repository at this point in the history
  • Loading branch information
nw committed May 17, 2010
1 parent fb3f34c commit b60e35b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 10 deletions.
7 changes: 0 additions & 7 deletions examples/christkv.js

This file was deleted.

39 changes: 39 additions & 0 deletions examples/fs.js
@@ -0,0 +1,39 @@
var sys = require('sys'),
path = require('path'),
fs = require('fs'),
mongoose = require('../mongoose').Mongoose, // get Mongoose
db = mongoose.connect('mongodb://localhost/test'); // connect to localhost, test db


mongoose.load(__dirname+'/models/file.js'); // load model

File = mongoose.get('File',db);

recursiveFolderScan = function(dir,file){
var resource = (file) ? path.join(dir,file) : dir;

var stats = fs.statSync(resource), obj = { dir : dir, file : file };
for(i in stats) if(typeof stats[i] != 'function') obj[i] = stats[i];

new File(obj).save(); // save modified stats object

if(stats.isDirectory()){
fs.readdirSync(resource).forEach(function(path){
recursiveFolderScan(resource,path);
});
}
};

File.drop(function(){ // resetting data for example

recursiveFolderScan(path.join(process.ENV.PWD)); // process 'examples' directory

File.modifiedInLastWeek().each(function(doc){
// using custom getters
sys.puts('\nModified this Week:\t\n\t'+doc.filePath +'\n\tsize: '+doc.sizeInKB+'\n\tmod time: '+doc.ISODateString(doc.mtime));

},true).then(function(){
File.close();
});

});
71 changes: 71 additions & 0 deletions examples/models/file.js
@@ -0,0 +1,71 @@

Model.define('File', {

collection : 'fs',

types: {
dir : String,
file : String,
dev : Number,
ino : Number,
mode : Number,
nlink : Number,
uid : Number,
gid : Number,
rdev: Number,
size: Number,
blksize: Number,
blocks: Number,
atime: Date,
mtime: Date,
ctime: Date
},

indexes : ['dir','file','size','ctime','mtime'],

static : {

modifiedInLastWeek : function(){
return this.find().gt({mtime : (Date.now() - 604800000)+'' });
}

},

methods : {
ISODateString : function(ts){
var d = new Date(parseInt(ts));
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
},

setters: {
atime : function(date){
if(!date) return;
if(Object.prototype.toString.call(date) == '[object Date]') return date.getTime()+'';
else return date;
},
mtime : function(date){
if(!date) return;
if(Object.prototype.toString.call(date) == '[object Date]') return date.getTime()+'';
else return date;
},
ctime : function(date){
if(!date) return;
if(Object.prototype.toString.call(date) == '[object Date]') return date.getTime()+'';
else return date;
}

},

getters: {
sizeInKB : function(v){ return (this.size/1024).toFixed(2) + ' kb'; },
filePath : function(){ return require('path').join(this.dir,this.file); }
}

});
2 changes: 0 additions & 2 deletions examples/user.js
Expand Up @@ -19,8 +19,6 @@ var sys = require('sys'),

Array.prototype.random = function(){ return this[Math.floor(Math.random()*this.length)]; }

// User.insert(doc,func,hydrate);


for(i=0,l=1000; i < l; i++){
objs.push({
Expand Down
2 changes: 1 addition & 1 deletion lib/model/index.js
Expand Up @@ -225,7 +225,7 @@ var object = require('../utils/object'),
model = (function(){
return function(obj){
if(obj._id) obj._id = { id : obj._id.id };
else obj._id = { id : new ObjectID().toHexString() }
else obj._id = { id : new ObjectID().toHexString() };
buildDefinition(this,model.__meta__.type,def.getters,def.setters,obj,this.__meta__.doc,this);
addCustomValues(obj,model.__meta__.type,this);
addCustomGetters(this,def.getters,this);
Expand Down

0 comments on commit b60e35b

Please sign in to comment.