@expressive/sql 0.2.1 → 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.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
  }
@@ -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;
@@ -1076,15 +1039,12 @@ Query.fn = { ...MathOps, bit: BitWise };
1076
1039
 
1077
1040
  // src/type/Table.ts
1078
1041
  function Table(arg1, arg2) {
1079
- return new Field(({ parent }) => {
1042
+ return Field.does((parent) => {
1080
1043
  if (typeof arg1 == "string") {
1081
- const [name2, schema] = arg1.split(".").reverse();
1082
- arg1 = { schema, ...arg2, name: name2 };
1044
+ const [table, schema] = arg1.split(".").reverse();
1045
+ arg1 = { schema, ...arg2, table };
1083
1046
  }
1084
- const { name, ...rest } = arg1;
1085
- if (name)
1086
- parent.table = name;
1087
- assign(parent, rest);
1047
+ assign(parent, arg1);
1088
1048
  });
1089
1049
  }
1090
1050
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressive/sql",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Query builder and ORM",
5
5
  "license": "ISC",
6
6
  "publishConfig": {