forj 0.1.0 → 0.1.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "forj",
3
3
  "description": "SQLite ORM and Query Builder whitout dependencies",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
7
7
  "files": ["src"],
@@ -1,3 +1,4 @@
1
+ import pluralize from 'pluralize'
1
2
  import Column from './column'
2
3
  import ForeignKey from './foreign-key'
3
4
  import { tableName } from '../utils'
@@ -127,8 +128,22 @@ export class Blueprint {
127
128
  return this.softDelete(columnType, name)
128
129
  }
129
130
 
130
- foreign(column: string) {
131
- const fk: ForeignKeyDefinition = { column, references: '', on: '' }
131
+ foreignId(name: string) {
132
+ const opts = { name, type: 'INTEGER', onDelete: 'CASCADE', onUpdate: 'RESTRICT' } as ColumnDefinition
133
+ const sufixIndex = name.lastIndexOf('_id')
134
+
135
+ if (sufixIndex > -1)
136
+ return this.#column({
137
+ ...opts,
138
+ references: name.substring(sufixIndex + 1),
139
+ on: pluralize(name.substring(0, sufixIndex)),
140
+ })
141
+
142
+ return this.#column(opts)
143
+ }
144
+
145
+ foreign(name: string) {
146
+ const fk: ForeignKeyDefinition = { name, references: '', on: '' }
132
147
  this.#foreignKeys.push(fk)
133
148
  return new ForeignKey(fk)
134
149
  }
@@ -88,6 +88,9 @@ export default class SchemaBuilder {
88
88
  if (column.raw)
89
89
  sql += ' '+ column.raw
90
90
 
91
+ if (column.references && column.on)
92
+ sql += this.#foreignKey(column, true)
93
+
91
94
  // if (column.comment)
92
95
  // sql += ` COMMENT '${column.comment.replace(/'/g, "''")}'`
93
96
 
@@ -128,8 +131,8 @@ export default class SchemaBuilder {
128
131
  }
129
132
  }
130
133
 
131
- static #foreignKey(fk: ForeignKeyDefinition): string {
132
- let sql = `FOREIGN KEY (${fk.column}) REFERENCES ${fk.on}(${fk.references})`
134
+ static #foreignKey(fk: ColumnDefinition | ForeignKeyDefinition, column: boolean = false): string {
135
+ let sql = (column ? '' : `FOREIGN KEY (${fk.name})`) +` REFERENCES ${fk.on}(${fk.references})`
133
136
 
134
137
  if (fk.onDelete)
135
138
  sql += ` ON DELETE ${fk.onDelete.toUpperCase()}`
@@ -1,4 +1,4 @@
1
- import type { ForeignKeyDefinition } from './types'
1
+ import type { ForeignKeyDefinition, ForeignKeyAction } from './types'
2
2
 
3
3
  export default class ForeignKey {
4
4
  constructor(private fk: ForeignKeyDefinition) {}
@@ -13,12 +13,12 @@ export default class ForeignKey {
13
13
  return this
14
14
  }
15
15
 
16
- onDelete(action: 'cascade' | 'set null' | 'restrict' | 'no action') {
16
+ onDelete(action: ForeignKeyAction) {
17
17
  this.fk.onDelete = action;
18
18
  return this;
19
19
  }
20
20
 
21
- onUpdate(action: 'cascade' | 'set null' | 'restrict' | 'no action') {
21
+ onUpdate(action: ForeignKeyAction) {
22
22
  this.fk.onUpdate = action
23
23
  return this
24
24
  }
@@ -6,7 +6,7 @@ export type BlueprintFn = (table: Blueprint) => void
6
6
 
7
7
  // TODO: refactor bellow
8
8
 
9
- export interface ColumnDefinition {
9
+ export type ColumnDefinition = {
10
10
  name: string,
11
11
  type: string,
12
12
  length?: number,
@@ -19,26 +19,30 @@ export interface ColumnDefinition {
19
19
  index?: boolean,
20
20
  comment?: string,
21
21
  raw?: string,
22
+ references?: string,
23
+ on?: string,
24
+ onDelete?: ForeignKeyAction,
25
+ onUpdate?: ForeignKeyAction,
22
26
  }
23
27
 
24
- export interface IndexDefinition {
28
+ export type IndexDefinition = {
25
29
  columns: string[],
26
30
  type: 'index' | 'unique' | 'primary',
27
31
  name?: string,
28
32
  }
29
33
 
30
- export interface ForeignKeyDefinition {
31
- column: string,
34
+ export type ForeignKeyDefinition = {
35
+ name: string,
32
36
  references: string,
33
37
  on: string,
34
- onDelete?: 'cascade' | 'set null' | 'restrict' | 'no action',
35
- onUpdate?: 'cascade' | 'set null' | 'restrict' | 'no action',
38
+ onDelete?: ForeignKeyAction,
39
+ onUpdate?: ForeignKeyAction,
36
40
  }
37
41
 
38
42
  export type QueueType = 'pending' | 'migrated'
39
43
  export type Queue = Record<QueueType, MigrationInfo[]>
40
44
 
41
- export interface MigrationInfo {
45
+ export type MigrationInfo = {
42
46
  timestamp: number,
43
47
  name: string,
44
48
  className: string,
@@ -48,7 +52,7 @@ export interface MigrationInfo {
48
52
  migrated: boolean,
49
53
  }
50
54
 
51
- export interface MigrationRecord {
55
+ export type MigrationRecord = {
52
56
  migration: string;
53
57
  batch: number;
54
58
  executed_at: Date;
@@ -64,6 +68,8 @@ export type MigrationClass = {
64
68
  // getMigrationName?(): string;
65
69
  }
66
70
 
71
+ export type ForeignKeyAction = 'cascade' | 'set null' | 'restrict' | 'no action' | 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION'
72
+
67
73
  // export interface SchemaConnection {
68
74
  // execute(sql: string): Promise<any>,
69
75
  // query(sql: string): Promise<any[]>,