sandstorm
v0.3.28
Published
Not a MongoDB ORM
Downloads
231
Readme
Sandstorm
Installation
$ npm i sandstorm
Usage
Adding new schema
const sandstorm = new Sandstorm();
sandstorm.register("Author", {
name: {
type: "String",
required: true,
min: 3,
max: 64
},
last_name: {
type: "String",
required: true,
min: 3,
max: 64
},
nick: "String"
});
sandstorm.register("Book", {
title: {
type: "String",
required: true,
max: 256
},
author: {
type: "Author",
required: true,
search: [
"name",
"last_name",
"nick"
]
}
});
Creating new models
const book = sandstorm.create("Book");
book.set({title: "The Art of Deception",
author: {
last_name: "Mitnick",
name: "Kevin"
}
}).then(() => book.save()).then((_id) => {
console.log("ObjectID of saved book", _id);
}).catch((err) => {
console.error("Ups", err);
});
Finding documents
sandstorm.find("Book", {"author.name": "Kevin"}).toArray().then((docs) => {
console.log("Books written by Kevin", docs.map(book => book.title)).join(", ");
}).catch((err) => {
console.error("Ups", err);
});
Updating
Use merge
to update only chosen fields or set
to overwrite whole document
book.merge({title: "The Art of Deception: Controling the Human Element of Security"})
.then(() => book.save()).then((_id) => {
console.log("ObjectID of saved book", _id);
}).catch((err) => {
console.error("Ups", err);
});
Deleting
sandstorm.find("Book", {"author.name": "Kevin"}).toArray()
.then((docs) => Promise.all(docs.map(book => book.delete())).then(() => {
console.log("All documents deleted");
})).catch((err) => {
console.error("Ups", err);
});