Skip to content

Commit

Permalink
release: 1.1.0 ✨ (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba authored Dec 10, 2016
1 parent 3f15362 commit 3968c8e
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 85 deletions.
104 changes: 104 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,109 @@
# Lux Changelog

### 1.1.0 (Dec 10, 2016)

##### Features

###### Transactions

This release introduces a public transaction api to the model class. Changes are
backwards compatible with the previous model api.

Internally, all methods that modify the state of the database are wrapped in
transactions. If the transaction fails, all calls to `create`, `save`, or
`update` will be rolled back to the state before the transaction began.

**Example:**

```javascript
import User from 'app/models/user';

// This internally uses a transaction.
await User.create({
firstName: 'New',
lastName: 'User'
});
```

### Working With Transactions

You have the ability to manually specify the transaction that will be used for a
`create`, `update`, or `save` call with the static and instance method, `transacting`.

**Example:**

```javascript
import { Model } from 'lux-framework';

import Profile from 'app/models/profile';

class User extends Model {
static hasOne = {
profile: {
inverse: 'user'
}
};

static hooks = {
async beforeCreate(user, trx) {
// If the transaction fails the profile instance will not be persisted.
user.profile = await Profile
.transacting(trx)
.create();
}
};
}
```

You can also manually trigger create a transaction if you plan on creating many
model instances as once.

**Example:**

```javascript
import User from 'app/models/user';

User.transaction(trx => (
Promise.all([
User
.transacting(trx)
.create({
firstName: 'New',
lastName: 'User'
}),
User
.transacting(trx)
.create({
firstName: 'New',
lastName: 'User'
}),
User
.transacting(trx)
.create({
firstName: 'New',
lastName: 'User'
})
])
));
```

##### Commits

* [[`3f15362600`](https://github.com/postlight/lux/commit/3f15362600)] - **deps**: update babel-core to version 6.20.0 (#556) (Greenkeeper)
* [[`9a20c5ce11`](https://github.com/postlight/lux/commit/9a20c5ce11)] - **deps**: update eslint to version 3.12.0 (#557) (Greenkeeper)
* [[`7f53cd230c`](https://github.com/postlight/lux/commit/7f53cd230c)] - **docs**: fix broken logo in readme (#555) (Zachary Golba)
* [[`590956ed52`](https://github.com/postlight/lux/commit/590956ed52)] - **docs**: add preliminary guide files (#554) (Zachary Golba)
* [[`16d224b4e7`](https://github.com/postlight/lux/commit/16d224b4e7)] - **feat**: use transactions when writing to the database (#527) (Zachary Golba)
* [[`9e89b042cd`](https://github.com/postlight/lux/commit/9e89b042cd)] - **deps**: update eslint-plugin-flowtype to version 2.29.1 (#549) (Greenkeeper)
* [[`5b3e91e5f9`](https://github.com/postlight/lux/commit/5b3e91e5f9)] - **deps**: update eslint to version 3.11.1 (#547) (Greenkeeper)
* [[`4eb0c9b926`](https://github.com/postlight/lux/commit/4eb0c9b926)] - **deps**: update eslint-plugin-flowtype to version 2.28.2 (#546) (Greenkeeper)
* [[`42f1707ac8`](https://github.com/postlight/lux/commit/42f1707ac8)] - **deps**: update eslint to version 3.11.0 🚀 (#539) (Greenkeeper)
* [[`39adf76c3a`](https://github.com/postlight/lux/commit/39adf76c3a)] - **deps**: update rollup to version 0.36.4 (#536) (Greenkeeper)
* [[`23189f535b`](https://github.com/postlight/lux/commit/23189f535b)] - **deps**: update flow-bin to version 0.36.0 🚀 (#537) (Greenkeeper)
* [[`394d3132e7`](https://github.com/postlight/lux/commit/394d3132e7)] - **deps**: update nyc to version 10.0.0 (#535) (Greenkeeper)
* [[`ef33526860`](https://github.com/postlight/lux/commit/ef33526860)] - **deps**: update mocha to version 3.2.0 (#538) (Greenkeeper)
* [[`760ae5f68c`](https://github.com/postlight/lux/commit/760ae5f68c)] - **release**: 1.0.5 🔧 (#534) (Zachary Golba)

### 1.0.5 (Nov 20, 2016)

##### Commits
Expand Down
24 changes: 14 additions & 10 deletions examples/social-network/app/models/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const createMessageTemplate = resourceType => (name, reactionType) => (
*/
class Action extends Model {
static hooks = {
async afterCreate(action) {
await action.notifyOwner();
async afterCreate(action, trx) {
await action.notifyOwner(trx);
}
};

Expand All @@ -42,10 +42,12 @@ class Action extends Model {
]);

if (user && post) {
await Notification.create({
message: `${user.name} commented on your post!`,
recipientId: post.userId
});
await Notification
.transacting(trx)
.create({
message: `${user.name} commented on your post!`,
recipientId: post.userId
});
}
}
} else if (trackableType === 'Reaction') {
Expand Down Expand Up @@ -80,10 +82,12 @@ class Action extends Model {
]);

if (user && reactable) {
await Notification.create({
message: createMessage(user.name, reaction.type),
recipientId: reactable.userId
});
await Notification
.transacting(trx)
.create({
message: createMessage(user.name, reaction.type),
recipientId: reactable.userId,
});
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/social-network/app/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Comment extends Model {
};

static hooks = {
async afterCreate(comment) {
await track(comment);
async afterCreate(comment, trx) {
await track(comment, trx);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions examples/social-network/app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Post extends Model {
};

static hooks = {
async afterCreate(post) {
await track(post);
async afterCreate(post, trx) {
await track(post, trx);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions examples/social-network/app/models/reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Reaction extends Model {
}
},

async afterCreate(reaction) {
await track(reaction);
async afterCreate(reaction, trx) {
await track(reaction, trx);
}
};
}
Expand Down
23 changes: 10 additions & 13 deletions examples/social-network/app/utils/track.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Action from 'app/models/action';
import Action from '../models/action';

export default async function track(trackable) {
export default function track(trackable, trx) {
if (trackable) {
const {
id: trackableId,
constructor: {
name: trackableType
}
} = trackable;

return await Action.create({
trackableType,
trackableId
});
return Action
.transacting(trx)
.create({
trackableId: trackable.id,
trackableType: trackable.constructor.name
});
}

return Promise.resolve();
}
96 changes: 54 additions & 42 deletions examples/social-network/db/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,83 @@ const {
lorem,
random,
internet,

helpers: {
randomize
}
} = faker;

export default async function seed() {
export default async function seed(trx) {
await Promise.all(
Array.from(range(1, 100)).map(() => User.create({
name: `${name.firstName()} ${name.lastName()}`,
email: internet.email(),
password: internet.password(randomize([...range(8, 127)]))
}))
Array.from(range(1, 100)).map(() => (
User.transacting(trx).create({
name: `${name.firstName()} ${name.lastName()}`,
email: internet.email(),
password: internet.password(randomize([...range(8, 127)]))
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Friendship.create({
followerId: randomize([...range(1, 100)]),
followeeId: randomize([...range(1, 100)])
}))
Array.from(range(1, 100)).map(() => (
Friendship.transacting(trx).create({
followerId: randomize([...range(1, 100)]),
followeeId: randomize([...range(1, 100)])
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Post.create({
body: lorem.paragraphs(),
title: lorem.sentence(),
userId: randomize([...range(1, 100)]),
isPublic: random.boolean()
}))
Array.from(range(1, 100)).map(() => (
Post.transacting(trx).create({
body: lorem.paragraphs(),
title: lorem.sentence(),
userId: randomize([...range(1, 100)]),
isPublic: random.boolean()
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Tag.create({
name: lorem.word()
}))
Array.from(range(1, 100)).map(() => (
Tag.transacting(trx).create({
name: lorem.word()
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Categorization.create({
postId: randomize([...range(1, 100)]),
tagId: randomize([...range(1, 100)])
}))
Array.from(range(1, 100)).map(() => (
Categorization.transacting(trx).create({
postId: randomize([...range(1, 100)]),
tagId: randomize([...range(1, 100)])
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Comment.create({
message: lorem.sentence(),
edited: random.boolean(),
userId: randomize([...range(1, 100)]),
postId: randomize([...range(1, 100)])
}))
Array.from(range(1, 100)).map(() => (
Comment.transacting(trx).create({
message: lorem.sentence(),
edited: random.boolean(),
userId: randomize([...range(1, 100)]),
postId: randomize([...range(1, 100)])
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Reaction.create({
[`${randomize(['comment', 'post'])}Id`]: randomize([...range(1, 100)]),
userId: randomize([...range(1, 100)]),

type: randomize([
':+1:',
':heart:',
':confetti_ball:',
':laughing:',
':disappointed:'
])
}))
Array.from(range(1, 100)).map(() => (
Reaction.transacting(trx).create({
[`${randomize(['comment', 'post'])}Id`]: randomize([...range(1, 100)]),
userId: randomize([...range(1, 100)]),
type: randomize([
':+1:',
':heart:',
':confetti_ball:',
':laughing:',
':disappointed:'
])
})
))
);
};
2 changes: 1 addition & 1 deletion examples/social-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"babel-preset-lux": "1.3.0",
"bcryptjs": "2.3.0",
"knex": "0.12.6",
"lux-framework": "1.0.5",
"lux-framework": "1.1.0",
"sqlite3": "3.1.4"
},
"devDependencies": {
Expand Down
25 changes: 14 additions & 11 deletions examples/todo/db/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ const {
hacker,
random,
company,

helpers: {
randomize
}
} = faker;

export default async function seed() {
export default async function seed(trx) {
await Promise.all(
Array.from(range(1, 4)).map(() => List.create({
name: `${company.bsAdjective()} tasks`
}))
Array.from(range(1, 4)).map(() => (
List.transacting(trx).create({
name: `${company.bsAdjective()} tasks`
})
))
);

await Promise.all(
Array.from(range(1, 100)).map(() => Task.create({
name: hacker.phrase(),
listId: randomize(Array.from(range(1, 4))),
dueDate: date.future(),
isCompleted: random.boolean()
}))
Array.from(range(1, 100)).map(() => (
Task.transacting(trx).create({
name: hacker.phrase(),
listId: randomize(Array.from(range(1, 4))),
dueDate: date.future(),
isCompleted: random.boolean()
})
))
);
}
2 changes: 1 addition & 1 deletion examples/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"babel-core": "6.14.0",
"babel-preset-lux": "1.3.0",
"knex": "0.12.6",
"lux-framework": "1.0.5",
"lux-framework": "1.1.0",
"sqlite3": "3.1.4"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 3968c8e

Please sign in to comment.