orm2 中文文档 3.1 模型属性

译者:飞龙

来源:Model Properties

模型和一些关联具有一个或多个属性,每个属性有类型以及一些可选设置,你可以自行选择它们(或使用默认设置)。

类型

受支持的类型是:

  • text:文本字符串;
  • number:浮点数。你可以指定size2 | 4 | 8
  • integer:整数。你可以指定size2 | 4 | 8
  • booleantruefalse的值;
  • date:日期对象。你可以指定timetrue
  • enum:一个备选列表中的值;
  • object:JSON对象;
  • point:N维的点(不被广泛支持);
  • binary:二进制数据;
  • serial:自增长的整数,用于主键。

每个类型都有额外的选项。这个模型定义使用了它们中的绝大多数:

var Person = db.define("person", {
    name    : { type: "text", size: 50 },
    surname : { type: "text", defaultValue: "Doe" },
    male    : { type: "boolean" },
    vat     : { type: "integer", unique: true },
    country : { type: "enum", values: [ "USA", "Canada", "Rest of the World" ] },
    birth   : { type: "date", time: false }
});

所有类型都支持required(布尔值),unique(布尔值)和defaultValue(文本)。文本类型也支持最大尺寸(数值)和big(布尔值,用于非常长的字符串)。数值类型是浮点数,支持size(数值,字节大小)和unsigned(布尔值)。日期类型支持time(布尔值)。

要注意8字节的数值有其局限性

如果你打算用默认选项,你可以使用原生类型来指定属性类型:

var Person = db.define("person", {
    name    : String,
    male    : Boolean,
    vat     : Number, // FLOAT
    birth   : Date,
    country : [ "USA", "Canada", "Rest of the World" ],
    meta    : Object, // JSON
    photo   : Buffer  // binary
});

将ORM字段映射到不同名称的数据库列中

var Person = db.define("person", {
    name    : { type: 'text', mapsTo: 'fullname' }
});

ORM属性name映射person表的fullname列。

自定义类型

你可以向ORM添加你自己的类型,像这样:

db.defineType('numberArray', {
  datastoreType: function(prop) {
    return 'TEXT'
  },
  // This is optional
  valueToProperty: function(value, prop) {
    if (Array.isArray(value)) {
      return value;
    } else {
      return value.split(',').map(function (v) {
        return Number(v);
      });
    }
  },
  // This is also optional
  propertyToValue: function(value, prop) {
    return value.join(',')
  }
});
var LottoTicket = db.define('lotto_ticket', {
  numbers: { type: 'numberArray' }
});

一些可用的高级自定义类型,能够让你像 PostGIS 那样使用模型。请见这个 spec

展开阅读全文

使用Gorm插入和选择PostGIS Geometry

02-09
<div class="post-text" itemprop="text"> <p>I've been trying to find a way to insert and retrieve geometric types using Golang, and specifically the library <a href="http://gorm.io/docs/" rel="noreferrer">gorm</a>. I'm also attempting to use the library <a href="https://github.com/paulmach/orb" rel="noreferrer">orb</a> that defines different types for geometries, and provides encoding/decoding between different formats.</p> <p>Orb has <code>Scan()</code> and <code>Value()</code> methods already implemented for each type. This allows go's <code>Insert()</code> and <code>Scan()</code> functions to work with types other than primitives. Orb expects however to be using geometry represented in the well-known binary (WKB) format.</p> <p>The orb documentation shows that to accomplish this, you should simply wrap the field in the PostGIS functions <code>ST_AsBinary()</code> and <code>ST_GeomFromWKB()</code> for querying and inserting respectively. For example, with a table defined as:</p> <pre><code>_, err = db.Exec(` CREATE TABLE IF NOT EXISTS orbtest ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, geom geometry(POLYGON, 4326) NOT NULL ); `) </code></pre> <p>You can just do:</p> <pre><code>rows, err := db.Query("SELECT id, name, ST_AsBinary(geom) FROM orbtest LIMIT 1") </code></pre> <p>And for insert (where p is an orb.Point):</p> <pre><code>db.Exec("INSERT INTO orbtest (id, name, geom) VALUES ($1, $2, ST_GeomFromWKB($3))", 1, "Test", wkb.Value(p)) </code></pre> <p>Here's my issue: By using GORM, I don't have the luxury of being able to build those queries with those functions. GORM will automatically insert values into the database given a struct, and will scan in data into the whole hierarchy of the struct. Those <code>Scan()</code> and <code>Value()</code> methods are called behind the scenes, without my control.</p> <p>Trying to directly insert binary data into a geometry column won't work, and directly querying a geometry column will give the result in hex.</p> <p>I've tried multiple database approaches to solve this. I've attempted creating views that automatically call the needed functions on the geometry columns. This worked for querying, but not inserting.</p> <p>Is it possible to make some sort of trigger or rule that would automatically call the needed functions on the data coming in/out?</p> <p>I should also note that the library I'm working on works completely independent of the data and schemas, so I don't have the luxury of hard coding any sort of query. I could of course write a function that scans the entire data model, and generates queries from scratch, but I'd prefer if there was a better option.</p> <p>Does anyone know of a way of making this work in SQL? Being able to call functions on a column automatically by just querying the column itself?</p> <p>Any advice would be greatly appreciated.</p> </div>
©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值