Skip to content

alexandrajs/sandstorm

Repository files navigation

Sandstorm

Build Status Coverage Status Maintainability

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);
});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published