chore: initial
This commit is contained in:
commit
a3b7d2c2e9
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules
|
13
actions.js
Normal file
13
actions.js
Normal 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
3
compose.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
module.exports = (config) => {
|
||||
};
|
||||
|
84
index.js
Normal file
84
index.js
Normal 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
20
package.json
Normal 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"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user