pinstripe 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/database.js CHANGED
@@ -11,14 +11,17 @@ export const Database = Class.extend().include({
11
11
  this.assignProps({ name: 'database' });
12
12
  },
13
13
 
14
- async initialize(client, options = {}){
14
+ async initialize(client){
15
15
  this.client = client;
16
- this.options = options;
17
16
  if(!loadSchemaPromise) loadSchemaPromise = Row.loadSchema(client);
18
17
  await loadSchemaPromise;
19
18
  return trapify(this);
20
19
  },
21
20
 
21
+ get withoutTenantScope(){
22
+ return Database.new(this.client);
23
+ },
24
+
22
25
  table(name, fn){
23
26
  const out = Table.create(name, this);
24
27
  if(fn) return fn.call(out, out);
@@ -16,7 +16,7 @@ Table.include({
16
16
 
17
17
  initialize(...args){
18
18
  const out = initialize.call(this, ...args);
19
- if(this.constructor.columns.tenantId && this.database.options.tenant){
19
+ if(this.constructor.columns.tenantId && this.database.scopedByTenant){
20
20
  if(this.database.tenant){
21
21
  this.where({ tenantId: this.database.tenant.id });
22
22
  } else {
@@ -2,26 +2,7 @@
2
2
  import { Database } from '../../database.js';
3
3
 
4
4
  Database.include({
5
- meta(){
6
- const { initialize } = this.prototype;
7
-
8
- this.include({
9
- async initialize(...args){
10
- const out = await initialize.call(this, ...args);
11
- const { tenant } = this.options;
12
- if(tenant && out.info.tenants){
13
- const { name, host } = tenant;
14
- this.tenant = await (await out.tenants.tap(function(){
15
- this.where('(? = ? or ? = ?)', this.tableReference.createColumnReference('name'), name, this.tableReference.createColumnReference('host'), host);
16
- })).first();
17
- }
18
- return out;
19
- },
20
-
21
- get withoutTenantScope(){
22
- const { tenant, ...options } = this.options;
23
- return Database.new(this.client, options);
24
- }
25
- });
5
+ get scopedByTenant(){
6
+ return this.hasOwnProperty('tenant');
26
7
  }
27
8
  });
@@ -8,12 +8,25 @@ export default {
8
8
  if(!this.context.root.databaseClient){
9
9
  this.context.root.databaseClient = Client.new(await this.config.database);
10
10
  }
11
- return Database.new(this.context.root.databaseClient, {
12
- tenant: {
13
- name: this.initialParams._headers['x-tenant'] || this.initialParams._url.hostname.replace(/\..*$/, ''),
14
- host: this.initialParams._url.hostname
11
+
12
+ const out = await Database.new(this.context.root.databaseClient);
13
+
14
+ if(out.info.tenants){
15
+ const headers = this.initialParams._headers;
16
+ const hostname = this.initialParams._url.hostname;
17
+ const host = (headers['host'] || hostname).toLowerCase();
18
+ const { primaryDomain = '' } = await this.config;
19
+ const domainSuffix = `\.${primaryDomain}`.toLowerCase();
20
+
21
+ if(host.endsWith(domainSuffix)){
22
+ const name = host.substr(0, host.length - domainSuffix.length);
23
+ out.tenant = await out.tenants.where({ name }).first();
24
+ } else {
25
+ out.tenant = await out.tenants.where({ host }).first();
15
26
  }
16
- })
27
+ }
28
+
29
+ return out;
17
30
  });
18
31
  }
19
32
  };
@@ -39,17 +39,25 @@ export default {
39
39
 
40
40
  const environment = process.env.NODE_ENV || 'development';
41
41
 
42
- if(adapter == 'sqlite'){
42
+ if(adapter == 'mysql'){
43
43
  return Object.assign({
44
- filename: `${await this.project.rootPath}/${environment}.db`
44
+ host: 'localhost',
45
+ user: 'root',
46
+ password: '',
47
+ database: `${await this.project.name}_${environment}`
45
48
  }, out);
46
49
  }
47
50
 
48
51
  return Object.assign({
49
- host: 'localhost',
50
- user: 'root',
51
- password: '',
52
- database: `${await this.project.name}_${environment}`
52
+ adapter: 'sqlite',
53
+ filename: `${await this.project.rootPath}/${environment}.db`
53
54
  }, out);
55
+ },
56
+
57
+ normalizeMailConfig(config){
58
+ return {
59
+ defaults: {},
60
+ ...config
61
+ };
54
62
  }
55
63
  };
@@ -13,9 +13,9 @@ export default {
13
13
  });
14
14
  },
15
15
 
16
- createDummy(){
16
+ createDummy({ defaults }){
17
17
  return (mailOptions = {}) => {
18
- const { text, html, ...otherMailOptions } = mailOptions;
18
+ const { text, html, ...otherMailOptions } = { ...defaults, ...mailOptions };
19
19
  console.log('');
20
20
  console.log('----------------------------------------');
21
21
  console.log(chalk.green('sendMail'));
@@ -39,8 +39,8 @@ export default {
39
39
  }
40
40
  },
41
41
 
42
- createSmtp(config){
43
- const transport = createTransport(config);
42
+ createSmtp({ defaults, ...config }){
43
+ const transport = createTransport(config, defaults);
44
44
  return (mailOptions = {}) => transport.sendMail(mailOptions);
45
45
  }
46
46
  };
@@ -40,8 +40,6 @@ export default {
40
40
  const { method, url, headers } = request;
41
41
  const _url = new URL(url, 'http://localhost');
42
42
 
43
- if(headers['x-host']) _url.host = headers['x-host'];
44
-
45
43
  const urlParams = {};
46
44
  _url.searchParams.forEach((value, key) => {
47
45
  urlParams[key] = value;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "pinstripe",
4
4
  "description": "A slick web framework for Node.js.",
5
- "version": "0.18.0",
5
+ "version": "0.19.0",
6
6
  "author": "Jody Salt",
7
7
  "license": "MIT",
8
8
  "exports": {
@@ -48,5 +48,5 @@
48
48
  "tmp": "^0.2.1",
49
49
  "unionfs": "^4.4.0"
50
50
  },
51
- "gitHead": "b638218b06f4cdea77f3ba0a4f9c082989528ccf"
51
+ "gitHead": "bfe451ab9cdec0d5e4da557562c633d86920fb7a"
52
52
  }