commit a3b7d2c2e978c423ba7d9c9ef289035c36e11c20 Author: cha0s Date: Tue Mar 5 23:06:40 2019 -0600 chore: initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/actions.js b/actions.js new file mode 100644 index 0000000..db68369 --- /dev/null +++ b/actions.js @@ -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'], + }), +}); diff --git a/compose.js b/compose.js new file mode 100644 index 0000000..83f7ebe --- /dev/null +++ b/compose.js @@ -0,0 +1,3 @@ +module.exports = (config) => { +}; + diff --git a/hooks.js b/hooks.js new file mode 100644 index 0000000..bd318b8 --- /dev/null +++ b/hooks.js @@ -0,0 +1,2 @@ +module.exports = () => ({ +}); diff --git a/index.js b/index.js new file mode 100644 index 0000000..e26ccda --- /dev/null +++ b/index.js @@ -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; + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..1ff43b5 --- /dev/null +++ b/package.json @@ -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" + } +}