json-rest-api 1.0.6 → 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 +99 -33
- 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
|
@@ -150,10 +150,10 @@ await api.addResource('posts', {
|
|
|
150
150
|
|
|
151
151
|
searchSchema: {
|
|
152
152
|
// These filters work for both REST and WebSocket
|
|
153
|
-
status: { type: 'string',
|
|
154
|
-
author_id: { type: 'id',
|
|
155
|
-
published_at: { type: 'dateTime',
|
|
156
|
-
view_count: { type: 'number',
|
|
153
|
+
status: { type: 'string', filterOperator: '=' },
|
|
154
|
+
author_id: { type: 'id', filterOperator: '=' },
|
|
155
|
+
published_at: { type: 'dateTime', filterOperator: '>=' },
|
|
156
|
+
view_count: { type: 'number', filterOperator: '>' }
|
|
157
157
|
}
|
|
158
158
|
});
|
|
159
159
|
|
|
@@ -203,20 +203,20 @@ Filters using simple operators (`=`, `>`, `>=`, `<`, `<=`, `!=`, `like`, `in`, `
|
|
|
203
203
|
```javascript
|
|
204
204
|
searchSchema: {
|
|
205
205
|
// Equality
|
|
206
|
-
status: { type: 'string',
|
|
206
|
+
status: { type: 'string', filterOperator: '=' },
|
|
207
207
|
|
|
208
208
|
// Comparison
|
|
209
|
-
price: { type: 'number',
|
|
210
|
-
stock: { type: 'number',
|
|
209
|
+
price: { type: 'number', filterOperator: '>=' },
|
|
210
|
+
stock: { type: 'number', filterOperator: '>' },
|
|
211
211
|
|
|
212
212
|
// Pattern matching
|
|
213
|
-
title: { type: 'string',
|
|
213
|
+
title: { type: 'string', filterOperator: 'like' },
|
|
214
214
|
|
|
215
215
|
// Multiple values
|
|
216
|
-
category_id: { type: 'array',
|
|
216
|
+
category_id: { type: 'array', filterOperator: 'in' },
|
|
217
217
|
|
|
218
218
|
// Range
|
|
219
|
-
created_at: { type: 'date',
|
|
219
|
+
created_at: { type: 'date', filterOperator: 'between' }
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
// These work for both REST and WebSocket
|
|
@@ -234,7 +234,7 @@ socket.emit('subscribe', {
|
|
|
234
234
|
|
|
235
235
|
### Complex Filters with filterRecord
|
|
236
236
|
|
|
237
|
-
When `
|
|
237
|
+
When `filterOperator` is a function (for complex SQL queries), you must provide `filterRecord` for WebSocket support:
|
|
238
238
|
|
|
239
239
|
```javascript
|
|
240
240
|
searchSchema: {
|
|
@@ -243,7 +243,7 @@ searchSchema: {
|
|
|
243
243
|
type: 'string',
|
|
244
244
|
|
|
245
245
|
// For REST API - builds SQL query
|
|
246
|
-
|
|
246
|
+
filterOperator: function(query, value, { tableName }) {
|
|
247
247
|
query.where(function() {
|
|
248
248
|
this.where(`${tableName}.title`, 'like', `%${value}%`)
|
|
249
249
|
.orWhere(`${tableName}.description`, 'like', `%${value}%`)
|
|
@@ -269,7 +269,7 @@ searchSchema: {
|
|
|
269
269
|
type: 'object',
|
|
270
270
|
|
|
271
271
|
// REST: Haversine formula in SQL
|
|
272
|
-
|
|
272
|
+
filterOperator: function(query, value, { tableName }) {
|
|
273
273
|
const { lat, lng, radius = 10 } = value;
|
|
274
274
|
query.whereRaw(`
|
|
275
275
|
(6371 * acos(
|
|
@@ -296,7 +296,7 @@ searchSchema: {
|
|
|
296
296
|
type: 'object',
|
|
297
297
|
|
|
298
298
|
// REST: Complex JOIN with user permissions
|
|
299
|
-
|
|
299
|
+
filterOperator: function(query, value, { tableName }) {
|
|
300
300
|
const { user_id, include_private } = value;
|
|
301
301
|
query.where(`${tableName}.owner_id`, user_id);
|
|
302
302
|
if (!include_private) {
|
|
@@ -937,14 +937,14 @@ Check:
|
|
|
937
937
|
|
|
938
938
|
**2. "UNSUPPORTED_FILTER" errors**
|
|
939
939
|
|
|
940
|
-
If using custom
|
|
940
|
+
If using custom filterOperator functions, you must provide filterRecord:
|
|
941
941
|
|
|
942
942
|
```javascript
|
|
943
943
|
// ❌ This will error for WebSocket
|
|
944
944
|
searchSchema: {
|
|
945
945
|
complex_search: {
|
|
946
946
|
type: 'string',
|
|
947
|
-
|
|
947
|
+
filterOperator: function(query, value) { /* SQL */ }
|
|
948
948
|
}
|
|
949
949
|
}
|
|
950
950
|
|
|
@@ -952,7 +952,7 @@ searchSchema: {
|
|
|
952
952
|
searchSchema: {
|
|
953
953
|
complex_search: {
|
|
954
954
|
type: 'string',
|
|
955
|
-
|
|
955
|
+
filterOperator: function(query, value) { /* SQL */ },
|
|
956
956
|
filterRecord: function(record, value) { /* JavaScript */ }
|
|
957
957
|
}
|
|
958
958
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# JSON REST API Complete Guide
|
|
2
|
+
|
|
3
|
+
Welcome to the comprehensive guide for JSON REST API. This guide will walk you through everything from initial setup to advanced features.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
### Core Chapters
|
|
8
|
+
|
|
9
|
+
1. **[Initial Setup](GUIDE_1_Initial_Setup.md)**
|
|
10
|
+
Get started with JSON REST API, create your first API instance, and understand the basic concepts.
|
|
11
|
+
|
|
12
|
+
2. **[Data and Relations](GUIDE_2_Data_And_Relations.md)**
|
|
13
|
+
Learn how to define resources, set up relationships, and work with your data model.
|
|
14
|
+
|
|
15
|
+
3. **[Field Transformations](GUIDE_3_Field_Transformations.md)**
|
|
16
|
+
Transform data with virtual fields, getters, setters, computed fields, and visibility control.
|
|
17
|
+
|
|
18
|
+
4. **[Authentication and Permissions](GUIDE_4_Authentication_And_Permissions.md)**
|
|
19
|
+
Implement security with authentication strategies and fine-grained permissions.
|
|
20
|
+
|
|
21
|
+
5. **[Non-Database Stores](GUIDE_5_Non-Db_Stores.md)**
|
|
22
|
+
Connect your API to alternative data sources beyond traditional databases.
|
|
23
|
+
|
|
24
|
+
6. **[Positioning](GUIDE_6_Positioning.md)**
|
|
25
|
+
Implement drag-and-drop functionality with automatic position management.
|
|
26
|
+
|
|
27
|
+
7. **[Hooks, Data Management, and Plugins](GUIDE_7_Hooks_Data_Management_And_Plugins.md)**
|
|
28
|
+
Master the hook system for data validation, transformation, and business logic.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Additional Topics
|
|
32
|
+
|
|
33
|
+
- **[Bulk Operations](GUIDE_X_Bulk_Operations.md)**
|
|
34
|
+
Handle multiple records efficiently with bulk create, update, and delete operations.
|
|
35
|
+
|
|
36
|
+
- **[CORS Configuration](GUIDE_X_Cors.md)**
|
|
37
|
+
Set up Cross-Origin Resource Sharing for browser-based applications.
|
|
38
|
+
|
|
39
|
+
- **[Multihome Support](GUIDE_X_Multihome.md)**
|
|
40
|
+
Run multiple API instances or serve different domains from a single server.
|
|
41
|
+
|
|
42
|
+
- **[Socket.IO Integration](GUIDE_X_SocketIO.md)**
|
|
43
|
+
Add real-time capabilities to your API with WebSocket support.
|
|
44
|
+
|
|
45
|
+
- **[Appendices](GUIDE_Y_Appendices.md)**
|
|
46
|
+
Reference material, troubleshooting, and additional resources.
|
|
47
|
+
|
|
48
|
+
## Prerequisites
|
|
49
|
+
|
|
50
|
+
Before starting this guide, you should have:
|
|
51
|
+
|
|
52
|
+
- Node.js 20+ installed
|
|
53
|
+
- Basic knowledge of JavaScript and REST APIs
|
|
54
|
+
- Familiarity with npm/yarn package management
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
Ready to get started? Head to [Chapter 1: Initial Setup](GUIDE_1_Initial_Setup.md) →
|