chenos c2ff7882bc
feat: database next (#130)
* FIX: database test with sqlite

* more types

* filter test

* split filter parser

* filter test

* filter test: hasMany

* define inverse association for belongsTo & hasMany

* chore: console.log

* repository count method

* chore: Collection

* repository filter & appends & fields & expect

* repository: sort option

* chore: test

* add: test

* find & findAndCount

* chore: test

* database-next: update guard

* database-next: update guard associationKeysToBeUpdate

* chore: comment

* update-guard OneToOne Association

* has one repository

* support through table value

* belongs to many repository

* has many repository

* has many repository find

* fix: has many find and count

* clean code

* add count method

* chore: multiple relation

* chore: single relation

* repository find

* relation repository builder

* repository count

* repository count test

* fix test

* close db afterEach test

* sort with associations

* repository update

* has many repository: destroy

* belongs to many repository: destroy

* add transaction decorator

* belongs to many with transaction

* has many with transaction

* clean types

* clean types

* clean types

* repository transaction

* fix test

* single relation repository with transaction

* single relation repository with transaction

* fix: test

* fix: option parser fields append

* fix: typo

* fix: string type

* fix: import

* collection field methods

* cleanup

* collection sync

* fix: import

* fix: test

* collection update field

* collection update options

* database hook

* database test

* database event test

* update database event

* add async emmit mixin

* async model event

* database import

* fix: model hook type

* fix: collection event

* recall model.init on collection update

* skip redefine collection test

* skip collection model update

* add model hook class

* global model event support

* chore

* chore

* change utils import

* add field types

* database import

* more import test

* test case

* fix: through model init...

* bugfix

* fix

* update database import

* collection sync by foreachModel

* fix collection model sync

* update

* add field types

* custom operator

* sqlite array field

* postgresql array field

* array query escape

* mysql array operators

* date operators

* array field sqlite fix

* association operator

* date operator empty & notEmpty

* fix: fields import

* fix array field nested association

* filter parse prepare

* fix test

* string field empty

* add date operator test

* field option types

* fix typo

* fix: operator name conflict

* rename function

Co-authored-by: Chareice <chareice@live.com>
2021-12-06 21:12:54 +08:00

78 lines
1.8 KiB
TypeScript

import { Database } from '../../database';
import { mockDatabase } from '../';
describe('string field', () => {
let db: Database;
beforeEach(() => {
db = mockDatabase();
});
afterEach(async () => {
await db.close();
});
it('define', async () => {
const Test = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name' }],
});
await db.sync();
expect(Test.model.rawAttributes['name']).toBeDefined();
const model = await Test.model.create({
name: 'abc',
});
expect(model.toJSON()).toMatchObject({
name: 'abc',
});
});
it('set', async () => {
const Test = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name1' }],
});
await db.sync();
Test.addField('name2', { type: 'string', name: 'name2' });
await db.sync({
alter: true,
});
expect(Test.model.rawAttributes['name1']).toBeDefined();
expect(Test.model.rawAttributes['name2']).toBeDefined();
const model = await Test.model.create({
name1: 'a1',
name2: 'a2',
});
expect(model.toJSON()).toMatchObject({
name1: 'a1',
name2: 'a2',
});
});
it('model hook', async () => {
const collection = db.collection({
name: 'tests',
fields: [{ type: 'string', name: 'name' }],
});
await db.sync();
collection.model.beforeCreate((model) => {
const changed = model.changed();
for (const name of changed || []) {
model.set(name, `${model.get(name)}111`);
}
});
collection.addField('name2', { type: 'string', name: 'name2' });
await db.sync({
alter: true,
});
const model = await collection.model.create({
name: 'n1',
name2: 'n2',
});
expect(model.toJSON()).toMatchObject({
name: 'n1111',
name2: 'n2111',
});
});
});