Skip to content

Commit

Permalink
Moved copy() from VingKind to VingRecord as its easier to use and les…
Browse files Browse the repository at this point in the history
…s fragile.
  • Loading branch information
rizen committed Apr 12, 2024
1 parent 8ea8352 commit 4e547e8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
3 changes: 3 additions & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ outline: deep
---
# Change Log

## 2024-04-12
* Moved copy() from VingKind to VingRecord as its easier to use and less fragile.

## 2024-04-11
* Made currentUserStore more fault tollerant.
* Implemented: add required to FormSelect #95
Expand Down
2 changes: 1 addition & 1 deletion docs/subsystems/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Combines a bunch of commonly used ving utilities into a single roll up package.

Exports all of the useful functions from drizzle that are normally spread out across many different libraries.

### ving/record/VingRecord.mjs
### ving/record/utils.mjs

Exports a `useKind()` function that allows you to dynamically instanciate any VingKind.

Expand Down
31 changes: 16 additions & 15 deletions ving/record/VingRecord.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ export class VingRecord {
throw ving.ouch(403, `You do not have the privileges to access ${schema.kind}.`)
}

/**
* Creates a new record based upon the props of an old one. Note that this doesn't save the new record to the database, you'll need to call `insert()` on it to do that.
*
* Usage: `const newRecord = this.copy()`
*
* @returns {Object} a newly minted record based upon the original props
*/
async copy() {
const schema = findVingSchema(getTableName(this.table));
const kind = await ving.useKind(schema.kind);
let props = { ...this.getAll() };
delete props.id;
delete props.createdAt;
return kind.mint(props);
}

/**
* Removes the record from the database
*
Expand Down Expand Up @@ -631,21 +647,6 @@ export class VingKind {
return undefined;
}

/**
* Creates a new record based upon the props of an old one. Note that this doesn't save the new record to the database, you'll need to call `insert()` on it to do that.
*
* Usage: `const newRecord = Users.copy(user.getAll())`
*
* @param {Object} originalProps a list of props to be copied
* @returns a newly minted record based upon the original props
*/
copy(originalProps) {
let props = { ...originalProps };
delete props.id;
delete props.createdAt;
return this.mint(props);
}

/**
* Get the number of records of this kind
*
Expand Down
3 changes: 2 additions & 1 deletion ving/tests/Users.vingrecord.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ describe('Users', async () => {
await warden.setPostedProps({ useAsDisplayName: 'email' }, warden);
expect(warden.get('useAsDisplayName')).toBe('email');
});
const guard = Users.copy(captain.getAll());
const guard = await captain.copy();
test("clone a record", () => {
expect(guard.get('realName')).toBe('Byron Hadley');
guard.setAll({
username: 'guard',
email: 'guard@shawshank.jail',
Expand Down

0 comments on commit 4e547e8

Please sign in to comment.