json-rest-api 1.0.7 → 1.0.8
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/.claude/agents/library-guide-expert.md +29 -0
- package/.claude/agents/rest-api-internals-expert.md +53 -0
- package/CLAUDE.md +139 -1
- package/README.md +2 -5
- package/docTest.js +238 -0
- package/docs/GUIDE/GUIDE_1_Initial_Setup.md +955 -0
- package/docs/GUIDE/GUIDE_2_1_The_Starting_Point.md +54 -0
- package/docs/GUIDE/GUIDE_2_2_Manipulating_And_Searching_Tables.md +750 -0
- package/docs/GUIDE/GUIDE_2_3_BelongsTo_Relationships.md +494 -0
- package/docs/GUIDE/GUIDE_2_4_HasMany_Records.md +398 -0
- package/docs/GUIDE/GUIDE_2_5_HasMany_Polymorphic.md +882 -0
- package/docs/GUIDE/GUIDE_2_6_Many_To_Many.md +709 -0
- package/docs/GUIDE/GUIDE_2_7_Pagination_And_Ordering.md +1233 -0
- package/docs/GUIDE/GUIDE_2_8_Effects_of_PUT_and_PATCH.md +796 -0
- package/{README_X_Relationship_Plugin.md → docs/GUIDE/GUIDE_2_9_Relationships_Urls.md} +1 -4
- package/docs/GUIDE/GUIDE_2_Data_And_Relations.md +66 -0
- package/docs/GUIDE/GUIDE_3_Field_Transformations.md +1246 -0
- package/docs/GUIDE/GUIDE_5_Non-Db_Stores.md +0 -0
- package/docs/GUIDE/GUIDE_7_Hooks_Data_Management_And_Plugins.md +1490 -0
- package/{README_X_SocketIO.md → docs/GUIDE/GUIDE_X_SocketIO.md} +17 -17
- package/docs/GUIDE/index.md +58 -0
- package/docs/GUIDE.md +61 -2238
- package/docs/ONBOARDING/Computed_Properties_And_Sparse_Fields.md +398 -0
- package/docs/README.md +57 -2239
- package/{README_2_Computed_and_hidden_fields.md → docs/README_VARIOUS/GUIDE_2_Computed_and_hidden_fields.md} +22 -17
- package/docs/README_VARIOUS/GUIDE_IN_DOCS.md +2277 -0
- package/{README.ORIG.md → docs/README_VARIOUS/README.ORIG.md} +8 -8
- package/{README_8_Polymorphic_Relationships.md → docs/README_VARIOUS/README_8_Polymorphic_Relationships.md} +1 -1
- package/docs/README_VARIOUS/README_IN_DOCS.ms +2277 -0
- package/{RELATIONSHIPS_ADVANCED.md → docs/README_VARIOUS/RELATIONSHIPS_ADVANCED.md} +3 -3
- package/{correct-schema-format.md → docs/README_VARIOUS/correct-schema-format.md} +13 -13
- package/{pagination.md → docs/README_VARIOUS/pagination.md} +11 -8
- package/docs/assets/css/style.scss +78 -5
- package/docs/index.md +2 -12
- package/helper-reorganization-plan.md +85 -0
- package/helper-usage-analysis.md +94 -0
- package/package.json +5 -4
- package/plugins/core/bulk-operations-plugin.js +2 -2
- package/plugins/core/connectors/express-plugin.js +90 -32
- package/plugins/core/lib/querying/knex-cross-table-search.js +609 -0
- package/plugins/core/lib/querying/knex-json-api-transformers-querying.js +491 -0
- package/plugins/core/lib/querying/knex-pagination-helpers.js +544 -0
- package/plugins/core/lib/{knex-process-includes.js → querying/knex-process-includes.js} +65 -50
- package/plugins/core/lib/querying/knex-query-helpers-base.js +74 -0
- package/plugins/core/lib/{knex-query-helpers.js → querying/knex-query-helpers.js} +416 -180
- package/plugins/core/lib/{knex-relationship-includes.js → querying/knex-relationship-includes.js} +430 -114
- package/plugins/core/lib/querying/knex-window-queries.js +328 -0
- package/plugins/core/lib/querying-writing/compile-schemas.js +328 -0
- package/plugins/core/{utils → lib/querying-writing}/connectors-query-parser.js +62 -57
- package/plugins/core/lib/{database-capabilities.js → querying-writing/database-capabilities.js} +113 -7
- package/plugins/core/lib/querying-writing/database-value-normalizers.js +369 -0
- package/plugins/core/lib/querying-writing/default-data-helpers.js +72 -0
- package/plugins/core/{utils → lib/querying-writing}/field-utils.js +41 -31
- package/plugins/core/{utils → lib/querying-writing}/knex-constants.js +1 -0
- package/plugins/core/lib/{knex-field-helpers.js → querying-writing/knex-field-helpers.js} +190 -57
- package/plugins/core/lib/querying-writing/knex-json-api-transformers.js +162 -0
- package/plugins/core/lib/{payload-validators.js → querying-writing/payload-validators.js} +297 -217
- package/plugins/core/lib/querying-writing/schema-helpers.js +497 -0
- package/plugins/core/lib/querying-writing/scope-validations.js +431 -0
- package/plugins/core/lib/{simplified-helpers.js → querying-writing/simplified-helpers.js} +216 -176
- package/plugins/core/lib/writing/knex-json-api-transformers-writing.js +110 -0
- package/plugins/core/lib/writing/many-to-many-manipulations.js +300 -0
- package/plugins/core/lib/writing/relationship-processor.js +252 -0
- package/plugins/core/rest-api-knex-plugin.js +183 -62
- package/plugins/core/rest-api-plugin-hooks/compile-resource-schemas.js +7 -0
- package/plugins/core/rest-api-plugin-hooks/register-relationship-routes.js +72 -0
- package/plugins/core/rest-api-plugin-hooks/register-scope-routes.js +102 -0
- package/plugins/core/rest-api-plugin-hooks/turn-scope-init-into-vars.js +50 -0
- package/plugins/core/rest-api-plugin-hooks/validate-include-configurations.js +39 -0
- package/plugins/core/rest-api-plugin-methods/add-route.js +23 -0
- package/plugins/core/rest-api-plugin-methods/check-permissions.js +18 -0
- package/plugins/core/rest-api-plugin-methods/common.js +605 -0
- package/plugins/core/rest-api-plugin-methods/delete-relationship.js +115 -0
- package/plugins/core/rest-api-plugin-methods/delete.js +109 -0
- package/plugins/core/rest-api-plugin-methods/enrich-attributes.js +186 -0
- package/plugins/core/rest-api-plugin-methods/get-related.js +295 -0
- package/plugins/core/rest-api-plugin-methods/get-relationship.js +62 -0
- package/plugins/core/rest-api-plugin-methods/get.js +187 -0
- package/plugins/core/rest-api-plugin-methods/patch-relationship.js +56 -0
- package/plugins/core/rest-api-plugin-methods/patch.js +193 -0
- package/plugins/core/rest-api-plugin-methods/post-relationship.js +104 -0
- package/plugins/core/rest-api-plugin-methods/post.js +157 -0
- package/plugins/core/rest-api-plugin-methods/put.js +257 -0
- package/plugins/core/rest-api-plugin-methods/query.js +208 -0
- package/plugins/core/rest-api-plugin.js +108 -2498
- package/plugins/core/socketio-plugin.js +28 -5
- package/plugins/storage/local-storage.js +2 -2
- package/tests/computed-fields.test.js +483 -0
- package/tests/cors.test.js +4 -4
- package/tests/curl.test.js +5 -4
- package/tests/custom-idproperty-relationships.test.js +891 -0
- package/tests/field-getters.test.js +353 -0
- package/tests/field-setters.test.js +310 -0
- package/tests/fixtures/api-configs.js +800 -28
- package/tests/jwt-auth.test.js +0 -2
- package/tests/nested-includes.test.js +1 -1
- package/tests/pagination-cursor-multifield.test.js +514 -0
- package/tests/pagination-enhanced.test.js +12 -9
- package/tests/polymorphic-relationships.test.js +10 -8
- package/tests/polymorphic-simple.test.js +6 -4
- package/tests/positioning-advanced.test.js +12 -12
- package/tests/positioning-extended.test.js +25 -25
- package/tests/positioning-multifilter.test.js +24 -24
- package/tests/positioning.test.js +3 -3
- package/tests/query-limits.test.js +24 -8
- package/tests/relationship-endpoints.test.js +4 -5
- package/tests/relationships.test.js +0 -1
- package/tests/socketio.test.js +12 -1
- package/tests/virtual-fields.test.js +13 -13
- package/PLAN_FOR_WEBSOCKETS.md +0 -398
- package/README_1_Initial_Setup.md +0 -393
- package/README_X_Hooks.md +0 -647
- package/TODO_rest-api-plugin-extras.todo +0 -23
- package/connectors_ideas/express-plugin.js +0 -120
- package/connectors_ideas/http-plugin.js +0 -138
- package/docs/examples/express-plugin.js +0 -366
- package/example.js +0 -301
- package/plugins/core/HELPER_MODULES.md +0 -285
- package/plugins/core/lib/compile-schemas.js +0 -245
- package/plugins/core/lib/database-value-normalizers.js +0 -192
- package/plugins/core/lib/default-data-helpers.js +0 -100
- package/plugins/core/lib/knex-cross-table-search.js +0 -442
- package/plugins/core/lib/knex-json-api-helpers.js +0 -84
- package/plugins/core/lib/knex-json-api-transformers.js +0 -449
- package/plugins/core/lib/knex-pagination-helpers.js +0 -193
- package/plugins/core/lib/knex-window-queries.js +0 -142
- package/plugins/core/lib/many-to-many-manipulations.js +0 -343
- package/plugins/core/lib/relationship-processor.js +0 -278
- package/plugins/core/lib/schema-helpers.js +0 -343
- package/plugins/core/lib/scope-validations.js +0 -252
- package/plugins/core/rest-api-relationships-plugin.js +0 -700
- package/plugins/core/utils/knex-query-helpers-base.js +0 -56
- /package/{README_X_Permissions.md → docs/GUIDE/GUIDE_4_Authentication_And_Permissions.md} +0 -0
- /package/{README_X_Positioning.md → docs/GUIDE/GUIDE_6_Positioning.md} +0 -0
- /package/{README_X_Bulk_Operations.md → docs/GUIDE/GUIDE_X_Bulk_Operations.md} +0 -0
- /package/{README_X_Cors.md → docs/GUIDE/GUIDE_X_Cors.md} +0 -0
- /package/{README_X_Multihome.md → docs/GUIDE/GUIDE_X_Multihome.md} +0 -0
- /package/{README_X_Appendices.md → docs/GUIDE/GUIDE_Y_Appendices.md} +0 -0
- /package/{QUERY_DEPTH_PROTECTION.md → docs/README_VARIOUS/QUERY_DEPTH_PROTECTION.md} +0 -0
- /package/{RELATIONSHIPS.md → docs/README_VARIOUS/RELATIONSHIPS.md} +0 -0
- /package/{WEBSOCKET_IMPLEMENTATION_STATUS.md → docs/README_VARIOUS/WEBSOCKET_IMPLEMENTATION_STATUS.md} +0 -0
- /package/{helper-functions-chart.md → docs/README_VARIOUS/helper-functions-chart.md} +0 -0
- /package/docs/{implementation-hidden-computed-fields.md → README_VARIOUS/implementation-hidden-computed-fields.md} +0 -0
- /package/docs/{include-limits-migration-guide.md → README_VARIOUS/include-limits-migration-guide.md} +0 -0
- /package/{readmeSnippets → docs/README_VARIOUS}/test_put_behavior.js +0 -0
- /package/{readmeSnippets → docs/README_VARIOUS}/test_put_fields_without_defaults.js +0 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { describe, it, before, beforeEach, after } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import knexLib from 'knex';
|
|
4
|
+
import { createFieldGettersApi } from './fixtures/api-configs.js';
|
|
5
|
+
import { cleanTables } from './helpers/test-utils.js';
|
|
6
|
+
|
|
7
|
+
// Create Knex instance for tests
|
|
8
|
+
const knex = knexLib({
|
|
9
|
+
client: 'better-sqlite3',
|
|
10
|
+
connection: {
|
|
11
|
+
filename: ':memory:'
|
|
12
|
+
},
|
|
13
|
+
useNullAsDefault: true
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// API instance that persists across tests
|
|
17
|
+
let api;
|
|
18
|
+
|
|
19
|
+
describe('Field Getters', () => {
|
|
20
|
+
before(async () => {
|
|
21
|
+
// Initialize API once
|
|
22
|
+
api = await createFieldGettersApi(knex);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
after(async () => {
|
|
26
|
+
// Close database connection to allow tests to exit
|
|
27
|
+
await knex.destroy();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('Basic Field Transformations', () => {
|
|
31
|
+
beforeEach(async () => {
|
|
32
|
+
await cleanTables(knex, ['getter_users']);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should apply simple getter transformations', async () => {
|
|
36
|
+
// Create user with data that needs transformation
|
|
37
|
+
const user = await api.resources.users.post({
|
|
38
|
+
email: ' USER@EXAMPLE.COM ',
|
|
39
|
+
name: ' John Doe ',
|
|
40
|
+
phone: '1234567890',
|
|
41
|
+
metadata_json: '{"key": "value", "num": 123}',
|
|
42
|
+
tags_csv: 'tag1, tag2, tag3'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Check transformations applied on create
|
|
46
|
+
assert.equal(user.email, 'user@example.com');
|
|
47
|
+
assert.equal(user.name, 'John Doe');
|
|
48
|
+
assert.equal(user.phone, '(123) 456-7890');
|
|
49
|
+
assert.deepEqual(user.metadata_json, { key: 'value', num: 123 });
|
|
50
|
+
assert.deepEqual(user.tags_csv, ['tag1', 'tag2', 'tag3']);
|
|
51
|
+
|
|
52
|
+
// Verify getters apply on fetch too
|
|
53
|
+
const fetched = await api.resources.users.get({ id: user.id });
|
|
54
|
+
assert.equal(fetched.email, 'user@example.com');
|
|
55
|
+
assert.equal(fetched.name, 'John Doe');
|
|
56
|
+
assert.equal(fetched.phone, '(123) 456-7890');
|
|
57
|
+
assert.deepEqual(fetched.metadata_json, { key: 'value', num: 123 });
|
|
58
|
+
assert.deepEqual(fetched.tags_csv, ['tag1', 'tag2', 'tag3']);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should handle null and undefined values', async () => {
|
|
62
|
+
const user = await api.resources.users.post({
|
|
63
|
+
email: null,
|
|
64
|
+
name: undefined,
|
|
65
|
+
phone: null,
|
|
66
|
+
metadata_json: null,
|
|
67
|
+
tags_csv: null
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
assert.equal(user.email, undefined); // null?.toLowerCase().trim() returns undefined
|
|
71
|
+
assert.equal(user.name, undefined); // undefined is passed, getter returns undefined
|
|
72
|
+
assert.equal(user.phone, null); // getter explicitly returns null for falsy values
|
|
73
|
+
assert.deepEqual(user.metadata_json, {});
|
|
74
|
+
assert.deepEqual(user.tags_csv, []);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should handle invalid data gracefully', async () => {
|
|
78
|
+
const user = await api.resources.users.post({
|
|
79
|
+
email: ' mixed@CASE.com ',
|
|
80
|
+
name: ' ',
|
|
81
|
+
phone: '123', // Too short
|
|
82
|
+
metadata_json: 'invalid json',
|
|
83
|
+
tags_csv: ', , empty, , values, '
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
assert.equal(user.email, 'mixed@case.com');
|
|
87
|
+
assert.equal(user.name, '');
|
|
88
|
+
assert.equal(user.phone, '123'); // Unchanged when can't format
|
|
89
|
+
assert.deepEqual(user.metadata_json, {}); // Empty object on parse error
|
|
90
|
+
assert.deepEqual(user.tags_csv, ['empty', 'values']); // Filters empty values
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('Getters with Computed Fields', () => {
|
|
95
|
+
beforeEach(async () => {
|
|
96
|
+
await cleanTables(knex, ['getter_products']);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should apply getters before computed fields', async () => {
|
|
100
|
+
const product = await api.resources.products.post({
|
|
101
|
+
name: 'widget',
|
|
102
|
+
description: 'This is a very long product description that should be truncated',
|
|
103
|
+
price_str: '100.00',
|
|
104
|
+
tax_rate_str: '0.20'
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Getters should transform values
|
|
108
|
+
assert.equal(product.name, 'WIDGET');
|
|
109
|
+
assert.equal(product.description, 'This is a very long product description that sh...');
|
|
110
|
+
assert.equal(typeof product.price_str, 'number');
|
|
111
|
+
assert.equal(product.price_str, 100);
|
|
112
|
+
assert.equal(product.tax_rate_str, 0.20);
|
|
113
|
+
|
|
114
|
+
// Computed field should see getter-transformed values
|
|
115
|
+
assert.equal(product.total_price, 120);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should work with sparse fieldsets', async () => {
|
|
119
|
+
const product = await api.resources.products.post({
|
|
120
|
+
name: 'another widget',
|
|
121
|
+
description: 'Short desc',
|
|
122
|
+
price_str: '50.00',
|
|
123
|
+
tax_rate_str: '0.10'
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Fetch with sparse fieldsets
|
|
127
|
+
const sparse = await api.resources.products.get({
|
|
128
|
+
id: product.id,
|
|
129
|
+
queryParams: {
|
|
130
|
+
fields: { products: 'name,total_price' }
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
assert.equal(sparse.name, 'ANOTHER WIDGET');
|
|
135
|
+
assert.ok(Math.abs(sparse.total_price - 55) < 0.0001, `Expected total_price to be ~55, got ${sparse.total_price}`);
|
|
136
|
+
assert.equal(sparse.description, undefined);
|
|
137
|
+
assert.equal(sparse.price_str, undefined);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('Getter Dependencies', () => {
|
|
142
|
+
beforeEach(async () => {
|
|
143
|
+
await cleanTables(knex, ['getter_formatted']);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should apply getters in dependency order', async () => {
|
|
147
|
+
const data = await api.resources.formatted_data.post({
|
|
148
|
+
step1: ' hello ',
|
|
149
|
+
step2: 'world',
|
|
150
|
+
step3: 'end'
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Check that getters ran in order
|
|
154
|
+
assert.equal(data.step1, 'hello'); // Trimmed
|
|
155
|
+
assert.equal(data.step2, 'world [step1: hello]'); // Sees trimmed step1
|
|
156
|
+
assert.equal(data.step3, 'end [step2: world [step1: hello]]'); // Sees processed step2
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should handle missing dependencies gracefully', async () => {
|
|
160
|
+
const data = await api.resources.formatted_data.post({
|
|
161
|
+
step3: 'only step3'
|
|
162
|
+
// step1 and step2 are missing
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
assert.equal(data.step1, undefined);
|
|
166
|
+
assert.equal(data.step2, null);
|
|
167
|
+
assert.equal(data.step3, 'only step3 [step2: null]');
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('Async Getters', () => {
|
|
172
|
+
beforeEach(async () => {
|
|
173
|
+
await cleanTables(knex, ['getter_encrypted']);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should support async getter functions', async () => {
|
|
177
|
+
// Helper to simulate encryption
|
|
178
|
+
const encrypt = (value) => Buffer.from(value).toString('base64');
|
|
179
|
+
|
|
180
|
+
// Store encrypted data directly in DB
|
|
181
|
+
const id = await knex('getter_encrypted').insert({
|
|
182
|
+
secret: encrypt('my-secret-value'),
|
|
183
|
+
data: encrypt('sensitive-info')
|
|
184
|
+
}).returning('id').then(r => r[0].id);
|
|
185
|
+
|
|
186
|
+
// Fetch should decrypt via async getters
|
|
187
|
+
const record = await api.resources.encrypted_data.get({ id });
|
|
188
|
+
assert.equal(record.secret, 'my-secret-value');
|
|
189
|
+
assert.equal(record.data, '[encrypted_data] sensitive-info');
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should handle null values in async getters', async () => {
|
|
193
|
+
const record = await api.resources.encrypted_data.post({
|
|
194
|
+
secret: null,
|
|
195
|
+
data: null
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
assert.equal(record.secret, null);
|
|
199
|
+
assert.equal(record.data, null);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('Getters in Included Resources', () => {
|
|
204
|
+
beforeEach(async () => {
|
|
205
|
+
await cleanTables(knex, ['getter_products', 'getter_reviews']);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should apply getters to included resources', async () => {
|
|
209
|
+
const product = await api.resources.products.post({
|
|
210
|
+
name: 'test product',
|
|
211
|
+
description: 'A product',
|
|
212
|
+
price_str: '99.99',
|
|
213
|
+
tax_rate_str: '0'
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Add reviews
|
|
217
|
+
await api.resources.reviews.post({
|
|
218
|
+
product_id: product.id,
|
|
219
|
+
content: 'Great product!',
|
|
220
|
+
rating: 5
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
await api.resources.reviews.post({
|
|
224
|
+
product_id: product.id,
|
|
225
|
+
content: 'Not bad',
|
|
226
|
+
rating: 3
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Fetch with includes
|
|
230
|
+
const result = await api.resources.products.get({
|
|
231
|
+
id: product.id,
|
|
232
|
+
queryParams: {
|
|
233
|
+
include: ['reviews']
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Product getters should apply
|
|
238
|
+
assert.equal(result.name, 'TEST PRODUCT');
|
|
239
|
+
|
|
240
|
+
// Review getters should apply to included resources
|
|
241
|
+
assert.equal(result.reviews.length, 2);
|
|
242
|
+
assert.equal(result.reviews[0].content, '[REVIEW] Great product!');
|
|
243
|
+
assert.equal(result.reviews[1].content, '[REVIEW] Not bad');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('should work with sparse fieldsets on included resources', async () => {
|
|
247
|
+
const product = await api.resources.products.post({
|
|
248
|
+
name: 'another product',
|
|
249
|
+
description: 'Description',
|
|
250
|
+
price_str: '10',
|
|
251
|
+
tax_rate_str: '0'
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
await api.resources.reviews.post({
|
|
255
|
+
product_id: product.id,
|
|
256
|
+
content: 'Review text',
|
|
257
|
+
rating: 4
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const result = await api.resources.products.get({
|
|
261
|
+
id: product.id,
|
|
262
|
+
queryParams: {
|
|
263
|
+
include: ['reviews'],
|
|
264
|
+
fields: {
|
|
265
|
+
products: 'name',
|
|
266
|
+
reviews: 'content' // Only content, not rating
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
assert.equal(result.name, 'ANOTHER PRODUCT');
|
|
272
|
+
assert.equal(result.description, undefined);
|
|
273
|
+
|
|
274
|
+
assert.equal(result.reviews.length, 1);
|
|
275
|
+
assert.equal(result.reviews[0].content, '[REVIEW] Review text');
|
|
276
|
+
assert.equal(result.reviews[0].rating, undefined);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe('Getter Error Handling', () => {
|
|
281
|
+
it('should handle getter errors gracefully', async () => {
|
|
282
|
+
// Add a resource with a failing getter
|
|
283
|
+
await api.addResource('error_test', {
|
|
284
|
+
schema: {
|
|
285
|
+
id: { type: 'id' },
|
|
286
|
+
good_field: {
|
|
287
|
+
type: 'string',
|
|
288
|
+
getter: (value) => value?.toUpperCase()
|
|
289
|
+
},
|
|
290
|
+
bad_field: {
|
|
291
|
+
type: 'string',
|
|
292
|
+
getter: (value) => {
|
|
293
|
+
throw new Error('Getter failed!');
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
tableName: 'getter_errors'
|
|
298
|
+
});
|
|
299
|
+
await api.resources.error_test.createKnexTable();
|
|
300
|
+
|
|
301
|
+
const record = await api.resources.error_test.post({
|
|
302
|
+
good_field: 'hello',
|
|
303
|
+
bad_field: 'world'
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// Good field should be transformed
|
|
307
|
+
assert.equal(record.good_field, 'HELLO');
|
|
308
|
+
// Bad field should keep original value (error logged but not thrown)
|
|
309
|
+
assert.equal(record.bad_field, 'world');
|
|
310
|
+
|
|
311
|
+
await cleanTables(knex, ['getter_errors']);
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
describe('Circular Dependencies', () => {
|
|
316
|
+
it('should detect circular getter dependencies', async () => {
|
|
317
|
+
await assert.rejects(
|
|
318
|
+
api.addResource('circular_test', {
|
|
319
|
+
schema: {
|
|
320
|
+
fieldA: {
|
|
321
|
+
type: 'string',
|
|
322
|
+
getter: (v) => v,
|
|
323
|
+
runGetterAfter: ['fieldB']
|
|
324
|
+
},
|
|
325
|
+
fieldB: {
|
|
326
|
+
type: 'string',
|
|
327
|
+
getter: (v) => v,
|
|
328
|
+
runGetterAfter: ['fieldA']
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
tableName: 'circular_test'
|
|
332
|
+
}),
|
|
333
|
+
/Circular dependency detected/
|
|
334
|
+
);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('should detect unknown getter dependencies', async () => {
|
|
338
|
+
await assert.rejects(
|
|
339
|
+
api.addResource('unknown_test', {
|
|
340
|
+
schema: {
|
|
341
|
+
field1: {
|
|
342
|
+
type: 'string',
|
|
343
|
+
getter: (v) => v,
|
|
344
|
+
runGetterAfter: ['nonexistent']
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
tableName: 'unknown_test'
|
|
348
|
+
}),
|
|
349
|
+
/getter dependency 'nonexistent' that does not exist/
|
|
350
|
+
);
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
});
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { describe, it, before, beforeEach, after } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import knexLib from 'knex';
|
|
4
|
+
import { createFieldSettersApi } from './fixtures/api-configs.js';
|
|
5
|
+
import { cleanTables } from './helpers/test-utils.js';
|
|
6
|
+
|
|
7
|
+
// Create Knex instance for tests
|
|
8
|
+
const knex = knexLib({
|
|
9
|
+
client: 'better-sqlite3',
|
|
10
|
+
connection: {
|
|
11
|
+
filename: ':memory:'
|
|
12
|
+
},
|
|
13
|
+
useNullAsDefault: true
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// API instance that persists across tests
|
|
17
|
+
let api;
|
|
18
|
+
|
|
19
|
+
describe('Field Setters', () => {
|
|
20
|
+
before(async () => {
|
|
21
|
+
// Initialize API once
|
|
22
|
+
api = await createFieldSettersApi(knex);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
after(async () => {
|
|
26
|
+
// Close database connection to allow tests to exit
|
|
27
|
+
await knex.destroy();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('Basic Field Transformations', () => {
|
|
31
|
+
beforeEach(async () => {
|
|
32
|
+
await cleanTables(knex, ['setter_users']);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should apply simple setter transformations on create', async () => {
|
|
36
|
+
// Create user with data that needs transformation
|
|
37
|
+
const user = await api.resources.users.post({
|
|
38
|
+
email: ' USER@EXAMPLE.COM ',
|
|
39
|
+
username: ' JohnDoe ',
|
|
40
|
+
tags: 'tag1,tag2,tag3', // Pass as string
|
|
41
|
+
preferences: '{"theme":"dark","notifications":true}' // Pass as JSON string
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Get the user to verify setters were applied
|
|
45
|
+
const fetchedUser = await api.resources.users.get({ id: user.id });
|
|
46
|
+
|
|
47
|
+
// The setters transformed the data before storage
|
|
48
|
+
assert.equal(fetchedUser.email, 'user@example.com');
|
|
49
|
+
assert.equal(fetchedUser.username, 'johndoe');
|
|
50
|
+
assert.equal(fetchedUser.tags, 'tag1,tag2,tag3');
|
|
51
|
+
assert.equal(fetchedUser.preferences, '{"theme":"dark","notifications":true}');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should apply setter transformations on update (PUT)', async () => {
|
|
55
|
+
// Create initial user
|
|
56
|
+
const user = await api.resources.users.post({
|
|
57
|
+
email: 'original@example.com',
|
|
58
|
+
username: 'originaluser',
|
|
59
|
+
tags: 'old',
|
|
60
|
+
preferences: '{"theme":"light"}'
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Update with PUT
|
|
64
|
+
await api.resources.users.put({
|
|
65
|
+
id: user.id,
|
|
66
|
+
email: ' UPDATED@EXAMPLE.COM ',
|
|
67
|
+
username: ' UpdatedUser ',
|
|
68
|
+
tags: 'new1,new2',
|
|
69
|
+
preferences: '{"theme":"dark","lang":"en"}'
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Get the user to verify setters were applied
|
|
73
|
+
const updatedUser = await api.resources.users.get({ id: user.id });
|
|
74
|
+
assert.equal(updatedUser.email, 'updated@example.com');
|
|
75
|
+
assert.equal(updatedUser.username, 'updateduser');
|
|
76
|
+
assert.equal(updatedUser.tags, 'new1,new2');
|
|
77
|
+
assert.equal(updatedUser.preferences, '{"theme":"dark","lang":"en"}');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should apply setter transformations on partial update (PATCH)', async () => {
|
|
81
|
+
// Create initial user
|
|
82
|
+
const user = await api.resources.users.post({
|
|
83
|
+
email: 'original@example.com',
|
|
84
|
+
username: 'originaluser',
|
|
85
|
+
tags: 'old',
|
|
86
|
+
preferences: '{"theme":"light"}'
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Update only email with PATCH
|
|
90
|
+
await api.resources.users.patch({
|
|
91
|
+
id: user.id,
|
|
92
|
+
email: ' PATCHED@EXAMPLE.COM '
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Get the user to verify setter was applied only to patched field
|
|
96
|
+
const patchedUser = await api.resources.users.get({ id: user.id });
|
|
97
|
+
assert.equal(patchedUser.email, 'patched@example.com');
|
|
98
|
+
assert.equal(patchedUser.username, 'originaluser'); // Unchanged
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('Type Conversion Setters', () => {
|
|
103
|
+
beforeEach(async () => {
|
|
104
|
+
await cleanTables(knex, ['setter_products']);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should apply type conversion setters after validation', async () => {
|
|
108
|
+
const product = await api.resources.products.post({
|
|
109
|
+
name: 'Test Product',
|
|
110
|
+
price: 99.999, // Will be rounded to cents
|
|
111
|
+
discount_percent: 15.678, // Will be rounded to integer
|
|
112
|
+
metadata: { key: 'value' } // Will be stringified
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Get the product to verify setters were applied
|
|
116
|
+
const fetchedProduct = await api.resources.products.get({ id: product.id });
|
|
117
|
+
assert.equal(fetchedProduct.price, 10000); // 99.999 * 100 rounded = 10000 cents
|
|
118
|
+
assert.equal(fetchedProduct.discount_percent, 16); // 15.678 rounded
|
|
119
|
+
assert.equal(fetchedProduct.metadata, '{"key":"value"}');
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('Async Setters', () => {
|
|
124
|
+
beforeEach(async () => {
|
|
125
|
+
await cleanTables(knex, ['setter_secure']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should support async setter functions', async () => {
|
|
129
|
+
const record = await api.resources.secure_data.post({
|
|
130
|
+
password: 'mysecretpassword',
|
|
131
|
+
api_key: 'test-key-123',
|
|
132
|
+
data: 'sensitive information'
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
console.log('**********************************************')
|
|
136
|
+
console.log(record)
|
|
137
|
+
// Check if record has id
|
|
138
|
+
assert.ok(record.id, 'Record should have an id');
|
|
139
|
+
|
|
140
|
+
// Get the record to verify async setters were applied
|
|
141
|
+
const fetchedRecord = await api.resources.secure_data.get({ id: record.id });
|
|
142
|
+
|
|
143
|
+
// Password should be hashed (mock hash)
|
|
144
|
+
assert.equal(fetchedRecord.password, 'hashed:mysecretpassword');
|
|
145
|
+
|
|
146
|
+
// API key should be encrypted (base64)
|
|
147
|
+
assert.equal(fetchedRecord.api_key, Buffer.from('test-key-123').toString('base64'));
|
|
148
|
+
|
|
149
|
+
// Data should be encrypted
|
|
150
|
+
assert.equal(fetchedRecord.data, Buffer.from('sensitive information').toString('base64'));
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('Setter Dependencies', () => {
|
|
155
|
+
beforeEach(async () => {
|
|
156
|
+
await cleanTables(knex, ['setter_computed']);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should apply setters in dependency order', async () => {
|
|
160
|
+
const data = await api.resources.computed_data.post({
|
|
161
|
+
base_value: 100,
|
|
162
|
+
multiplier: 2,
|
|
163
|
+
adjustment: 10
|
|
164
|
+
// calculated_value depends on base_value and multiplier
|
|
165
|
+
// final_value depends on calculated_value and adjustment
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const fetchedData = await api.resources.computed_data.get({ id: data.id });
|
|
169
|
+
assert.equal(fetchedData.base_value, 100);
|
|
170
|
+
assert.equal(fetchedData.multiplier, 2);
|
|
171
|
+
assert.equal(fetchedData.adjustment, 10);
|
|
172
|
+
assert.equal(fetchedData.calculated_value, 200); // 100 * 2
|
|
173
|
+
assert.equal(fetchedData.final_value, 210); // 200 + 10
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('Error Handling', () => {
|
|
178
|
+
beforeEach(async () => {
|
|
179
|
+
await cleanTables(knex, ['setter_users']);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should handle setter errors gracefully', async () => {
|
|
183
|
+
// Create a resource with a failing setter
|
|
184
|
+
await api.addResource('error_test', {
|
|
185
|
+
schema: {
|
|
186
|
+
id: { type: 'id' },
|
|
187
|
+
good_field: {
|
|
188
|
+
type: 'string',
|
|
189
|
+
setter: (value) => value?.toLowerCase()
|
|
190
|
+
},
|
|
191
|
+
bad_field: {
|
|
192
|
+
type: 'string',
|
|
193
|
+
setter: (value) => {
|
|
194
|
+
throw new Error('Setter failed!');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
tableName: 'setter_errors'
|
|
199
|
+
});
|
|
200
|
+
await api.resources.error_test.createKnexTable();
|
|
201
|
+
|
|
202
|
+
const record = await api.resources.error_test.post({
|
|
203
|
+
good_field: 'HELLO',
|
|
204
|
+
bad_field: 'world'
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const fetchedRecord = await api.resources.error_test.get({ id: record.id });
|
|
208
|
+
// Good field should be transformed
|
|
209
|
+
assert.equal(fetchedRecord.good_field, 'hello');
|
|
210
|
+
// Bad field should keep validated value (error logged but not thrown)
|
|
211
|
+
assert.equal(fetchedRecord.bad_field, 'world');
|
|
212
|
+
|
|
213
|
+
await cleanTables(knex, ['setter_errors']);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe('Null and Undefined Handling', () => {
|
|
218
|
+
beforeEach(async () => {
|
|
219
|
+
await cleanTables(knex, ['setter_nullable']);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should handle null and undefined values in setters', async () => {
|
|
223
|
+
const data = await api.resources.nullable_data.post({
|
|
224
|
+
field1: null,
|
|
225
|
+
field2: undefined,
|
|
226
|
+
field3: '',
|
|
227
|
+
field4: 0
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const fetchedData = await api.resources.nullable_data.get({ id: data.id });
|
|
231
|
+
assert.equal(fetchedData.field1, null);
|
|
232
|
+
assert.equal(fetchedData.field2, null);
|
|
233
|
+
assert.equal(fetchedData.field3, 'empty'); // Empty string transformed
|
|
234
|
+
assert.equal(fetchedData.field4, -1); // Zero transformed
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe('Validation Before Setters', () => {
|
|
239
|
+
beforeEach(async () => {
|
|
240
|
+
await cleanTables(knex, ['setter_validated']);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('should run setters only after successful validation', async () => {
|
|
244
|
+
// Try to create with invalid data
|
|
245
|
+
await assert.rejects(
|
|
246
|
+
api.resources.validated_data.post({
|
|
247
|
+
email: 'not-an-email', // Should fail validation
|
|
248
|
+
age: 150 // Should fail max validation
|
|
249
|
+
}),
|
|
250
|
+
/Schema validation failed/
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// No records should be created - verify with query
|
|
254
|
+
const records = await api.resources.validated_data.query();
|
|
255
|
+
assert.equal(records?.length || 0, 0);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should apply setters to validated type-cast data', async () => {
|
|
259
|
+
const data = await api.resources.validated_data.post({
|
|
260
|
+
email: ' VALID@EXAMPLE.COM ',
|
|
261
|
+
age: '25', // String will be cast to number
|
|
262
|
+
score: '98.7' // String will be cast to number
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const fetchedData = await api.resources.validated_data.get({ id: data.id });
|
|
266
|
+
assert.equal(fetchedData.email, 'valid@example.com');
|
|
267
|
+
assert.equal(fetchedData.age, 25); // Setter sees number, not string
|
|
268
|
+
assert.equal(fetchedData.score, 99); // Rounded up by setter
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe('Circular Dependencies', () => {
|
|
273
|
+
it('should detect circular setter dependencies', async () => {
|
|
274
|
+
await assert.rejects(
|
|
275
|
+
api.addResource('circular_setters', {
|
|
276
|
+
schema: {
|
|
277
|
+
fieldA: {
|
|
278
|
+
type: 'string',
|
|
279
|
+
setter: (v) => v,
|
|
280
|
+
runSetterAfter: ['fieldB']
|
|
281
|
+
},
|
|
282
|
+
fieldB: {
|
|
283
|
+
type: 'string',
|
|
284
|
+
setter: (v) => v,
|
|
285
|
+
runSetterAfter: ['fieldA']
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
tableName: 'circular_setters'
|
|
289
|
+
}),
|
|
290
|
+
/Circular dependency detected/
|
|
291
|
+
);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should detect unknown setter dependencies', async () => {
|
|
295
|
+
await assert.rejects(
|
|
296
|
+
api.addResource('unknown_deps', {
|
|
297
|
+
schema: {
|
|
298
|
+
field1: {
|
|
299
|
+
type: 'string',
|
|
300
|
+
setter: (v) => v,
|
|
301
|
+
runSetterAfter: ['nonexistent']
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
tableName: 'unknown_deps'
|
|
305
|
+
}),
|
|
306
|
+
/setter dependency 'nonexistent' that does not exist/
|
|
307
|
+
);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|