cormo
ORM framework for Node.js
Last updated 22 days ago by sixmen .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install cormo 
SYNC missed versions from official npm registry.

npm version Build Status

About

CORMO is an ORM framework for Node.js.

Currently supports:

  • multi-DB: MySQL, MongoDB, SQLite3, PostgreSQL
  • constraints
  • validations
  • associations
  • geospatial query
  • callbacks
  • aggregation
  • nested column

See https://github.com/croquiscom/cormo/wiki/Future-Plans for future plans.

Overview

You can see detail guides on http://croquiscom.github.io/cormo/.

Define models

import * as cormo from 'cormo';

const connection = new cormo.Connection('mysql', { database: 'test' });

// this will create two tables - users, posts - in the database.

@cormo.Model()
class User extends cormo.BaseModel {
  @cormo.Column({ type: String })
  name?: string;

  @cormo.Column({ type: cormo.types.Integer })
  age?: number;
}

@cormo.Model({ connection }) // set connection explictly
class Post extends cormo.BaseModel {
  @cormo.Column(String) // `String` is the same as `type: String`
  title?: string;

  @cormo.Column('string') // you can also use `string` to specify a string type
  body?: string;

  @cormo.Column(Date)
  date?: Date;

  @cormo.ObjectColumn({type: Author}) // You can delcare a nested column with `ObjectColumn` type
  author: Author;
}

class Auther extends User {
  @cormo.Column(Date)
  date_joined? Date;
}
const cormo = require('cormo');
const connection = new cormo.Connection('mysql', { database: 'test' });

// this will create two tables - users, posts - in the database.

const User = connection.model('User', {
  name: { type: String },
  age: { type: cormo.types.Integer }
});

const Post = connection.model('Post', {
  title: String, // `String` is the same as `type: String`
  body: 'string', // you can also use `string` to specify a string type
  date: Date,
});

cormo = require 'cormo'
connection = new cormo.Connection 'mysql', database: 'test'

# this will create two tables - users, posts - in the database.

class User extends cormo.BaseModel
  @column 'name', type: String
  @column 'age', type: cormo.types.Integer

class Post extends cormo.BaseModel
  @column 'title', String # `String` is the same as `type: String`
  @column 'body', 'string' # you can also use `string` to specify a string type
  @column 'date', Date

Create records

const user1 = new User({ name: 'John Doe', age: 27 });
await user1.save();

const user2 = await User.create({ name: 'John Doe', age: 27 });
const user1 = new User({ name: 'John Doe', age: 27 });
await user1.save();

const user2 = await User.create({ name: 'John Doe', age: 27 });
user1 = new User name: 'John Doe', age: 27
await user1.save()

user2 = await User.create name: 'John Doe', age: 27

Retreive records

const user = await User.find(1);

const users = await User.where({name: 'John Doe'});

const users = await User.where({age: 27})
  .select(['name'])
  .order('name')
  .limit(5)
  .skip(100);

const users = await User.where({$or: [ { age: { $lt: 20 } }, { age: { $gt: 60 } } ]})
  .where({name: { $contains: 'smi' }});

const posts = await Post.where({
  title: 'Lorem ipsum',
  'author.name': 'John Doe',
});

const user = await User.find(1);

const users = await User.where({name: 'John Doe'});

const users = await User.where({age: 27})
  .select(['name'])
  .order('name')
  .limit(5)
  .skip(100);

const users = await User.where({$or: [ { age: { $lt: 20 } }, { age: { $gt: 60 } } ]})
  .where({name: { $contains: 'smi' }});
user = await User.find 1

users = await User.where name: 'John Doe'

users = await await User.where(age: 27)
  .select(['name'])
  .order('name')
  .limit(5)
  .skip(100)

users = await User.where($or: [ { age: $lt: 20 }, { age: $gt: 60 } ])
  .where(name: $contains: 'smi')

Count records

const count = await User.count();

const count = await User.count({age: 27});
const count = await User.count();

const count = await User.count({age: 27});
count = await User.count()

count = await User.count age: 27

Update records

const user = await User.find(1);
user.age = 30;
await user.save();

const count = await User.find(1).update({age: 10});

const count = await User.where({age: 27}).update({age: 10});

const count = await User.where({age: 35}).update({age: {$inc: 3}});

const posts = await Post.where({title: 'Lorem Ipsum'}).update({ author: {age: {$inc: 5}}});
const user = await User.find(1);
user.age = 30;
await user.save();

const count = await User.find(1).update({age: 10});

const count = await User.where({age: 27}).update({age: 10});

const count = await User.where({age: 35}).update({age: {$inc: 3}});
user = await User.find 1
user.age = 30
await user.save()

count = await User.find(1).update age: 10

count = await User.where(age: 27).update age: 10

count = await User.where(age: 35).update age: $inc: 3

Delete records

const count = await User.delete({age: 27});
const count = await User.delete({age: 27});
count = await User.delete age: 27

Constraint

@cormo.Model()
class User extends cormo.BaseModel {
  @cormo.Column({ type: String, required: true })
  name!: string;

  @cormo.Column({ type: Number, required: true })
  age!: number;

  @cormo.Column({ type: String, unique: true, required: true })
  email!: string;
}
const User = connection.model('User', {
  name: { type: String, required: true },
  age: { type: Number, required: true },
  email: { type: String, unique: true, required: true }
});
class User extends cormo.BaseModel
  @column 'name', type: String, required: true
  @column 'age', type: Number, required: true
  @column 'email', type: String, unique: true, required: true

Validation

@cormo.Model()
class User extends cormo.BaseModel {
  @cormo.Column(String)
  name?: string;

  @cormo.Column(Number)
  age?: number;

  @cormo.Column(String)
  email?: string;
}

User.addValidator((record) => {
  if (record.age<18) {
    return 'too young';
  }
});
const User = connection.model('User', {
  name: String,
  age: Number,
  email: String
});

User.addValidator((record) => {
  if (record.age<18) {
    return 'too young';
  }
});
class User extends cormo.BaseModel
  @column 'name', String
  @column 'age', Number
  @column 'email', String

  @addValidator (record) ->
    if record.age < 18
      return 'too young'

Callbacks

@cormo.Model()
class User extends cormo.BaseModel {
  @cormo.Column(String)
  name?: string;

  @cormo.Column(Number)
  age?: number;
}

User.beforeSave(() => {
  this.name = this.name.trim();
});
const User = connection.model('User', {
  name: String,
  age: Number
});

User.beforeSave(() => {
  this.name = this.name.trim();
});
class User extends cormo.BaseModel
  @column 'name', String
  @column 'age', Number

  @beforeSave ->
    @name = @name.trim()

Associations

@cormo.Model()
class User extends cormo.BaseModel {
  @cormo.Column(String)
  name?: string;

  @cormo.Column(Number)
  age?: number;

  @cormo.HasMany()
  posts?: Post[];
}

@cormo.Model()
class Post extends cormo.BaseModel {
  @cormo.Column(String)
  title?: string;

  @cormo.Column(String)
  body?: string;

  @cormo.BelongsTo()
  user?: User;
}
var User = connection.model('User', {
  name: String,
  age: Number
});

var Post = connection.model('Post', {
  title: String,
  body: String
});

User.hasMany(Post);
Post.belongsTo(User);
class User extends cormo.BaseModel
  @column 'name', String
  @column 'age', Number
  @hasMany 'posts'

class Post extends cormo.BaseModel
  @column 'title', String
  @column 'body', String
  @belongsTo 'user'

Aggregation

const records = await Order.where({price: {$lt: 10}})
  .group(null, {count: {$sum: 1}, total: {$sum: '$price'}})
  .order('total');

const records = await Order.group('customer', {min_price: {$min: '$price'}, max_price: {$max: '$price'}});
const records = await Order.where({price: {$lt: 10}})
  .group(null, {count: {$sum: 1}, total: {$sum: '$price'}})
  .order('total');

const records = await Order.group('customer', {min_price: {$min: '$price'}, max_price: {$max: '$price'}});
records = await Order.where price: $lt: 10
  .group null, count: {$sum: 1}, total: {$sum: '$price'}
  .order 'total'

records = await Order.group 'customer', min_price: { $min: '$price' }, max_price: { $max: '$price' }

Geospatial query

@cormo.Model()
class Place extends cormo.BaseModel {
  @cormo.Column(String)
  name?: string;

  @cormo.Column(cormo.types.GeoPoint)
  location?: [number, number];
}

// create
await Place.create({name: 'Carrier Dome', location: [-76.136131, 43.036240]});

// query
const places = await Place.query().near({location: [-5, 45]}).limit(4);
console.log(places);
const Place = connection.model('Place', {
  name: String,
  location: cormo.types.GeoPoint
});

// create
await Place.create({name: 'Carrier Dome', location: [-76.136131, 43.036240]});

// query
const places = await Place.query().near({location: [-5, 45]}).limit(4);
console.log(places);
class Place extends cormo.BaseModel
  @column 'name', String
  @column 'location', cormo.types.GeoPoint

# create
await Place.create name: 'Carrier Dome', location: [-76.136131, 43.036240]

# query
places = await Place.query().near(location: [-5, 45]).limit(4)
console.log places

License

MIT licenses. See LICENSE for more details.

Current Tags

  • 0.14.2-alpha.4                                ...           alpha (2 months ago)
  • 0.14.12                                ...           latest (22 days ago)

135 Versions

  • 0.14.12                                ...           22 days ago
  • 0.14.11                                ...           a month ago
  • 0.14.10                                ...           a month ago
  • 0.14.9                                ...           a month ago
  • 0.14.8                                ...           2 months ago
  • 0.14.7                                ...           2 months ago
  • 0.14.6                                ...           2 months ago
  • 0.14.5                                ...           2 months ago
  • 0.14.4                                ...           2 months ago
  • 0.14.3                                ...           2 months ago
  • 0.14.2                                ...           2 months ago
  • 0.14.2-alpha.4 [deprecated]           ...           2 months ago
  • 0.14.2-alpha.3 [deprecated]           ...           2 months ago
  • 0.14.2-alpha.2 [deprecated]           ...           2 months ago
  • 0.14.2-alpha.1 [deprecated]           ...           2 months ago
  • 0.14.1                                ...           2 months ago
  • 0.14.0                                ...           3 months ago
  • 0.13.1                                ...           5 months ago
  • 0.13.0                                ...           6 months ago
  • 0.12.10                                ...           7 months ago
  • 0.12.9                                ...           7 months ago
  • 0.12.8                                ...           7 months ago
  • 0.12.7                                ...           8 months ago
  • 0.12.6                                ...           8 months ago
  • 0.12.5                                ...           9 months ago
  • 0.12.4                                ...           9 months ago
  • 0.12.3                                ...           10 months ago
  • 0.12.2                                ...           10 months ago
  • 0.12.1                                ...           a year ago
  • 0.12.0                                ...           a year ago
  • 0.11.13                                ...           a year ago
  • 0.11.12                                ...           a year ago
  • 0.11.11                                ...           a year ago
  • 0.11.10                                ...           a year ago
  • 0.11.9                                ...           a year ago
  • 0.11.8                                ...           a year ago
  • 0.11.7                                ...           a year ago
  • 0.11.6                                ...           a year ago
  • 0.11.4                                ...           a year ago
  • 0.11.3                                ...           a year ago
  • 0.11.2                                ...           a year ago
  • 0.11.1                                ...           a year ago
  • 0.11.0                                ...           a year ago
  • 0.10.10                                ...           a year ago
  • 0.10.9                                ...           a year ago
  • 0.10.8                                ...           a year ago
  • 0.10.7                                ...           2 years ago
  • 0.10.6                                ...           2 years ago
  • 0.10.5                                ...           2 years ago
  • 0.10.4                                ...           2 years ago
  • 0.10.3                                ...           2 years ago
  • 0.10.2                                ...           2 years ago
  • 0.10.1                                ...           2 years ago
  • 0.10.0                                ...           2 years ago
  • 0.9.22                                ...           3 years ago
  • 0.9.21                                ...           3 years ago
  • 0.9.20                                ...           3 years ago
  • 0.9.19                                ...           3 years ago
  • 0.9.18                                ...           3 years ago
  • 0.9.17                                ...           3 years ago
  • 0.9.16                                ...           3 years ago
  • 0.9.15                                ...           3 years ago
  • 0.9.14                                ...           3 years ago
  • 0.9.13                                ...           3 years ago
  • 0.9.12                                ...           3 years ago
  • 0.9.11                                ...           4 years ago
  • 0.9.10                                ...           4 years ago
  • 0.9.9                                ...           4 years ago
  • 0.9.8                                ...           4 years ago
  • 0.9.7                                ...           4 years ago
  • 0.9.6                                ...           4 years ago
  • 0.9.5                                ...           4 years ago
  • 0.9.4                                ...           4 years ago
  • 0.9.3                                ...           5 years ago
  • 0.9.2                                ...           5 years ago
  • 0.9.1                                ...           5 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.19                                ...           5 years ago
  • 0.8.18                                ...           5 years ago
  • 0.8.17                                ...           5 years ago
  • 0.8.16                                ...           5 years ago
  • 0.8.15                                ...           5 years ago
  • 0.8.14                                ...           5 years ago
  • 0.8.13                                ...           5 years ago
  • 0.8.12                                ...           5 years ago
  • 0.8.11                                ...           5 years ago
  • 0.8.10                                ...           5 years ago
  • 0.8.9                                ...           5 years ago
  • 0.8.8                                ...           5 years ago
  • 0.8.7                                ...           5 years ago
  • 0.8.6                                ...           6 years ago
  • 0.8.5                                ...           6 years ago
  • 0.8.4                                ...           6 years ago
  • 0.8.3                                ...           6 years ago
  • 0.8.2                                ...           6 years ago
  • 0.8.1                                ...           6 years ago
  • 0.8.0                                ...           6 years ago
  • 0.7.6                                ...           6 years ago
  • 0.7.5                                ...           6 years ago
  • 0.7.4                                ...           6 years ago
  • 0.7.3                                ...           6 years ago
  • 0.7.2                                ...           6 years ago
  • 0.7.2-pre1                                ...           6 years ago
  • 0.7.1                                ...           6 years ago
  • 0.7.0                                ...           6 years ago
  • 0.6.8                                ...           7 years ago
  • 0.6.7                                ...           7 years ago
  • 0.6.6                                ...           7 years ago
  • 0.6.5                                ...           7 years ago
  • 0.6.4                                ...           7 years ago
  • 0.6.3                                ...           7 years ago
  • 0.6.2                                ...           7 years ago
  • 0.6.1                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.7                                ...           7 years ago
  • 0.5.6                                ...           7 years ago
  • 0.5.5                                ...           7 years ago
  • 0.5.4                                ...           7 years ago
  • 0.5.3                                ...           7 years ago
  • 0.5.2                                ...           7 years ago
  • 0.5.1                                ...           7 years ago
  • 0.5.0                                ...           7 years ago
  • 0.4.1                                ...           7 years ago
  • 0.4.0                                ...           7 years ago
  • 0.3.9                                ...           7 years ago
  • 0.3.8                                ...           7 years ago
  • 0.3.7                                ...           7 years ago
  • 0.3.6                                ...           7 years ago
  • 0.3.5                                ...           7 years ago
  • 0.3.4                                ...           7 years ago
  • 0.3.3                                ...           7 years ago
  • 0.3.2                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.0                                ...           8 years ago
Downloads
Today 0
This Week 0
This Month 36
Last Day 0
Last Week 0
Last Month 106
Dependencies (5)
Dev Dependencies (26)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |