happy-sql 1.6.0 → 1.7.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/ChangeLog CHANGED
@@ -1,3 +1,9 @@
1
+ 2026.08.18, v1.7.0
2
+
3
+ feature:
4
+ - 9446e52 happy-sql: nextval
5
+ - 60be73e nextval
6
+
1
7
  2026.08.15, v1.6.0
2
8
 
3
9
  fix:
@@ -11,21 +11,41 @@ const getDataType = (dataType) => {
11
11
 
12
12
  const isSerial = (dataType) => getDataType(dataType).name === 'SERIAL';
13
13
 
14
+ const convertGenerated = (constraint) => {
15
+ const kws = [];
16
+
17
+ for (const kw of constraint.generatedKw)
18
+ kws.push(kw.name);
19
+
20
+ if (kws.includes('DEFAULT'))
21
+ return callExpression(identifier('identityByDefault'), []);
22
+
23
+ return callExpression(identifier('identity'), []);
24
+ };
25
+
26
+ const convertDefault = ({expr}) => {
27
+ if (expr.type !== 'func_call' || expr.name.name !== 'nextval')
28
+ return null;
29
+
30
+ const seqName = expr.args.expr.args.items[0].value;
31
+
32
+ return callExpression(identifier('nextval'), [
33
+ identifier(seqName),
34
+ ]);
35
+ };
36
+
37
+ const CONSTRAINT_VISITORS = {
38
+ constraint_primary_key: () => callExpression(identifier('primaryKey'), []),
39
+ constraint_generated: convertGenerated,
40
+ constraint_not_null: () => null,
41
+ constraint_default: convertDefault,
42
+ };
43
+
14
44
  const convertConstraint = (constraint) => {
15
- if (constraint.type === 'constraint_primary_key')
16
- return callExpression(identifier('primaryKey'), []);
45
+ const visitor = CONSTRAINT_VISITORS[constraint.type];
17
46
 
18
- if (constraint.type === 'constraint_generated') {
19
- const kws = [];
20
-
21
- for (const kw of constraint.generatedKw)
22
- kws.push(kw.name);
23
-
24
- if (kws.includes('DEFAULT'))
25
- return callExpression(identifier('identityByDefault'), []);
26
-
27
- return callExpression(identifier('identity'), []);
28
- }
47
+ if (visitor)
48
+ return visitor(constraint);
29
49
 
30
50
  return callExpression(identifier('autoIncrement'), []);
31
51
  };
@@ -46,8 +66,12 @@ export const convertColumnDefinition = (column) => {
46
66
  else
47
67
  args.push(identifier(getDataType(dataType).text));
48
68
 
49
- for (const constraint of constraints)
50
- args.push(convertConstraint(constraint));
69
+ for (const constraint of constraints) {
70
+ const node = convertConstraint(constraint);
71
+
72
+ if (node)
73
+ args.push(node);
74
+ }
51
75
 
52
76
  return callExpression(identifier('column'), args);
53
77
  };
@@ -1,4 +1,3 @@
1
1
  [
2
2
  createSequence(users_id_seq),
3
3
  ];
4
-
@@ -0,0 +1,5 @@
1
+ [
2
+ createTable(t, [
3
+ column(id, INTEGER, primaryKey()),
4
+ ]),
5
+ ];
@@ -0,0 +1 @@
1
+ CREATE TABLE t (id INTEGER DEFAULT 42 PRIMARY KEY)
@@ -0,0 +1,6 @@
1
+ [
2
+ createTable(users, [
3
+ column(id, INTEGER, nextval(users_id_seq), primaryKey()),
4
+ column(name, TEXT),
5
+ ]),
6
+ ];
@@ -0,0 +1 @@
1
+ CREATE TABLE users (id INTEGER DEFAULT nextval('users_id_seq') PRIMARY KEY, name TEXT)
@@ -0,0 +1,5 @@
1
+ [
2
+ createTable(t, [
3
+ column(id, INTEGER, primaryKey()),
4
+ ]),
5
+ ];
@@ -0,0 +1 @@
1
+ CREATE TABLE t (id INTEGER NOT NULL PRIMARY KEY)
@@ -23,6 +23,7 @@ import {serial} from './serial/serial.js';
23
23
  import {primaryKey} from './primary-key/primary-key.js';
24
24
  import {lastInsertRowid} from './last-insert-rowid/last-insert-rowid.js';
25
25
  import {lastval} from './lastval/lastval.js';
26
+ import {nextval} from './nextval/nextval.js';
26
27
  import {like} from './like/like.js';
27
28
  import {isNull, isNotNull} from './is-null/is-null.js';
28
29
  import {inList} from './in-list/in-list.js';
@@ -57,6 +58,7 @@ export const clauses = {
57
58
  primaryKey,
58
59
  lastInsertRowid,
59
60
  lastval,
61
+ nextval,
60
62
  like,
61
63
  isNull,
62
64
  isNotNull,
@@ -0,0 +1 @@
1
+ CREATE TABLE users (id INTEGER DEFAULT nextval('users_id_seq') PRIMARY KEY, name TEXT)
@@ -0,0 +1,6 @@
1
+ [
2
+ createTable(users, [
3
+ column(id, INTEGER, nextval(users_id_seq), primaryKey()),
4
+ column(name, TEXT),
5
+ ]),
6
+ ];
@@ -0,0 +1,4 @@
1
+ export const nextval = (path, {write}) => {
2
+ const [seq] = path.node.arguments;
3
+ write(`DEFAULT nextval('${seq.name}')`);
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "happy-sql",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "type": "module",
5
5
  "description": "sql to js ast parser and printer",
6
6
  "exports": {
@@ -1,2 +0,0 @@
1
- SELECT COUNT(*)
2
- FROM users