Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee811e0
commit a4da850
Showing
7 changed files
with
96 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ reopen } = require("../core").Util | ||
InsertBuilder = require "../sql/insert_builder" | ||
Connection = require "../db/connection" | ||
|
||
module.exports = (Table) -> | ||
|
||
reopen Table, -> | ||
toInsertSql: (args...) -> | ||
(new InsertBuilder).visit(this, args...).toSql() | ||
|
||
create: (args..., f) -> | ||
Connection.query(@toInsertSql(args...), f) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ Monarch, async, pg } = require "../spec_helper" | ||
|
||
describe "Relations.Table", -> | ||
class Blog extends Monarch.Record | ||
@extended(this) | ||
@columns | ||
public: 'boolean' | ||
title: 'string' | ||
authorId: 'integer' | ||
|
||
blogs = Blog.table | ||
|
||
beforeEach (done) -> | ||
Monarch.Db.Connection.query("TRUNCATE TABLE blogs;", done) | ||
|
||
describe "#create", -> | ||
describe "when a single attribute hash is passed", -> | ||
it "inserts a record with the given attributes", (done) -> | ||
blogs.create { public: true, title: "New Blog 1", authorId: 11 }, -> | ||
blogs.find { title: "New Blog 1" }, (err, record) -> | ||
expect(record.public()).toBeTruthy() | ||
expect(record.authorId()).toBe(11) | ||
done() | ||
|
||
describe "when an array of attribute hashes is passed", -> | ||
it "inserts a record with each set of attributes", (done) -> | ||
hashes = [ | ||
{ id: 1, public: true, title: "New Blog 1", authorId: 11 } | ||
{ id: 2, public: true, title: "New Blog 2", authorId: 12 } | ||
] | ||
|
||
blogs.create hashes, -> | ||
blogs.all (err, records) -> | ||
expect(records).toEqualRecords(Blog, [ | ||
{ id: 1, public: true, title: "New Blog 1", authorId: 11 } | ||
{ id: 2, public: true, title: "New Blog 2", authorId: 12 } | ||
]) | ||
done() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters