Comparing version 0.0.41 to 0.1.0
@@ -743,3 +743,8 @@ /** | ||
var sql = []; | ||
var primaryKeys = this._models[model].settings.primaryKeys || []; | ||
var foreignKeys = this._models[model].settings.foreignKeys || []; | ||
primaryKeys = primaryKeys.slice(0); | ||
foreignKeys = foreignKeys.slice(0); | ||
Object.keys(this._models[model].properties).forEach(function (prop) { | ||
@@ -752,4 +757,2 @@ if (prop === 'id') { | ||
var primaryKeys = this._models[model].settings.primaryKeys || []; | ||
primaryKeys = primaryKeys.slice(0); | ||
if (primaryKeys.length) { | ||
@@ -764,4 +767,2 @@ for (var i = 0, length = primaryKeys.length; i < length; i++) { | ||
var foreignKeys = this._models[model].settings.foreignKeys || []; | ||
foreignKeys = foreignKeys.slice(0); | ||
if (foreignKeys.length) { | ||
@@ -768,0 +769,0 @@ for (var i = 0, length = foreignKeys.length; i < length; i++) { |
@@ -325,6 +325,8 @@ /** | ||
self.queryAll('PRAGMA TABLE_INFO(' + self.tableEscaped(model) + ');', function (err, fields) { | ||
if(err) done(err); | ||
self.queryAll('PRAGMA INDEX_LIST(' + self.tableEscaped(model) + ');', function (err, indexes) { | ||
if(err) done(err); | ||
if (!err && fields.length) { | ||
self.alterTable(model, fields, indexes, done); | ||
} else { | ||
} else { | ||
self.createTable(model, indexes, done); | ||
@@ -635,5 +637,8 @@ } | ||
SQLite3.prototype.propertiesSQL = function (model) { | ||
var self = this; | ||
var sql = []; | ||
Object.keys(self._models[model].properties).forEach(function (prop) { | ||
var self = this, id = false, sql = [], props = Object.keys(self._models[model].properties); | ||
var primaryKeys = this._models[model].settings.primaryKeys || []; | ||
primaryKeys = primaryKeys.slice(0); | ||
props.forEach(function (prop) { | ||
if (prop === 'id') { | ||
@@ -647,12 +652,20 @@ return; | ||
var primaryKeys = this._models[model].settings.primaryKeys || []; | ||
primaryKeys = primaryKeys.slice(0); | ||
if (primaryKeys.length) { | ||
for (var i = 0, length = primaryKeys.length; i < length; i++) { | ||
primaryKeys[i] = "`" + primaryKeys[i] + "`"; | ||
if(props.indexOf(primaryKeys[i]) === -1) { | ||
if(primaryKeys[i] === 'id') { | ||
id = true; | ||
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'); | ||
} else { | ||
sql.push('`' + primaryKeys[i] + '` ' + self.propertySettingsSQL(model, primaryKeys[i])); | ||
} | ||
} | ||
} | ||
sql.push("PRIMARY KEY (" + primaryKeys.join(', ') + ")"); | ||
if(!id) { | ||
sql.push('PRIMARY KEY (`' + primaryKeys.join('`, `') + '`)'); | ||
} | ||
} else { | ||
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'); | ||
if(!id) { | ||
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'); | ||
} | ||
} | ||
@@ -659,0 +672,0 @@ return sql.join(',\n '); |
{ | ||
"name": "caminte", | ||
"description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb", | ||
"version": "0.0.41", | ||
"version": "0.1.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Aleksej Gordejev", |
102
README.md
@@ -57,2 +57,104 @@ [![Build Status](https://travis-ci.org/biggora/caminte.png?branch=master)](https://travis-ci.org/biggora/caminte) | ||
## Usage | ||
```javascript | ||
var caminte = require('caminte'); | ||
var Schema = caminte.Schema; | ||
var schema = new Schema('redis', {port: 6379}); | ||
// define models | ||
var Post = schema.define('Post', { | ||
title: { type: schema.String, limit: 255 }, | ||
userId: { type: schema.Number }, | ||
content: { type: schema.Text }, | ||
created: { type: schema.Date, default: Date.now }, | ||
updated: { type: schema.Date }, | ||
published: { type: schema.Boolean, default: false, index: true } | ||
}); | ||
var User = schema.define('User', { | ||
name: { type: schema.String, limit: 255 }, | ||
bio: { type: schema.Text }, | ||
email: { type: schema.String, limit: 155, unique: true }, | ||
approved: { type: schema.Boolean, default: false, index: true } | ||
joinedAt: { type: schema.Date, default: Date.now }, | ||
age: { type: schema.Number }, | ||
gender: { type: schema.String, limit: 10 } | ||
}); | ||
// setup hooks | ||
Post.afterUpdate = function (next) { | ||
this.updated = new Date(); | ||
this.save(); | ||
next(); | ||
}; | ||
// define any custom method for instance | ||
User.prototype.getNameAndAge = function () { | ||
return this.name + ', ' + this.age; | ||
}; | ||
// define scope | ||
Post.scope('active', { published : true }); | ||
// setup validations | ||
User.validatesPresenceOf('name', 'email'); | ||
User.validatesUniquenessOf('email', {message: 'email is not unique'}); | ||
User.validatesInclusionOf('gender', {in: ['male', 'female']}); | ||
User.validatesNumericalityOf('age', {int: true}); | ||
// setup relationships | ||
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'}); | ||
// Common API methods | ||
var user = new User({ | ||
name: 'Alex', | ||
email: 'example@domain.aga', | ||
age: 40, | ||
gender: 'male' | ||
}); | ||
user.isValid(function (valid) { | ||
if (!valid) { | ||
return console.log(user.errors); | ||
} | ||
user.save(function(err){ | ||
if (!err) { | ||
return console.log(err); | ||
} | ||
console.log('User created'); | ||
}); | ||
}) | ||
// just instantiate model | ||
new Post | ||
// save model (of course async) | ||
Post.create(cb); | ||
// all posts | ||
Post.all(cb) | ||
// all posts by user | ||
Post.all({where: {userId: user.id}, order: 'id', limit: 10, skip: 20}); | ||
// the same as prev | ||
user.posts(cb) | ||
// get one latest post | ||
Post.findOne({where: {published: true}, order: 'date DESC'}, cb); | ||
// same as new Post({userId: user.id}); | ||
user.posts.build | ||
// save as Post.create({userId: user.id}, cb); | ||
user.posts.create(cb) | ||
// find instance by id | ||
User.findById(1, cb) | ||
// count instances | ||
User.count([conditions, ]cb) | ||
// destroy instance | ||
user.destroy(cb); | ||
// destroy all instances | ||
User.destroyAll(cb); | ||
// models also accessible in schema: | ||
schema.models.User; | ||
schema.models.Post; | ||
``` | ||
## Package structure | ||
@@ -59,0 +161,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
694661
13560
213