sworm
Version:
a lightweight write-only ORM for MSSQL, MySQL, PostgreSQL, Oracle, Sqlite 3
100 lines (89 loc) • 3.58 kB
JavaScript
var describeDatabase = require('./describeDatabase')
var sworm = require('..')
var expect = require('chai').expect
var database = {
createTables: function(db, tables) {
function createTable(name, sql) {
tables.push(name);
return db.query("if object_id('dbo." + name + "', 'U') is not null drop table [dbo].[" + name + "]").then(function() {
return db.query(sql);
});
}
return createTable("people",
'CREATE TABLE [dbo].[people]([id] [int] IDENTITY(1,1) NOT NULL, [name] [nvarchar](50) NOT NULL, [address_id] [int] NULL, photo varbinary(10) null)'
).then(function() {
return createTable("pets",
'CREATE TABLE [dbo].[pets]([id] [int] IDENTITY(1,1) NOT NULL, [name] [nvarchar](50) NOT NULL, [owner_id] [int] NULL)'
);
}).then(function() {
return createTable("people_addresses",
'CREATE TABLE [dbo].[people_addresses]([address_id] [int] NOT NULL, [person_id] [int] NOT NULL, [rating] [int] NULL)'
);
}).then(function () {
return createTable("addresses",
'CREATE TABLE [dbo].[addresses]([id] [int] IDENTITY(1,1) NOT NULL, [address] [nvarchar](50) NOT NULL)'
);
}).then(function () {
return createTable("people_weird_id",
'CREATE TABLE [dbo].[people_weird_id]([weird_id] [int] IDENTITY(1,1) NOT NULL, [name] [nvarchar](50) NULL, [address_weird_id] [int] NULL)'
);
}).then(function () {
return createTable("people_explicit_id",
'CREATE TABLE [dbo].[people_explicit_id]([id] [int] NOT NULL, [name] [nvarchar](50) NOT NULL)'
);
}).then(function () {
return createTable("other_people",
'CREATE TABLE [dbo].[other_people]([id] [int] IDENTITY(1,1) NOT NULL, [name] [nvarchar](50) NOT NULL)'
);
}).then(function () {
return createTable("people_uuid",
'CREATE TABLE [dbo].[people_uuid](id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY, [name] [nvarchar](50) NULL)'
);
}).then(function () {
return db.query(
"CREATE TRIGGER people_trigger " +
" ON people " +
" FOR INSERT AS " +
"BEGIN " +
" INSERT other_people VALUES ('person') " +
"END;"
);
});
},
"true": true,
"false": false,
clean: function(records) {
return records;
},
driverModuleName: "mssql"
};
var config = {
driver: "mssql",
config: { user: "user", password: "password", server: "windows", database: "sworm" }
}
if (!process.env.TRAVIS) {
describeDatabase("mssql", config, database, function () {
describe('generatedId', function () {
it('can insert row with uniqueidentifier and get id correctly', function () {
var db = sworm.db(config)
var person = db.model({table: 'people_uuid', generatedId: 'output'});
var bob = person({name: 'bob'})
return bob.save().then(function () {
return person.query('select * from people_uuid where id = @id', {id: bob.identity()})
}).then(function (savedBob) {
expect(savedBob[0].id).to.eql(bob.id)
})
})
it('can insert empty row with uniqueidentifier and get id correctly', function () {
var db = sworm.db(config)
var person = db.model({table: 'people_uuid', generatedId: 'output'});
var bob = person({})
return bob.save().then(function () {
return person.query('select * from people_uuid where id = @id', {id: bob.identity()})
}).then(function (savedBob) {
expect(savedBob[0].id).to.eql(bob.id)
})
})
})
});
}