@ignisia/sql 0.2.2 → 0.4.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.
Files changed (130) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/column/constants.d.cts +58 -5
  3. package/dist/cjs/column/constants.js +79 -22
  4. package/dist/cjs/column/index.d.cts +1 -0
  5. package/dist/cjs/column/index.js +31 -18
  6. package/dist/cjs/column/utils.d.cts +133 -0
  7. package/dist/cjs/column/utils.js +34 -0
  8. package/dist/cjs/database/alter.d.cts +3 -2
  9. package/dist/cjs/database/alter.js +15 -15
  10. package/dist/cjs/database/column.d.cts +3 -2
  11. package/dist/cjs/database/column.js +9 -7
  12. package/dist/cjs/database/contract.d.cts +3 -2
  13. package/dist/cjs/database/index.d.cts +3 -2
  14. package/dist/cjs/database/index.js +15 -2
  15. package/dist/cjs/database/table.d.cts +3 -2
  16. package/dist/cjs/database/table.js +12 -4
  17. package/dist/cjs/database/types.d.cts +2 -1
  18. package/dist/cjs/database/wrapper.d.cts +17 -21
  19. package/dist/cjs/database/wrapper.js +28 -52
  20. package/dist/cjs/index-CHxuUiO4.d.cts +472 -0
  21. package/dist/cjs/{index---zaMa69.d.cts → index-CZhrzE5r.d.cts} +5 -3
  22. package/dist/cjs/index.d.cts +3 -2
  23. package/dist/cjs/migration/index.d.cts +3 -2
  24. package/dist/cjs/migration/runner.js +1 -1
  25. package/dist/cjs/migration/type.d.cts +3 -2
  26. package/dist/cjs/query/builder.d.cts +3 -2
  27. package/dist/cjs/query/builder.js +1 -1
  28. package/dist/cjs/query/condition/common.d.cts +41 -0
  29. package/dist/cjs/query/condition/common.js +62 -0
  30. package/dist/cjs/query/condition/core.d.cts +8 -0
  31. package/dist/cjs/query/condition/core.js +57 -0
  32. package/dist/cjs/query/condition/index.d.cts +10 -0
  33. package/dist/cjs/query/condition/index.js +33 -0
  34. package/dist/cjs/query/condition/not.d.cts +34 -0
  35. package/dist/cjs/query/condition/not.js +51 -0
  36. package/dist/cjs/query/condition/raw.d.cts +8 -0
  37. package/dist/cjs/query/condition/raw.js +54 -0
  38. package/dist/cjs/query/constants.d.cts +27 -1
  39. package/dist/cjs/query/constants.js +28 -2
  40. package/dist/cjs/query/contract.d.cts +4 -3
  41. package/dist/cjs/query/explain.d.cts +12 -0
  42. package/dist/cjs/query/explain.js +65 -0
  43. package/dist/cjs/query/helper.d.cts +3 -2
  44. package/dist/cjs/query/helper.js +1 -1
  45. package/dist/cjs/query/index.d.cts +3 -2
  46. package/dist/cjs/query/index.js +69 -65
  47. package/dist/cjs/query/join.d.cts +18 -5
  48. package/dist/cjs/query/join.js +38 -9
  49. package/dist/cjs/query/sql.d.cts +9 -6
  50. package/dist/cjs/query/sql.js +80 -21
  51. package/dist/cjs/query/types.d.cts +2 -1
  52. package/dist/cjs/query/utilities.d.cts +7 -3
  53. package/dist/cjs/query/utilities.js +86 -5
  54. package/dist/cjs/table/constants.d.cts +1 -0
  55. package/dist/cjs/table/constants.js +1 -0
  56. package/dist/cjs/table/index.d.cts +3 -2
  57. package/dist/cjs/table/index.js +29 -9
  58. package/dist/cjs/table/types.d.cts +3 -2
  59. package/dist/cjs/table/utilities.d.cts +2 -1
  60. package/dist/cjs/types.d.cts +5 -1
  61. package/dist/cjs/utilities.d.cts +2 -1
  62. package/dist/cjs/utilities.js +22 -0
  63. package/dist/esm/column/constants.d.ts +58 -5
  64. package/dist/esm/column/constants.js +78 -23
  65. package/dist/esm/column/index.d.ts +1 -0
  66. package/dist/esm/column/index.js +31 -18
  67. package/dist/esm/column/utils.d.ts +133 -0
  68. package/dist/esm/column/utils.js +32 -0
  69. package/dist/esm/database/alter.d.ts +3 -2
  70. package/dist/esm/database/alter.js +15 -15
  71. package/dist/esm/database/column.d.ts +3 -2
  72. package/dist/esm/database/column.js +9 -7
  73. package/dist/esm/database/contract.d.ts +3 -2
  74. package/dist/esm/database/index.d.ts +3 -2
  75. package/dist/esm/database/index.js +17 -4
  76. package/dist/esm/database/table.d.ts +3 -2
  77. package/dist/esm/database/table.js +12 -4
  78. package/dist/esm/database/types.d.ts +2 -1
  79. package/dist/esm/database/wrapper.d.ts +17 -21
  80. package/dist/esm/database/wrapper.js +27 -53
  81. package/dist/esm/index-CjurLJdK.d.ts +472 -0
  82. package/dist/esm/{index-DFrpzXEn.d.ts → index-DgOs61lH.d.ts} +5 -3
  83. package/dist/esm/index.d.ts +3 -2
  84. package/dist/esm/migration/index.d.ts +3 -2
  85. package/dist/esm/migration/runner.js +1 -1
  86. package/dist/esm/migration/type.d.ts +3 -2
  87. package/dist/esm/query/builder.d.ts +3 -2
  88. package/dist/esm/query/builder.js +1 -1
  89. package/dist/esm/query/condition/common.d.ts +41 -0
  90. package/dist/esm/query/condition/common.js +56 -0
  91. package/dist/esm/query/condition/core.d.ts +8 -0
  92. package/dist/esm/query/condition/core.js +55 -0
  93. package/dist/esm/query/condition/index.d.ts +10 -0
  94. package/dist/esm/query/condition/index.js +4 -0
  95. package/dist/esm/query/condition/not.d.ts +34 -0
  96. package/dist/esm/query/condition/not.js +46 -0
  97. package/dist/esm/query/condition/raw.d.ts +8 -0
  98. package/dist/esm/query/condition/raw.js +50 -0
  99. package/dist/esm/query/constants.d.ts +27 -1
  100. package/dist/esm/query/constants.js +27 -3
  101. package/dist/esm/query/contract.d.ts +4 -3
  102. package/dist/esm/query/explain.d.ts +12 -0
  103. package/dist/esm/query/explain.js +64 -0
  104. package/dist/esm/query/helper.d.ts +3 -2
  105. package/dist/esm/query/helper.js +2 -2
  106. package/dist/esm/query/index.d.ts +3 -2
  107. package/dist/esm/query/index.js +72 -68
  108. package/dist/esm/query/join.d.ts +18 -5
  109. package/dist/esm/query/join.js +37 -9
  110. package/dist/esm/query/sql.d.ts +9 -6
  111. package/dist/esm/query/sql.js +80 -23
  112. package/dist/esm/query/types.d.ts +2 -1
  113. package/dist/esm/query/utilities.d.ts +7 -3
  114. package/dist/esm/query/utilities.js +86 -6
  115. package/dist/esm/table/constants.d.ts +1 -0
  116. package/dist/esm/table/constants.js +1 -0
  117. package/dist/esm/table/index.d.ts +3 -2
  118. package/dist/esm/table/index.js +29 -9
  119. package/dist/esm/table/types.d.ts +3 -2
  120. package/dist/esm/table/utilities.d.ts +2 -1
  121. package/dist/esm/types.d.ts +5 -1
  122. package/dist/esm/utilities.d.ts +2 -1
  123. package/dist/esm/utilities.js +22 -1
  124. package/package.json +8 -2
  125. package/dist/cjs/index-CwiFQh0I.d.cts +0 -358
  126. package/dist/cjs/query/condition.d.cts +0 -7
  127. package/dist/cjs/query/condition.js +0 -106
  128. package/dist/esm/index-FMT0YEO7.d.ts +0 -358
  129. package/dist/esm/query/condition.d.ts +0 -7
  130. package/dist/esm/query/condition.js +0 -98
@@ -1,7 +1,8 @@
1
- export { T as Table } from '../index-CwiFQh0I.cjs';
1
+ export { T as Table } from '../index-CHxuUiO4.cjs';
2
2
  import '../column/index.cjs';
3
3
  import './constants.cjs';
4
- import '../column/constants.cjs';
4
+ import 'bun';
5
5
  import '../types.cjs';
6
6
  import '../query/constants.cjs';
7
+ import '../column/constants.cjs';
7
8
  import '../column/types.cjs';
@@ -5,22 +5,21 @@ var utilities = require('./utilities');
5
5
 
6
6
  class Table {
7
7
  client;
8
- dialect;
8
+ _dialect;
9
9
  name;
10
10
  columns;
11
11
  timestamp;
12
12
  paranoid;
13
13
  _output;
14
14
  constructor(options) {
15
- this.dialect = options.dialect;
15
+ this._dialect = options.dialect || null;
16
16
  this.name = options.name;
17
17
  this.columns = options.columns;
18
18
  this.paranoid = options.paranoid || null;
19
19
  this.timestamp = options.timestamp || null;
20
20
  this.client = null;
21
- for (const column of Object.values(this.columns)) {
22
- column.dialect(options.dialect);
23
- }
21
+ if (!this._dialect) return;
22
+ this.setColumnDialect(this._dialect);
24
23
  }
25
24
  infer() {
26
25
  return null;
@@ -32,18 +31,39 @@ class Table {
32
31
  columns
33
32
  });
34
33
  }
35
- async create(db = this.client) {
34
+ setColumnDialect(dialect) {
35
+ for (const column of Object.values(this.columns)) {
36
+ column.dialect(dialect);
37
+ }
38
+ }
39
+ get dialect() {
40
+ return this._dialect;
41
+ }
42
+ setDialect(dialect) {
43
+ this._dialect = dialect;
44
+ this.setColumnDialect(dialect);
45
+ return this;
46
+ }
47
+ async create(options = {}) {
48
+ const db = options.db || this.client;
36
49
  if (!db) throw new Error("Database client not defined");
37
50
  const sql = `CREATE TABLE IF NOT EXISTS ${this.name} (${Object.entries(
38
51
  this.columns
39
52
  ).map(([name, column]) => `${name} ${column.toQuery().query}`).join(", ")});`;
40
- await db.exec(sql);
53
+ await db.exec({
54
+ sql,
55
+ tx: options.tx
56
+ });
41
57
  return this;
42
58
  }
43
- async drop(db = this.client) {
59
+ async drop(options = {}) {
60
+ const db = options.db || this.client;
44
61
  if (!db) throw new Error("Database client not defined");
45
62
  const sql = `DROP TABLE IF EXISTS ${this.name};`;
46
- await db.exec(sql);
63
+ await db.exec({
64
+ sql,
65
+ tx: options.tx
66
+ });
47
67
  return this;
48
68
  }
49
69
  query() {
@@ -1,7 +1,8 @@
1
- export { M as MergeTimestampParanoid, s as TableOptions, t as TableOutput, a as TimestampOptions } from '../index-CwiFQh0I.cjs';
1
+ import 'bun';
2
+ export { X as ExecOptions, M as MergeTimestampParanoid, U as TableOptions, V as TableOutput, f as TimestampOptions } from '../index-CHxuUiO4.cjs';
2
3
  import '../column/index.cjs';
3
4
  import './constants.cjs';
4
- import '../column/constants.cjs';
5
5
  import '../types.cjs';
6
6
  import '../query/constants.cjs';
7
+ import '../column/constants.cjs';
7
8
  import '../column/types.cjs';
@@ -1,7 +1,8 @@
1
1
  import '../column/constants.cjs';
2
2
  import '../column/index.cjs';
3
3
  import './constants.cjs';
4
- export { p as createdAt, r as defineColumns, q as deletedAt, u as updatedAt } from '../index-CwiFQh0I.cjs';
4
+ export { K as createdAt, O as defineColumns, N as deletedAt, L as updatedAt } from '../index-CHxuUiO4.cjs';
5
5
  import '../column/types.cjs';
6
+ import 'bun';
6
7
  import '../types.cjs';
7
8
  import '../query/constants.cjs';
@@ -1,3 +1,7 @@
1
1
  type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
2
+ type BuildTuple<N extends number, T extends unknown[] = []> = T['length'] extends N ? T : BuildTuple<N, [...T, unknown]>;
3
+ type Add<A extends number, B extends number, Result extends unknown[] = [...BuildTuple<A>, ...BuildTuple<B>]> = Result['length'] extends number ? Result['length'] : never;
4
+ type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...BuildTuple<B>, ...infer Rest] ? Rest['length'] : never;
5
+ type Multiply<A extends number, B extends number, Result extends unknown[] = []> = B extends 0 ? Result['length'] : Multiply<A, Subtract<B, 1>, [...Result, ...BuildTuple<A>]>;
2
6
 
3
- export type { UnionToIntersection };
7
+ export type { Add, BuildTuple, Multiply, Subtract, UnionToIntersection };
@@ -1,4 +1,5 @@
1
1
  declare function deepClone<T>(obj: T): T;
2
+ declare function cloneDefinition<T extends Record<string, unknown>>(def: T): T;
2
3
  declare function quoteIdentifier<T extends string, U extends `"${T}"`>(identifier: T): U;
3
4
 
4
- export { deepClone, quoteIdentifier };
5
+ export { cloneDefinition, deepClone, quoteIdentifier };
@@ -13,9 +13,31 @@ function deepClone(obj) {
13
13
  }
14
14
  return obj;
15
15
  }
16
+ function cloneArray(arr) {
17
+ if (arr.length === 0) return [];
18
+ if (typeof arr[0] === "object" && arr[0] !== null) {
19
+ return arr.map((item) => ({ ...item }));
20
+ }
21
+ return arr.slice();
22
+ }
23
+ function cloneDefinition(def) {
24
+ const clone = {};
25
+ for (const key in def) {
26
+ const val = def[key];
27
+ if (val === null || typeof val !== "object") {
28
+ clone[key] = val;
29
+ } else if (Array.isArray(val)) {
30
+ clone[key] = cloneArray(val);
31
+ } else {
32
+ clone[key] = { ...val };
33
+ }
34
+ }
35
+ return clone;
36
+ }
16
37
  function quoteIdentifier(identifier) {
17
38
  return `"${identifier.replace(/"/g, '""')}"`;
18
39
  }
19
40
 
41
+ exports.cloneDefinition = cloneDefinition;
20
42
  exports.deepClone = deepClone;
21
43
  exports.quoteIdentifier = quoteIdentifier;
@@ -23,75 +23,128 @@ declare const ColumnTypeMapping: {
23
23
  readonly INTEGER: {
24
24
  readonly sqlite: "INTEGER";
25
25
  readonly postgres: "INTEGER";
26
+ readonly mysql: "INT";
26
27
  };
27
28
  readonly STRING: {
28
29
  readonly sqlite: "TEXT";
29
30
  readonly postgres: "VARCHAR";
31
+ readonly mysql: "VARCHAR";
30
32
  };
31
33
  readonly BOOLEAN: {
32
34
  readonly sqlite: "INTEGER";
33
35
  readonly postgres: "BOOLEAN";
36
+ readonly mysql: "TINYINT";
34
37
  };
35
38
  readonly DATE: {
36
39
  readonly sqlite: "TEXT";
37
40
  readonly postgres: "DATE";
41
+ readonly mysql: "DATE";
38
42
  };
39
43
  readonly FLOAT: {
40
44
  readonly sqlite: "REAL";
41
45
  readonly postgres: "FLOAT";
46
+ readonly mysql: "FLOAT PRECISION";
42
47
  };
43
48
  readonly DECIMAL: {
44
- readonly sqlite: "TEXT";
49
+ readonly sqlite: "NUMERIC";
45
50
  readonly postgres: "DECIMAL";
51
+ readonly mysql: "DECIMAL";
46
52
  };
47
53
  readonly BIGINT: {
48
- readonly sqlite: "TEXT";
54
+ readonly sqlite: "INTEGER";
49
55
  readonly postgres: "BIGINT";
56
+ readonly mysql: "BIGINT";
50
57
  };
51
58
  readonly TEXT: {
52
59
  readonly sqlite: "TEXT";
53
60
  readonly postgres: "TEXT";
61
+ readonly mysql: "TEXT";
54
62
  };
55
63
  readonly BLOB: {
56
64
  readonly sqlite: "BLOB";
57
65
  readonly postgres: "BYTEA";
66
+ readonly mysql: "BLOB";
58
67
  };
59
68
  readonly JSON: {
60
69
  readonly sqlite: "TEXT";
61
70
  readonly postgres: "JSONB";
71
+ readonly mysql: "JSON";
62
72
  };
63
73
  readonly VARCHAR: {
64
- readonly sqlite: "TEXT";
74
+ readonly sqlite: "VARCHAR";
65
75
  readonly postgres: "VARCHAR";
76
+ readonly mysql: "VARCHAR";
66
77
  };
67
78
  readonly TIME: {
68
79
  readonly sqlite: "TEXT";
69
80
  readonly postgres: "TIME";
81
+ readonly mysql: "TIME";
70
82
  };
71
83
  readonly TIMESTAMP: {
72
84
  readonly sqlite: "TEXT";
73
85
  readonly postgres: "TIMESTAMP";
86
+ readonly mysql: "DATETIME";
74
87
  };
75
88
  readonly DOUBLE: {
76
89
  readonly sqlite: "REAL";
77
90
  readonly postgres: "DOUBLE PRECISION";
91
+ readonly mysql: "DOUBLE PRECISION";
78
92
  };
79
93
  readonly DATETIME: {
80
94
  readonly sqlite: "TEXT";
81
95
  readonly postgres: "TIMESTAMP";
96
+ readonly mysql: "DATETIME";
82
97
  };
83
98
  readonly DATEONLY: {
84
99
  readonly sqlite: "TEXT";
85
100
  readonly postgres: "DATE";
101
+ readonly mysql: "DATE";
86
102
  };
87
103
  readonly ENUM: {
88
104
  readonly sqlite: "TEXT";
89
105
  readonly postgres: "TEXT";
106
+ readonly mysql: "TEXT";
90
107
  };
91
108
  readonly SERIAL: {
92
- readonly sqlite: "INTEGER AUTOINCREMENT";
109
+ readonly sqlite: "INTEGER";
93
110
  readonly postgres: "SERIAL";
111
+ readonly mysql: "INT";
112
+ };
113
+ };
114
+ declare const ColumnProperties: {
115
+ readonly NOT_NULL: "NOT_NULL";
116
+ readonly UNIQUE: "UNIQUE";
117
+ readonly DEFAULT: "DEFAULT";
118
+ readonly AUTO_INCREMENT: "AUTO_INCREMENT";
119
+ readonly PRIMARY_KEY: "PRIMARY_KEY";
120
+ };
121
+ type ColumnProperties = (typeof ColumnProperties)[keyof typeof ColumnProperties];
122
+ declare const ColumnPropertyMapping: {
123
+ NOT_NULL: {
124
+ sqlite: string;
125
+ postgres: string;
126
+ mysql: string;
127
+ };
128
+ UNIQUE: {
129
+ sqlite: string;
130
+ postgres: string;
131
+ mysql: string;
132
+ };
133
+ DEFAULT: {
134
+ sqlite: string;
135
+ postgres: string;
136
+ mysql: string;
137
+ };
138
+ AUTO_INCREMENT: {
139
+ sqlite: string;
140
+ postgres: string;
141
+ mysql: string;
142
+ };
143
+ PRIMARY_KEY: {
144
+ sqlite: string;
145
+ postgres: string;
146
+ mysql: string;
94
147
  };
95
148
  };
96
149
 
97
- export { AcceptedColumnTypes, ColumnTypeMapping };
150
+ export { AcceptedColumnTypes, ColumnProperties, ColumnPropertyMapping, ColumnTypeMapping };
@@ -24,76 +24,131 @@ var AcceptedColumnTypes = {
24
24
  var ColumnTypeMapping = {
25
25
  [AcceptedColumnTypes.INTEGER]: {
26
26
  [Dialect.SQLITE]: "INTEGER",
27
- [Dialect.POSTGRES]: "INTEGER"
27
+ [Dialect.POSTGRES]: "INTEGER",
28
+ [Dialect.MYSQL]: "INT"
28
29
  },
29
30
  [AcceptedColumnTypes.STRING]: {
30
31
  [Dialect.SQLITE]: "TEXT",
31
- [Dialect.POSTGRES]: "VARCHAR"
32
+ [Dialect.POSTGRES]: "VARCHAR",
33
+ [Dialect.MYSQL]: "VARCHAR"
32
34
  },
33
35
  [AcceptedColumnTypes.BOOLEAN]: {
34
36
  [Dialect.SQLITE]: "INTEGER",
35
- [Dialect.POSTGRES]: "BOOLEAN"
37
+ [Dialect.POSTGRES]: "BOOLEAN",
38
+ [Dialect.MYSQL]: "TINYINT"
36
39
  },
37
40
  [AcceptedColumnTypes.DATE]: {
38
41
  [Dialect.SQLITE]: "TEXT",
39
- [Dialect.POSTGRES]: "DATE"
42
+ [Dialect.POSTGRES]: "DATE",
43
+ [Dialect.MYSQL]: "DATE"
40
44
  },
41
45
  [AcceptedColumnTypes.FLOAT]: {
42
46
  [Dialect.SQLITE]: "REAL",
43
- [Dialect.POSTGRES]: "FLOAT"
47
+ [Dialect.POSTGRES]: "FLOAT",
48
+ [Dialect.MYSQL]: "FLOAT PRECISION"
44
49
  },
45
50
  [AcceptedColumnTypes.DECIMAL]: {
46
- [Dialect.SQLITE]: "TEXT",
47
- [Dialect.POSTGRES]: "DECIMAL"
51
+ [Dialect.SQLITE]: "NUMERIC",
52
+ [Dialect.POSTGRES]: "DECIMAL",
53
+ [Dialect.MYSQL]: "DECIMAL"
48
54
  },
49
55
  [AcceptedColumnTypes.BIGINT]: {
50
- [Dialect.SQLITE]: "TEXT",
51
- [Dialect.POSTGRES]: "BIGINT"
56
+ [Dialect.SQLITE]: "INTEGER",
57
+ [Dialect.POSTGRES]: "BIGINT",
58
+ [Dialect.MYSQL]: "BIGINT"
52
59
  },
53
60
  [AcceptedColumnTypes.TEXT]: {
54
61
  [Dialect.SQLITE]: "TEXT",
55
- [Dialect.POSTGRES]: "TEXT"
62
+ [Dialect.POSTGRES]: "TEXT",
63
+ [Dialect.MYSQL]: "TEXT"
56
64
  },
57
65
  [AcceptedColumnTypes.BLOB]: {
58
66
  [Dialect.SQLITE]: "BLOB",
59
- [Dialect.POSTGRES]: "BYTEA"
67
+ [Dialect.POSTGRES]: "BYTEA",
68
+ [Dialect.MYSQL]: "BLOB"
60
69
  },
61
70
  [AcceptedColumnTypes.JSON]: {
62
71
  [Dialect.SQLITE]: "TEXT",
63
- [Dialect.POSTGRES]: "JSONB"
72
+ [Dialect.POSTGRES]: "JSONB",
73
+ [Dialect.MYSQL]: "JSON"
64
74
  },
65
75
  [AcceptedColumnTypes.VARCHAR]: {
66
- [Dialect.SQLITE]: "TEXT",
67
- [Dialect.POSTGRES]: "VARCHAR"
76
+ [Dialect.SQLITE]: "VARCHAR",
77
+ [Dialect.POSTGRES]: "VARCHAR",
78
+ [Dialect.MYSQL]: "VARCHAR"
68
79
  },
69
80
  [AcceptedColumnTypes.TIME]: {
70
81
  [Dialect.SQLITE]: "TEXT",
71
- [Dialect.POSTGRES]: "TIME"
82
+ [Dialect.POSTGRES]: "TIME",
83
+ [Dialect.MYSQL]: "TIME"
72
84
  },
73
85
  [AcceptedColumnTypes.TIMESTAMP]: {
74
86
  [Dialect.SQLITE]: "TEXT",
75
- [Dialect.POSTGRES]: "TIMESTAMP"
87
+ [Dialect.POSTGRES]: "TIMESTAMP",
88
+ [Dialect.MYSQL]: "DATETIME"
76
89
  },
77
90
  [AcceptedColumnTypes.DOUBLE]: {
78
91
  [Dialect.SQLITE]: "REAL",
79
- [Dialect.POSTGRES]: "DOUBLE PRECISION"
92
+ [Dialect.POSTGRES]: "DOUBLE PRECISION",
93
+ [Dialect.MYSQL]: "DOUBLE PRECISION"
80
94
  },
81
95
  [AcceptedColumnTypes.DATETIME]: {
82
96
  [Dialect.SQLITE]: "TEXT",
83
- [Dialect.POSTGRES]: "TIMESTAMP"
97
+ [Dialect.POSTGRES]: "TIMESTAMP",
98
+ [Dialect.MYSQL]: "DATETIME"
84
99
  },
85
100
  [AcceptedColumnTypes.DATEONLY]: {
86
101
  [Dialect.SQLITE]: "TEXT",
87
- [Dialect.POSTGRES]: "DATE"
102
+ [Dialect.POSTGRES]: "DATE",
103
+ [Dialect.MYSQL]: "DATE"
88
104
  },
89
105
  [AcceptedColumnTypes.ENUM]: {
90
106
  [Dialect.SQLITE]: "TEXT",
91
- [Dialect.POSTGRES]: "TEXT"
107
+ [Dialect.POSTGRES]: "TEXT",
108
+ [Dialect.MYSQL]: "TEXT"
92
109
  },
93
110
  [AcceptedColumnTypes.SERIAL]: {
94
- [Dialect.SQLITE]: "INTEGER AUTOINCREMENT",
95
- [Dialect.POSTGRES]: "SERIAL"
111
+ [Dialect.SQLITE]: "INTEGER",
112
+ [Dialect.POSTGRES]: "SERIAL",
113
+ [Dialect.MYSQL]: "INT"
114
+ }
115
+ };
116
+ var ColumnProperties = {
117
+ NOT_NULL: "NOT_NULL",
118
+ UNIQUE: "UNIQUE",
119
+ DEFAULT: "DEFAULT",
120
+ AUTO_INCREMENT: "AUTO_INCREMENT",
121
+ PRIMARY_KEY: "PRIMARY_KEY"
122
+ };
123
+ var ColumnPropertyMapping = {
124
+ [ColumnProperties.NOT_NULL]: {
125
+ [Dialect.SQLITE]: "NOT NULL",
126
+ [Dialect.POSTGRES]: "NOT NULL",
127
+ [Dialect.MYSQL]: "NOT NULL"
128
+ },
129
+ [ColumnProperties.UNIQUE]: {
130
+ [Dialect.SQLITE]: "UNIQUE",
131
+ [Dialect.POSTGRES]: "UNIQUE",
132
+ [Dialect.MYSQL]: "UNIQUE"
133
+ },
134
+ [ColumnProperties.DEFAULT]: {
135
+ [Dialect.SQLITE]: "DEFAULT",
136
+ [Dialect.POSTGRES]: "DEFAULT",
137
+ [Dialect.MYSQL]: "DEFAULT"
138
+ },
139
+ [ColumnProperties.AUTO_INCREMENT]: {
140
+ // sqlite does not support AUTOINCREMENT
141
+ [Dialect.SQLITE]: "AUTOINCREMENT",
142
+ // postgres does not support AUTOINCREMENT
143
+ [Dialect.POSTGRES]: "AUTOINCREMENT",
144
+ // Exists only for mysql since mysql supports AUTOINCREMENT
145
+ [Dialect.MYSQL]: "AUTO_INCREMENT"
146
+ },
147
+ [ColumnProperties.PRIMARY_KEY]: {
148
+ [Dialect.SQLITE]: "PRIMARY KEY",
149
+ [Dialect.POSTGRES]: "PRIMARY KEY",
150
+ [Dialect.MYSQL]: "PRIMARY KEY"
96
151
  }
97
152
  };
98
153
 
99
- export { AcceptedColumnTypes, ColumnTypeMapping };
154
+ export { AcceptedColumnTypes, ColumnProperties, ColumnPropertyMapping, ColumnTypeMapping };
@@ -31,6 +31,7 @@ declare class Column<Type extends AcceptedColumnTypes = AcceptedColumnTypes, Val
31
31
  dialect<DbDialect extends Dialect>(dialect: DbDialect): Column<Type, Values, Options, ColumnValue, Value, Definition & {
32
32
  dialect: DbDialect;
33
33
  }>;
34
+ clone(): Column<Type, Values, Options, ColumnValue, Value, {}>;
34
35
  toQuery(): {
35
36
  query: string;
36
37
  params: never[];
@@ -1,5 +1,7 @@
1
1
  import { Dialect } from '../table/constants.js';
2
+ import { cloneDefinition } from '../utilities.js';
2
3
  import { ColumnTypeMapping, AcceptedColumnTypes } from './constants.js';
4
+ import { columnProperty } from './utils.js';
3
5
 
4
6
  // src/column/index.ts
5
7
  var Column = class _Column {
@@ -18,14 +20,7 @@ var Column = class _Column {
18
20
  if ("values" in options) {
19
21
  this.enums = options.values;
20
22
  }
21
- this.definition = {
22
- autoIncrement: false,
23
- primaryKey: false,
24
- notNull: false,
25
- unique: false,
26
- comment: null,
27
- default: void 0
28
- };
23
+ this.definition = {};
29
24
  }
30
25
  static define(options) {
31
26
  return new _Column(options);
@@ -58,40 +53,58 @@ var Column = class _Column {
58
53
  this.definition.dialect = dialect;
59
54
  return this;
60
55
  }
56
+ clone() {
57
+ const column = new _Column({
58
+ type: this.type,
59
+ ...this.length && { length: this.length },
60
+ ...this.enums.length && { values: this.enums }
61
+ });
62
+ Object.assign(column.definition, cloneDefinition(this.definition));
63
+ return column;
64
+ }
61
65
  toQuery() {
62
66
  if (!this.definition.dialect) {
63
67
  throw new Error("No DB Dialect defined");
64
68
  }
65
- const correctType = ColumnTypeMapping[this.type][this.definition.dialect];
66
- let sql = correctType + (this.length ? `(${this.length})` : "");
69
+ const type = ColumnTypeMapping[this.type][this.definition.dialect];
70
+ const properties = columnProperty(this.definition.dialect);
71
+ let sql = type + (this.length ? `(${this.length})` : "");
67
72
  if (this.definition.primaryKey) {
68
- sql += " PRIMARY KEY";
73
+ sql += ` ${properties.PRIMARY_KEY}`;
69
74
  }
70
75
  if (this.definition.autoIncrement || this.type === AcceptedColumnTypes.SERIAL) {
71
76
  const isPrimaryKey = !!this.definition.primaryKey;
77
+ const sqls = [];
72
78
  if (this.definition.dialect === Dialect.POSTGRES) {
73
- sql = `SERIAL${isPrimaryKey ? " PRIMARY KEY" : ""}`;
79
+ sqls.push(type);
80
+ if (isPrimaryKey) {
81
+ sqls.push(properties.PRIMARY_KEY);
82
+ }
83
+ sql = sqls.join(" ");
74
84
  } else {
75
85
  if (this.type !== AcceptedColumnTypes.SERIAL) {
76
- sql += " AUTOINCREMENT";
86
+ sql += ` ${properties.AUTO_INCREMENT}`;
77
87
  } else {
78
- const sqls = ["INTEGER", "PRIMARY KEY", "AUTOINCREMENT"];
79
- if (!isPrimaryKey) sqls.splice(1, 1);
88
+ sqls.push(type);
89
+ if (isPrimaryKey) {
90
+ sqls.push(properties.PRIMARY_KEY);
91
+ }
92
+ sqls.push(properties.AUTO_INCREMENT);
80
93
  sql = sqls.join(" ");
81
94
  }
82
95
  }
83
96
  }
84
97
  if (this.definition.notNull) {
85
- sql += " NOT NULL";
98
+ sql += ` ${properties.NOT_NULL}`;
86
99
  }
87
100
  if (this.definition.unique) {
88
- sql += " UNIQUE";
101
+ sql += ` ${properties.UNIQUE}`;
89
102
  }
90
103
  if (this.definition.default !== void 0) {
91
104
  const value = this.definition.default;
92
105
  const isString = typeof this.definition.default === "string";
93
106
  const finalValue = isString ? `'${value}'` : value;
94
- sql += ` DEFAULT ${finalValue}`;
107
+ sql += ` ${properties.DEFAULT} ${finalValue}`;
95
108
  }
96
109
  return { query: sql, params: [] };
97
110
  }
@@ -0,0 +1,133 @@
1
+ import { Dialect } from '../table/constants.js';
2
+ import { ColumnProperties } from './constants.js';
3
+
4
+ declare function getColumnProperty<DbDialect extends Dialect>(dialect: DbDialect, property: ColumnProperties): ({
5
+ sqlite: string;
6
+ postgres: string;
7
+ mysql: string;
8
+ } | {
9
+ sqlite: string;
10
+ postgres: string;
11
+ mysql: string;
12
+ } | {
13
+ sqlite: string;
14
+ postgres: string;
15
+ mysql: string;
16
+ } | {
17
+ sqlite: string;
18
+ postgres: string;
19
+ mysql: string;
20
+ } | {
21
+ sqlite: string;
22
+ postgres: string;
23
+ mysql: string;
24
+ })[DbDialect];
25
+ declare function columnProperty<DbDialect extends Dialect>(dialect: DbDialect): {
26
+ NOT_NULL: ({
27
+ sqlite: string;
28
+ postgres: string;
29
+ mysql: string;
30
+ } | {
31
+ sqlite: string;
32
+ postgres: string;
33
+ mysql: string;
34
+ } | {
35
+ sqlite: string;
36
+ postgres: string;
37
+ mysql: string;
38
+ } | {
39
+ sqlite: string;
40
+ postgres: string;
41
+ mysql: string;
42
+ } | {
43
+ sqlite: string;
44
+ postgres: string;
45
+ mysql: string;
46
+ })[DbDialect];
47
+ UNIQUE: ({
48
+ sqlite: string;
49
+ postgres: string;
50
+ mysql: string;
51
+ } | {
52
+ sqlite: string;
53
+ postgres: string;
54
+ mysql: string;
55
+ } | {
56
+ sqlite: string;
57
+ postgres: string;
58
+ mysql: string;
59
+ } | {
60
+ sqlite: string;
61
+ postgres: string;
62
+ mysql: string;
63
+ } | {
64
+ sqlite: string;
65
+ postgres: string;
66
+ mysql: string;
67
+ })[DbDialect];
68
+ DEFAULT: ({
69
+ sqlite: string;
70
+ postgres: string;
71
+ mysql: string;
72
+ } | {
73
+ sqlite: string;
74
+ postgres: string;
75
+ mysql: string;
76
+ } | {
77
+ sqlite: string;
78
+ postgres: string;
79
+ mysql: string;
80
+ } | {
81
+ sqlite: string;
82
+ postgres: string;
83
+ mysql: string;
84
+ } | {
85
+ sqlite: string;
86
+ postgres: string;
87
+ mysql: string;
88
+ })[DbDialect];
89
+ AUTO_INCREMENT: ({
90
+ sqlite: string;
91
+ postgres: string;
92
+ mysql: string;
93
+ } | {
94
+ sqlite: string;
95
+ postgres: string;
96
+ mysql: string;
97
+ } | {
98
+ sqlite: string;
99
+ postgres: string;
100
+ mysql: string;
101
+ } | {
102
+ sqlite: string;
103
+ postgres: string;
104
+ mysql: string;
105
+ } | {
106
+ sqlite: string;
107
+ postgres: string;
108
+ mysql: string;
109
+ })[DbDialect];
110
+ PRIMARY_KEY: ({
111
+ sqlite: string;
112
+ postgres: string;
113
+ mysql: string;
114
+ } | {
115
+ sqlite: string;
116
+ postgres: string;
117
+ mysql: string;
118
+ } | {
119
+ sqlite: string;
120
+ postgres: string;
121
+ mysql: string;
122
+ } | {
123
+ sqlite: string;
124
+ postgres: string;
125
+ mysql: string;
126
+ } | {
127
+ sqlite: string;
128
+ postgres: string;
129
+ mysql: string;
130
+ })[DbDialect];
131
+ };
132
+
133
+ export { columnProperty, getColumnProperty };