Skip to content

Instantly share code, notes, and snippets.

@jannesiera
Last active May 19, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jannesiera/662b6b51b45f50c098fa337650a9ae06 to your computer and use it in GitHub Desktop.
Save jannesiera/662b6b51b45f50c098fa337650a9ae06 to your computer and use it in GitHub Desktop.
Example API for an ORM for Vuex (graph-like/inspired)
/* New test relational (graph-like) db */
// NOTE: Relationships are like *arrows* from one entity to another, they are directed (the relationship is defined on the target entity)
// and *always* many-to-many. If you want a one-to-many relationship, make sure you just add one *arrow* and always take the first().
// Demo Structure of state
const s = {
db: {
posts: {
guid1: {
entity: {
title: 'some title',
content: 'some very long string'
},
relations: {
// none
}
},
guid2: {
entity: {
title: 'some other title',
content: 'some other very long string'
},
relations: {
// none
}
},
},
comments: {
guid3: {
entity: {
value: 'some comment'
},
relations: {
posts: ["guid1"]
}
}
}
}
}
// Demo API
const orm = undefined;
// get by id
const someComment = orm.comments['guid3'].get(); // output: { id: 'guid3', value: 'some comment' }
// get by query on entity value
const sameComment = orm.comments.where(comment => comment.value === 'some comment').first(); // output: { id: 'guid3', value: 'some comment' }
// get by query on relation
const somePostByComment = orm.comments['guid3'].relation.posts.first(); // ouput: { id: 'guid1', title: 'some title', content: 'some very long string' }
// get by object
const samePostByComment = orm.from(someComment).relation.posts.first(); // ouput: { id: 'guid1', title: 'some title', content: 'some very long string' }
// get list
const commentList = orm.comments.where(comment => comment.value === 'some comment').all(); // output: [ { id: 'guid3', value: 'some comment' } ]
// get list from relation
const postList = orm.comments['guid3'].relation.posts.all(); // ouput: [ { id: 'guid1', title: 'some title', content: 'some very long string' }, { id: 'guid2', title: 'some other title', content: 'some other very long string' } ]
// update
orm.posts['guid1'].update(post => ({ title: 'not some title' })); // will do Object.assign internally
// or
orm.from(somePostByComment).update(post => ({ title: 'not some title' })); // will do Object.assign internally
// delete
orm.posts['guid1'].delete(); // will cascade all relations to this (but not entities pointing to this)
// or
orm.from(somePostByComment).delete(); // will cascade all relations to this (but not entities pointing to this)
// create
const createdPost = orm.posts.create({ title: 'The Tao of Pooh', content: 'some nice text' }).get(); // add relations afterwards / or don't get and chain relations then
// add relation
const createdComment = orm.comments.create({ value: 'Great read!' })
.relate(createdPost)
.get();
// deleate relation
orm.from(createdComment).unrelate(createdPost);
// get all comments for a post
const someAll = orm.comments.where((comment, relations) => relations.posts['guid1']).all();
// get all posts that have a comment with the value 'some comment'
const posts = orm.comments.where(comment => comment.value === 'some comment').posts.all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment