@expressive/sql 0.2.0 → 0.3.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/dist/index.js CHANGED
@@ -67,29 +67,30 @@ var {
67
67
  // src/type/Field.ts
68
68
  var REGISTER = /* @__PURE__ */ new Map();
69
69
  var Field = class {
70
- constructor(options) {
71
- this.options = options;
70
+ constructor() {
72
71
  this.type = "";
73
72
  this.unique = false;
74
73
  this.nullable = false;
75
74
  this.optional = false;
76
75
  this.increment = false;
77
76
  }
78
- init(property, parent) {
79
- let options = this.options;
77
+ /** Real datatype of this field in database. */
78
+ get datatype() {
79
+ return this.type;
80
+ }
81
+ get column() {
82
+ return underscore(this.property);
83
+ }
84
+ set column(value) {
85
+ Object.defineProperty(this, "column", { value });
86
+ }
87
+ static new(options) {
88
+ return Object.assign(new this(), options);
89
+ }
90
+ create(property, parent) {
80
91
  const field = create(this);
81
92
  field.parent = parent;
82
93
  field.property = property;
83
- field.column = underscore(property);
84
- if (typeof options === "function")
85
- options = options(field) || {};
86
- for (const key in options) {
87
- const value = options[key];
88
- if (value !== void 0)
89
- field[key] = value;
90
- }
91
- if (!field.datatype)
92
- field.datatype = field.type;
93
94
  return field;
94
95
  }
95
96
  /**
@@ -128,6 +129,9 @@ var Field = class {
128
129
  };
129
130
  }
130
131
  };
132
+ defineProperty(Field, "does", {
133
+ value: (callback) => callback
134
+ });
131
135
  function fields(from) {
132
136
  let fields2 = REGISTER.get(from);
133
137
  if (!fields2) {
@@ -136,13 +140,15 @@ function fields(from) {
136
140
  const reference = new from();
137
141
  for (const key in reference) {
138
142
  const { value } = getOwnPropertyDescriptor(reference, key);
139
- if (!(value instanceof Field))
140
- throw new Error(
141
- `Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
142
- );
143
- const instance = value.init(key, from);
144
- from.fields.set(key, instance);
145
- freeze(instance);
143
+ if (value instanceof Field) {
144
+ const instance = value.create(key, from);
145
+ from.fields.set(key, instance);
146
+ freeze(instance);
147
+ } else if (typeof value === "function") {
148
+ value(from, key);
149
+ } else throw new Error(
150
+ `Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
151
+ );
146
152
  }
147
153
  }
148
154
  return fields2;
@@ -150,76 +156,47 @@ function fields(from) {
150
156
 
151
157
  // src/columns/Bool.ts
152
158
  var BooleanColumn = class extends Field {
153
- constructor(opts = {}) {
154
- let type = "tinyint";
155
- let datatype = "tinyint";
156
- let TRUE = 1;
157
- let FALSE = 0;
158
- if ("either" in opts && opts.either) {
159
- [TRUE, FALSE] = opts.either;
160
- type = "varchar";
161
- datatype = `varchar(${Math.max(TRUE.length, FALSE.length)})`;
162
- }
163
- super(opts);
164
- this.type = "tinyint";
165
- this.TRUE = 1;
166
- this.FALSE = 0;
167
- this.type = type;
168
- this.datatype = datatype;
169
- this.TRUE = TRUE;
170
- this.FALSE = FALSE;
171
- }
172
159
  get(value) {
173
- return value === this.TRUE;
160
+ const [TRUE] = this.either || [1];
161
+ return value === true || value === TRUE;
174
162
  }
175
163
  set(value) {
164
+ const [TRUE, FALSE] = this.either || [1, 0];
176
165
  if (typeof value !== "boolean")
177
166
  throw "Value must be a boolean.";
178
- return value ? this.TRUE : this.FALSE;
167
+ return value ? TRUE : FALSE;
179
168
  }
180
169
  };
181
170
  function Bool(opts) {
182
- return new BooleanColumn(opts);
171
+ return Bool.Type.new(opts);
183
172
  }
184
173
  Bool.Type = BooleanColumn;
185
174
 
186
175
  // src/columns/Num.ts
187
176
  var NumericColumn = class extends Field {
188
- constructor() {
189
- super(...arguments);
190
- this.type = "int";
191
- }
192
177
  set(value) {
193
178
  if (typeof value !== "number" || isNaN(value))
194
179
  throw `Got '${value}' but value must be a number.`;
195
- if (this.type === "int" && value !== Math.floor(value))
196
- throw `Got '${value}' but datatype is integer.`;
197
180
  return value;
198
181
  }
199
182
  };
200
183
  function Num(opts) {
201
- return new NumericColumn(opts);
184
+ return Num.Type.new(opts);
202
185
  }
203
186
  Num.Type = NumericColumn;
204
187
 
205
188
  // src/columns/One.ts
206
189
  var ForeignColumn = class extends Field {
207
- constructor(type, nullable) {
208
- super(({ property }) => ({
209
- column: underscore(property) + "_id"
210
- }));
211
- this.foreignKey = "id";
190
+ constructor() {
191
+ super(...arguments);
212
192
  this.nullable = false;
213
- this.entity = type;
214
- this.type = "int";
215
- this.foreignKey = "id";
216
- this.foreignTable = type.table;
217
- this.foreignTable = type.table;
218
- this.nullable = nullable || false;
193
+ }
194
+ get column() {
195
+ return underscore(this.property) + "_id";
219
196
  }
220
197
  set(value) {
221
198
  if (this.query?.tables.has(value))
222
- return value[this.foreignKey];
199
+ return value[this.reference.column];
223
200
  return value;
224
201
  }
225
202
  use(query) {
@@ -228,30 +205,17 @@ var ForeignColumn = class extends Field {
228
205
  return table;
229
206
  }
230
207
  };
231
- function One(type, nullable) {
232
- return new ForeignColumn(type, nullable);
208
+ function One(entity, arg2) {
209
+ if (typeof arg2 === "boolean")
210
+ arg2 = { nullable: arg2 };
211
+ const opts = Object.assign({}, arg2);
212
+ const reference = entity.fields.get("id");
213
+ return One.Type.new({ entity, reference, ...opts });
233
214
  }
234
215
  One.Type = ForeignColumn;
235
216
 
236
217
  // src/columns/Str.ts
237
- var VARIABLE_TYPE = /* @__PURE__ */ new Set(["char", "varchar", "text"]);
238
218
  var StringColumn = class extends Field {
239
- constructor(opts) {
240
- super(({ parent, property }) => {
241
- let { type = "varchar", length = 255, datatype } = opts || {};
242
- if (VARIABLE_TYPE.has(type))
243
- if (length)
244
- datatype = `${datatype || type}(${length})`;
245
- else
246
- throw `Can't determine datatype! Length is not specified for ${parent.name}.${property}.`;
247
- return {
248
- datatype,
249
- ...opts
250
- };
251
- });
252
- this.type = "varchar";
253
- this.length = 255;
254
- }
255
219
  set(value) {
256
220
  if (typeof value !== "string")
257
221
  throw "Value must be a string.";
@@ -261,31 +225,30 @@ var StringColumn = class extends Field {
261
225
  }
262
226
  };
263
227
  function Str(opts) {
264
- return new StringColumn(opts);
228
+ return Str.Type.new(opts);
265
229
  }
266
230
  Str.Type = StringColumn;
267
231
 
268
232
  // src/columns/Time.ts
269
233
  var TimeColumn = class extends Field {
270
- constructor() {
271
- super(...arguments);
272
- this.type = "datetime";
273
- this.fallback = void 0;
274
- }
275
234
  get(value) {
276
- return new global.Date(value.replace(/[-]/g, "/") + "Z");
235
+ if (value instanceof Date)
236
+ return value;
237
+ return new Date(value.replace(/[-]/g, "/"));
277
238
  }
278
239
  set(value) {
279
240
  if (value === "NOW")
280
241
  return "CURRENT_TIMESTAMP";
281
242
  else if (value instanceof Date)
282
243
  return value.toISOString().slice(0, 19).replace("T", " ");
244
+ else if (typeof value === "string")
245
+ return value;
283
246
  else
284
- throw "Value must be a Date or 'NOW'.";
247
+ throw "Value must be a Date, string, or 'NOW'.";
285
248
  }
286
249
  };
287
- function Time(options) {
288
- return new TimeColumn(options);
250
+ function Time(opts) {
251
+ return Time.Type.new(opts);
289
252
  }
290
253
  Time.Type = TimeColumn;
291
254
 
@@ -477,7 +440,7 @@ var Builder = class {
477
440
  return where;
478
441
  }
479
442
  if (right instanceof Parameter) {
480
- right.digest = left.set.bind(this);
443
+ right.digest = left.set.bind(left);
481
444
  }
482
445
  return this.filters.add(left, op2, right);
483
446
  }
@@ -902,7 +865,7 @@ var Connection = class {
902
865
  return new generator(builder).toString();
903
866
  }
904
867
  async then(onfulfilled, onrejected) {
905
- return (this.ready ? Promise.resolve() : this.sync(true)).then(() => {
868
+ return (this.ready ? Promise.resolve() : this.sync()).then(() => {
906
869
  Object.defineProperty(this, "then", { value: void 0 });
907
870
  if (onfulfilled)
908
871
  onfulfilled(this);
@@ -935,7 +898,7 @@ var NoConnection = class extends Connection {
935
898
  };
936
899
  }
937
900
  async sync() {
938
- return;
901
+ return this;
939
902
  }
940
903
  async valid() {
941
904
  return true;
@@ -1020,7 +983,7 @@ var Type = _Type;
1020
983
  var Primary = class extends Field {
1021
984
  constructor() {
1022
985
  super(...arguments);
1023
- this.type = "int";
986
+ this.type = "integer";
1024
987
  this.increment = true;
1025
988
  this.optional = true;
1026
989
  this.nullable = false;
@@ -1104,8 +1067,10 @@ function Query(factory) {
1104
1067
  });
1105
1068
  return query;
1106
1069
  };
1107
- if (typeof args == "number")
1070
+ if (typeof args == "number") {
1071
+ runner.toString = statement.toString;
1108
1072
  return runner;
1073
+ }
1109
1074
  return runner();
1110
1075
  }
1111
1076
  Query.Builder = Builder;
@@ -1113,15 +1078,12 @@ Query.fn = { ...MathOps, bit: BitWise };
1113
1078
 
1114
1079
  // src/type/Table.ts
1115
1080
  function Table(arg1, arg2) {
1116
- return new Field(({ parent }) => {
1081
+ return Field.does((parent) => {
1117
1082
  if (typeof arg1 == "string") {
1118
- const [name2, schema] = arg1.split(".").reverse();
1119
- arg1 = { schema, ...arg2, name: name2 };
1083
+ const [table, schema] = arg1.split(".").reverse();
1084
+ arg1 = { schema, ...arg2, table };
1120
1085
  }
1121
- const { name, ...rest } = arg1;
1122
- if (name)
1123
- parent.table = name;
1124
- assign(parent, rest);
1086
+ assign(parent, arg1);
1125
1087
  });
1126
1088
  }
1127
1089
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -28,29 +28,30 @@ var {
28
28
  // src/type/Field.ts
29
29
  var REGISTER = /* @__PURE__ */ new Map();
30
30
  var Field = class {
31
- constructor(options) {
32
- this.options = options;
31
+ constructor() {
33
32
  this.type = "";
34
33
  this.unique = false;
35
34
  this.nullable = false;
36
35
  this.optional = false;
37
36
  this.increment = false;
38
37
  }
39
- init(property, parent) {
40
- let options = this.options;
38
+ /** Real datatype of this field in database. */
39
+ get datatype() {
40
+ return this.type;
41
+ }
42
+ get column() {
43
+ return underscore(this.property);
44
+ }
45
+ set column(value) {
46
+ Object.defineProperty(this, "column", { value });
47
+ }
48
+ static new(options) {
49
+ return Object.assign(new this(), options);
50
+ }
51
+ create(property, parent) {
41
52
  const field = create(this);
42
53
  field.parent = parent;
43
54
  field.property = property;
44
- field.column = underscore(property);
45
- if (typeof options === "function")
46
- options = options(field) || {};
47
- for (const key in options) {
48
- const value = options[key];
49
- if (value !== void 0)
50
- field[key] = value;
51
- }
52
- if (!field.datatype)
53
- field.datatype = field.type;
54
55
  return field;
55
56
  }
56
57
  /**
@@ -89,6 +90,9 @@ var Field = class {
89
90
  };
90
91
  }
91
92
  };
93
+ defineProperty(Field, "does", {
94
+ value: (callback) => callback
95
+ });
92
96
  function fields(from) {
93
97
  let fields2 = REGISTER.get(from);
94
98
  if (!fields2) {
@@ -97,13 +101,15 @@ function fields(from) {
97
101
  const reference = new from();
98
102
  for (const key in reference) {
99
103
  const { value } = getOwnPropertyDescriptor(reference, key);
100
- if (!(value instanceof Field))
101
- throw new Error(
102
- `Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
103
- );
104
- const instance = value.init(key, from);
105
- from.fields.set(key, instance);
106
- freeze(instance);
104
+ if (value instanceof Field) {
105
+ const instance = value.create(key, from);
106
+ from.fields.set(key, instance);
107
+ freeze(instance);
108
+ } else if (typeof value === "function") {
109
+ value(from, key);
110
+ } else throw new Error(
111
+ `Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
112
+ );
107
113
  }
108
114
  }
109
115
  return fields2;
@@ -111,76 +117,47 @@ function fields(from) {
111
117
 
112
118
  // src/columns/Bool.ts
113
119
  var BooleanColumn = class extends Field {
114
- constructor(opts = {}) {
115
- let type = "tinyint";
116
- let datatype = "tinyint";
117
- let TRUE = 1;
118
- let FALSE = 0;
119
- if ("either" in opts && opts.either) {
120
- [TRUE, FALSE] = opts.either;
121
- type = "varchar";
122
- datatype = `varchar(${Math.max(TRUE.length, FALSE.length)})`;
123
- }
124
- super(opts);
125
- this.type = "tinyint";
126
- this.TRUE = 1;
127
- this.FALSE = 0;
128
- this.type = type;
129
- this.datatype = datatype;
130
- this.TRUE = TRUE;
131
- this.FALSE = FALSE;
132
- }
133
120
  get(value) {
134
- return value === this.TRUE;
121
+ const [TRUE] = this.either || [1];
122
+ return value === true || value === TRUE;
135
123
  }
136
124
  set(value) {
125
+ const [TRUE, FALSE] = this.either || [1, 0];
137
126
  if (typeof value !== "boolean")
138
127
  throw "Value must be a boolean.";
139
- return value ? this.TRUE : this.FALSE;
128
+ return value ? TRUE : FALSE;
140
129
  }
141
130
  };
142
131
  function Bool(opts) {
143
- return new BooleanColumn(opts);
132
+ return Bool.Type.new(opts);
144
133
  }
145
134
  Bool.Type = BooleanColumn;
146
135
 
147
136
  // src/columns/Num.ts
148
137
  var NumericColumn = class extends Field {
149
- constructor() {
150
- super(...arguments);
151
- this.type = "int";
152
- }
153
138
  set(value) {
154
139
  if (typeof value !== "number" || isNaN(value))
155
140
  throw `Got '${value}' but value must be a number.`;
156
- if (this.type === "int" && value !== Math.floor(value))
157
- throw `Got '${value}' but datatype is integer.`;
158
141
  return value;
159
142
  }
160
143
  };
161
144
  function Num(opts) {
162
- return new NumericColumn(opts);
145
+ return Num.Type.new(opts);
163
146
  }
164
147
  Num.Type = NumericColumn;
165
148
 
166
149
  // src/columns/One.ts
167
150
  var ForeignColumn = class extends Field {
168
- constructor(type, nullable) {
169
- super(({ property }) => ({
170
- column: underscore(property) + "_id"
171
- }));
172
- this.foreignKey = "id";
151
+ constructor() {
152
+ super(...arguments);
173
153
  this.nullable = false;
174
- this.entity = type;
175
- this.type = "int";
176
- this.foreignKey = "id";
177
- this.foreignTable = type.table;
178
- this.foreignTable = type.table;
179
- this.nullable = nullable || false;
154
+ }
155
+ get column() {
156
+ return underscore(this.property) + "_id";
180
157
  }
181
158
  set(value) {
182
159
  if (this.query?.tables.has(value))
183
- return value[this.foreignKey];
160
+ return value[this.reference.column];
184
161
  return value;
185
162
  }
186
163
  use(query) {
@@ -189,30 +166,17 @@ var ForeignColumn = class extends Field {
189
166
  return table;
190
167
  }
191
168
  };
192
- function One(type, nullable) {
193
- return new ForeignColumn(type, nullable);
169
+ function One(entity, arg2) {
170
+ if (typeof arg2 === "boolean")
171
+ arg2 = { nullable: arg2 };
172
+ const opts = Object.assign({}, arg2);
173
+ const reference = entity.fields.get("id");
174
+ return One.Type.new({ entity, reference, ...opts });
194
175
  }
195
176
  One.Type = ForeignColumn;
196
177
 
197
178
  // src/columns/Str.ts
198
- var VARIABLE_TYPE = /* @__PURE__ */ new Set(["char", "varchar", "text"]);
199
179
  var StringColumn = class extends Field {
200
- constructor(opts) {
201
- super(({ parent, property }) => {
202
- let { type = "varchar", length = 255, datatype } = opts || {};
203
- if (VARIABLE_TYPE.has(type))
204
- if (length)
205
- datatype = `${datatype || type}(${length})`;
206
- else
207
- throw `Can't determine datatype! Length is not specified for ${parent.name}.${property}.`;
208
- return {
209
- datatype,
210
- ...opts
211
- };
212
- });
213
- this.type = "varchar";
214
- this.length = 255;
215
- }
216
180
  set(value) {
217
181
  if (typeof value !== "string")
218
182
  throw "Value must be a string.";
@@ -222,31 +186,30 @@ var StringColumn = class extends Field {
222
186
  }
223
187
  };
224
188
  function Str(opts) {
225
- return new StringColumn(opts);
189
+ return Str.Type.new(opts);
226
190
  }
227
191
  Str.Type = StringColumn;
228
192
 
229
193
  // src/columns/Time.ts
230
194
  var TimeColumn = class extends Field {
231
- constructor() {
232
- super(...arguments);
233
- this.type = "datetime";
234
- this.fallback = void 0;
235
- }
236
195
  get(value) {
237
- return new global.Date(value.replace(/[-]/g, "/") + "Z");
196
+ if (value instanceof Date)
197
+ return value;
198
+ return new Date(value.replace(/[-]/g, "/"));
238
199
  }
239
200
  set(value) {
240
201
  if (value === "NOW")
241
202
  return "CURRENT_TIMESTAMP";
242
203
  else if (value instanceof Date)
243
204
  return value.toISOString().slice(0, 19).replace("T", " ");
205
+ else if (typeof value === "string")
206
+ return value;
244
207
  else
245
- throw "Value must be a Date or 'NOW'.";
208
+ throw "Value must be a Date, string, or 'NOW'.";
246
209
  }
247
210
  };
248
- function Time(options) {
249
- return new TimeColumn(options);
211
+ function Time(opts) {
212
+ return Time.Type.new(opts);
250
213
  }
251
214
  Time.Type = TimeColumn;
252
215
 
@@ -438,7 +401,7 @@ var Builder = class {
438
401
  return where;
439
402
  }
440
403
  if (right instanceof Parameter) {
441
- right.digest = left.set.bind(this);
404
+ right.digest = left.set.bind(left);
442
405
  }
443
406
  return this.filters.add(left, op2, right);
444
407
  }
@@ -863,7 +826,7 @@ var Connection = class {
863
826
  return new generator(builder).toString();
864
827
  }
865
828
  async then(onfulfilled, onrejected) {
866
- return (this.ready ? Promise.resolve() : this.sync(true)).then(() => {
829
+ return (this.ready ? Promise.resolve() : this.sync()).then(() => {
867
830
  Object.defineProperty(this, "then", { value: void 0 });
868
831
  if (onfulfilled)
869
832
  onfulfilled(this);
@@ -896,7 +859,7 @@ var NoConnection = class extends Connection {
896
859
  };
897
860
  }
898
861
  async sync() {
899
- return;
862
+ return this;
900
863
  }
901
864
  async valid() {
902
865
  return true;
@@ -981,7 +944,7 @@ var Type = _Type;
981
944
  var Primary = class extends Field {
982
945
  constructor() {
983
946
  super(...arguments);
984
- this.type = "int";
947
+ this.type = "integer";
985
948
  this.increment = true;
986
949
  this.optional = true;
987
950
  this.nullable = false;
@@ -1065,8 +1028,10 @@ function Query(factory) {
1065
1028
  });
1066
1029
  return query;
1067
1030
  };
1068
- if (typeof args == "number")
1031
+ if (typeof args == "number") {
1032
+ runner.toString = statement.toString;
1069
1033
  return runner;
1034
+ }
1070
1035
  return runner();
1071
1036
  }
1072
1037
  Query.Builder = Builder;
@@ -1074,15 +1039,12 @@ Query.fn = { ...MathOps, bit: BitWise };
1074
1039
 
1075
1040
  // src/type/Table.ts
1076
1041
  function Table(arg1, arg2) {
1077
- return new Field(({ parent }) => {
1042
+ return Field.does((parent) => {
1078
1043
  if (typeof arg1 == "string") {
1079
- const [name2, schema] = arg1.split(".").reverse();
1080
- arg1 = { schema, ...arg2, name: name2 };
1044
+ const [table, schema] = arg1.split(".").reverse();
1045
+ arg1 = { schema, ...arg2, table };
1081
1046
  }
1082
- const { name, ...rest } = arg1;
1083
- if (name)
1084
- parent.table = name;
1085
- assign(parent, rest);
1047
+ assign(parent, arg1);
1086
1048
  });
1087
1049
  }
1088
1050
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressive/sql",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Query builder and ORM",
5
5
  "license": "ISC",
6
6
  "publishConfig": {