Allow relative driver paths, adopt for node 0.8
This commit is contained in:
parent
11fc70b89a
commit
7a2e300181
|
@ -2,6 +2,8 @@ var fs = require('fs');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var Schema = railway.orm.Schema;
|
var Schema = railway.orm.Schema;
|
||||||
|
|
||||||
|
var existsSync = fs.existsSync || path.existsSync;
|
||||||
|
|
||||||
railway.orm._schemas = [];
|
railway.orm._schemas = [];
|
||||||
|
|
||||||
module.exports = function init(root) {
|
module.exports = function init(root) {
|
||||||
|
@ -9,9 +11,13 @@ module.exports = function init(root) {
|
||||||
var confFile = (root || app.root) + '/config/database.json';
|
var confFile = (root || app.root) + '/config/database.json';
|
||||||
var config;
|
var config;
|
||||||
|
|
||||||
if (path.existsSync(confFile)) {
|
if (existsSync(confFile)) {
|
||||||
try {
|
try {
|
||||||
config = JSON.parse(fs.readFileSync(confFile, 'utf-8'))[app.set('env')];
|
config = JSON.parse(fs.readFileSync(confFile, 'utf-8'))[app.set('env')];
|
||||||
|
// when driver name started with point - look for driver in app root (relative path)
|
||||||
|
if (config.driver && config.driver.match(/^\./)) {
|
||||||
|
config.driver = path.join(app.root, conf.driver);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Could not parse config/database.json');
|
console.log('Could not parse config/database.json');
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -28,9 +34,9 @@ context = prepareContext(schema);
|
||||||
|
|
||||||
// run schema first
|
// run schema first
|
||||||
var schemaFile = (root || app.root) + '/db/schema.';
|
var schemaFile = (root || app.root) + '/db/schema.';
|
||||||
if (path.existsSync(schemaFile + 'js')) {
|
if (existsSync(schemaFile + 'js')) {
|
||||||
schemaFile += 'js';
|
schemaFile += 'js';
|
||||||
} else if (path.existsSync(schemaFile + 'coffee')) {
|
} else if (existsSync(schemaFile + 'coffee')) {
|
||||||
schemaFile += 'coffee';
|
schemaFile += 'coffee';
|
||||||
} else {
|
} else {
|
||||||
schemaFile = false;
|
schemaFile = false;
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
var AbstractClass = require('./abstract-class').AbstractClass;
|
var AbstractClass = require('./abstract-class').AbstractClass;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
|
var existsSync = fs.existsSync || path.existsSync;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export public API
|
* Export public API
|
||||||
|
@ -59,7 +62,7 @@ function Schema(name, settings) {
|
||||||
// this is only one initialization entry point of adapter
|
// this is only one initialization entry point of adapter
|
||||||
// this module should define `adapter` member of `this` (schema)
|
// this module should define `adapter` member of `this` (schema)
|
||||||
var adapter;
|
var adapter;
|
||||||
if (path.existsSync(__dirname + '/adapters/' + name + '.js')) {
|
if (existsSync(__dirname + '/adapters/' + name + '.js')) {
|
||||||
adapter = require('./adapters/' + name);
|
adapter = require('./adapters/' + name);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "jugglingdb",
|
"name": "jugglingdb",
|
||||||
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
|
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
|
||||||
"version": "0.1.11",
|
"version": "0.1.12",
|
||||||
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
|
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },
|
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },
|
||||||
|
|
|
@ -1,67 +0,0 @@
|
||||||
juggling = require('../index')
|
|
||||||
Schema = juggling.Schema
|
|
||||||
Text = Schema.Text
|
|
||||||
|
|
||||||
DBNAME = process.env.DBNAME || 'myapp_test' #this db must already exist and will be destroyed
|
|
||||||
DBUSER = process.env.DBUSER || 'postgres'
|
|
||||||
DBPASS = ''
|
|
||||||
DBENGINE = process.env.DBENGINE || 'postgres'
|
|
||||||
|
|
||||||
require('./spec_helper').init module.exports
|
|
||||||
|
|
||||||
schema = new Schema DBENGINE, database: '', username: DBUSER, password: DBPASS
|
|
||||||
schema.log = (q) -> console.log q
|
|
||||||
|
|
||||||
query = (sql, cb) ->
|
|
||||||
schema.adapter.query sql, cb
|
|
||||||
|
|
||||||
User = schema.define 'User',
|
|
||||||
name: {type: String, default: "guest"}
|
|
||||||
credits: {type: Number, default: 0}
|
|
||||||
|
|
||||||
withBlankDatabase = (cb) ->
|
|
||||||
db = schema.settings.database = DBNAME
|
|
||||||
query 'DROP DATABASE IF EXISTS ' + db, (err) ->
|
|
||||||
query 'CREATE DATABASE ' + db, ->
|
|
||||||
schema.automigrate(cb)
|
|
||||||
|
|
||||||
it 'default values should not interfere with fully specified objects', (test)->
|
|
||||||
withBlankDatabase (err)->
|
|
||||||
test.ok !err, "error while setting up blank database"
|
|
||||||
new User()
|
|
||||||
User.create {name: "Steve", credits: 47}, (err, obj)->
|
|
||||||
console.log "error creating user: #{err}"
|
|
||||||
test.ok !err, "error occurred when saving user with all values specified"
|
|
||||||
test.ok obj.id?, 'saved object has no id'
|
|
||||||
console.log "id: #{obj.id}"
|
|
||||||
test.equals obj.name, "Steve", "User's name didn't save correctly"
|
|
||||||
test.equals obj.credits, 47, "User's credits didn't save correctly"
|
|
||||||
test.done()
|
|
||||||
|
|
||||||
it 'objects should have default values when some fields are unspecified', (test)->
|
|
||||||
User.create {credits: 2}, (err, obj)->
|
|
||||||
console.log "error creating user: #{err}"
|
|
||||||
test.ok !err, "error occurred when saving user with some values unspecified"
|
|
||||||
test.ok obj.id?, 'saved object has no id'
|
|
||||||
test.equals obj.name, "guest", "User's name didn't save correctly"
|
|
||||||
test.equals obj.credits, 2, "User's credits didn't save correctly"
|
|
||||||
User.create {name: "Jeanette Adele McKenzie"}, (err, obj)->
|
|
||||||
console.log "error creating user: #{err}"
|
|
||||||
test.ok !err, "error occurred when saving user with some values unspecified"
|
|
||||||
test.ok obj.id?, 'saved object has no id'
|
|
||||||
test.equals obj.name, "Jeanette Adele McKenzie", "User's name didn't save correctly"
|
|
||||||
test.equals obj.credits, 0, "User's credits didn't save correctly"
|
|
||||||
test.done()
|
|
||||||
|
|
||||||
it 'objects should have default values when all fields are left unspecified', (test)->
|
|
||||||
User.create {}, (err, obj)->
|
|
||||||
console.log "error creating user: #{err}"
|
|
||||||
test.ok !err, "error occurred when saving user with all values specified"
|
|
||||||
test.ok obj.id?, 'saved object has no id'
|
|
||||||
test.equals obj.name, "guest", "User's name didn't save correctly"
|
|
||||||
test.equals obj.credits, 0, "User's credits didn't save correctly"
|
|
||||||
test.done()
|
|
||||||
|
|
||||||
it 'should disconnect when done', (test)->
|
|
||||||
schema.disconnect()
|
|
||||||
test.done()
|
|
|
@ -1,132 +0,0 @@
|
||||||
juggling = require('../index')
|
|
||||||
Schema = juggling.Schema
|
|
||||||
Text = Schema.Text
|
|
||||||
|
|
||||||
DBNAME = process.env.DBNAME || 'myapp_test' #this db must already exist and will be destroyed
|
|
||||||
DBUSER = process.env.DBUSER || 'postgres'
|
|
||||||
DBPASS = ''
|
|
||||||
DBENGINE = process.env.DBENGINE || 'postgres'
|
|
||||||
|
|
||||||
require('./spec_helper').init module.exports
|
|
||||||
|
|
||||||
schema = new Schema DBENGINE, database: '', username: DBUSER, password: DBPASS
|
|
||||||
schema.log = (q) -> console.log q
|
|
||||||
|
|
||||||
query = (sql, cb) ->
|
|
||||||
schema.adapter.query sql, cb
|
|
||||||
|
|
||||||
User = schema.define 'User',
|
|
||||||
email: { type: String, null: false, index: true }
|
|
||||||
name: String
|
|
||||||
bio: Text
|
|
||||||
password: String
|
|
||||||
birthDate: Date
|
|
||||||
pendingPeriod: Number
|
|
||||||
createdByAdmin: Boolean
|
|
||||||
|
|
||||||
withBlankDatabase = (cb) ->
|
|
||||||
db = schema.settings.database = DBNAME
|
|
||||||
query 'DROP DATABASE IF EXISTS ' + db, (err) ->
|
|
||||||
query 'CREATE DATABASE ' + db, cb
|
|
||||||
|
|
||||||
getColumnDescriptions = (model, cb)->
|
|
||||||
query "SELECT column_name, data_type, is_nullable, column_default FROM information_schema.COLUMNS WHERE table_name = '#{model}'", (err, res)->
|
|
||||||
if err
|
|
||||||
cb err
|
|
||||||
else
|
|
||||||
fields = {}
|
|
||||||
fields[entry.column_name] = entry for entry in res
|
|
||||||
cb err, fields
|
|
||||||
|
|
||||||
it 'should run migration', (test)->
|
|
||||||
withBlankDatabase (err)->
|
|
||||||
schema.automigrate ->
|
|
||||||
getColumnDescriptions 'User', (err, fields)->
|
|
||||||
test.deepEqual fields,
|
|
||||||
id:
|
|
||||||
column_name: 'id'
|
|
||||||
data_type: 'integer'
|
|
||||||
is_nullable: 'NO'
|
|
||||||
column_default: 'nextval(\'"User_id_seq"\'::regclass)'
|
|
||||||
email:
|
|
||||||
column_name: 'email'
|
|
||||||
data_type: 'character varying'
|
|
||||||
is_nullable: 'NO'
|
|
||||||
column_default: null
|
|
||||||
name:
|
|
||||||
column_name: 'name'
|
|
||||||
data_type: 'character varying'
|
|
||||||
is_nullable: 'YES'
|
|
||||||
column_default: null
|
|
||||||
bio:
|
|
||||||
column_name: 'bio'
|
|
||||||
data_type: 'text'
|
|
||||||
is_nullable: 'YES'
|
|
||||||
column_default: null
|
|
||||||
password:
|
|
||||||
column_name: 'password'
|
|
||||||
data_type: 'character varying'
|
|
||||||
is_nullable: 'YES'
|
|
||||||
column_default: null
|
|
||||||
birthDate:
|
|
||||||
column_name: 'birthDate'
|
|
||||||
data_type: 'timestamp without time zone'
|
|
||||||
is_nullable: 'YES'
|
|
||||||
column_default: null
|
|
||||||
pendingPeriod:
|
|
||||||
column_name: 'pendingPeriod'
|
|
||||||
data_type: 'integer'
|
|
||||||
is_nullable: 'YES'
|
|
||||||
column_default: null
|
|
||||||
createdByAdmin:
|
|
||||||
column_name: 'createdByAdmin'
|
|
||||||
data_type: 'boolean'
|
|
||||||
is_nullable: 'YES'
|
|
||||||
column_default: null
|
|
||||||
test.done()
|
|
||||||
|
|
||||||
it 'should autoupdate', (test) ->
|
|
||||||
getColumnDescriptions 'User', (err, fields)->
|
|
||||||
|
|
||||||
userExists = (cb) ->
|
|
||||||
query 'SELECT * FROM "User"', (err, res) ->
|
|
||||||
cb(not err and res[0].email == 'test@example.com')
|
|
||||||
|
|
||||||
User.create email: 'test@example.com', (err, user) ->
|
|
||||||
test.ok not err, "error occurred while creating User: #{err}"
|
|
||||||
userExists (yep) ->
|
|
||||||
test.ok yep, 'userExists returned false'
|
|
||||||
User.defineProperty 'email', type: String
|
|
||||||
User.defineProperty 'createdByAdmin', type: String
|
|
||||||
User.defineProperty 'newProperty', type: Number
|
|
||||||
User.defineProperty 'pendingPeriod', false
|
|
||||||
schema.autoupdate (err) ->
|
|
||||||
getColumnDescriptions 'User', (err, fields) ->
|
|
||||||
# email should now be nullable
|
|
||||||
test.equal fields.email.is_nullable, 'YES', "email's nullability did not change to nullable"
|
|
||||||
# change type of createdByAdmin
|
|
||||||
test.equal fields.createdByAdmin.data_type, 'character varying', "createdByAdmin's data type did not change"
|
|
||||||
# new column should be added
|
|
||||||
test.ok fields.newProperty, 'New column was not added'
|
|
||||||
if fields.newProperty
|
|
||||||
test.equal fields.newProperty.data_type, 'integer', 'New column type is not integer'
|
|
||||||
# pendingPeriod should be dropped
|
|
||||||
test.ok not fields.pendingPeriod, 'pendingPeriod was not dropped'
|
|
||||||
|
|
||||||
# user should still exist
|
|
||||||
userExists (yep) ->
|
|
||||||
test.ok yep, 'user does not still exist after update'
|
|
||||||
test.done()
|
|
||||||
|
|
||||||
it 'should check actuality of schema', (test) ->
|
|
||||||
# drop column
|
|
||||||
User.schema.isActual (err, ok) ->
|
|
||||||
test.ok ok, "User is not actual before schema is modified"
|
|
||||||
User.defineProperty 'email', false
|
|
||||||
User.schema.isActual (err, ok) ->
|
|
||||||
test.ok not ok, "User should not be actual after schema is modified"
|
|
||||||
test.done()
|
|
||||||
|
|
||||||
it 'should disconnect when done', (test)->
|
|
||||||
schema.disconnect()
|
|
||||||
test.done()
|
|
Loading…
Reference in New Issue