meadow 2.0.23 → 2.0.27
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/README.md +110 -141
- package/docs/README.md +34 -230
- package/docs/_cover.md +14 -0
- package/docs/_sidebar.md +44 -12
- package/docs/_topbar.md +5 -0
- package/docs/api/doCount.md +109 -0
- package/docs/api/doCreate.md +132 -0
- package/docs/api/doDelete.md +101 -0
- package/docs/api/doRead.md +122 -0
- package/docs/api/doReads.md +136 -0
- package/docs/api/doUndelete.md +98 -0
- package/docs/api/doUpdate.md +129 -0
- package/docs/api/getRoleName.md +84 -0
- package/docs/api/loadFromPackage.md +153 -0
- package/docs/api/marshalRecordFromSourceToObject.md +92 -0
- package/docs/api/query.md +133 -0
- package/docs/api/rawQueries.md +197 -0
- package/docs/api/reference.md +117 -0
- package/docs/api/setAuthorizer.md +103 -0
- package/docs/api/setDefault.md +90 -0
- package/docs/api/setDefaultIdentifier.md +84 -0
- package/docs/api/setDomain.md +56 -0
- package/docs/api/setIDUser.md +91 -0
- package/docs/api/setJsonSchema.md +92 -0
- package/docs/api/setProvider.md +87 -0
- package/docs/api/setSchema.md +107 -0
- package/docs/api/setScope.md +68 -0
- package/docs/api/validateObject.md +119 -0
- package/docs/architecture.md +316 -0
- package/docs/audit-tracking.md +226 -0
- package/docs/configuration.md +317 -0
- package/docs/providers/meadow-endpoints.md +306 -0
- package/docs/providers/mongodb.md +319 -0
- package/docs/providers/postgresql.md +312 -0
- package/docs/providers/rocksdb.md +297 -0
- package/docs/query-dsl.md +269 -0
- package/docs/quick-start.md +384 -0
- package/docs/raw-queries.md +193 -0
- package/docs/retold-catalog.json +61 -1
- package/docs/retold-keyword-index.json +15860 -4839
- package/docs/soft-deletes.md +224 -0
- package/package.json +44 -13
- package/scripts/bookstore-seed-postgresql.sql +135 -0
- package/scripts/dgraph-test-db.sh +144 -0
- package/scripts/meadow-test-cleanup.sh +5 -1
- package/scripts/mongodb-test-db.sh +98 -0
- package/scripts/postgresql-test-db.sh +124 -0
- package/scripts/solr-test-db.sh +135 -0
- package/source/Meadow.js +5 -0
- package/source/providers/Meadow-Provider-DGraph.js +679 -0
- package/source/providers/Meadow-Provider-MeadowEndpoints.js +1 -1
- package/source/providers/Meadow-Provider-MongoDB.js +527 -0
- package/source/providers/Meadow-Provider-PostgreSQL.js +361 -0
- package/source/providers/Meadow-Provider-RocksDB.js +1300 -0
- package/source/providers/Meadow-Provider-Solr.js +726 -0
- package/test/Meadow-Provider-DGraph_tests.js +741 -0
- package/test/Meadow-Provider-MongoDB_tests.js +661 -0
- package/test/Meadow-Provider-PostgreSQL_tests.js +787 -0
- package/test/Meadow-Provider-RocksDB_tests.js +887 -0
- package/test/Meadow-Provider-SQLiteBrowser-Headless_tests.js +657 -0
- package/test/Meadow-Provider-SQLiteBrowser_tests.js +895 -0
- package/test/Meadow-Provider-Solr_tests.js +679 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# setIDUser
|
|
2
|
+
|
|
3
|
+
Set the user ID used for create, update, and delete audit stamps.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
meadow.setIDUser(pIDUser)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Name | Type | Description |
|
|
14
|
+
|------|------|-------------|
|
|
15
|
+
| `pIDUser` | `number` | The user ID to stamp on records |
|
|
16
|
+
|
|
17
|
+
## Returns
|
|
18
|
+
|
|
19
|
+
Returns the Meadow instance for chaining.
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
`setIDUser` sets the fallback user ID that Meadow uses when stamping audit
|
|
24
|
+
columns (`CreatingIDUser`, `ModifyingIDUser`, `DeletingIDUser`) during create,
|
|
25
|
+
update, and delete operations.
|
|
26
|
+
|
|
27
|
+
The user ID resolution order for CRUD operations is:
|
|
28
|
+
|
|
29
|
+
1. `pQuery.query.IDUser` -- if already set directly on the query
|
|
30
|
+
2. `pQuery.userID` -- if set on the query and is a non-negative integer
|
|
31
|
+
3. `meadow.userIdentifier` -- the fallback value set by `setIDUser`
|
|
32
|
+
|
|
33
|
+
The default value is `0`.
|
|
34
|
+
|
|
35
|
+
## Examples
|
|
36
|
+
|
|
37
|
+
### Set a global user ID
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
var meadow = libMeadow.new(tmpFable, 'Book')
|
|
41
|
+
.setIDUser(42);
|
|
42
|
+
|
|
43
|
+
console.log(meadow.userIdentifier); // 42
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Override per-query
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
meadow.setIDUser(1); // Default user
|
|
50
|
+
|
|
51
|
+
// This query uses user 99 instead
|
|
52
|
+
var tmpQuery = meadow.query
|
|
53
|
+
.setIDUser(99)
|
|
54
|
+
.addRecord({ Title: 'Dune' });
|
|
55
|
+
|
|
56
|
+
meadow.doCreate(tmpQuery,
|
|
57
|
+
function (pError, pCreateQuery, pReadQuery, pRecord)
|
|
58
|
+
{
|
|
59
|
+
// CreatingIDUser will be 99, not 1
|
|
60
|
+
console.log(pRecord.CreatingIDUser);
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Typical use in a web server
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
// In a request handler, set the user from the session
|
|
68
|
+
app.get('/api/Books',
|
|
69
|
+
function (pRequest, pResponse)
|
|
70
|
+
{
|
|
71
|
+
var tmpMeadow = meadow.setIDUser(pRequest.session.UserID);
|
|
72
|
+
var tmpQuery = tmpMeadow.query;
|
|
73
|
+
|
|
74
|
+
tmpMeadow.doReads(tmpQuery,
|
|
75
|
+
function (pError, pQuery, pRecords)
|
|
76
|
+
{
|
|
77
|
+
pResponse.send(pRecords);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Notes
|
|
83
|
+
|
|
84
|
+
- The value set by `setIDUser` is accessible via the `userIdentifier`
|
|
85
|
+
read-only property.
|
|
86
|
+
|
|
87
|
+
- `setIDUser` sets a fallback. If the query already has `IDUser` set, the
|
|
88
|
+
query-level value takes precedence.
|
|
89
|
+
|
|
90
|
+
- The default value is `0`, which typically represents an unauthenticated or
|
|
91
|
+
system user.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# setJsonSchema
|
|
2
|
+
|
|
3
|
+
Set the JSON Schema used for object validation.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
meadow.setJsonSchema(pJsonSchema)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Name | Type | Description |
|
|
14
|
+
|------|------|-------------|
|
|
15
|
+
| `pJsonSchema` | `Object` | A JSON Schema (draft-04) object |
|
|
16
|
+
|
|
17
|
+
## Returns
|
|
18
|
+
|
|
19
|
+
Returns the Meadow instance for chaining.
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
`setJsonSchema` replaces the current JSON Schema and creates a new validator
|
|
24
|
+
using the `is-my-json-valid` library with `greedy` and `verbose` modes enabled.
|
|
25
|
+
|
|
26
|
+
- **greedy**: The validator checks all properties rather than stopping at the
|
|
27
|
+
first error.
|
|
28
|
+
- **verbose**: Error objects include the full path and value of the failing
|
|
29
|
+
property.
|
|
30
|
+
|
|
31
|
+
Once set, use `validateObject(pObject)` to validate records against the schema.
|
|
32
|
+
|
|
33
|
+
## Examples
|
|
34
|
+
|
|
35
|
+
### Set a JSON Schema
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
meadow.setJsonSchema({
|
|
39
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
|
40
|
+
"title": "Book",
|
|
41
|
+
"type": "object",
|
|
42
|
+
"properties": {
|
|
43
|
+
"Title": {
|
|
44
|
+
"type": "string",
|
|
45
|
+
"minLength": 1
|
|
46
|
+
},
|
|
47
|
+
"Author": {
|
|
48
|
+
"type": "string"
|
|
49
|
+
},
|
|
50
|
+
"PageCount": {
|
|
51
|
+
"type": "integer",
|
|
52
|
+
"minimum": 1
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"required": ["Title"]
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Validate after setting
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
var tmpResult = meadow.validateObject({ Title: 'Dune', PageCount: 412 });
|
|
63
|
+
console.log(tmpResult.Valid); // true
|
|
64
|
+
|
|
65
|
+
var tmpBad = meadow.validateObject({ PageCount: -5 });
|
|
66
|
+
console.log(tmpBad.Valid); // false
|
|
67
|
+
console.log(tmpBad.Errors); // Array of error details
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Chain with other configuration
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
var meadow = libMeadow.new(tmpFable, 'Book')
|
|
74
|
+
.setSchema(bookSchema)
|
|
75
|
+
.setJsonSchema(bookJsonSchema)
|
|
76
|
+
.setDefault(bookDefaultObject);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Notes
|
|
80
|
+
|
|
81
|
+
- If `pJsonSchema` is not an object, a minimal fallback schema is used:
|
|
82
|
+
`{ title: 'Unknown', type: 'object', required: [] }`.
|
|
83
|
+
|
|
84
|
+
- The JSON Schema is separate from the column schema (set via `setSchema`).
|
|
85
|
+
The column schema drives query generation and marshalling. The JSON Schema
|
|
86
|
+
drives object validation.
|
|
87
|
+
|
|
88
|
+
- The JSON Schema is accessible via the `jsonSchema` read-only property.
|
|
89
|
+
|
|
90
|
+
- Validation uses JSON Schema draft-04. See
|
|
91
|
+
[json-schema.org](http://json-schema.org/draft-04/schema#) for the
|
|
92
|
+
specification.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# setProvider
|
|
2
|
+
|
|
3
|
+
Set the database provider used for query execution.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
meadow.setProvider(pProviderName)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Name | Type | Description |
|
|
14
|
+
|------|------|-------------|
|
|
15
|
+
| `pProviderName` | `string` | The name of the database provider |
|
|
16
|
+
|
|
17
|
+
## Returns
|
|
18
|
+
|
|
19
|
+
Returns the Meadow instance for chaining.
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
`setProvider` loads a database provider module by name and binds it to the
|
|
24
|
+
Meadow instance. After loading, it calls `updateProviderState()` to synchronize
|
|
25
|
+
the current scope, schema, and identifier settings with the new provider.
|
|
26
|
+
|
|
27
|
+
If `pProviderName` is not a string, or if the provider module fails to load,
|
|
28
|
+
Meadow falls back to the `'None'` provider and logs an error.
|
|
29
|
+
|
|
30
|
+
The provider is also initialized during Meadow construction from the
|
|
31
|
+
`MeadowProvider` Fable setting (default `'None'`).
|
|
32
|
+
|
|
33
|
+
## Valid Provider Names
|
|
34
|
+
|
|
35
|
+
| Provider | Description |
|
|
36
|
+
|----------|-------------|
|
|
37
|
+
| `'MySQL'` | MySQL / MariaDB |
|
|
38
|
+
| `'MSSQL'` | Microsoft SQL Server |
|
|
39
|
+
| `'PostgreSQL'` | PostgreSQL |
|
|
40
|
+
| `'SQLite'` | SQLite |
|
|
41
|
+
| `'MongoDB'` | MongoDB |
|
|
42
|
+
| `'RocksDB'` | RocksDB |
|
|
43
|
+
| `'DGraph'` | Dgraph |
|
|
44
|
+
| `'Solr'` | Apache Solr |
|
|
45
|
+
| `'ALASQL'` | AlaSQL (in-memory / browser) |
|
|
46
|
+
| `'MeadowEndpoints'` | Remote Meadow REST endpoints |
|
|
47
|
+
| `'None'` | No-op provider (default) |
|
|
48
|
+
|
|
49
|
+
## Examples
|
|
50
|
+
|
|
51
|
+
### Set provider explicitly
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
var meadow = libMeadow.new(tmpFable, 'Book')
|
|
55
|
+
.setProvider('MySQL');
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Set provider via Fable settings
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
var tmpFable = libFable.new({ MeadowProvider: 'PostgreSQL' });
|
|
62
|
+
var meadow = libMeadow.new(tmpFable, 'Book');
|
|
63
|
+
// Provider is already PostgreSQL from settings
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Chain with other configuration
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
var meadow = libMeadow.new(tmpFable, 'Book')
|
|
70
|
+
.setProvider('MySQL')
|
|
71
|
+
.setSchema(bookSchema)
|
|
72
|
+
.setDefaultIdentifier('IDBook');
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Notes
|
|
76
|
+
|
|
77
|
+
- Provider names are **case sensitive**. `'MySQL'` is valid; `'mysql'` is not.
|
|
78
|
+
|
|
79
|
+
- When a provider fails to load, Meadow logs the error and silently falls back
|
|
80
|
+
to `'None'`. The `'None'` provider is a no-op that does not execute any
|
|
81
|
+
database operations.
|
|
82
|
+
|
|
83
|
+
- Changing the provider calls `updateProviderState()`, which passes the current
|
|
84
|
+
scope, schema, default identifier, and GUID identifier to the new provider.
|
|
85
|
+
|
|
86
|
+
- The provider can be changed at any time, but in practice it is typically set
|
|
87
|
+
once during initialization.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# setSchema
|
|
2
|
+
|
|
3
|
+
Set the column schema array for this Meadow instance.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
meadow.setSchema(pSchema)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Name | Type | Description |
|
|
14
|
+
|------|------|-------------|
|
|
15
|
+
| `pSchema` | `Array` | An array of schema column definition objects |
|
|
16
|
+
|
|
17
|
+
## Returns
|
|
18
|
+
|
|
19
|
+
Returns the Meadow instance for chaining.
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
`setSchema` replaces the current column schema with a new array of column
|
|
24
|
+
definitions. Each element describes a column and its behavior during CRUD
|
|
25
|
+
operations. After setting the schema, `updateProviderState()` is called to
|
|
26
|
+
synchronize the schema with the active provider.
|
|
27
|
+
|
|
28
|
+
## Schema Column Object
|
|
29
|
+
|
|
30
|
+
Each element in the schema array is an object with the following properties:
|
|
31
|
+
|
|
32
|
+
| Property | Type | Required | Description |
|
|
33
|
+
|----------|------|----------|-------------|
|
|
34
|
+
| `Column` | `string` | Yes | The column name |
|
|
35
|
+
| `Type` | `string` | Yes | The column type (see below) |
|
|
36
|
+
| `Size` | `string` | No | Size constraint (e.g. `'128'` for VARCHAR(128)) |
|
|
37
|
+
|
|
38
|
+
## Column Types
|
|
39
|
+
|
|
40
|
+
| Type | Description |
|
|
41
|
+
|------|-------------|
|
|
42
|
+
| `AutoIdentity` | Auto-incrementing primary key |
|
|
43
|
+
| `AutoGUID` | Automatically generated GUID |
|
|
44
|
+
| `CreateDate` | Timestamp set on record creation |
|
|
45
|
+
| `CreateIDUser` | User ID stamped on record creation |
|
|
46
|
+
| `UpdateDate` | Timestamp set on record update |
|
|
47
|
+
| `UpdateIDUser` | User ID stamped on record update |
|
|
48
|
+
| `Deleted` | Soft-delete flag (0 or 1) |
|
|
49
|
+
| `DeleteIDUser` | User ID stamped on soft delete |
|
|
50
|
+
| `DeleteDate` | Timestamp set on soft delete |
|
|
51
|
+
| `String` | String/VARCHAR column |
|
|
52
|
+
| `Number` | Numeric column |
|
|
53
|
+
| `DateTime` | Date/time column |
|
|
54
|
+
|
|
55
|
+
## Examples
|
|
56
|
+
|
|
57
|
+
### Full schema definition
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
meadow.setSchema([
|
|
61
|
+
{ Column: 'IDBook', Type: 'AutoIdentity' },
|
|
62
|
+
{ Column: 'GUIDBook', Type: 'AutoGUID' },
|
|
63
|
+
{ Column: 'Title', Type: 'String', Size: '256' },
|
|
64
|
+
{ Column: 'Author', Type: 'String', Size: '128' },
|
|
65
|
+
{ Column: 'PageCount', Type: 'Number' },
|
|
66
|
+
{ Column: 'Created', Type: 'CreateDate' },
|
|
67
|
+
{ Column: 'CreatingIDUser', Type: 'CreateIDUser' },
|
|
68
|
+
{ Column: 'Modified', Type: 'UpdateDate' },
|
|
69
|
+
{ Column: 'ModifyingIDUser', Type: 'UpdateIDUser' },
|
|
70
|
+
{ Column: 'Deleted', Type: 'Deleted' },
|
|
71
|
+
{ Column: 'DeletingIDUser', Type: 'DeleteIDUser' },
|
|
72
|
+
{ Column: 'DeleteDate', Type: 'DeleteDate' }
|
|
73
|
+
]);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Minimal schema without soft delete
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
meadow.setSchema([
|
|
80
|
+
{ Column: 'IDWidget', Type: 'AutoIdentity' },
|
|
81
|
+
{ Column: 'GUIDWidget', Type: 'AutoGUID' },
|
|
82
|
+
{ Column: 'Name', Type: 'String', Size: '64' }
|
|
83
|
+
]);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Access the schema after setting it
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
console.log(meadow.schema);
|
|
90
|
+
// [{ Column: 'IDBook', Type: 'AutoIdentity' }, ...]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Notes
|
|
94
|
+
|
|
95
|
+
- If `pSchema` is not an object, the schema falls back to a minimal default:
|
|
96
|
+
`{ title: 'Unknown', type: 'object', required: [] }`.
|
|
97
|
+
|
|
98
|
+
- The schema determines whether soft delete or hard delete is used. If the
|
|
99
|
+
schema contains a column with `Type: 'Deleted'`, all `doDelete` calls perform
|
|
100
|
+
soft deletes.
|
|
101
|
+
|
|
102
|
+
- The schema is passed to the provider via `updateProviderState()` so the
|
|
103
|
+
provider can use it for query generation and marshalling.
|
|
104
|
+
|
|
105
|
+
- The `schema` property returns the raw schema array. The `schemaFull` property
|
|
106
|
+
returns the full Meadow-Schema object which includes `validateObject`,
|
|
107
|
+
`defaultObject`, and `authorizer`.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# setScope
|
|
2
|
+
|
|
3
|
+
Set the entity scope (table name) for this Meadow instance.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
meadow.setScope(pScope)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Name | Type | Description |
|
|
14
|
+
|------|------|-------------|
|
|
15
|
+
| `pScope` | `string` | The entity scope, typically a database table name (e.g. `'Book'`) |
|
|
16
|
+
|
|
17
|
+
## Returns
|
|
18
|
+
|
|
19
|
+
Returns the Meadow instance for chaining.
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
`setScope` updates the internal scope and synchronizes it with the FoxHound
|
|
24
|
+
query object and the active provider. The scope is used as the table name (or
|
|
25
|
+
collection name) in generated queries.
|
|
26
|
+
|
|
27
|
+
The scope is also set during construction via the second argument to
|
|
28
|
+
`libMeadow.new(pFable, pScope)`. If no scope is provided at construction time,
|
|
29
|
+
it defaults to `'Unknown'`.
|
|
30
|
+
|
|
31
|
+
## Examples
|
|
32
|
+
|
|
33
|
+
### Set scope at construction
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
var meadow = libMeadow.new(tmpFable, 'Book');
|
|
37
|
+
console.log(meadow.scope); // 'Book'
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Change scope after construction
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
var meadow = libMeadow.new(tmpFable, 'Book');
|
|
44
|
+
meadow.setScope('Author');
|
|
45
|
+
console.log(meadow.scope); // 'Author'
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Chain with other configuration
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
var meadow = libMeadow.new(tmpFable)
|
|
52
|
+
.setScope('Book')
|
|
53
|
+
.setSchema(bookSchema)
|
|
54
|
+
.setProvider('MySQL');
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Notes
|
|
58
|
+
|
|
59
|
+
- Changing the scope calls `updateProviderState()` to synchronize the new scope
|
|
60
|
+
with the active provider.
|
|
61
|
+
|
|
62
|
+
- The scope also updates the internal FoxHound query object via
|
|
63
|
+
`_Query.setScope(pScope)`, so that all subsequent cloned queries inherit the
|
|
64
|
+
new scope.
|
|
65
|
+
|
|
66
|
+
- The `defaultIdentifier` is **not** automatically updated when the scope
|
|
67
|
+
changes. If you change the scope, you may also need to call
|
|
68
|
+
`setDefaultIdentifier` to match the new table's primary key column.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# validateObject
|
|
2
|
+
|
|
3
|
+
Validate a JavaScript object against the JSON Schema.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
meadow.validateObject(pObject)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Also accessible via the full schema object:
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
meadow.schemaFull.validateObject(pObject)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Parameters
|
|
18
|
+
|
|
19
|
+
| Name | Type | Description |
|
|
20
|
+
|------|------|-------------|
|
|
21
|
+
| `pObject` | `Object` | The plain JavaScript object to validate |
|
|
22
|
+
|
|
23
|
+
## Returns
|
|
24
|
+
|
|
25
|
+
| Property | Type | Description |
|
|
26
|
+
|----------|------|-------------|
|
|
27
|
+
| `Valid` | `boolean` | `true` if the object passes validation, `false` otherwise |
|
|
28
|
+
| `Errors` | `Array` | Present only when `Valid` is `false`. Array of error detail objects |
|
|
29
|
+
|
|
30
|
+
## Description
|
|
31
|
+
|
|
32
|
+
`validateObject` checks the given object against the JSON Schema that was
|
|
33
|
+
previously set via `setJsonSchema()`. The underlying validator is
|
|
34
|
+
`is-my-json-valid` with `greedy` and `verbose` modes, meaning:
|
|
35
|
+
|
|
36
|
+
- All properties are checked (not just until the first failure).
|
|
37
|
+
- Each error includes the full property path and the actual value.
|
|
38
|
+
|
|
39
|
+
## Error Object Format
|
|
40
|
+
|
|
41
|
+
Each entry in the `Errors` array is an object with properties including:
|
|
42
|
+
|
|
43
|
+
| Property | Type | Description |
|
|
44
|
+
|----------|------|-------------|
|
|
45
|
+
| `field` | `string` | Dot-path to the failing property (e.g. `'data.Title'`) |
|
|
46
|
+
| `message` | `string` | Description of the validation failure |
|
|
47
|
+
| `value` | `*` | The actual value that failed validation |
|
|
48
|
+
|
|
49
|
+
## Examples
|
|
50
|
+
|
|
51
|
+
### Successful validation
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
meadow.setJsonSchema({
|
|
55
|
+
"title": "Book",
|
|
56
|
+
"type": "object",
|
|
57
|
+
"properties": {
|
|
58
|
+
"Title": { "type": "string", "minLength": 1 },
|
|
59
|
+
"PageCount": { "type": "integer", "minimum": 1 }
|
|
60
|
+
},
|
|
61
|
+
"required": ["Title"]
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
var tmpResult = meadow.validateObject({
|
|
65
|
+
Title: 'Dune',
|
|
66
|
+
PageCount: 412
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
console.log(tmpResult.Valid); // true
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Failed validation
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
var tmpResult = meadow.validateObject({
|
|
76
|
+
PageCount: -5
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log(tmpResult.Valid); // false
|
|
80
|
+
console.log(tmpResult.Errors);
|
|
81
|
+
// [
|
|
82
|
+
// { field: 'data.Title', message: 'is required', ... },
|
|
83
|
+
// { field: 'data.PageCount', message: 'is less than minimum', value: -5, ... }
|
|
84
|
+
// ]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Validate before create
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
var tmpRecord = { Title: 'Neuromancer', Author: 'Gibson' };
|
|
91
|
+
var tmpValidation = meadow.validateObject(tmpRecord);
|
|
92
|
+
|
|
93
|
+
if (!tmpValidation.Valid)
|
|
94
|
+
{
|
|
95
|
+
console.error('Validation errors:', tmpValidation.Errors);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
var tmpQuery = meadow.query.addRecord(tmpRecord);
|
|
100
|
+
meadow.doCreate(tmpQuery,
|
|
101
|
+
function (pError, pCreateQuery, pReadQuery, pRecord)
|
|
102
|
+
{
|
|
103
|
+
console.log('Created:', pRecord);
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Notes
|
|
108
|
+
|
|
109
|
+
- `setJsonSchema()` must be called before `validateObject()` for meaningful
|
|
110
|
+
validation. If no JSON Schema has been set, the validator may produce
|
|
111
|
+
unexpected results.
|
|
112
|
+
|
|
113
|
+
- `validateObject` is the same function reference on both `meadow` and
|
|
114
|
+
`meadow.schemaFull`. Both point to the same underlying validator.
|
|
115
|
+
|
|
116
|
+
- Meadow does **not** automatically validate records during `doCreate` or
|
|
117
|
+
`doUpdate`. Validation must be called explicitly by application code.
|
|
118
|
+
|
|
119
|
+
- The validator uses JSON Schema draft-04 semantics.
|