@parsrun/entity 0.1.37 → 0.1.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/define.test.ts +112 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parsrun/entity",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.38",
|
|
4
4
|
"description": "Single-source entity definitions for Pars - generates ArkType schemas and Drizzle tables",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"arktype": "^2.0.0",
|
|
29
|
-
"@parsrun/core": "0.1.
|
|
29
|
+
"@parsrun/core": "0.1.38"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"drizzle-orm": "^0.44.0",
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { defineEntity, ref, enumField, jsonField, decimal } from './define.js'
|
|
3
|
+
|
|
4
|
+
describe('defineEntity', () => {
|
|
5
|
+
it('should create entity with basic fields', () => {
|
|
6
|
+
const Product = defineEntity({
|
|
7
|
+
name: 'products',
|
|
8
|
+
fields: {
|
|
9
|
+
name: 'string',
|
|
10
|
+
price: 'number',
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
expect(Product.name).toBe('products')
|
|
15
|
+
expect(Product.requiredFields).toContain('name')
|
|
16
|
+
expect(Product.requiredFields).toContain('price')
|
|
17
|
+
expect(Product.autoFields).toContain('id')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should handle tenant-scoped entities', () => {
|
|
21
|
+
const Order = defineEntity({
|
|
22
|
+
name: 'orders',
|
|
23
|
+
tenant: true,
|
|
24
|
+
fields: {
|
|
25
|
+
total: 'number',
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
expect(Order.requiredFields).toContain('tenantId')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should handle timestamps', () => {
|
|
33
|
+
const Post = defineEntity({
|
|
34
|
+
name: 'posts',
|
|
35
|
+
timestamps: true,
|
|
36
|
+
fields: {
|
|
37
|
+
title: 'string',
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
expect(Post.autoFields).toContain('insertedAt')
|
|
42
|
+
expect(Post.autoFields).toContain('updatedAt')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should handle soft delete', () => {
|
|
46
|
+
const User = defineEntity({
|
|
47
|
+
name: 'users',
|
|
48
|
+
softDelete: true,
|
|
49
|
+
fields: {
|
|
50
|
+
email: 'string.email',
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
expect(User.autoFields).toContain('deletedAt')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('should handle optional fields', () => {
|
|
58
|
+
const Profile = defineEntity({
|
|
59
|
+
name: 'profiles',
|
|
60
|
+
fields: {
|
|
61
|
+
bio: { type: 'string', optional: true },
|
|
62
|
+
age: 'number',
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
expect(Profile.optionalFields).toContain('bio')
|
|
67
|
+
expect(Profile.requiredFields).toContain('age')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should handle fields with defaults', () => {
|
|
71
|
+
const Settings = defineEntity({
|
|
72
|
+
name: 'settings',
|
|
73
|
+
fields: {
|
|
74
|
+
theme: { type: 'string', default: 'light' },
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
expect(Settings.optionalFields).toContain('theme')
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
describe('helper functions', () => {
|
|
83
|
+
it('ref should create a reference field', () => {
|
|
84
|
+
const field = ref('users', { onDelete: 'cascade' })
|
|
85
|
+
|
|
86
|
+
expect(field.type).toBe('string.uuid')
|
|
87
|
+
expect(field.db?.references?.entity).toBe('users')
|
|
88
|
+
expect(field.db?.references?.onDelete).toBe('cascade')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('enumField should create union type', () => {
|
|
92
|
+
const field = enumField(['draft', 'published', 'archived'] as const)
|
|
93
|
+
|
|
94
|
+
expect(field.type).toBe("'draft' | 'published' | 'archived'")
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('jsonField should create json field', () => {
|
|
98
|
+
const field = jsonField({ optional: true })
|
|
99
|
+
|
|
100
|
+
expect(field.type).toBe('json')
|
|
101
|
+
expect(field.optional).toBe(true)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('decimal should create decimal field', () => {
|
|
105
|
+
const field = decimal(10, 2, { min: 0 })
|
|
106
|
+
|
|
107
|
+
expect(field.type).toBe('number')
|
|
108
|
+
expect(field.db?.precision).toBe(10)
|
|
109
|
+
expect(field.db?.scale).toBe(2)
|
|
110
|
+
expect(field.min).toBe(0)
|
|
111
|
+
})
|
|
112
|
+
})
|