Merge pull request #2307 from automatisch/create-folder-model

feat: Create folder model
This commit is contained in:
Ömer Faruk Aydın
2025-01-24 12:14:36 +01:00
committed by GitHub
4 changed files with 101 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
export async function up(knex) {
return knex.schema.createTable('folders', (table) => {
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'));
table.string('name');
table.uuid('user_id').references('id').inTable('users');
table.timestamps(true, true);
});
}
export async function down(knex) {
return knex.schema.dropTable('folders');
}

View File

@@ -0,0 +1,27 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Folder model > jsonSchema should have correct validations 1`] = `
{
"properties": {
"createdAt": {
"type": "string",
},
"id": {
"format": "uuid",
"type": "string",
},
"name": {
"minLength": 1,
"type": "string",
},
"updatedAt": {
"type": "string",
},
"userId": {
"format": "uuid",
"type": "string",
},
},
"type": "object",
}
`;

View File

@@ -0,0 +1,30 @@
import Base from './base.js';
import User from './user.js';
class Folder extends Base {
static tableName = 'folders';
static jsonSchema = {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
name: { type: 'string', minLength: 1 },
userId: { type: 'string', format: 'uuid' },
createdAt: { type: 'string' },
updatedAt: { type: 'string' },
},
};
static relationMappings = () => ({
user: {
relation: Base.BelongsToOneRelation,
modelClass: User,
join: {
from: 'folders.user_id',
to: 'users.id',
},
},
});
}
export default Folder;

View File

@@ -0,0 +1,31 @@
import { describe, it, expect } from 'vitest';
import Folder from './folder';
import User from './user';
import Base from './base';
describe('Folder model', () => {
it('tableName should return correct name', () => {
expect(Folder.tableName).toBe('folders');
});
it('jsonSchema should have correct validations', () => {
expect(Folder.jsonSchema).toMatchSnapshot();
});
it('relationMappings should return correct associations', () => {
const relationMappings = Folder.relationMappings();
const expectedRelations = {
user: {
relation: Base.BelongsToOneRelation,
modelClass: User,
join: {
from: 'folders.user_id',
to: 'users.id',
},
},
};
expect(relationMappings).toStrictEqual(expectedRelations);
});
});