noodlehaus/node-fakedb — Issue #2: db.all([...]).length != after deleting the only item
Author: brunoqc
Created: 2011-10-26 03:57:19+00:00
State: open

I'm not sure it's a bug but in the following code, users.length is still equal to 1 even after I delete the entry. When I run the code a second time, users.length is equal to 0.

Also, is it normal that the old data doesn't get deleted from the file? Wouldn't the file get big after a while?

code

var db = require('fakedb').init('example.db');

db.key('username');
var users = db.all({ where: { username: 'jaydee' } });
console.log("users.length", users.length, users[0]);

var key = db.add({ username: 'jaydee', type: 'user' });

db.key('username');
users = db.all({ where: { username: 'jaydee' } });
console.log("users.length", users.length, users[0]);

db.del(key);

users = db.all({ where: { username: 'jaydee' } });
console.log("users.length", users.length, users[0]);

output

$ node test.js
users.length 0 undefined
users.length 1 { key: '_b6589fc6ab0dc82cf12099d1c2d40ab994e8410c', doc: { username: 'jaydee', type: 'user' } }
users.length 1 undefined
$
$ node test.js
users.length 0 undefined
users.length 1 { key: '_b6589fc6ab0dc82cf12099d1c2d40ab994e8410c', doc: { username: 'jaydee', type: 'user' } }
users.length 1 undefined
$

example.db

{"key":"_b6589fc6ab0dc82cf12099d1c2d40ab994e8410c","doc":{"username":"jaydee","type":"user"}}
{"key":"_b6589fc6ab0dc82cf12099d1c2d40ab994e8410c","doc":null}
{"key":"_b6589fc6ab0dc82cf12099d1c2d40ab994e8410c","doc":{"username":"jaydee","type":"user"}}
{"key":"_b6589fc6ab0dc82cf12099d1c2d40ab994e8410c","doc":null}