chore: initial

This commit is contained in:
cha0s 2019-03-05 23:06:40 -06:00
commit a3b7d2c2e9
6 changed files with 123 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

13
actions.js Normal file
View File

@ -0,0 +1,13 @@
const {graphql} = require('graphql');
module.exports = (schema) => ({
'@truss/graphql': async ({payload: {query}}) => {
const result = await graphql(schema, query);
return {
status: 200,
response: JSON.stringify(result),
};
},
'@truss/schema': () => ({
executors: ['@truss/graphql'],
}),
});

3
compose.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = (config) => {
};

2
hooks.js Normal file
View File

@ -0,0 +1,2 @@
module.exports = () => ({
});

84
index.js Normal file
View File

@ -0,0 +1,84 @@
// 3rd party.
const {createDispatcher, invokeHookFlat} = require('@truss/comm');
const Waterline = require('waterline');
const {getGraphQLSchemaFrom} = require('waterline-to-graphql');
// Instance of waterline.
const waterline = new Waterline();
// Dispatcher.
const dispatcher = createDispatcher();
dispatcher.lookupActions(require('./actions'));
dispatcher.lookupHooks(require('./hooks'));
// Connect dispatcher.
dispatcher.connect();
// Gather and register collections.
invokeHookFlat('ormCollections').then((serviceCollections) => {
for (const collections of serviceCollections) {
for (collection of collections) {
waterline.registerModel(Waterline.Collection.extend(collection));
}
}
// Adapter config.
const sailsDiskAdapter = require('sails-disk');
const config = {
adapters: {
disk: sailsDiskAdapter,
},
datastores: {
default: {
adapter: 'disk',
},
},
};
// Initialize waterline.
waterline.initialize(config, async (error, ontology) => {
if (error) {
return console.error(error);
}
const models = ontology.collections || [];
setupAssociations(models);
const schema = getGraphQLSchemaFrom(models);
dispatcher.setArgs(schema);
dispatcher.lookupActions(require('./actions'));
dispatcher.lookupHooks(require('./hooks'));
if (module.hot) {
module.hot.accept('./actions', () => {
dispatcher.lookupActions(require('./actions'));
});
module.hot.accept('./hooks', () => {
dispatcher.lookupHooks(require('./hooks'));
});
}
});
});
function setupAssociations(models) {
for (const id in models) {
const model = models[id];
model._attributes = model.attributes;
const associations = [];
for (const name in model.attributes) {
const attribute = model.attributes[name];
if ('object' !== typeof attribute) {
continue;
}
if (!attribute.model && !attribute.collection) {
continue;
}
var association = {
alias: name,
type: attribute.model ? 'model' : 'collection',
};
if (attribute.model) {
association.model = attribute.model;
}
if (attribute.collection) {
association.collection = attribute.collection;
}
if (attribute.via) {
association.via = attribute.via;
}
associations.push(association);
}
model.associations = associations;
}
}

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "orm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"build": "node -e '' -r '@truss/webpack/task/build'",
"default": "yarn run dev",
"dev": "node -e '' -r '@truss/webpack/task/scaffold'"
},
"license": "MIT",
"dependencies": {
"@truss/comm": "1.x",
"@truss/webpack": "1.x",
"sails-disk": "^1.0.1",
"waterline": "^0.13.6",
"waterline-to-graphql": "^0.0.3"
}
}