simple-odm
v0.4.5
Published
a simple object document mapper
Downloads
62
Readme
Simple ODM
Simple ODM is an object-document mapper for NoSQL databases. It only supports MongoDB now.
Instrallation
To install Simple ODM:
$ npm install simple-odm --save
Get started
Schema, Model, and Driver
First, you need to create a schema:
import { MongoSchema } from 'simple-odm';
const schema = new MongoSchema({
name: 'user',
paths: {
account_name: {
required: true
}
}
});
Then, create your model class by:
- extending MongoModel class
- overridding schema getter with the schema you created
import { MongoModel } from 'simple-odm';
class User extends MongoModel
{
static get schema () {
return schema;
};
}
You also need to setup a DB driver:
import { mongoDriver } from 'simple-odm';
mongoDriver.setUp({
database: 'my-database'
});
Persist Data
const user = new User({
account_name: "John"
});
try {
user.save().then(() => {
User.findMany().then(users => {
const newUser = users[0];
console.log(newUser.values.account_name); // John
});
});
} catch (e) {
// Error Handling
}
License
MIT