Not Seeing Id In Sails JS Response - sails.js

So I'm messing around with SailsJS to try to get an API up and running real fast. I haven't setup a data store yet (will probably use mongodb) but I saw that there is what I'm assuming is like a SQLite database or something. So I generated a model and controller for a User. So in the browser I hit user/create. I see createdAt and updatedAt but no Id. Do I need a real datastore to see an ID? Is this still something you get for free?
Adapter
// Configure installed adapters
// If you define an attribute in your model definition,
// it will override anything from this global config.
module.exports.adapters = {
// If you leave the adapter config unspecified
// in a model definition, 'default' will be used.
'default': 'disk',
// In-memory adapter for DEVELOPMENT ONLY
// (data is NOT preserved when the server shuts down)
memory: {
module: 'sails-dirty',
inMemory: true
},
// Persistent adapter for DEVELOPMENT ONLY
// (data IS preserved when the server shuts down)
// PLEASE NOTE: disk adapter not compatible with node v0.10.0 currently
// because of limitations in node-dirty
// See https://github.com/felixge/node-dirty/issues/34
disk: {
module: 'sails-dirty',
filePath: './.tmp/dirty.db',
inMemory: false
},
// MySQL is the world's most popular relational database.
// Learn more: http://en.wikipedia.org/wiki/MySQL
mysql: {
module : 'sails-mysql',
host : 'YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS',
user : 'YOUR_MYSQL_USER',
password : 'YOUR_MYSQL_PASSWORD',
database : 'YOUR_MYSQL_DB'
}
};
Model
/*---------------------
:: User
-> model
---------------------*/
module.exports = {
attributes: {
firstName: 'STRING',
lastName: 'STRING'
}
};

Do .../user/findAll to list all the users. Each user should have an 'id' property, e.g.
{
"username":"admin",
"password":"$2a$10$SP/hWtlJINkMgkAuAc9bxO1iWsvVcglwEU4ctGCiYpx.7ykaFWt56",
"createdAt":"2013-07-12T17:36:29.852Z",
"updatedAt":"2013-07-12T17:36:29.852Z",
"id":1
}

they removes findAll you can simply use find instad
u can also request like this http://example.de/Model/create?firstname=Foo&lastname=bar

I have checkout this by switching between different DB in a same sails application and its working absolutely fine.
So In this case you can add a property of autoPK to your model and setting it to true like following:
module.exports = {
attribute:{
//your model attributes with validations.
},
autoPK:true
}
If this doesn't works for you then I think there is something wrong with your copy of sails you installed on your system.
Please try to install the newer version (v0.9.8) of sails using npm or try to update your package.json with newer version (v0.9.8) details and excute npm install.

Related

can't get customToJSON working in sails1 model

Using sails 1.2.4 on Ubuntu 18.04. I tried adding customToJSON before and after the attributes in model User, but when I serve the user object to a page, password still shows up. When I serve the user object, I don't want password or isSuperAdmin to appear.
Here is the end of my User.js model script:
}, // end attributes
customToJSON: function() {
return _.omit(this, ['password', 'isSuperAdmin'])
},
}; //end module exports
I may just be omitting something dumb, but if so please let me know.
thx

Working with URL parameters in custom Kibana plugin

I am working on a custom plugin to Kibana (7.5.2). The plugin is of type 'app'. I would like to be able to pass parameters to this plugin in order to pre-load some data from Elasticsearch. I.e., I need to provide users with some specific URLs containing parameters that will be used by the plugin to show only a relevant portion of data.
My problem is that I was not able to find sufficient documentation on this and I do not know what the correct approach should be. I will try to summarize what I know/have done so far:
I have read the official resources on plugin development
I am aware of the fact that _g and _a URL parameters are used to pass state in Kibana applications. However, a) I am not sure if this is the correct approach in my case and b) I also failed to find any information on how my plugin should access the data from these parameters.
I checked the sources of other known plugins, but again, failed to find any clues.
I am able to inject some configuration values using injectUiAppVars in the init method of my plugin (index.js) and retrieve these values in my app (main.js):
index.js:
export default function (kibana) {
return new kibana.Plugin({
require: ['elasticsearch'],
name: ...,
uiExports: {
...
},
...
init(server, options) { // eslint-disable-line no-unused-vars
server.injectUiAppVars('logviewer', async () => {
var kibana_vars = await server.getInjectedUiAppVars('kibana');
var aggregated_vars = { ...kibana_vars, ...{ mycustomparameter: "some value" } }
return aggregated_vars
});
...
}
});
}
main.js
import chrome from 'ui/chrome';
. . .
const mycustomparameter = chrome.getInjected('mycustomparameter');
Providing that I manage to obtain parameters from URL, this would allow me to pass them to my app (via mycustomparameter), but again, I am not sure if this approach is correct.
I tried to get some help via the Elastic forum, but did not receive any answer yet.
My questions
1. Is there any source of information on this particular topic? I am aware of the fact that the plugin API changes frequently, hence I do not expect to find an extensive documentation. Maybe a good example?
Am I completely off course with the way I am trying to achieve it?
Thanks for reading this, any help would be much appreciated!

Multi Tenancy with Mongodb and Gorm

I am trying to save objects in mongodb using multi tenancy at DATABASE level. However, objects always gets saved in the default database.
my application.yml
development:
grails:
mongodb:
tenantResolverClass: org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver
url: mongodb://localhost/test
connections:
1:
url: mongodb://localhost/test1db
2:
url: mongodb://localhost/test2db
options:
maxWaitTime: 10000
I can see that all the connections(default, 1 and 2) are loaded fine into connectionSourceMap in InMemoryConnectionSources.groovy class.
Interceptor function is invoked before controller function and interceptor sets the following property:
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "1")
I was expecting that since Tenant id has been set to "1" and object against key "1" exists in connectionSourceMap, so now objects will be saved in test1db. However this is not the case when I try to save object
Tenants.withCurrent {
domainObj.save(flush: flush, validate: false)
}
I tried to debug the application.
MongoDataStore.java
#Override
public <T1> T1 withNewSession(Serializable tenantId, Closure<T1> callable) {
MongoDatastore mongoDatastore = getDatastoreForTenantId(tenantId);
Session session = mongoDatastore.connect();
try {
DatastoreUtils.bindNewSession(session);
return callable.call(session);
}
finally {
DatastoreUtils.unbindSession(session);
}
}
mongoDataStore object appears to contain the right information with default database string set to test1db. However, the session object is again referring to test db as default db. I could not understand why this is happening and how could I resolve this.
I have a feeling that issue might be related to the following point in the documentation[1]:
11.2.3. Multi Tenancy and the Session Factory
Note that if you reference the default SessionFactory or PlatformTransactionManager in your classes that are injected via Spring, these will not be tenant aware and will point directly to default data source.
But could not figure out on how to resolve the issue.
Thanks for your help and time. Any help is appreciated.
1: http://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy

Cannot get my collection's data in meteor client side js in Meteor

I have written the following code in my client side js:
var resolutionsQ;
Template.body.onCreated(function bodyOnCreated() {
resolutionsQ = new Mongo.Collection("res");
});
Template.body.helpers({
resolutions: function() {
var res = resolutionsQ.find({});
console.log(res);
return resolutionsQ.find({});
}
});
Then in my project folder(in terminal), i wrote:
meteor mongo
After the mongo db console started, I worte:
db.res.insert({title: "hello #1", createdAt: new Date()});
This also worked.
When I wrote this, my frontend application showed everything as expected. Then I shut down my computer, and after sometime switched it on again and tried to run my meteor application. Now I see nothing, I get no error either in server console or browser's console. I don't know what went wrong then.
Please help.
You've created a client-side collection by defining the collection only in client code. A collection needs to be defined on both the server and the client in order to persist documents to the database.
The quick solution is to create a shared file like lib/collections/resolutions.js which will contain:
Resolutions = new Mongo.Collection("resolutions");
Using the new-style imports mechanism, you would create a file like imports/api/resolutions/resolutions.js which will contain:
import { Mongo } from 'meteor/mongo';
export const Todos = new TodosCollection('Todos');
See this section of the guide for more details.

Set custom log level for tests when using Sails.js

I'd like to set the log level to ERROR when running unit, integration and acceptance tests. Is it possible to do this while keeping the default setting for the application in config/log.js unchanged?
Sails.js documentation recommends setting the log level to ERROR for tests but doesn't explain how you can do it. Is it possible by setting an environment variable or other means?
Thanks
If you want to set log level to 'error' only in your test you can do it by modifying your test/bootstrap.test.js file.
before(function(done) {
this.timeout(5000);
Sails.lift({
log: {
level: 'error'
}
}, function(err, server) {
sails = server;
if (err) return done(err);
// here you can load fixtures, etc.
done(err, sails);
});
});
By that you can modify every setting from config you want. For example i disabled csrf and 2 hooks i don't use. You can also change connection to database if needed in this place.