feat(database): improve text field

This commit is contained in:
chenos 2024-04-20 22:01:09 +08:00
parent 65b1e7c5a5
commit c26e43a34f
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,47 @@
import { mockDatabase } from '../';
import { Database } from '../../database';
describe('text field', () => {
let db: Database;
beforeEach(async () => {
db = mockDatabase();
await db.clean({ drop: true });
});
afterEach(async () => {
await db.close();
});
it('should create text field type', async () => {
const Test = db.collection({
name: 'tests',
fields: [
{
type: 'text',
name: 'text1',
defaultValue: 'a',
},
{
type: 'text',
name: 'text2',
length: 'tiny',
defaultValue: 'a',
},
{
type: 'text',
name: 'text3',
length: 'medium',
defaultValue: 'a',
},
{
type: 'text',
name: 'text4',
length: 'long',
defaultValue: 'a',
},
],
});
await Test.sync();
});
});

View File

@ -3,10 +3,20 @@ import { BaseColumnFieldOptions, Field } from './field';
export class TextField extends Field {
get dataType() {
if (this.database.inDialect('mysql', 'mariadb') && this.options.length) {
return DataTypes.TEXT(this.options.length);
}
return DataTypes.TEXT;
}
init() {
if (this.database.inDialect('mysql', 'mariadb')) {
this.options.defaultValue = null;
}
}
}
export interface TextFieldOptions extends BaseColumnFieldOptions {
type: 'text';
length?: 'tiny' | 'medium' | 'long';
}