An internal class that the user is not expected to instantiate directly. Instances are created by patio.Database#createTable. It is used to specify table creation parameters. It takes a Database object and a block of column/index/constraint specifications, and gives the Database a table description, which the database uses to create a table.

patio.SchemaGenerator has some methods but also includes method_missing, allowing users to specify column type as a method instead of using the column method, which makes for a cleaner code.

Example
  1. comb.executeInOrder(DB, function(DB){
  2. DB.createTable("airplane_type", function () {
  3. this.primaryKey("id");
  4. this.name(String, {allowNull:false});
  5. this.max_seats(Number, {size:3, allowNull:false});
  6. this.company(String, {allowNull:false});
  7. });
  8. DB.createTable("airplane", function () {
  9. this.primaryKey("id");
  10. this.total_no_of_seats(Number, {size:3, allowNull:false});
  11. this.foreignKey("typeId", "airplane_type", {key:"id"});
  12. });
  13. DB.createTable("flight_leg", function () {
  14. this.primaryKey("id");
  15. this.scheduled_departure_time("time");
  16. this.scheduled_arrival_time("time");
  17. this.foreignKey("departure_code", "airport", {key:"airport_code", type : String, size : 4});
  18. this.foreignKey("arrival_code", "airport", {key:"airport_code", type : String, size : 4});
  19. this.foreignKey("flight_id", "flight", {key:"id"});
  20. });
  21. DB.createTable("leg_instance", function () {
  22. this.primaryKey("id");
  23. this.date("date");
  24. this.arr_time("datetime");
  25. this.dep_time("datetime");
  26. this.foreignKey("airplane_id", "airplane", {key:"id"});
  27. this.foreignKey("flight_leg_id", "flight_leg", {key:"id"});
  28. });
  29. });

Constructor

Defined database/schemaGenerators.js Source
  1. function (db){
  2. this.db = db;
  3. this.columns = [];
  4. this.indexes = [];
  5. this.constraints = [];
  6. this.__primaryKey = null;
  7. }

__compositeForeignKey Function Private


Defined database/schemaGenerators.js

Arguments Source
  1. function (columns,opts){
  2. this.constraints.push(merge({type:"foreignKey", columns:columns}, opts));
  3. }

__compositePrimaryKey Function Private


Defined database/schemaGenerators.js

Arguments Source
  1. function (columns){
  2. var args = argsToArray(arguments, 1);
  3. var opts = args.pop() || {};
  4. this.constraints.push(merge({type:"primaryKey", columns:columns}, opts));
  5. }

check Function Public


Defined database/schemaGenerators.js

Add an unnamed constraint to the DDL, specified by the given block or args:

Example
  1. db.createTable("test", function(){
  2. this.check({num : {between : [1,5]}})
  3. //=> CHECK num >= 1 AND num <= 5
  4. this.check(function(){return this.num.gt(5);});
  5. //=> CHECK num > 5
  6. });
Source
  1. function (){
  2. return this.constraint.apply(this, [null].concat(argsToArray(arguments)));
  3. }

column Function Public


Defined database/schemaGenerators.js

Add a column with the given name, type, and opts to the DDL.

 DB.createTable("test", function(){
     this.column("num", "integer");
         //=> num INTEGER
     this.column("name", String, {allowNull : false, "default" : "a");
         //=> name varchar(255) NOT NULL DEFAULT 'a'
     this.column("ip", "inet");
         //=> ip inet
  });

You can also create columns via method missing, so the following are equivalent:

DB.createTable("test", function(){
  this.column("number", "integer");
  this.number("integer");
});

Arguments Source
  1. function (name,type,opts){
  2. opts = opts || {};
  3. this.columns.push(merge({name:name, type:type}, opts));
  4. if (opts.index) {
  5. this.index(name);
  6. }
  7. }

constraint Function Public


Defined database/schemaGenerators.js

Adds a named constraint (or unnamed if name is nil) to the DDL, with the given block or args.

Example
  1. DB.createTable("test", function(){
  2. this.constraint("blah", {num : {between : [1,5]}})
  3. //=> CONSTRAINT blah CHECK num >= 1 AND num <= 5
  4. this.check("foo", function(){
  5. return this.num.gt(5);
  6. }); # CONSTRAINT foo CHECK num > 5
  7. });
Arguments Source
  1. function (name,args){
  2. args = argsToArray(arguments).slice(1);
  3. var block = isFunction(args[args.length - 1]) ? args.pop : null;
  4. this.constraints.push({name:name, type:"check", check:block || args});
  5. }

foreignKey Function Public


Defined database/schemaGenerators.js

Add a foreign key in the table that references another table to the DDL. See {@link patio.SchemaGenerator#column{ for options.

Example
  1. DB.createTable("flight_leg", function () {
  2. this.primaryKey("id");
  3. this.scheduled_departure_time("time");
  4. this.scheduled_arrival_time("time");
  5. this.foreignKey("departure_code", "airport", {key:"airport_code", type : String, size : 4});
  6. this.foreignKey("arrival_code", "airport", {key:"airport_code", type : String, size : 4});
  7. this.foreignKey("flight_id", "flight", {key:"id"});
  8. });
Arguments Source
  1. function (name,table,opts){
  2. opts = opts || {};
  3. opts = isHash(table) ? merge({}, table, opts) : isString(table) ? merge({table:table}, opts) : opts;
  4. if (isArray(name)) {
  5. return this.__compositeForeignKey(name, opts);
  6. } else {
  7. return this.column(name, "integer", opts);
  8. }
  9. }

fullTextIndex Function Public


Defined database/schemaGenerators.js

Add a full text index on the given columns to the DDL.

Example
  1. DB.createTable("posts", function () {
  2. this.title("text");
  3. this.body("text");
  4. this.fullTextIndex("title");
  5. this.fullTextIndex(["title", "body"]);
  6. });
Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. return this.index(columns, merge({type:"fullText"}, opts));
  4. }

hasColumn Function Public


Defined database/schemaGenerators.js

Check if the DDL includes the creation of a column with the given name.

Arguments Returns Source
  1. function (name){
  2. return this.columns.some(function (c) {
  3. return c.name === name;
  4. });
  5. }

index Function Public


Defined database/schemaGenerators.js

Add an index on the given column(s) with the given options to the DDL. The available options are:

Example
  1. DB.createTable("test", function(table) {
  2. table.primaryKey("id", "integer", {null : false});
  3. table.column("name", "text");
  4. table.index("name", {unique : true});
  5. });
Arguments Source
  1. function (columns,opts){
  2. this.indexes.push(merge({columns:toArray(columns)}, opts || {}));
  3. }

primaryKey Function Public


Defined database/schemaGenerators.js

Adds an auto-incrementing primary key column or a primary key constraint to the DDL. To create a constraint, the first argument should be an array of columns specifying the primary key columns. To create an auto-incrementing primary key column, a single column can be used. In both cases, an options hash can be used as the second argument.

If you want to create a primary key column that is not auto-incrementing, you should not use this method. Instead, you should use the regular patio.SchemaGenerator#column method with a {primaryKey : true} option.

Example
  1. db.createTable("airplane_type", function () {
  2. this.primaryKey("id");
  3. //=> id integer NOT NULL PRIMARY KEY AUTOINCREMENT
  4. this.name(String, {allowNull:false});
  5. this.max_seats(Number, {size:3, allowNull:false});
  6. this.company(String, {allowNull:false});
  7. });
Arguments Source
  1. function (name){
  2. if (isArray(name)) {
  3. return this.__compositePrimaryKey.apply(this, arguments);
  4. } else {
  5. var args = argsToArray(arguments, 1), type;
  6. var opts = args.pop();
  7. this.__primaryKey = merge({}, this.db.serialPrimaryKeyOptions, {name:name}, opts);
  8. if (isDefined((type = args.pop()))) {
  9. merge(opts, {type:type});
  10. }
  11. merge(this.__primaryKey, opts);
  12. return this.__primaryKey;
  13. }
  14. }

spatialIndex Function Public


Defined database/schemaGenerators.js

Add a spatial index on the given columns to the DDL.

Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. return this.index(columns, merge({type:"spatial"}, opts));
  4. }

unique Function Public


Defined database/schemaGenerators.js

Add a unique constraint on the given columns to the DDL. See patio.SchemaGenerator#constraint for argument types.

Example
  1. DB.createTable("test", function(){
  2. this.unique("name");
  3. //=> UNIQUE (name)
  4. });
Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. this.constraints.push(merge({type:"unique", columns:toArray(columns)}, opts));
  4. }

Documentation generated using coddoc.