|
| 1 | +const { Connection, Repository } = require('../../dist/index'); |
| 2 | + |
| 3 | +// Import mixins |
| 4 | +const Timestampable = require('./models/Timestampable'); |
| 5 | +const SoftDeletable = require('./models/SoftDeletable'); |
| 6 | + |
| 7 | +// Import models |
| 8 | +const User = require('./models/User'); |
| 9 | +const Document = require('./models/Document'); |
| 10 | + |
| 11 | +async function main() { |
| 12 | + console.log('=== NormalJS Mixins Demo ===\n'); |
| 13 | + |
| 14 | + // Setup connection and repository |
| 15 | + const conn = new Connection({ |
| 16 | + client: 'sqlite3', |
| 17 | + connection: { filename: ':memory:' }, |
| 18 | + useNullAsDefault: true |
| 19 | + }); |
| 20 | + |
| 21 | + const repo = new Repository(conn); |
| 22 | + |
| 23 | + // Register mixins first |
| 24 | + repo.register(Timestampable); |
| 25 | + repo.register(SoftDeletable); |
| 26 | + |
| 27 | + // Register models |
| 28 | + repo.register(User); |
| 29 | + repo.register(Document); |
| 30 | + |
| 31 | + // Sync schema |
| 32 | + await repo.sync({ force: true }); |
| 33 | + |
| 34 | + // Get models |
| 35 | + const Users = repo.get('User'); |
| 36 | + const Documents = repo.get('Document'); |
| 37 | + |
| 38 | + console.log('1. Testing Timestampable Mixin'); |
| 39 | + console.log('================================\n'); |
| 40 | + |
| 41 | + // Create a user (timestamps are automatically set) |
| 42 | + const user = await Users.create({ |
| 43 | + email: 'john@example.com', |
| 44 | + name: 'John Doe' |
| 45 | + }); |
| 46 | + |
| 47 | + console.log(`Created user: ${user.displayName}`); |
| 48 | + console.log(` created_at: ${user.created_at.toISOString()}`); |
| 49 | + console.log(` updated_at: ${user.updated_at.toISOString()}`); |
| 50 | + |
| 51 | + // Wait a bit to see the timestamp difference |
| 52 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 53 | + |
| 54 | + // Update the user (updated_at is automatically updated) |
| 55 | + await user.write({ name: 'Jane Doe' }); |
| 56 | + |
| 57 | + console.log(`\nUpdated user: ${user.displayName}`); |
| 58 | + console.log(` created_at: ${user.created_at.toISOString()}`); |
| 59 | + console.log(` updated_at: ${user.updated_at.toISOString()}`); |
| 60 | + console.log(' (Note: updated_at changed, created_at stayed the same)\n'); |
| 61 | + |
| 62 | + console.log('2. Testing SoftDeletable Mixin'); |
| 63 | + console.log('================================\n'); |
| 64 | + |
| 65 | + // Create a document |
| 66 | + const doc = await Documents.create({ |
| 67 | + title: 'Important Document', |
| 68 | + content: 'This is important content.', |
| 69 | + author_id: user.id |
| 70 | + }); |
| 71 | + |
| 72 | + console.log(`Created document: ${doc.title}`); |
| 73 | + console.log(` id: ${doc.id}`); |
| 74 | + console.log(` deleted_at: ${doc.deleted_at}`); |
| 75 | + console.log(` isDeleted: ${doc.isDeleted}\n`); |
| 76 | + |
| 77 | + // Soft delete the document |
| 78 | + await doc.unlink(); |
| 79 | + |
| 80 | + console.log('After soft delete:'); |
| 81 | + console.log(` deleted_at: ${doc.deleted_at?.toISOString()}`); |
| 82 | + console.log(` isDeleted: ${doc.isDeleted}\n`); |
| 83 | + |
| 84 | + // Try to find the document (should not be found due to default scope) |
| 85 | + const foundDoc = await Documents.where({ id: doc.id }).first(); |
| 86 | + console.log(`Document found with normal query: ${foundDoc !== null}`); |
| 87 | + |
| 88 | + // Find with unscoped (bypasses default scope) |
| 89 | + const unscopedDoc = await Documents.unscoped().where({ id: doc.id }).first(); |
| 90 | + console.log(`Document found with unscoped query: ${unscopedDoc !== null}\n`); |
| 91 | + |
| 92 | + // Restore the document |
| 93 | + await unscopedDoc.restore(); |
| 94 | + |
| 95 | + console.log('After restore:'); |
| 96 | + console.log(` deleted_at: ${unscopedDoc.deleted_at}`); |
| 97 | + console.log(` isDeleted: ${unscopedDoc.isDeleted}\n`); |
| 98 | + |
| 99 | + // Now it should be found with normal queries |
| 100 | + const restoredDoc = await Documents.where({ id: doc.id }).first(); |
| 101 | + console.log(`Document found after restore: ${restoredDoc !== null}\n`); |
| 102 | + |
| 103 | + console.log('3. Testing Hard Delete'); |
| 104 | + console.log('================================\n'); |
| 105 | + |
| 106 | + // Soft delete again |
| 107 | + await restoredDoc.unlink(); |
| 108 | + console.log('Soft deleted again'); |
| 109 | + |
| 110 | + // Get the document via unscoped |
| 111 | + const docToDelete = await Documents.unscoped().where({ id: doc.id }).first(); |
| 112 | + |
| 113 | + // Hard delete (actually remove from database) |
| 114 | + await docToDelete.forceUnlink(); |
| 115 | + console.log('Hard deleted (removed from database)'); |
| 116 | + |
| 117 | + // Try to find with unscoped (should not be found) |
| 118 | + const finalCheck = await Documents.unscoped().where({ id: doc.id }).first(); |
| 119 | + console.log(`Document found after hard delete: ${finalCheck !== null}\n`); |
| 120 | + |
| 121 | + console.log('4. Testing Combined Mixins'); |
| 122 | + console.log('================================\n'); |
| 123 | + |
| 124 | + // Create another document to show both mixins working together |
| 125 | + const doc2 = await Documents.create({ |
| 126 | + title: 'Test Document', |
| 127 | + content: 'Testing timestamps + soft delete', |
| 128 | + author_id: user.id |
| 129 | + }); |
| 130 | + |
| 131 | + console.log(`Created document: ${doc2.title}`); |
| 132 | + console.log(` created_at: ${doc2.created_at.toISOString()}`); |
| 133 | + console.log(` updated_at: ${doc2.updated_at.toISOString()}`); |
| 134 | + console.log(` deleted_at: ${doc2.deleted_at}\n`); |
| 135 | + |
| 136 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 137 | + |
| 138 | + // Update it |
| 139 | + await doc2.write({ title: 'Updated Test Document' }); |
| 140 | + |
| 141 | + console.log('After update:'); |
| 142 | + console.log(` created_at: ${doc2.created_at.toISOString()}`); |
| 143 | + console.log(` updated_at: ${doc2.updated_at.toISOString()}`); |
| 144 | + console.log(' (updated_at changed automatically)\n'); |
| 145 | + |
| 146 | + // Soft delete it |
| 147 | + await doc2.unlink(); |
| 148 | + |
| 149 | + console.log('After soft delete:'); |
| 150 | + console.log(` deleted_at: ${doc2.deleted_at?.toISOString()}`); |
| 151 | + console.log(` isDeleted: ${doc2.isDeleted}\n`); |
| 152 | + |
| 153 | + console.log('=== Demo Complete ==='); |
| 154 | + |
| 155 | + await conn.destroy(); |
| 156 | +} |
| 157 | + |
| 158 | +if (require.main === module) { |
| 159 | + main().catch(console.error); |
| 160 | +} |
| 161 | + |
| 162 | +module.exports = main; |
0 commit comments