10tcl 中文文档教程

发布于 10年前 浏览 21 项目主页 更新于 3年前

What is it?

10tcl 是一个 CRUD 插件,它将向您的 express 应用程序添加默认路由,在您的 mongodb 中读取和存储数据。

How is it?

有用! …大多。 现在我不能推荐它。 在认真使用之前给它几个月。 它有一些局限性和误解,而且不会很快好转。

Install

$ npm install 10tcl

Config

配置文件必须包含:

module.exports = {
    // used as title for your pages
    brand: 'Day of the Tentacle IS HERE!',
    // used to assemble the connection string to your mongodb instance
    db: { 
        usr: 'Purple',
        pwd: 'Tentacle',
        srv: 'ds044356.mongolab.com:49842',
        db:  'day_of_the_tentacle',
        par: 'auto_reconnect'
    },
    // static admin user to allow first login
    admin: { usr: 'admin', pwd: 'ElGranSecreto', name: 'Admin', role: 'admin'}
    // __dirname of your root
    root: root, 
    // if not informed it defaults to 'root/models', 'root/controllers' and 'root/views'
    pathToCtrls: '/app/controllers',
    pathToModels: '/app/models',
    pathToViews: '/app/views',
    pathToValidator: 'app/models/validator'
}

Use

// __dirname will be the root to locate folders indicated in pathTo... properties
var app = require('10tcl').attack(__dirname, '/config/yourConfigFile')
app.listen(yourPortHere)

10tcl 攻击导致 express 应用程序配置了基于您的模型和控制器的路由。

Database

app.db 包含到您的数据库的连接。 它是通过使用 Mongoskin 和配置对象上的凭据完成的。

Controllers

您的控制器文件夹下的每个 .js 文件都是必需的,接收(应用程序、基础、配置)。

控制器文件的一个例子是:

module.exports = function(app, base, config){

    function hello(req, res){
        res.send('hello world')
    }

    app.get('/hello', base.auth, hello )

    // will be used to create a menu
    return {name: 'hello', label: 'Hello World'}

}

Models

模型文件夹下的每个 .js 文件也是必需的,没有参数。 每个都通过 model.name 属性绑定到 app.db,所以现在 app.db[modelNameHere] 指向一个能够进行 restfull crud 的集合。

模型的一个例子是:

module.exports = {
    // model and db collection name
    name: 'victims',
    // Title for pages and menu
    label: 'Victims',
    // used with mustache to format list and drop down descriptions 
    format: 'Victim {{name}} {{lastName}}',
    // fields to format and forms (see next topic)
    fields: [
        { name: 'name', label: 'Nome', type: 'string', checks: ['hasValue'] },
        { name: 'lastName', label: 'Sobrenome', type: 'string' }
    ],
    // create route '<your-site>.com/victims' pointing to 10tcl CRUD
    routeTo10tcl: true,
    // read the entire collection into the app, changes it before persisting
    keepInCache: true,
    // restrict access to users with one of these roles
    onlyFor: ['admin'],
    // used if you run 'node myApp.js mock'
    mock: [
        {name: 'Mussum', _id: '508e0077d42bd6182f000001'},
        {name: 'Zacarias', _id: '508e0077d42bd6182f000002'},
        {name: 'Dedé', lastname: 'Santana', _id: '508e0077d42bd6182f000003'},
        {name: 'Didi', lastname: 'Mocó', _id: '508e0077d42bd6182f000004'}
    ]
}

Field types

10tcl 理解:字符串、数字、电子邮件、电话、日期、html、文本、参考(类型:'reference',to:'criminal') 每个都会产生不同的 html 元素和布局。 与上述不同的字段类型将导致常规输入字段。

Validation

10tcl modelValidator 提供类型关联检查,在 POST/PUT 事务上自动执行。 除此之外,字段可以包含属性检查:['hasValue', …] checks 数组中的每个字符串都会触发 modelValidator 对应的函数。 定义 pathToValidator,您可以实现自己的验证器,而不是 10tcl 验证器。

Users

用户和配置文件是提供访问控制的预加载模型。 配置属性“admin”为首次登录提供原始用户。

i18n

不……对不起。 我正在 PT-BR 中输出静态文本。 对我试过的任何包裹都不满意。 想要将文本保存在缓存数据库中以供在线编辑和翻译。

CRUD

这些视图是用 bootstrap、jquery 和一些自制的绑定创建的(Angular 让我很生气)。 在移动设备中,它们更简单,由 jquerymobile 和 jquery 制作。 移动端和桌面端之间的决定是通过阅读请求标头来做出的。

What is it?

10tcl is a CRUD plugin that will add default routes to your express app, that read and store data in your mongodb.

How is it?

It works! …mostly. Right now I could not recommend it. Give it a few months before serious usage. It has some limitations and misconceptions, and it is not getting better soon.

Install

$ npm install 10tcl

Config

A config file must contain:

module.exports = {
    // used as title for your pages
    brand: 'Day of the Tentacle IS HERE!',
    // used to assemble the connection string to your mongodb instance
    db: { 
        usr: 'Purple',
        pwd: 'Tentacle',
        srv: 'ds044356.mongolab.com:49842',
        db:  'day_of_the_tentacle',
        par: 'auto_reconnect'
    },
    // static admin user to allow first login
    admin: { usr: 'admin', pwd: 'ElGranSecreto', name: 'Admin', role: 'admin'}
    // __dirname of your root
    root: root, 
    // if not informed it defaults to 'root/models', 'root/controllers' and 'root/views'
    pathToCtrls: '/app/controllers',
    pathToModels: '/app/models',
    pathToViews: '/app/views',
    pathToValidator: 'app/models/validator'
}

Use

// __dirname will be the root to locate folders indicated in pathTo... properties
var app = require('10tcl').attack(__dirname, '/config/yourConfigFile')
app.listen(yourPortHere)

10tcl attack results in an express app configured with routes based on your models and controllers.

Database

app.db contains a connection to your database. It was done by using Mongoskin and the credentials on the config object.

Controllers

Every .js file under your controllers folder was required, receiving (app, base, config).

An example of controller file would be:

module.exports = function(app, base, config){

    function hello(req, res){
        res.send('hello world')
    }

    app.get('/hello', base.auth, hello )

    // will be used to create a menu
    return {name: 'hello', label: 'Hello World'}

}

Models

Every .js file under your models folder was required as well, no arguments. Each binded to app.db by the model.name property, so now app.db[modelNameHere] points to a collection capable of restfull crud.

An example of model would be:

module.exports = {
    // model and db collection name
    name: 'victims',
    // Title for pages and menu
    label: 'Victims',
    // used with mustache to format list and drop down descriptions 
    format: 'Victim {{name}} {{lastName}}',
    // fields to format and forms (see next topic)
    fields: [
        { name: 'name', label: 'Nome', type: 'string', checks: ['hasValue'] },
        { name: 'lastName', label: 'Sobrenome', type: 'string' }
    ],
    // create route '<your-site>.com/victims' pointing to 10tcl CRUD
    routeTo10tcl: true,
    // read the entire collection into the app, changes it before persisting
    keepInCache: true,
    // restrict access to users with one of these roles
    onlyFor: ['admin'],
    // used if you run 'node myApp.js mock'
    mock: [
        {name: 'Mussum', _id: '508e0077d42bd6182f000001'},
        {name: 'Zacarias', _id: '508e0077d42bd6182f000002'},
        {name: 'Dedé', lastname: 'Santana', _id: '508e0077d42bd6182f000003'},
        {name: 'Didi', lastname: 'Mocó', _id: '508e0077d42bd6182f000004'}
    ]
}

Field types

10tcl understands: string, number, email, tel, date, html, text, reference (type: 'reference', to: 'criminal') Each will result in a different html element and layout. A field type different from the above will result in a regular input field.

Validation

10tcl modelValidator provides type associated checks, that are automatically executed on POST/PUT transactions. Other than that, a field can contain a property checks: ['hasValue', …] Each string in checks array will trigger the correspondent function of the modelValidator. Defining pathToValidator, you can implement your own validator that will be required instead of the 10tcl one.

Users

User and Profile are pre-loaded models that provide access control. The config property "admin" provides a proto user for the first login.

i18n

Nop… sorry. And I'm outputting static texts in PT-BR. Not happy with any package I've tried. Want to keep texts in a cached db for online editing and translation.

CRUD

The views are made with bootstrap, jquery, and some home-made binding (Angular was just making me angry). In mobile they are much simpler, made with jquerymobile and jquery. The decision between mobile and desktop is made by reading request headers.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文