Classes
Members
External
Db
Kind: global class
- Db
- .statics
- .methods
.setStatic.setMethod- .configure([config])
- .sync([options]) ⇒
Promise - .define(name, core, [options]) ⇒
Model - .get(name) ⇒
Model - .end() ⇒
Promise - .withConnection(fn) ⇒
Promise - .withTx(fn) ⇒
Promise - .query(query, [options]) ⇒
Promise - .create([config]) ⇒
Db
db.statics
Add a static method global to all models
Kind: instance property of Db
db.methods
Add a method global to all model instances
Kind: instance property of Db
db.setStatic
Deprecated
Add a static method global to all models
Kind: instance property of Db
| Param | Type | Description |
|---|---|---|
| name | string |
Name of the method |
| fn | function |
The method |
db.setMethod
Deprecated
Add a method global to all model instances
Kind: instance property of Db
| Param | Type | Description |
|---|---|---|
| name | string |
Name of the method |
| fn | function |
The method |
db.configure([config])
Configure the mysql handler by creating the connection pool. Of course you cannot query the database if ziti is not configured.
Kind: instance method of Db
See: Node MySQL connection options
| Param | Type | Default | Description |
|---|---|---|---|
| [config] | Object |
The MySQL config | |
| [config.host] | string |
"localhost" |
The hostname of the database you are connecting to |
| [config.port] | Number |
3306 |
The port number to connect to |
| [config.user] | string |
The MySQL user to authenticate as | |
| [config.password] | string |
The password of that MySQL user | |
| [config.database] | string |
The database to use for the connection |
Example
var ziti = require('ziti');
// You can define models before configuring ziti
var User = ziti.define('User', {
name: ziti.String,
photos: [ 'Photo' ]
});
ziti.configure({ host: 'localhost', user: 'root', database: 'test' });
// Or after
var Photo = ziti.define('Photo', {
path: ziti.String().unique()
});
db.sync([options]) ⇒ Promise
Synchronizes models with the database
Kind: instance method of Db
Resolve: Object
| Param | Type | Default | Description |
|---|---|---|---|
| [options] | Object |
||
| [options.autoCreateModels] | boolean |
true |
Create models automatically when used as a reference dependency (useful for many to many relationships) |
| [options.dropTable] | boolean |
false |
Drop all model tables |
| [options.createTable] | boolean |
true |
Create all model tables |
| [options.autoMigrate.addColumns] | boolean |
true |
Add missing table columns |
| [options.autoMigrate.delColumns] | boolean |
false |
Remove missing table columns |
Example
ziti.sync({ dropTable: true, createTable: true, autoMigrate: false })
.then(function (models) {
models.forEach(function (modelName) {
console.log('* %s Model has been synchronized', modelName);
});
});
// * User Model has been synchronized
// * Photo Model has been synchronized
db.define(name, core, [options]) ⇒ Model
Create a new Model
Kind: instance method of Db
| Param | Type | Default | Description |
|---|---|---|---|
| name | string |
The name of the model | |
| core | Object |
core of the model in the form { fieldName: fieldType } | |
| [options] | Object |
||
| [options.createTable] | boolean |
true |
Create the table during synchronization |
| [options.tableName] | string |
Name of the table, by default the name of the model in snakeCase | |
| [options.engine] | string |
"InnoDB" |
Engine to use when creating the table |
| [options.autoId] | string | boolean |
true |
Create a primary key auto increment 'id' pr using the provided field name |
Example
var User = ziti.define('User', {
email: ziti.String().unique().notNull(),
password: ziti.Varbinary(16).notNull(),
age: ziti.Int,
fullname: ziti.String,
photos: [ Photo ]
}, { autoId: 'user_id', engine: 'InnoDb', tableName: 'user' });
db.get(name) ⇒ Model
Get a Model from its name
Kind: instance method of Db
| Param | Type | Description |
|---|---|---|
| name | string |
Name of the model |
db.end() ⇒ Promise
End all connections
Kind: instance method of Db
db.withConnection(fn) ⇒ Promise
Provide a connection from the pool to the callback
Kind: instance method of Db
Resolve: PoolConnection
| Param | Type | Description |
|---|---|---|
| fn | function |
The callback |
db.withTx(fn) ⇒ Promise
Provide a transaction from the pool to the callback starting a transaction. It commits the change when the callback returns. If something goes wrong in the callback (exception or a promise rejected), it rollbacks instead.
Kind: instance method of Db
Resolve: PoolConnection
| Param | Type | Description |
|---|---|---|
| fn | function |
The callback |
Example
ziti.withTx(function (tx) {
return ziti.query('INSERT INTO Animal SET `type` = \'cat\'', { using: tx })
.then(function () {
return ziti.query('INSERT INTO Animal SET `type` = \'dog\'', { using: tx })
});
}).then(function () {
console.log('Animals have been inserted!');
});
// START TRANSACTION
// INSERT INTO Animal SET `type` = 'cat'
// INSERT INTO Animal SET `type` = 'dog'
// COMMIT
// Animals have been inserted!
db.query(query, [options]) ⇒ Promise
Send a SQL query
Kind: instance method of Db
Resolve: (Array.<Object>\|Object)
| Param | Type | Description |
|---|---|---|
| query | Array.<*> | Object | string |
The query |
| query.sql | string |
A query with parameters defined as ? |
| query.values | Array.<*> |
An array of values to pass |
| [options] | Object |
|
| [options.using] | PoolConnection |
Use this connection |
Example
ziti.query({ sql: 'SELECT ?? FROM ?? WHERE ?? = ?', values: [ 'id', 'user', 'id', 42 ] });
// SELECT `id` FROM `user` WHERE `id` = 42
ziti.query({ sql: 'INSERT INTO ?? SET ?', values: [ 'user', { name: 'Heisenberg', age: 42 } ] });
// INSERT INTO `user` SET `name` = 'Heisenberg', `age` = 42
ziti.query('SELECT id FROM `user` WHERE `id` = 42');
// SELECT `id` FROM `user` WHERE `id` = 42
ziti.query([ 'SELECT ?? FROM ?? WHERE ?? = ?', 'id', 'user', 'id', 42 ]);
// SELECT `id` FROM `user` WHERE `id` = 42
db.create([config]) ⇒ Db
Create a new connection.
This is helpful when dealing with multiple databases.
Note that you cannot use a reference (one, many or foreignkey) to a Model from another database.
Kind: instance method of Db
See: Node MySQL connection options
| Param | Type | Default | Description |
|---|---|---|---|
| [config] | Object |
The MySQL config | |
| [config.host] | string |
"localhost" |
The hostname of the database you are connecting to |
| [config.port] | Number |
3306 |
The port number to connect to |
| [config.user] | string |
The MySQL user to authenticate as | |
| [config.password] | string |
The password of that MySQL user | |
| [config.database] | string |
The database to use for the connection |
Example
var db = ziti.create({ host: 'localhost', user: 'root', database: 'test' });
var User = db.define('User', {
name: ziti.String
});
ziti : Db
Kind: global variable
Mixes: Types, Functions
Types
SQL types
Kind: global external
See: Types
Functions
SQL Functions
Kind: global external
See: Functions