Skip to content

Commit

Permalink
Added pseudo props to Records so that in addition to `user.set('admin…
Browse files Browse the repository at this point in the history
…', true)` you can also do `user.admin = true` for both setters and getters.
  • Loading branch information
rizen committed Mar 15, 2024
1 parent 9e0bc3d commit bfcecdb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ outline: deep
---
# Change Log

## 2024-03-15
* Added pseudo props to Records so that in addition to `user.set('admin', true)` you can also do `user.admin = true` for both setters and getters.

## 2024-03-12
* Fixed a security bug where passwords created via the CLI were stored incorrectly.
14 changes: 12 additions & 2 deletions docs/subsystems/ving-record.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const record = users.findOrDie('xxx');

Once you have a record you can use the following methods to manipulate it.


### Reading Data

#### describe
Expand All @@ -263,7 +264,7 @@ Formats everything known about a record into an object that is easily serializab
const description = await record.describe(params)
```

#### Result
##### Result
```js
{
props : { id: 'xxx', ... }, // database properties
Expand All @@ -283,7 +284,7 @@ const description = await record.describe(params)
}
```

#### Parameters
##### Parameters

- **currentUser** - Either a `User` record or a `Session` object can be used to determine what data should be included in the description based upon user privileges.
- **include** - An object to modify the output.
Expand Down Expand Up @@ -373,6 +374,15 @@ Calls `setPostedProps()` and `update()` in a single method call.
await record.updateAndVerify({foo: 'bar', one: 1}, currentUserOrSession);
```

### Pseudo Properties
In addition to all the methods below, every [schema](ving-schema) prop except those of type `virtual` will also get pseudo props added to the record. The means that in addition to using `get('username')` and `set('username', 'adufresne')` methods you can use the pseudo props as getters and setters like this:

```js
const username = user.username;
user.username = 'adufresne';
```


### Privileges

#### canEdit
Expand Down
15 changes: 15 additions & 0 deletions ving/record/VingRecord.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ export class VingRecord {
this.#inserted = inserted;
this.db = db;
this.table = table;
const schema = findVingSchema(getTableName(table));
const pseudoProps = {}
for (const prop of schema.props) {
if (prop.type == 'virtual')
continue;
pseudoProps[prop.name] = {
get() {
return this.get(prop.name);
},
set(value) {
return this.set(prop.name, value);
},
}
}
Object.defineProperties(this, pseudoProps);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ving/tests/Users.vingrecord.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('Users', async () => {
expect(warden.isOwner(captain)).toBe(false);
});
test("can update ving record", async () => {
warden.set('admin', true);
expect(warden.get('admin')).toBe(true);
warden.admin = true;
expect(warden.admin).toBe(true);
await warden.update();
});
test("can refetch ving record", async () => {
Expand Down

0 comments on commit bfcecdb

Please sign in to comment.