manyuanrong / dso

Simple Orm library for Deno based on deno_mysql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When create table, how about changing sequence base modelfield(created, updated)?

magichim opened this issue · comments

When I create table, I often place time column(like create_at, deleted_at..etc)at the end of table

example)
--------------------------------------------------
id | name | updated_at | created_at | deleted_at ....
--------------------------------------------------

current default)
--------------------------------------------------
| updated_at | created_at | id | name|deleted_at ....
--------------------------------------------------

How about changing column sequence as example style?

That has to separate the preset values, always through concat at the end
https://github.com/manyuanrong/dso/blob/master/src/model.ts#L46-L64

I know base field(create_at, updated_at) are preset values. but when I execute sync some my code, I get log like this.

[ DSO:SYNC ]
SQL:	 CREATE TABLE IF NOT EXISTS Album ( updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, id INT(11) AUTO_INCREMENT, name VARCHAR(32) NOT NULL, deleted_at TIMESTAMP NULL DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You can see updated_at, created_at is declared begin of query.

I mean, how about changing base field is moved at end of query.

Thanks to read. :)

Because created_at is preset to the front of the modelFields array, the array will be traversed when synchronizing the database table structure, so it will be declared first.
But this does not affect the use, you can manually move the order of the fields in the database management software.
Alternatively, you can change the modelFields getter of model.ts

get modelFields(): FieldOptions[] {
    const presets = [
        {
          type: FieldType.DATE,
          default: Defaults.CURRENT_TIMESTAMP,
          autoUpdate: true,
          name: "updated_at",
          property: "updated_at"
        },
        {
          type: FieldType.DATE,
          default: Defaults.CURRENT_TIMESTAMP,
          name: "created_at",
          property: "created_at"
        }
      ];
    return (
      Reflect.getMetadata("model:fields", this) || []
    ).concat(presets);// Please note that duplicates need to be removed here
  }

Oh I see, I sufficiently understand your meaning.
I just suggest field sequence. I think timestamp normally column(created_at...etc) are arranged in end of query. There is no perfect rule in making db table. :)

Anyway thanks to reply. I just will change sequence by myself.

thank you.